This repository was archived by the owner on Nov 12, 2019. It is now read-only.
forked from jycr/java-diff-utils
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEmptyContextUnifiedDiffTest.java
More file actions
90 lines (74 loc) · 2.65 KB
/
EmptyContextUnifiedDiffTest.java
File metadata and controls
90 lines (74 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package diffutils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import difflib.DiffUtils;
import difflib.Patch;
import difflib.PatchFailedException;
import junit.framework.TestCase;
public class EmptyContextUnifiedDiffTest extends TestCase {
public List<String> fileToLines(String filename) {
List<String> lines = new LinkedList<String>();
String line = "";
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(filename));
while ((line = in.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore ... any errors should already have been
// reported via an IOException from the final flush.
}
}
}
return lines;
}
public void testEmptyUnifiedContextPatch() {
List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "unified_empty_context_original.txt");
List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "unified_empty_context_revised.txt");
List<String> unifiedDiff = fileToLines(TestConstants.MOCK_FOLDER + "unified_empty_context_patch.txt");
List<String> patchedLines = null;
Patch patch = DiffUtils.parseUnifiedDiff(unifiedDiff);
try {
patchedLines = (List<String>) patch.applyTo(origLines);
} catch (PatchFailedException e) {
fail(e.getMessage());
}
verifyLinesEqual(patchedLines, revLines);
}
public void testEmptyUnifiedContextDiff() {
List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "unified_empty_context_original.txt");
List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "unified_empty_context_revised.txt");
List<String> patchedLines = null;
// Generate a 0-context diff then reapply
Patch generatedPatch = DiffUtils.diff(origLines, revLines);
List<String> generatedDiff = DiffUtils.generateUnifiedDiff("original", "revised", origLines, generatedPatch, 0);
Patch newPatch = DiffUtils.parseUnifiedDiff(generatedDiff);
try {
patchedLines = (List<String>) newPatch.applyTo(origLines);
} catch (PatchFailedException e) {
fail(e.getMessage());
}
verifyLinesEqual(patchedLines, revLines);
}
public void verifyLinesEqual(List<String> patchedLines, List<String> revLines) {
assertTrue(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");
}
}
}
}