Skip to content

Commit 9cc672e

Browse files
committed
test: create tests for GumTreeDiff.improveMatching
1 parent ef6a1dc commit 9cc672e

158 files changed

Lines changed: 995 additions & 22 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/org/variantsync/diffdetective/gumtree/VariationDiffAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public VariationDiffAdapter(Projection<L> node) {
2222
super(node);
2323
}
2424

25+
@Override
2526
protected VariationTreeAdapter<L> newInstance(VariationNode<?, L> node) {
2627
return new VariationDiffAdapter<>(Cast.unchecked(node));
2728
}

src/main/java/org/variantsync/diffdetective/gumtree/VariationTreeAdapter.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.Iterator;
5+
import java.util.LinkedHashMap;
56
import java.util.Map.Entry;
67
import java.util.stream.Collectors;
78

@@ -24,9 +25,11 @@
2425
public class VariationTreeAdapter<L extends Label> extends AbstractTree {
2526
private String cachedLabel;
2627
private VariationNode<?, L> backingNode;
28+
private LinkedHashMap<String, Object> metadata;
2729

2830
public VariationTreeAdapter(VariationNode<?, L> node) {
2931
this.backingNode = node;
32+
this.metadata = new LinkedHashMap<>();
3033

3134
if (backingNode.isConditionalAnnotation()) {
3235
cachedLabel = backingNode.getFormula().toString();
@@ -41,7 +44,7 @@ public VariationTreeAdapter(VariationNode<?, L> node) {
4144
setChildren(children);
4245
}
4346

44-
protected VariationTreeAdapter newInstance(VariationNode<?, L> node) {
47+
protected VariationTreeAdapter<L> newInstance(VariationNode<?, L> node) {
4548
return new VariationTreeAdapter<>(node);
4649
}
4750

@@ -65,12 +68,12 @@ public int getLength() {
6568

6669
@Override
6770
public Iterator<Entry<String, Object>> getMetadata() {
68-
throw new UnsupportedOperationException();
71+
return metadata.entrySet().iterator();
6972
}
7073

7174
@Override
72-
public Object getMetadata(String arg0) {
73-
throw new UnsupportedOperationException();
75+
public Object getMetadata(String key) {
76+
return metadata.get(key);
7477
}
7578

7679
/**
@@ -97,8 +100,8 @@ public void setLength(int length) {
97100
}
98101

99102
@Override
100-
public Object setMetadata(String name, Object value) {
101-
throw new UnsupportedOperationException();
103+
public Object setMetadata(String key, Object value) {
104+
return metadata.put(key, value);
102105
}
103106

104107
@Override

src/test/java/TreeDiffingTest.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import com.github.gumtreediff.matchers.Matcher;
22
import com.github.gumtreediff.matchers.Matchers;
33
import org.apache.commons.io.IOUtils;
4+
import org.eclipse.jgit.diff.DiffAlgorithm;
45
import org.junit.jupiter.params.ParameterizedTest;
56
import org.junit.jupiter.params.provider.MethodSource;
67
import org.variantsync.diffdetective.diff.result.DiffParseException;
7-
import org.variantsync.diffdetective.feature.cpp.CPPAnnotationParser;
88
import org.variantsync.diffdetective.util.IO;
99
import org.variantsync.diffdetective.variation.DiffLinesLabel;
10+
import org.variantsync.diffdetective.variation.diff.DiffNode;
1011
import org.variantsync.diffdetective.variation.diff.VariationDiff;
1112
import org.variantsync.diffdetective.variation.diff.construction.GumTreeDiff;
1213
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
@@ -17,6 +18,7 @@
1718
import org.variantsync.diffdetective.variation.diff.serialize.edgeformat.ChildOrderEdgeFormat;
1819
import org.variantsync.diffdetective.variation.diff.serialize.edgeformat.DefaultEdgeLabelFormat;
1920
import org.variantsync.diffdetective.variation.diff.serialize.nodeformat.FullNodeFormat;
21+
import org.variantsync.diffdetective.variation.diff.source.VariationDiffSource;
2022
import org.variantsync.diffdetective.variation.tree.VariationTree;
2123
import 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
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/tex/
1+
tex/
22
*_actual.lg

src/test/resources/tree-diffing/01_gumtree-hybrid_expected.lg renamed to src/test/resources/tree-diffing/createMatching/01_gumtree-hybrid_expected.lg

File renamed without changes.

src/test/resources/tree-diffing/01_gumtree_expected.lg renamed to src/test/resources/tree-diffing/createMatching/01_gumtree_expected.lg

File renamed without changes.

src/test/resources/tree-diffing/02_gumtree-hybrid_expected.lg renamed to src/test/resources/tree-diffing/createMatching/02_gumtree-hybrid_expected.lg

File renamed without changes.

src/test/resources/tree-diffing/02_gumtree_expected.lg renamed to src/test/resources/tree-diffing/createMatching/02_gumtree_expected.lg

File renamed without changes.

src/test/resources/tree-diffing/03_gumtree-hybrid_expected.lg renamed to src/test/resources/tree-diffing/createMatching/03_gumtree-hybrid_expected.lg

File renamed without changes.

src/test/resources/tree-diffing/03_gumtree_expected.lg renamed to src/test/resources/tree-diffing/createMatching/03_gumtree_expected.lg

File renamed without changes.

0 commit comments

Comments
 (0)