Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d30d9ca

Browse files
committed
1 parent 7d620c8 commit d30d9ca

File tree

3 files changed

+55
-38
lines changed

3 files changed

+55
-38
lines changed

src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,28 @@ private static void processDeltas(Consumer<String> writer,
114114
AbstractDelta<String> curDelta = deltas.get(0);
115115

116116
// NOTE: +1 to overcome the 0-offset Position
117-
int origStart = curDelta.getSource().getPosition() + 1;
117+
int origStart = curDelta.getSource().getPosition() + 1 - contextSize;
118118
if (origStart < 1) {
119119
origStart = 1;
120120
}
121121

122-
int revStart = curDelta.getTarget().getPosition() + 1;
122+
int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;
123123
if (revStart < 1) {
124124
revStart = 1;
125125
}
126126

127127
// find the start of the wrapper context code
128-
int contextStart = curDelta.getSource().getPosition();
128+
int contextStart = curDelta.getSource().getPosition() - contextSize;
129129
if (contextStart < 0) {
130130
contextStart = 0; // clamp to the start of the file
131131
}
132132

133-
// // output the context before the first Delta
134-
// for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
135-
// buffer.add(" " + curDelta.getSource().getLines().get(line - contextStart));
136-
// origTotal++;
137-
// revTotal++;
138-
// }
133+
// output the context before the first Delta
134+
for (line = contextStart; line < curDelta.getSource().getPosition(); line++) { //
135+
buffer.add(" " + origLines.get(line));
136+
origTotal++;
137+
revTotal++;
138+
}
139139
// output the first Delta
140140
getDeltaText(txt -> buffer.add(txt), curDelta);
141141
origTotal += curDelta.getSource().getLines().size();

src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.ArrayList;
1111
import java.util.Arrays;
1212
import java.util.List;
13+
import static java.util.stream.Collectors.joining;
1314
import static org.junit.Assert.assertEquals;
1415
import static org.junit.Assert.fail;
1516
import org.junit.Test;
@@ -110,6 +111,8 @@ private void verify(List<String> origLines, List<String> revLines,
110111
List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
111112
origLines, patch, 10);
112113

114+
System.out.println(unifiedDiff.stream().collect(joining("\n")));
115+
113116
Patch<String> fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
114117
List<String> patchedLines;
115118
try {

src/test/java/com/github/difflib/unifieddiff/UnifiedDiffRoundTripTest.java

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package com.github.difflib.unifieddiff;
22

33
import com.github.difflib.DiffUtils;
4+
import com.github.difflib.TestConstants;
45
import com.github.difflib.algorithm.DiffException;
56
import com.github.difflib.patch.Patch;
7+
import com.github.difflib.patch.PatchFailedException;
68
import java.io.BufferedReader;
9+
import java.io.ByteArrayInputStream;
710
import java.io.FileNotFoundException;
811
import java.io.FileReader;
912
import java.io.IOException;
1013
import java.io.StringWriter;
1114
import java.util.ArrayList;
1215
import java.util.Arrays;
1316
import java.util.List;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.fail;
1419
import org.junit.Test;
1520

1621
public class UnifiedDiffRoundTripTest {
@@ -26,13 +31,13 @@ public static List<String> fileToLines(String filename) throws FileNotFoundExcep
2631
return lines;
2732
}
2833

29-
// @Test
30-
// public void testGenerateUnified() throws DiffException, IOException {
31-
// List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
32-
// List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
33-
//
34-
// verify(origLines, revLines, "original.txt", "revised.txt");
35-
// }
34+
@Test
35+
public void testGenerateUnified() throws DiffException, IOException {
36+
List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
37+
List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
38+
39+
verify(origLines, revLines, "original.txt", "revised.txt");
40+
}
3641
//
3742
// @Test
3843
// public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
@@ -41,6 +46,7 @@ public static List<String> fileToLines(String filename) throws FileNotFoundExcep
4146
//
4247
// verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
4348
// }
49+
4450
@Test
4551
public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOException {
4652
List<String> test = Arrays.asList("abc");
@@ -109,26 +115,34 @@ public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOEx
109115
// UnifiedDiffUtils.parseUnifiedDiff(udiff);
110116
// }
111117
//
112-
// private void verify(List<String> origLines, List<String> revLines,
113-
// String originalFile, String revisedFile) throws DiffException {
114-
// Patch<String> patch = DiffUtils.diff(origLines, revLines);
115-
// List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile,
116-
// origLines, patch, 10);
117-
//
118-
// Patch<String> fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);
119-
// List<String> patchedLines;
120-
// try {
121-
// patchedLines = fromUnifiedPatch.applyTo(origLines);
122-
// assertEquals(revLines.size(), patchedLines.size());
123-
// for (int i = 0; i < revLines.size(); i++) {
124-
// String l1 = revLines.get(i);
125-
// String l2 = patchedLines.get(i);
126-
// if (!l1.equals(l2)) {
127-
// fail("Line " + (i + 1) + " of the patched file did not match the revised original");
128-
// }
129-
// }
130-
// } catch (PatchFailedException e) {
131-
// fail(e.getMessage());
132-
// }
133-
// }
118+
private void verify(List<String> origLines, List<String> revLines,
119+
String originalFile, String revisedFile) throws DiffException, IOException {
120+
Patch<String> patch = DiffUtils.diff(origLines, revLines);
121+
122+
StringWriter writer = new StringWriter();
123+
UnifiedDiffWriter.write(
124+
UnifiedDiff.from("header", "tail", UnifiedDiffFile.from(originalFile, revisedFile, patch)),
125+
name -> origLines,
126+
writer, 10);
127+
128+
System.out.println(writer.toString());
129+
130+
UnifiedDiff unifiedDiff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(writer.toString().getBytes()));
131+
132+
Patch<String> fromUnifiedPatch = unifiedDiff.getFiles().get(0).getPatch();
133+
List<String> patchedLines;
134+
try {
135+
patchedLines = fromUnifiedPatch.applyTo(origLines);
136+
assertEquals(revLines.size(), patchedLines.size());
137+
for (int i = 0; i < revLines.size(); i++) {
138+
String l1 = revLines.get(i);
139+
String l2 = patchedLines.get(i);
140+
if (!l1.equals(l2)) {
141+
fail("Line " + (i + 1) + " of the patched file did not match the revised original");
142+
}
143+
}
144+
} catch (PatchFailedException e) {
145+
fail(e.getMessage());
146+
}
147+
}
134148
}

0 commit comments

Comments
 (0)