This commit is contained in:
wumpz
2019-06-05 23:28:13 +02:00
parent 7d620c825c
commit d30d9ca23a
3 changed files with 55 additions and 38 deletions

View File

@@ -114,28 +114,28 @@ public class UnifiedDiffWriter {
AbstractDelta<String> 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();

View File

@@ -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<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
origLines, patch, 10);
System.out.println(unifiedDiff.stream().collect(joining("\n")));
Patch<String> fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
List<String> patchedLines;
try {

View File

@@ -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<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
// List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
//
// verify(origLines, revLines, "original.txt", "revised.txt");
// }
@Test
public void testGenerateUnified() throws DiffException, IOException {
List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
List<String> 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<String> test = Arrays.asList("abc");
@@ -109,26 +115,34 @@ public class UnifiedDiffRoundTripTest {
// UnifiedDiffUtils.parseUnifiedDiff(udiff);
// }
//
// private void verify(List<String> origLines, List<String> revLines,
// String originalFile, String revisedFile) throws DiffException {
// Patch<String> patch = DiffUtils.diff(origLines, revLines);
// List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
// origLines, patch, 10);
//
// Patch<String> fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
// List<String> 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<String> origLines, List<String> revLines,
String originalFile, String revisedFile) throws DiffException, IOException {
Patch<String> 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<String> fromUnifiedPatch = unifiedDiff.getFiles().get(0).getPatch();
List<String> 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());
}
}
}