11package com .github .difflib .unifieddiff ;
22
33import com .github .difflib .DiffUtils ;
4+ import com .github .difflib .TestConstants ;
45import com .github .difflib .algorithm .DiffException ;
56import com .github .difflib .patch .Patch ;
7+ import com .github .difflib .patch .PatchFailedException ;
68import java .io .BufferedReader ;
9+ import java .io .ByteArrayInputStream ;
710import java .io .FileNotFoundException ;
811import java .io .FileReader ;
912import java .io .IOException ;
1013import java .io .StringWriter ;
1114import java .util .ArrayList ;
1215import java .util .Arrays ;
1316import java .util .List ;
17+ import static org .junit .Assert .assertEquals ;
18+ import static org .junit .Assert .fail ;
1419import org .junit .Test ;
1520
1621public 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