aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreWhitespaceChangeTest.java
blob: c2b8641ebe407611b6990e804ea538f39a994c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2009-2010, Google Inc.
 * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.diff;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.eclipse.jgit.lib.Constants;
import org.junit.Test;

public class RawTextIgnoreWhitespaceChangeTest {
	private final RawTextComparator cmp = RawTextComparator.WS_IGNORE_CHANGE;

	@Test
	public void testEqualsWithoutWhitespace() {
		final RawText a = new RawText(Constants
				.encodeASCII("foo-a\nfoo-b\nfoo\n"));
		final RawText b = new RawText(Constants
				.encodeASCII("foo-b\nfoo-c\nf\n"));

		assertEquals(3, a.size());
		assertEquals(3, b.size());

		// foo-a != foo-b
		assertFalse(cmp.equals(a, 0, b, 0));
		assertFalse(cmp.equals(b, 0, a, 0));

		// foo-b == foo-b
		assertTrue(cmp.equals(a, 1, b, 0));
		assertTrue(cmp.equals(b, 0, a, 1));

		// foo != f
		assertFalse(cmp.equals(a, 2, b, 2));
		assertFalse(cmp.equals(b, 2, a, 2));
	}

	@Test
	public void testEqualsWithWhitespace() {
		final RawText a = new RawText(Constants
				.encodeASCII("foo-a\n         \n a b c\na      \n  foo\na  b  c\n"));
		final RawText b = new RawText(Constants
				.encodeASCII("foo-a        b\n\nab  c\na\nfoo\na b     c  \n"));

		// "foo-a" != "foo-a        b"
		assertFalse(cmp.equals(a, 0, b, 0));
		assertFalse(cmp.equals(b, 0, a, 0));

		// "         " == ""
		assertTrue(cmp.equals(a, 1, b, 1));
		assertTrue(cmp.equals(b, 1, a, 1));

		// " a b c" != "ab  c"
		assertFalse(cmp.equals(a, 2, b, 2));
		assertFalse(cmp.equals(b, 2, a, 2));

		// "a      " == "a"
		assertTrue(cmp.equals(a, 3, b, 3));
		assertTrue(cmp.equals(b, 3, a, 3));

		// "  foo" != "foo"
		assertFalse(cmp.equals(a, 4, b, 4));
		assertFalse(cmp.equals(b, 4, a, 4));

		// "a  b  c" == "a b     c  "
		assertTrue(cmp.equals(a, 5, b, 5));
		assertTrue(cmp.equals(b, 5, a, 5));
	}

	@Test
	public void testEqualsWithTabs() {
		RawText a = new RawText(
				Constants.encodeASCII("a\tb\t \na\tb\t c \n  foo\na b\na  b"));
		RawText b = new RawText(
				Constants.encodeASCII("a b \na b c\n\tfoo\nab\na \tb"));

		// "a\tb\t \n" == "a b \n"
		assertTrue(cmp.equals(a, 0, b, 0));
		assertTrue(cmp.equals(b, 0, a, 0));

		// "a\tb\t c \n" == "a b c\n"
		assertTrue(cmp.equals(a, 1, b, 1));
		assertTrue(cmp.equals(b, 1, a, 1));

		// " foo" == "\tfoo"
		assertTrue(cmp.equals(a, 2, b, 2));
		assertTrue(cmp.equals(b, 2, a, 2));

		// "a b" != "ab"
		assertFalse(cmp.equals(a, 3, b, 3));
		assertFalse(cmp.equals(b, 3, a, 3));

		// "a b" == "a \tb "
		assertTrue(cmp.equals(a, 4, b, 4));
		assertTrue(cmp.equals(b, 4, a, 4));
	}

	@Test
	public void testHashCode() {
		RawText a = new RawText(Constants
				.encodeASCII("a b  c\n\nab  c d  \n\ta bc d\nxyz\na  b  c"));
		RawText b = new RawText(Constants.encodeASCII(
				"a b  c\na   b c\nab  c d\na bc d\n  \t a bc d\na b c\n"));

		// Same line gives equal hash
		assertEquals(cmp.hash(a, 0), cmp.hash(a, 0));

		// Empty lines produce the same hash
		assertEquals(cmp.hash(a, 1), cmp.hash(a, 1));

		// Equal lines from different RawTexts get the same hash (RawText
		// instance is not part of the hash)
		assertEquals(cmp.hash(a, 0), cmp.hash(b, 0));

		// A blank produces the same hash as a TAB
		assertEquals(cmp.hash(new RawText(Constants.encodeASCII(" ")), 0),
				cmp.hash(new RawText(Constants.encodeASCII("\t")), 0));

		// Lines with only differing whitespace produce same hash
		assertEquals(cmp.hash(a, 0), cmp.hash(b, 1));

		// Lines with different trailing whitespace produce the same hash
		assertEquals(cmp.hash(a, 2), cmp.hash(b, 2));

		// A line with leading whitespace produces a hash different from the
		// same line without leading whitespace
		assertNotEquals(cmp.hash(a, 3), cmp.hash(b, 3));

		// Lines with different leading whitespace produce equal hashes
		assertEquals(cmp.hash(a, 3), cmp.hash(b, 4));

		// While different lines _should_ produce different hashes, that may not
		// always be the case. But for these two lines, it is.
		assertNotEquals(cmp.hash(a, 4), cmp.hash(b, 4));

		// A line without trailing \n produces the same hash as one without
		assertEquals(cmp.hash(a, 5), cmp.hash(b, 5));

	}
}