Java Code Examples for com.github.difflib.patch.Patch#generate()

The following examples show how to use com.github.difflib.patch.Patch#generate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyersDiffTest.java    From java-diff-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiffMyersExample1ForwardWithListener() {
    List<String> original = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revised = Arrays.asList("C", "B", "A", "B", "A", "C");
    
    List<String> logdata = new ArrayList<>();
    final Patch<String> patch = Patch.generate(original, revised, 
            new MyersDiff<String>().computeDiff(original, revised, new DiffAlgorithmListener() {
        @Override
        public void diffStart() {
            logdata.add("start");
        }

        @Override
        public void diffStep(int value, int max) {
            logdata.add(value + " - " + max);
        }

        @Override
        public void diffEnd() {
            logdata.add("end");
        }
    }));
    assertNotNull(patch);
    assertEquals(4, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString());
    System.out.println(logdata);
    assertEquals(8, logdata.size());
}
 
Example 2
Source File: MyersDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffMyersExample1Forward() {
    List<String> original = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revised = Arrays.asList("C", "B", "A", "B", "A", "C");
    final Patch<String> patch = Patch.generate(original, revised, new MyersDiff<String>().computeDiff(original, revised, null));
    assertNotNull(patch);
    assertEquals(4, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [InsertDelta, position: 3, lines: [B]], [DeleteDelta, position: 5, lines: [B]], [InsertDelta, position: 7, lines: [C]]]}", patch.toString());
}
 
Example 3
Source File: HistogramDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Test of diff method, of class HistogramDiff.
 */
@Test
public void testDiff() throws PatchFailedException {
    List<String> orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revList = Arrays.asList("C", "B", "A", "B", "A", "C");
    final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff().computeDiff(orgList, revList, null));
    System.out.println(patch);
    assertNotNull(patch);
    assertEquals(3, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString());

    List<String> patched = patch.applyTo(orgList);
    assertEquals(revList, patched);
}
 
Example 4
Source File: HistogramDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiffWithListener() throws PatchFailedException {
    List<String> orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
    List<String> revList = Arrays.asList("C", "B", "A", "B", "A", "C");
    
    List<String> logdata = new ArrayList<>();
    final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff().computeDiff(orgList, revList, new DiffAlgorithmListener() {
        @Override
        public void diffStart() {
            logdata.add("start");
        }

        @Override
        public void diffStep(int value, int max) {
            logdata.add(value + " - " + max);
        }

        @Override
        public void diffEnd() {
            logdata.add("end");
        }
    }));
    System.out.println(patch);
    assertNotNull(patch);
    assertEquals(3, patch.getDeltas().size());
    assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString());

    List<String> patched = patch.applyTo(orgList);
    assertEquals(revList, patched);
    
    System.out.println(logdata);
    assertEquals(17, logdata.size());
}
 
Example 5
Source File: LRHistogramDiffTest.java    From java-diff-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, PatchFailedException {
    ZipFile zip = new ZipFile("target/test-classes/mocks/large_dataset1.zip");
    List<String> original = readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta")));
    List<String> revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")));

    List<String> logdata = new ArrayList<>();
    Patch<String> patch = Patch.generate(original, revised, new HistogramDiff().computeDiff(original, revised, new DiffAlgorithmListener() {
        @Override
        public void diffStart() {
            logdata.add("start");
        }

        @Override
        public void diffStep(int value, int max) {
            logdata.add(value + " - " + max);
        }

        @Override
        public void diffEnd() {
            logdata.add("end");
        }
    }));

    assertEquals(34, patch.getDeltas().size());

    List<String> created = patch.applyTo(original);
    assertArrayEquals(revised.toArray(), created.toArray());
    
    assertEquals(246579, logdata.size());
}
 
Example 6
Source File: DiffUtils.java    From java-diff-utils with Apache License 2.0 3 votes vote down vote up
/**
 * Computes the difference between the original and revised list of elements with default diff
 * algorithm
 *
 * @param original The original text. Must not be {@code null}.
 * @param revised The revised text. Must not be {@code null}.
 * @param algorithm The diff algorithm. Must not be {@code null}.
 * @param progress The diff algorithm listener.
 * @param includeEqualParts Include equal data parts into the patch.
 * @return The patch describing the difference between the original and revised sequences. Never
 * {@code null}.
 */
public static <T> Patch<T> diff(List<T> original, List<T> revised,
        DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress, 
        boolean includeEqualParts) {
    Objects.requireNonNull(original, "original must not be null");
    Objects.requireNonNull(revised, "revised must not be null");
    Objects.requireNonNull(algorithm, "algorithm must not be null");

    return Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress), includeEqualParts);
}