11import com .github .gumtreediff .matchers .Matcher ;
22import com .github .gumtreediff .matchers .Matchers ;
33import org .apache .commons .io .IOUtils ;
4+ import org .eclipse .jgit .diff .DiffAlgorithm ;
45import org .junit .jupiter .params .ParameterizedTest ;
56import org .junit .jupiter .params .provider .MethodSource ;
67import org .variantsync .diffdetective .diff .result .DiffParseException ;
7- import org .variantsync .diffdetective .feature .cpp .CPPAnnotationParser ;
88import org .variantsync .diffdetective .util .IO ;
99import org .variantsync .diffdetective .variation .DiffLinesLabel ;
10+ import org .variantsync .diffdetective .variation .diff .DiffNode ;
1011import org .variantsync .diffdetective .variation .diff .VariationDiff ;
1112import org .variantsync .diffdetective .variation .diff .construction .GumTreeDiff ;
1213import org .variantsync .diffdetective .variation .diff .parse .VariationDiffParseOptions ;
1718import org .variantsync .diffdetective .variation .diff .serialize .edgeformat .ChildOrderEdgeFormat ;
1819import org .variantsync .diffdetective .variation .diff .serialize .edgeformat .DefaultEdgeLabelFormat ;
1920import org .variantsync .diffdetective .variation .diff .serialize .nodeformat .FullNodeFormat ;
21+ import org .variantsync .diffdetective .variation .diff .source .VariationDiffSource ;
2022import org .variantsync .diffdetective .variation .tree .VariationTree ;
2123import org .variantsync .diffdetective .variation .tree .source .LocalFileSource ;
2224
@@ -33,7 +35,7 @@ public class TreeDiffingTest {
3335 private final static Path testDir = Constants .RESOURCE_DIR .resolve ("tree-diffing" );
3436 private static Pattern expectedFileNameRegex = Pattern .compile ("([^_]+)_([^_]+)_expected.lg" );
3537
36- private static record TestCase (String basename , String matcherName , Matcher matcher ) {
38+ private static record TestCase (String expectedDir , String basename , String matcherName , Matcher matcher ) {
3739 public Path beforeEdit () {
3840 return testDir .resolve (String .format ("%s.before" , basename ()));
3941 }
@@ -43,28 +45,29 @@ public Path afterEdit() {
4345 }
4446
4547 public Path actual () {
46- return testDir .resolve (String .format ("%s_%s_actual.lg" , basename (), matcherName ()));
48+ return testDir .resolve (expectedDir ). resolve ( String .format ("%s_%s_actual.lg" , basename (), matcherName ()));
4749 }
4850
4951 public Path expected () {
50- return testDir .resolve (String .format ("%s_%s_expected.lg" , basename (), matcherName ()));
52+ return testDir .resolve (expectedDir ). resolve ( String .format ("%s_%s_expected.lg" , basename (), matcherName ()));
5153 }
5254
5355 public Path visualisation () {
54- return testDir .resolve ("tex" ).resolve (String .format ("%s_%s.tex" , basename (), matcherName ()));
56+ return testDir .resolve (expectedDir ). resolve ( "tex" ).resolve (String .format ("%s_%s.tex" , basename (), matcherName ()));
5557 }
5658 }
5759
58- private static Stream <TestCase > testCases () throws IOException {
60+ private static Stream <TestCase > testCases (String expectedDir ) throws IOException {
5961 return Files
60- .list (testDir )
62+ .list (testDir . resolve ( expectedDir ) )
6163 .mapMulti (((path , result ) -> {
6264 String filename = path .getFileName ().toString ();
6365 var filenameMatcher = expectedFileNameRegex .matcher (filename );
6466 if (filenameMatcher .matches ()) {
6567 var treeMatcherName = filenameMatcher .group (2 );
6668
6769 result .accept (new TestCase (
70+ expectedDir ,
6871 filenameMatcher .group (1 ),
6972 treeMatcherName ,
7073 Matchers .getInstance ().getMatcher (treeMatcherName ))
@@ -73,14 +76,40 @@ private static Stream<TestCase> testCases() throws IOException {
7376 }));
7477 }
7578
79+ private static Stream <TestCase > createMatchingTestCases () throws IOException {
80+ return testCases ("createMatching" );
81+ }
82+
7683 @ ParameterizedTest
77- @ MethodSource ("testCases " )
78- public void testCase (TestCase testCase ) throws IOException , DiffParseException {
84+ @ MethodSource ("createMatchingTestCases " )
85+ public void createMatchingTestCase (TestCase testCase ) throws IOException , DiffParseException {
7986 VariationTree <DiffLinesLabel > beforeEdit = parseVariationTree (testCase .beforeEdit ());
8087 VariationTree <DiffLinesLabel > afterEdit = parseVariationTree (testCase .afterEdit ());
88+ assertExpectedVariationDiffs (testCase , GumTreeDiff .diffUsingMatching (beforeEdit , afterEdit ));
89+ }
90+
91+ private static Stream <TestCase > improveMatchingTestCases () throws IOException {
92+ return testCases ("improveMatching" );
93+ }
8194
82- VariationDiff <DiffLinesLabel > variationDiff = GumTreeDiff .diffUsingMatching (beforeEdit , afterEdit );
95+ @ ParameterizedTest
96+ @ MethodSource ("improveMatchingTestCases" )
97+ public void improveMatchingTestCase (TestCase testCase ) throws IOException , DiffParseException {
98+ VariationDiff <DiffLinesLabel > variationDiff =
99+ VariationDiff .fromFiles (
100+ testCase .beforeEdit (),
101+ testCase .afterEdit (),
102+ DiffAlgorithm .SupportedAlgorithm .MYERS ,
103+ VariationDiffParseOptions .Default
104+ );
105+
106+ DiffNode <DiffLinesLabel > improvedDiffNode = GumTreeDiff .improveMatching (variationDiff .getRoot (), testCase .matcher ());
107+ VariationDiff <DiffLinesLabel > improvedVariationDiff = new VariationDiff <>(improvedDiffNode , VariationDiffSource .Unknown );
108+
109+ assertExpectedVariationDiffs (testCase , improvedVariationDiff );
110+ }
83111
112+ private static void assertExpectedVariationDiffs (TestCase testCase , VariationDiff <DiffLinesLabel > variationDiff ) throws IOException {
84113 try (var output = IO .newBufferedOutputStream (testCase .actual ())) {
85114 new LineGraphExporter <>(new Format <>(new FullNodeFormat (), new ChildOrderEdgeFormat <>()))
86115 .exportVariationDiff (variationDiff , output );
@@ -111,15 +140,12 @@ public void testCase(TestCase testCase) throws IOException, DiffParseException {
111140 }
112141 }
113142
114- public VariationTree <DiffLinesLabel > parseVariationTree (Path filename ) throws IOException , DiffParseException {
143+ private static VariationTree <DiffLinesLabel > parseVariationTree (Path filename ) throws IOException , DiffParseException {
115144 try (var file = Files .newBufferedReader (filename )) {
116145 return new VariationTree <>(
117146 VariationDiffParser .createVariationTree (
118147 file ,
119- new VariationDiffParseOptions (
120- new CPPAnnotationParser (),
121- false ,
122- false )
148+ VariationDiffParseOptions .Default
123149 ).getRoot ().projection (BEFORE ).toVariationTree (),
124150 new LocalFileSource (filename )
125151 );
0 commit comments