From d30d9ca23a17f60cdc703b90c405f1887b11e4cd Mon Sep 17 00:00:00 2001 From: wumpz Date: Wed, 5 Jun 2019 23:28:13 +0200 Subject: [PATCH] --- .../unifieddiff/UnifiedDiffWriter.java | 18 ++--- .../difflib/GenerateUnifiedDiffTest.java | 3 + .../unifieddiff/UnifiedDiffRoundTripTest.java | 72 +++++++++++-------- 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java index 8af5d9e..c61f83b 100644 --- a/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java +++ b/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java @@ -114,28 +114,28 @@ public class UnifiedDiffWriter { AbstractDelta curDelta = deltas.get(0); // NOTE: +1 to overcome the 0-offset Position - int origStart = curDelta.getSource().getPosition() + 1; + int origStart = curDelta.getSource().getPosition() + 1 - contextSize; if (origStart < 1) { origStart = 1; } - int revStart = curDelta.getTarget().getPosition() + 1; + int revStart = curDelta.getTarget().getPosition() + 1 - contextSize; if (revStart < 1) { revStart = 1; } // find the start of the wrapper context code - int contextStart = curDelta.getSource().getPosition(); + int contextStart = curDelta.getSource().getPosition() - contextSize; if (contextStart < 0) { contextStart = 0; // clamp to the start of the file } -// // output the context before the first Delta -// for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { // -// buffer.add(" " + curDelta.getSource().getLines().get(line - contextStart)); -// origTotal++; -// revTotal++; -// } + // output the context before the first Delta + for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { // + buffer.add(" " + origLines.get(line)); + origTotal++; + revTotal++; + } // output the first Delta getDeltaText(txt -> buffer.add(txt), curDelta); origTotal += curDelta.getSource().getLines().size(); diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java index 3fb2082..c6bfc4e 100644 --- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java +++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import static java.util.stream.Collectors.joining; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import org.junit.Test; @@ -110,6 +111,8 @@ public class GenerateUnifiedDiffTest { List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile, origLines, patch, 10); + System.out.println(unifiedDiff.stream().collect(joining("\n"))); + Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff); List patchedLines; try { diff --git a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java index f873795..0a98203 100644 --- a/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java +++ b/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java @@ -1,9 +1,12 @@ package com.github.difflib.unifieddiff; import com.github.difflib.DiffUtils; +import com.github.difflib.TestConstants; import com.github.difflib.algorithm.DiffException; import com.github.difflib.patch.Patch; +import com.github.difflib.patch.PatchFailedException; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; @@ -11,6 +14,8 @@ import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.junit.Test; public class UnifiedDiffRoundTripTest { @@ -26,13 +31,13 @@ public class UnifiedDiffRoundTripTest { return lines; } -// @Test -// public void testGenerateUnified() throws DiffException, IOException { -// List origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt"); -// List revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt"); -// -// verify(origLines, revLines, "original.txt", "revised.txt"); -// } + @Test + public void testGenerateUnified() throws DiffException, IOException { + List origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt"); + List revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt"); + + verify(origLines, revLines, "original.txt", "revised.txt"); + } // // @Test // public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException { @@ -41,6 +46,7 @@ public class UnifiedDiffRoundTripTest { // // verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt"); // } + @Test public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOException { List test = Arrays.asList("abc"); @@ -109,26 +115,34 @@ public class UnifiedDiffRoundTripTest { // UnifiedDiffUtils.parseUnifiedDiff(udiff); // } // -// private void verify(List origLines, List revLines, -// String originalFile, String revisedFile) throws DiffException { -// Patch patch = DiffUtils.diff(origLines, revLines); -// List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile, -// origLines, patch, 10); -// -// Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff); -// List patchedLines; -// try { -// patchedLines = fromUnifiedPatch.applyTo(origLines); -// assertEquals(revLines.size(), patchedLines.size()); -// for (int i = 0; i < revLines.size(); i++) { -// String l1 = revLines.get(i); -// String l2 = patchedLines.get(i); -// if (!l1.equals(l2)) { -// fail("Line " + (i + 1) + " of the patched file did not match the revised original"); -// } -// } -// } catch (PatchFailedException e) { -// fail(e.getMessage()); -// } -// } + private void verify(List origLines, List revLines, + String originalFile, String revisedFile) throws DiffException, IOException { + Patch patch = DiffUtils.diff(origLines, revLines); + + StringWriter writer = new StringWriter(); + UnifiedDiffWriter.write( + UnifiedDiff.from("header", "tail", UnifiedDiffFile.from(originalFile, revisedFile, patch)), + name -> origLines, + writer, 10); + + System.out.println(writer.toString()); + + UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(writer.toString().getBytes())); + + Patch fromUnifiedPatch = unifiedDiff.getFiles().get(0).getPatch(); + List patchedLines; + try { + patchedLines = fromUnifiedPatch.applyTo(origLines); + assertEquals(revLines.size(), patchedLines.size()); + for (int i = 0; i < revLines.size(); i++) { + String l1 = revLines.get(i); + String l2 = patchedLines.get(i); + if (!l1.equals(l2)) { + fail("Line " + (i + 1) + " of the patched file did not match the revised original"); + } + } + } catch (PatchFailedException e) { + fail(e.getMessage()); + } + } }