-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSimpleDiffTest.java
More file actions
186 lines (155 loc) · 5.48 KB
/
Copy pathSimpleDiffTest.java
File metadata and controls
186 lines (155 loc) · 5.48 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SimpleDiffTest {
static String diffDataCharOld = "The quick brown fox.";
static String diffDataCharNew = "The kuick brown fix.";
static String[][] diffDataCharExpected = {
{ "=", "The " },
{ "-", "q" },
{ "+", "k" },
{ "=", "uick brown f" },
{ "-", "o" },
{ "+", "i" },
{ "=", "x." },
};
@Test
public void testDiffString() {
List< Map.Entry<SimpleDiff.DiffOp, String > > ret;
List< Map.Entry<SimpleDiff.DiffOp, String > > expected;
ret = SimpleDiff.diffString(diffDataCharOld, diffDataCharNew);
expected = Arrays.asList( diffDataCharExpected ).stream()
.map(ar -> new AbstractMap.SimpleEntry<SimpleDiff.DiffOp, String>(
SimpleDiff.DiffOp.fromString(ar[0]), ar[1]) )
.collect(Collectors.toList());
assertEquals(expected, ret);;
}
static String[] diffDataWordOld1 = { "The", "quick", "brown", "fox" };
static String[] diffDataWordNew1 = { "The", "slow", "green", "turtle" };
static String[][] diffDataWordExpected1 = {
{"=", "The"},
{"-", "quick", "brown", "fox"},
{"+", "slow", "green", "turtle"},
};
static String[] diffDataWordOld2 = { "jumps", "over", "the", "lazy", "dog" };
static String[] diffDataWordNew2 = { "walks", "around", "the", "orange", "cat" };
static String[][] diffDataWordExpected2 = {
{"-", "jumps", "over" },
{"+", "walks", "around" },
{"=", "the" },
{"-", "lazy", "dog" },
{"+", "orange", "cat" },
};
@Test
public void testDiffWord1() {
subDiffWord(diffDataWordOld1, diffDataWordNew1, diffDataWordExpected1 );
}
@Test
public void testDiffWord2() {
subDiffWord(diffDataWordOld2, diffDataWordNew2, diffDataWordExpected2 );
}
public void subDiffWord( String[] oldWord, String[] newWord, String[][] expectedWord ) {
List< Map.Entry<SimpleDiff.DiffOp, List<String> > > ret;
List< Map.Entry<SimpleDiff.DiffOp, List<String> > > expected;
ret = SimpleDiff.diff(Arrays.asList(oldWord), Arrays.asList(newWord));
expected = Arrays.asList( expectedWord ).stream()
.map(ar -> new AbstractMap.SimpleEntry<SimpleDiff.DiffOp, List<String> >(
SimpleDiff.DiffOp.fromString(ar[0]),
Arrays.asList(ar).subList(1, ar.length) ) )
.collect(Collectors.toList());
assertEquals(expected, ret);
}
static int ADD=0;
static int DEL=1;
static int EQ=2;
static Integer[] diffDataIntOld1 = {1, 3, 4};
static Integer[] diffDataIntNew1 = {1, 2, 3, 4};
static Integer[][] diffDataIntExpected1 = {
{EQ ,1 },
{ADD, 2},
{EQ, 3, 4},
};
static Integer[] diffDataIntOld2 = {1, 2, 3, 8, 9, 12, 13 };
static Integer[] diffDataIntNew2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
static Integer[][] diffDataIntExpected2 = {
{ EQ, 1, 2, 3 },
{ ADD, 4, 5, 6, 7 },
{ EQ, 8, 9 },
{ ADD, 10, 11 },
{ EQ, 12, 13 },
{ ADD, 14, 15 },
};
static Integer[] diffDataIntOld34 = {1, 2, 3, 4, 5 };
static Integer[] diffDataIntNew34 = {1, 2, 2, 3, 4, 5};
static Integer[][] diffDataIntExpected34 = {
{ EQ, 1 },
{ ADD, 2 },
{ EQ, 2, 3, 4, 5 },
};
static Integer[] diffDataIntOld4 = {1, 2, 3, 4, 5 };
static Integer[] diffDataIntNew4 = {1, 2, 2, 3, 4, 4, 5};
static Integer[][] diffDataIntExpected4 = {
{ EQ, 1 },
{ ADD, 2 },
{ EQ, 2, 3, 4 },
{ ADD, 4 },
{ EQ, 5 },
};
static Integer[] diffDataIntOld5 = {1, 2, 3, 4, 5 };
static Integer[] diffDataIntNew5 = {1, 2, 1, 2, 3, 3, 2, 1, 4, 5};
static Integer[][] diffDataIntExpected5 = {
{ ADD, 1, 2 },
{ EQ, 1, 2, 3 },
{ ADD, 3, 2, 1 },
{ EQ, 4, 5 },
};
static Integer[] diffDataIntOld6 = {1, 2, 3, 4, 5};
static Integer[] diffDataIntNew6 = {1, 2, 5};
static Integer[][] diffDataIntExpected6 = {
{ EQ, 1, 2 },
{ DEL, 3, 4 },
{ EQ, 5 },
};
static Integer[] diffDataIntOld7 = {1, 2, 3, 4, 5, 6, 7, 8};
static Integer[] diffDataIntNew7 = {3, 6, 7};
static Integer[][] diffDataIntExpected7 = {
{ DEL, 1, 2 },
{ EQ, 3 },
{ DEL, 4, 5 },
{ EQ, 6, 7 },
{ DEL, 8 },
};
static Integer[] diffDataIntOld8 = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
static Integer[] diffDataIntNew8 = {1, 2, 3, 4, 5};
static Integer[][] diffDataIntExpected8 = {
{EQ, 1, 2, 3, 4, 5},
{DEL, 1, 2, 3, 4, 5},
};
@Test
public void testDiffInt1() {
subDiffInt(Arrays.asList(diffDataIntOld2),
Arrays.asList(diffDataIntNew2),
Arrays.asList(diffDataIntExpected2).stream()
.map(ar -> Arrays.asList(ar)).collect(Collectors.toList()));
}
public void subDiffInt( List<Integer> oldIntList, List<Integer> newIntList, List<List<Integer>> expectedIntList ) {
List< Map.Entry<SimpleDiff.DiffOp, List<Integer> > > ret;
List< Map.Entry<SimpleDiff.DiffOp, List<Integer> > > expected;
ret = SimpleDiff.diff(oldIntList, newIntList);
expected = expectedIntList.stream()
.map(li -> new AbstractMap.SimpleEntry<SimpleDiff.DiffOp, List<Integer> >(
SimpleDiff.DiffOp.fromInt(li.get(0)),
li.subList(1, li.size()) ) )
.collect(Collectors.toList());
assertEquals(expected, ret);
}
@Test
public void testListCharToString() {
Character[] listChar = { 't', 'o', 't', 'o' };
assertEquals( SimpleDiff.listCharToString( Arrays.asList(listChar) ), "toto" );
}
}