GeezerVison is showing github.com — implemented multi algorithm patch test · sharkbobo/java-diff-utils@2294c5b
File treeExpand file treeCollapse file tree Open diff view settings main/java/com/github/difflib/algorithm/myers test/java/com/github/difflib/patch
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/algorithm/myers/MeyersDiffWithLinearSpace.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | package com.github.difflib.algorithm.myers; | 17 | 17 |
| 18 | 18 | import com.github.difflib.algorithm.Change; | | 19 | +import com.github.difflib.algorithm.DiffAlgorithmFactory; | 19 | 20 | import com.github.difflib.algorithm.DiffAlgorithmI; | 20 | 21 | import com.github.difflib.algorithm.DiffAlgorithmListener; | 21 | 22 | import com.github.difflib.patch.DeltaType; | @@ -221,4 +222,23 @@ public Snake(final int start, final int end, final int diag) { | 221 | 222 | this.diag = diag; | 222 | 223 | } | 223 | 224 | } | | 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 | + } | 224 | 244 | } |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/test/java/com/github/difflib/patch/PatchWithAllDiffAlgorithmsTest.java +48-15Lines changed: 48 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | import java.util.Arrays; | 12 | 12 | import java.util.List; | 13 | 13 |
| 14 | | -import org.junit.jupiter.api.Test; | 15 | 14 |
| 16 | 15 | 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 | + } | 17 | 37 |
| 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 | + | 22 | 43 | final List<String> insertTest_from = Arrays.asList("hhh"); | 23 | 44 | final List<String> insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll"); | 24 | 45 |
| @@ -30,8 +51,11 @@ public void testPatch_Insert() { | 30 | 51 | } | 31 | 52 | } | 32 | 53 |
| 33 | | - @Test | 34 | | - public void testPatch_Delete() { | | 54 | + @ParameterizedTest | | 55 | + @MethodSource("provideAlgorithms") | | 56 | + public void testPatch_Delete(DiffAlgorithmFactory factory) { | | 57 | + DiffUtils.withDefaultDiffAlgorithmFactory(factory); | | 58 | + | 35 | 59 | final List<String> deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh"); | 36 | 60 | final List<String> deleteTest_to = Arrays.asList("ggg"); | 37 | 61 |
| @@ -43,8 +67,11 @@ public void testPatch_Delete() { | 43 | 67 | } | 44 | 68 | } | 45 | 69 |
| 46 | | - @Test | 47 | | - public void testPatch_Change() { | | 70 | + @ParameterizedTest | | 71 | + @MethodSource("provideAlgorithms") | | 72 | + public void testPatch_Change(DiffAlgorithmFactory factory) { | | 73 | + DiffUtils.withDefaultDiffAlgorithmFactory(factory); | | 74 | + | 48 | 75 | final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd"); | 49 | 76 | final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd"); | 50 | 77 |
| @@ -56,8 +83,11 @@ public void testPatch_Change() { | 56 | 83 | } | 57 | 84 | } | 58 | 85 |
| 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 | + | 61 | 91 | final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd"); | 62 | 92 | final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd"); | 63 | 93 |
| @@ -79,8 +109,11 @@ public void testPatch_Serializable() throws IOException, ClassNotFoundException | 79 | 109 |
| 80 | 110 | } | 81 | 111 |
| 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 | + | 84 | 117 | final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd"); | 85 | 118 | final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd"); | 86 | 119 |
| @@ -93,9 +126,9 @@ public void testPatch_Change_withExceptionProcessor() { | 93 | 126 | try { | 94 | 127 | List<String> data = DiffUtils.patch(changeTest_from, patch); | 95 | 128 | assertEquals(9, data.size()); | 96 | | - | | 129 | + | 97 | 130 | assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data); | 98 | | - | | 131 | + | 99 | 132 | } catch (PatchFailedException e) { | 100 | 133 | fail(e.getMessage()); | 101 | 134 | } | |
Collapse file Copy file name to clipboard +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboard +66Lines changed: 66 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|