implemented multi algorithm patch test

This commit is contained in:
Tobias Warneke
2021-08-15 00:51:24 +02:00
parent 7203d4890f
commit 2294c5be7b
4 changed files with 184 additions and 14 deletions

View File

@@ -16,6 +16,7 @@
package com.github.difflib.algorithm.myers;
import com.github.difflib.algorithm.Change;
import com.github.difflib.algorithm.DiffAlgorithmFactory;
import com.github.difflib.algorithm.DiffAlgorithmI;
import com.github.difflib.algorithm.DiffAlgorithmListener;
import com.github.difflib.patch.DeltaType;
@@ -221,4 +222,23 @@ public class MeyersDiffWithLinearSpace<T> implements DiffAlgorithmI<T> {
this.diag = diag;
}
}
/**
* Factory to create instances of this specific diff algorithm.
*/
public static DiffAlgorithmFactory factory() {
return new DiffAlgorithmFactory() {
@Override
public <T> DiffAlgorithmI<T>
create() {
return new MeyersDiffWithLinearSpace<T>();
}
@Override
public <T> DiffAlgorithmI<T>
create(BiPredicate < T, T > equalizer) {
return new MeyersDiffWithLinearSpace<T>(equalizer);
}
};
}
}

View File

@@ -11,14 +11,35 @@ import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.github.difflib.DiffUtils;
import com.github.difflib.algorithm.DiffAlgorithmFactory;
import com.github.difflib.algorithm.myers.MeyersDiff;
import com.github.difflib.algorithm.myers.MeyersDiffWithLinearSpace;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class PatchTest {
public class PatchWithAllDiffAlgorithmsTest {
@Test
public void testPatch_Insert() {
private static Stream<Arguments> provideAlgorithms() {
return Stream.of(
Arguments.of(MeyersDiff.factory()),
Arguments.of(MeyersDiffWithLinearSpace.factory()));
}
@AfterAll
public static void afterAll() {
DiffUtils.withDefaultDiffAlgorithmFactory(MeyersDiff.factory());
}
@ParameterizedTest
@MethodSource("provideAlgorithms")
public void testPatch_Insert(DiffAlgorithmFactory factory) {
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
final List<String> insertTest_from = Arrays.asList("hhh");
final List<String> insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
@@ -30,8 +51,11 @@ public class PatchTest {
}
}
@Test
public void testPatch_Delete() {
@ParameterizedTest
@MethodSource("provideAlgorithms")
public void testPatch_Delete(DiffAlgorithmFactory factory) {
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
final List<String> deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh");
final List<String> deleteTest_to = Arrays.asList("ggg");
@@ -43,8 +67,11 @@ public class PatchTest {
}
}
@Test
public void testPatch_Change() {
@ParameterizedTest
@MethodSource("provideAlgorithms")
public void testPatch_Change(DiffAlgorithmFactory factory) {
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
@@ -56,8 +83,11 @@ public class PatchTest {
}
}
@Test
public void testPatch_Serializable() throws IOException, ClassNotFoundException {
@ParameterizedTest
@MethodSource("provideAlgorithms")
public void testPatch_Serializable(DiffAlgorithmFactory factory) throws IOException, ClassNotFoundException {
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
@@ -79,8 +109,11 @@ public class PatchTest {
}
@Test
public void testPatch_Change_withExceptionProcessor() {
@ParameterizedTest
@MethodSource("provideAlgorithms")
public void testPatch_Change_withExceptionProcessor(DiffAlgorithmFactory factory) {
DiffUtils.withDefaultDiffAlgorithmFactory(factory);
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
@@ -93,9 +126,9 @@ public class PatchTest {
try {
List<String> data = DiffUtils.patch(changeTest_from, patch);
assertEquals(9, data.size());
assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
} catch (PatchFailedException e) {
fail(e.getMessage());
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2021 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib.patch;
import com.github.difflib.DiffUtils;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
/**
*
* @author tw
*/
public class PatchWithMeyerDiffTest {
@Test
public void testPatch_Change_withExceptionProcessor() {
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
changeTest_from.set(2, "CDC");
patch.withConflictOutput(Patch.CONFLICT_PRODUCES_MERGE_CONFLICT);
try {
List<String> data = DiffUtils.patch(changeTest_from, patch);
assertEquals(9, data.size());
assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
} catch (PatchFailedException e) {
fail(e.getMessage());
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2021 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib.patch;
import com.github.difflib.DiffUtils;
import com.github.difflib.algorithm.myers.MeyersDiff;
import com.github.difflib.algorithm.myers.MeyersDiffWithLinearSpace;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
*
* @author tw
*/
public class PatchWithMeyerDiffWithLinearSpaceTest {
@BeforeAll
public static void setupClass() {
DiffUtils.withDefaultDiffAlgorithmFactory(MeyersDiffWithLinearSpace.factory());
}
@AfterAll
public static void resetClass() {
DiffUtils.withDefaultDiffAlgorithmFactory(MeyersDiff.factory());
}
@Test
public void testPatch_Change_withExceptionProcessor() {
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
changeTest_from.set(2, "CDC");
patch.withConflictOutput(Patch.CONFLICT_PRODUCES_MERGE_CONFLICT);
try {
List<String> data = DiffUtils.patch(changeTest_from, patch);
assertEquals(11, data.size());
assertEquals(Arrays.asList("aaa", "bxb", "cxc", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
} catch (PatchFailedException e) {
fail(e.getMessage());
}
}
}