11import org .apache .commons .io .IOUtils ;
2+ import org .apache .commons .lang3 .function .FailableConsumer ;
3+ import org .junit .jupiter .api .Test ;
24import org .junit .jupiter .params .ParameterizedTest ;
35import org .junit .jupiter .params .provider .MethodSource ;
6+ import org .variantsync .diffdetective .diff .result .DiffParseException ;
7+ import org .variantsync .diffdetective .diff .text .DiffLineNumber ;
8+ import org .variantsync .diffdetective .util .IO ;
49import org .variantsync .diffdetective .variation .DiffLinesLabel ;
10+ import org .variantsync .diffdetective .variation .diff .DiffNode ;
11+ import org .variantsync .diffdetective .variation .diff .DiffType ;
12+ import org .variantsync .diffdetective .variation .diff .Time ;
513import org .variantsync .diffdetective .variation .diff .VariationDiff ;
614import org .variantsync .diffdetective .variation .diff .serialize .*;
715import org .variantsync .diffdetective .variation .diff .serialize .edgeformat .DefaultEdgeLabelFormat ;
816import org .variantsync .diffdetective .variation .diff .serialize .nodeformat .LabelOnlyDiffNodeFormat ;
917import org .variantsync .diffdetective .variation .diff .serialize .treeformat .CommitDiffVariationDiffLabelFormat ;
10- import org .variantsync .diffdetective .util .IO ;
11-
12- import static org .junit .jupiter .api .Assertions .fail ;
18+ import org .variantsync .diffdetective .variation .diff .source .CommitDiffVariationDiffSource ;
1319
20+ import java .io .BufferedOutputStream ;
1421import java .io .BufferedReader ;
1522import java .io .IOException ;
1623import java .nio .file .Files ;
1724import java .nio .file .Path ;
18- import java .nio .file .Paths ;
1925import java .util .List ;
2026import java .util .stream .Stream ;
2127
28+ import static org .junit .jupiter .api .Assertions .fail ;
29+
2230/**
2331 * For testing the import of a line graph.
2432 */
@@ -34,7 +42,9 @@ public class LineGraphTest {
3442 );
3543
3644 public static Stream <Path > testCases () throws IOException {
37- return Files .list (Paths .get ("src/test/resources/line_graph" ));
45+ return Files
46+ .list (Constants .RESOURCE_DIR .resolve ("line_graph" ))
47+ .filter (file -> file .getFileName ().toString ().endsWith (".lg" ));
3848 }
3949
4050 /**
@@ -49,22 +59,26 @@ public void idempotentReadWrite(Path testFile) throws IOException {
4959 }
5060 assertConsistencyForAll (variationDiffs );
5161
52- Path actualPath = testFile .getParent ().resolve (testFile .getFileName ().toString () + ".actual" );
53- try (var output = IO .newBufferedOutputStream (actualPath )) {
54- LineGraphExport .toLineGraphFormat (variationDiffs , EXPORT_OPTIONS , output );
55- }
62+ assertEqualsFile (testFile , output -> LineGraphExport .toLineGraphFormat (variationDiffs , EXPORT_OPTIONS , output ));
63+ }
5664
57- try (
58- var expectedFile = Files .newBufferedReader (testFile );
59- var actualFile = Files .newBufferedReader (actualPath );
60- ) {
61- if (!IOUtils .contentEqualsIgnoreEOL (expectedFile , actualFile )) {
62- fail ("The file " + testFile + " couldn't be exported or imported without modifications" );
63- } else {
64- // Only keep output file on errors
65- Files .delete (actualPath );
66- }
67- }
65+ @ Test
66+ public void testChildOrder () throws IOException , DiffParseException {
67+ Path expectedPath = Constants .RESOURCE_DIR .resolve ("line_graph" ).resolve ("childOrder.lg" );
68+
69+ // Note that this variation diff doesn't have a line diff representation because it
70+ // represents a move (it could be parseable using a tree differ).
71+ var root = DiffNode .createRoot (new DiffLinesLabel ());
72+ var A = DiffNode .createArtifact (DiffType .NON , new DiffLineNumber (1 , 1 , 2 ), new DiffLineNumber (1 , 1 , 2 ), new DiffLinesLabel (List .of (new DiffLinesLabel .Line ("A" , new DiffLineNumber (1 , DiffLineNumber .InvalidLineNumber , 2 )))));
73+ var B = DiffNode .createArtifact (DiffType .NON , new DiffLineNumber (2 , 2 , 1 ), new DiffLineNumber (2 , 2 , 1 ), new DiffLinesLabel (List .of (new DiffLinesLabel .Line ("B" , new DiffLineNumber (2 , DiffLineNumber .InvalidLineNumber , 1 )))));
74+ root .addChild (A , Time .BEFORE );
75+ root .addChild (B , Time .BEFORE );
76+ root .addChild (B , Time .AFTER );
77+ root .addChild (A , Time .AFTER );
78+
79+ final var variationDiff = new VariationDiff <DiffLinesLabel >(root , new CommitDiffVariationDiffSource (Path .of ("fileName" ), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ));
80+
81+ assertEqualsFile (expectedPath , output -> LineGraphExport .toLineGraphFormat (List .of (variationDiff ), EXPORT_OPTIONS , output ));
6882 }
6983
7084 /**
@@ -78,4 +92,23 @@ private static void assertConsistencyForAll(final List<VariationDiff<DiffLinesLa
7892// }
7993 treeList .forEach (VariationDiff ::assertConsistency );
8094 }
95+
96+ private static void assertEqualsFile (Path expectedPath , FailableConsumer <BufferedOutputStream , IOException > actualOutput ) throws IOException {
97+ Path actualPath = expectedPath .getParent ().resolve (expectedPath .getFileName ().toString () + ".actual" );
98+ try (var output = IO .newBufferedOutputStream (actualPath )) {
99+ actualOutput .accept (output );
100+ }
101+
102+ try (
103+ var expectedFile = Files .newBufferedReader (expectedPath );
104+ var actualFile = Files .newBufferedReader (actualPath );
105+ ) {
106+ if (!IOUtils .contentEqualsIgnoreEOL (expectedFile , actualFile )) {
107+ fail ("Expected the content of " + expectedPath + " but got the content of " + actualPath );
108+ } else {
109+ // Only keep output file on errors
110+ Files .delete (actualPath );
111+ }
112+ }
113+ }
81114}
0 commit comments