1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.diff;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertTrue;
16
17 import org.junit.Test;
18
19 public abstract class AbstractDiffTestCase {
20 @Test
21 public void testEmptyInputs() {
22 EditList r = diff(t(""), t(""));
23 assertTrue("is empty", r.isEmpty());
24 }
25
26 @Test
27 public void testCreateFile() {
28 EditList r = diff(t(""), t("AB"));
29 assertEquals(1, r.size());
30 assertEquals(new Edit(0, 0, 0, 2), r.get(0));
31 }
32
33 @Test
34 public void testDeleteFile() {
35 EditList r = diff(t("AB"), t(""));
36 assertEquals(1, r.size());
37 assertEquals(new Edit(0, 2, 0, 0), r.get(0));
38 }
39
40 @Test
41 public void testDegenerate_InsertMiddle() {
42 EditList r = diff(t("ac"), t("aBc"));
43 assertEquals(1, r.size());
44 assertEquals(new Edit(1, 1, 1, 2), r.get(0));
45 }
46
47 @Test
48 public void testDegenerate_DeleteMiddle() {
49 EditList r = diff(t("aBc"), t("ac"));
50 assertEquals(1, r.size());
51 assertEquals(new Edit(1, 2, 1, 1), r.get(0));
52 }
53
54 @Test
55 public void testDegenerate_ReplaceMiddle() {
56 EditList r = diff(t("bCd"), t("bEd"));
57 assertEquals(1, r.size());
58 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
59 }
60
61 @Test
62 public void testDegenerate_InsertsIntoMidPosition() {
63 EditList r = diff(t("aaaa"), t("aaXaa"));
64 assertEquals(1, r.size());
65 assertEquals(new Edit(2, 2, 2, 3), r.get(0));
66 }
67
68 @Test
69 public void testDegenerate_InsertStart() {
70 EditList r = diff(t("bc"), t("Abc"));
71 assertEquals(1, r.size());
72 assertEquals(new Edit(0, 0, 0, 1), r.get(0));
73 }
74
75 @Test
76 public void testDegenerate_DeleteStart() {
77 EditList r = diff(t("Abc"), t("bc"));
78 assertEquals(1, r.size());
79 assertEquals(new Edit(0, 1, 0, 0), r.get(0));
80 }
81
82 @Test
83 public void testDegenerate_InsertEnd() {
84 EditList r = diff(t("bc"), t("bcD"));
85 assertEquals(1, r.size());
86 assertEquals(new Edit(2, 2, 2, 3), r.get(0));
87 }
88
89 @Test
90 public void testDegenerate_DeleteEnd() {
91 EditList r = diff(t("bcD"), t("bc"));
92 assertEquals(1, r.size());
93 assertEquals(new Edit(2, 3, 2, 2), r.get(0));
94 }
95
96 @Test
97 public void testEdit_ReplaceCommonDelete() {
98 EditList r = diff(t("RbC"), t("Sb"));
99 assertEquals(2, r.size());
100 assertEquals(new Edit(0, 1, 0, 1), r.get(0));
101 assertEquals(new Edit(2, 3, 2, 2), r.get(1));
102 }
103
104 @Test
105 public void testEdit_CommonReplaceCommonDeleteCommon() {
106 EditList r = diff(t("aRbCd"), t("aSbd"));
107 assertEquals(2, r.size());
108 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
109 assertEquals(new Edit(3, 4, 3, 3), r.get(1));
110 }
111
112 @Test
113 public void testEdit_MoveBlock() {
114 EditList r = diff(t("aYYbcdz"), t("abcdYYz"));
115 assertEquals(2, r.size());
116 assertEquals(new Edit(1, 3, 1, 1), r.get(0));
117 assertEquals(new Edit(6, 6, 4, 6), r.get(1));
118 }
119
120 @Test
121 public void testEdit_InvertBlocks() {
122 EditList r = diff(t("aYYbcdXXz"), t("aXXbcdYYz"));
123 assertEquals(2, r.size());
124 assertEquals(new Edit(1, 3, 1, 3), r.get(0));
125 assertEquals(new Edit(6, 8, 6, 8), r.get(1));
126 }
127
128 @Test
129 public void testEdit_UniqueCommonLargerThanMatchPoint() {
130
131
132 EditList r = diff(t("AbdeZ"), t("PbdeQR"));
133 assertEquals(2, r.size());
134 assertEquals(new Edit(0, 1, 0, 1), r.get(0));
135 assertEquals(new Edit(4, 5, 4, 6), r.get(1));
136 }
137
138 @Test
139 public void testEdit_CommonGrowsPrefixAndSuffix() {
140
141
142 EditList r = diff(t("AaabccZ"), t("PaabccR"));
143 assertEquals(2, r.size());
144 assertEquals(new Edit(0, 1, 0, 1), r.get(0));
145 assertEquals(new Edit(6, 7, 6, 7), r.get(1));
146 }
147
148 @Test
149 public void testEdit_DuplicateAButCommonUniqueInB() {
150 EditList r = diff(t("AbbcR"), t("CbcS"));
151 assertEquals(2, r.size());
152 assertEquals(new Edit(0, 2, 0, 1), r.get(0));
153 assertEquals(new Edit(4, 5, 3, 4), r.get(1));
154 }
155
156 @Test
157 public void testEdit_InsertNearCommonTail() {
158 EditList r = diff(t("aq}nb"), t("aCq}nD}nb"));
159 assertEquals(new Edit(1, 1, 1, 2), r.get(0));
160 assertEquals(new Edit(4, 4, 5, 8), r.get(1));
161 assertEquals(2, r.size());
162 }
163
164 @Test
165 public void testEdit_DeleteNearCommonTail() {
166 EditList r = diff(t("aCq}nD}nb"), t("aq}nb"));
167 assertEquals(new Edit(1, 2, 1, 1), r.get(0));
168 assertEquals(new Edit(5, 8, 4, 4), r.get(1));
169 assertEquals(2, r.size());
170 }
171
172 @Test
173 public void testEdit_DeleteNearCommonCenter() {
174 EditList r = diff(t("abcd123123uvwxpq"), t("aBcd123uvwxPq"));
175 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
176 assertEquals(new Edit(7, 10, 7, 7), r.get(1));
177 assertEquals(new Edit(14, 15, 11, 12), r.get(2));
178 assertEquals(3, r.size());
179 }
180
181 @Test
182 public void testEdit_InsertNearCommonCenter() {
183 EditList r = diff(t("aBcd123uvwxPq"), t("abcd123123uvwxpq"));
184 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
185 assertEquals(new Edit(7, 7, 7, 10), r.get(1));
186 assertEquals(new Edit(11, 12, 14, 15), r.get(2));
187 assertEquals(3, r.size());
188 }
189
190 @Test
191 public void testEdit_LinuxBug() {
192 EditList r = diff(t("a{bcdE}z"), t("a{0bcdEE}z"));
193 assertEquals(new Edit(2, 2, 2, 3), r.get(0));
194 assertEquals(new Edit(6, 6, 7, 8), r.get(1));
195 assertEquals(2, r.size());
196 }
197
198 public EditList diff(RawText a, RawText b) {
199 return algorithm().diff(RawTextComparator.DEFAULT, a, b);
200 }
201
202 protected abstract DiffAlgorithm algorithm();
203
204 public static RawText t(String text) {
205 StringBuilder r = new StringBuilder();
206 for (int i = 0; i < text.length(); i++) {
207 r.append(text.charAt(i));
208 r.append('\n');
209 }
210 return new RawText(r.toString().getBytes(UTF_8));
211 }
212 }