1
1
package com .github .difflib .unifieddiff ;
2
2
3
3
import com .github .difflib .DiffUtils ;
4
+ import com .github .difflib .TestConstants ;
4
5
import com .github .difflib .algorithm .DiffException ;
5
6
import com .github .difflib .patch .Patch ;
7
+ import com .github .difflib .patch .PatchFailedException ;
6
8
import java .io .BufferedReader ;
9
+ import java .io .ByteArrayInputStream ;
7
10
import java .io .FileNotFoundException ;
8
11
import java .io .FileReader ;
9
12
import java .io .IOException ;
10
13
import java .io .StringWriter ;
11
14
import java .util .ArrayList ;
12
15
import java .util .Arrays ;
13
16
import java .util .List ;
17
+ import static org .junit .Assert .assertEquals ;
18
+ import static org .junit .Assert .fail ;
14
19
import org .junit .Test ;
15
20
16
21
public class UnifiedDiffRoundTripTest {
@@ -26,13 +31,13 @@ public static List<String> fileToLines(String filename) throws FileNotFoundExcep
26
31
return lines ;
27
32
}
28
33
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
+ }
36
41
//
37
42
// @Test
38
43
// public void testGenerateUnifiedWithOneDelta() throws DiffException, IOException {
@@ -41,6 +46,7 @@ public static List<String> fileToLines(String filename) throws FileNotFoundExcep
41
46
//
42
47
// verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
43
48
// }
49
+
44
50
@ Test
45
51
public void testGenerateUnifiedDiffWithoutAnyDeltas () throws DiffException , IOException {
46
52
List <String > test = Arrays .asList ("abc" );
@@ -109,26 +115,34 @@ public void testGenerateUnifiedDiffWithoutAnyDeltas() throws DiffException, IOEx
109
115
// UnifiedDiffUtils.parseUnifiedDiff(udiff);
110
116
// }
111
117
//
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
+ }
134
148
}
0 commit comments