Skip to content

Commit 2294c5b

Browse files
committed
implemented multi algorithm patch test
1 parent 7203d48 commit 2294c5b

File tree

4 files changed

+185
-15
lines changed

4 files changed

+185
-15
lines changed

java-diff-utils/src/main/java/com/github/difflib/algorithm/myers/MeyersDiffWithLinearSpace.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.github.difflib.algorithm.myers;
1717

1818
import com.github.difflib.algorithm.Change;
19+
import com.github.difflib.algorithm.DiffAlgorithmFactory;
1920
import com.github.difflib.algorithm.DiffAlgorithmI;
2021
import com.github.difflib.algorithm.DiffAlgorithmListener;
2122
import com.github.difflib.patch.DeltaType;
@@ -221,4 +222,23 @@ public Snake(final int start, final int end, final int diag) {
221222
this.diag = diag;
222223
}
223224
}
225+
226+
/**
227+
* Factory to create instances of this specific diff algorithm.
228+
*/
229+
public static DiffAlgorithmFactory factory() {
230+
return new DiffAlgorithmFactory() {
231+
@Override
232+
public <T> DiffAlgorithmI<T>
233+
create() {
234+
return new MeyersDiffWithLinearSpace<T>();
235+
}
236+
237+
@Override
238+
public <T> DiffAlgorithmI<T>
239+
create(BiPredicate < T, T > equalizer) {
240+
return new MeyersDiffWithLinearSpace<T>(equalizer);
241+
}
242+
};
243+
}
224244
}

java-diff-utils/src/test/java/com/github/difflib/patch/PatchTest.java renamed to java-diff-utils/src/test/java/com/github/difflib/patch/PatchWithAllDiffAlgorithmsTest.java

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,35 @@
1111
import java.util.Arrays;
1212
import java.util.List;
1313

14-
import org.junit.jupiter.api.Test;
1514

1615
import com.github.difflib.DiffUtils;
16+
import com.github.difflib.algorithm.DiffAlgorithmFactory;
17+
import com.github.difflib.algorithm.myers.MeyersDiff;
18+
import com.github.difflib.algorithm.myers.MeyersDiffWithLinearSpace;
19+
import java.util.stream.Stream;
20+
import org.junit.jupiter.api.AfterAll;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
25+
public class PatchWithAllDiffAlgorithmsTest {
26+
27+
private static Stream<Arguments> provideAlgorithms() {
28+
return Stream.of(
29+
Arguments.of(MeyersDiff.factory()),
30+
Arguments.of(MeyersDiffWithLinearSpace.factory()));
31+
}
32+
33+
@AfterAll
34+
public static void afterAll() {
35+
DiffUtils.withDefaultDiffAlgorithmFactory(MeyersDiff.factory());
36+
}
1737

18-
public class PatchTest {
19-
20-
@Test
21-
public void testPatch_Insert() {
38+
@ParameterizedTest
39+
@MethodSource("provideAlgorithms")
40+
public void testPatch_Insert(DiffAlgorithmFactory factory) {
41+
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
42+
2243
final List<String> insertTest_from = Arrays.asList("hhh");
2344
final List<String> insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
2445

@@ -30,8 +51,11 @@ public void testPatch_Insert() {
3051
}
3152
}
3253

33-
@Test
34-
public void testPatch_Delete() {
54+
@ParameterizedTest
55+
@MethodSource("provideAlgorithms")
56+
public void testPatch_Delete(DiffAlgorithmFactory factory) {
57+
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
58+
3559
final List<String> deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh");
3660
final List<String> deleteTest_to = Arrays.asList("ggg");
3761

@@ -43,8 +67,11 @@ public void testPatch_Delete() {
4367
}
4468
}
4569

46-
@Test
47-
public void testPatch_Change() {
70+
@ParameterizedTest
71+
@MethodSource("provideAlgorithms")
72+
public void testPatch_Change(DiffAlgorithmFactory factory) {
73+
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
74+
4875
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
4976
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
5077

@@ -56,8 +83,11 @@ public void testPatch_Change() {
5683
}
5784
}
5885

59-
@Test
60-
public void testPatch_Serializable() throws IOException, ClassNotFoundException {
86+
@ParameterizedTest
87+
@MethodSource("provideAlgorithms")
88+
public void testPatch_Serializable(DiffAlgorithmFactory factory) throws IOException, ClassNotFoundException {
89+
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
90+
6191
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
6292
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
6393

@@ -79,8 +109,11 @@ public void testPatch_Serializable() throws IOException, ClassNotFoundException
79109

80110
}
81111

82-
@Test
83-
public void testPatch_Change_withExceptionProcessor() {
112+
@ParameterizedTest
113+
@MethodSource("provideAlgorithms")
114+
public void testPatch_Change_withExceptionProcessor(DiffAlgorithmFactory factory) {
115+
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
116+
84117
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
85118
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
86119

@@ -93,9 +126,9 @@ public void testPatch_Change_withExceptionProcessor() {
93126
try {
94127
List<String> data = DiffUtils.patch(changeTest_from, patch);
95128
assertEquals(9, data.size());
96-
129+
97130
assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
98-
131+
99132
} catch (PatchFailedException e) {
100133
fail(e.getMessage());
101134
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2021 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.difflib.patch;
17+
18+
import com.github.difflib.DiffUtils;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.fail;
23+
import org.junit.jupiter.api.Test;
24+
25+
/**
26+
*
27+
* @author tw
28+
*/
29+
public class PatchWithMeyerDiffTest {
30+
@Test
31+
public void testPatch_Change_withExceptionProcessor() {
32+
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
33+
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
34+
35+
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
36+
37+
changeTest_from.set(2, "CDC");
38+
39+
patch.withConflictOutput(Patch.CONFLICT_PRODUCES_MERGE_CONFLICT);
40+
41+
try {
42+
List<String> data = DiffUtils.patch(changeTest_from, patch);
43+
assertEquals(9, data.size());
44+
45+
assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
46+
47+
} catch (PatchFailedException e) {
48+
fail(e.getMessage());
49+
}
50+
}
51+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2021 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.difflib.patch;
17+
18+
import com.github.difflib.DiffUtils;
19+
import com.github.difflib.algorithm.myers.MeyersDiff;
20+
import com.github.difflib.algorithm.myers.MeyersDiffWithLinearSpace;
21+
import java.util.Arrays;
22+
import java.util.List;
23+
import org.junit.jupiter.api.AfterAll;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.fail;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
*
31+
* @author tw
32+
*/
33+
public class PatchWithMeyerDiffWithLinearSpaceTest {
34+
35+
@BeforeAll
36+
public static void setupClass() {
37+
DiffUtils.withDefaultDiffAlgorithmFactory(MeyersDiffWithLinearSpace.factory());
38+
}
39+
40+
@AfterAll
41+
public static void resetClass() {
42+
DiffUtils.withDefaultDiffAlgorithmFactory(MeyersDiff.factory());
43+
}
44+
45+
@Test
46+
public void testPatch_Change_withExceptionProcessor() {
47+
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
48+
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
49+
50+
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
51+
52+
changeTest_from.set(2, "CDC");
53+
54+
patch.withConflictOutput(Patch.CONFLICT_PRODUCES_MERGE_CONFLICT);
55+
56+
try {
57+
List<String> data = DiffUtils.patch(changeTest_from, patch);
58+
assertEquals(11, data.size());
59+
60+
assertEquals(Arrays.asList("aaa", "bxb", "cxc", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
61+
62+
} catch (PatchFailedException e) {
63+
fail(e.getMessage());
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)