Skip to content

Commit ee71f32

Browse files
committed
test: fix the DiffTreeParser tests for non-GumTree matchers
Previously, all tests would run with the default GumTree matcher instead of the matcher stated in the test case file name. As stated in my bachelor thesis, some matchers fail to return a valid matching. Hence, some test cases need to be removed. In particular, the theta matcher contains a bug that results in matchings that are inconsistent (e.g., A->B but B->C). Furthermore, the gumtree-partition-id matcher tests have been removed. This matcher is already removed in a newer GumTree version.
1 parent 9cc672e commit ee71f32

26 files changed

Lines changed: 151 additions & 324 deletions

src/main/java/org/variantsync/diffdetective/variation/diff/construction/GumTreeDiff.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,18 @@ public class GumTreeDiff {
3535
* @see diffUsingMatching(VariationNode, VariationNode, Matcher)
3636
*/
3737
public static <L extends Label> VariationDiff<L> diffUsingMatching(VariationTree<L> before, VariationTree<L> after) {
38+
return diffUsingMatching(before, after, Matchers.getInstance().getMatcher());
39+
}
40+
41+
/**
42+
* Create a {@link VariationDiff} by matching nodes between {@code before} and {@code after}
43+
* with {@code matcher}.
44+
*/
45+
public static <L extends Label> VariationDiff<L> diffUsingMatching(VariationTree<L> before, VariationTree<L> after, Matcher matcher) {
3846
DiffNode<L> root = diffUsingMatching(
3947
before.root(),
4048
after.root(),
41-
Matchers.getInstance().getMatcher()
49+
matcher
4250
);
4351

4452
return new VariationDiff<>(root, new VariationTreeDiffSource(before.source(), after.source()));
@@ -85,7 +93,10 @@ public static <B extends VariationNode<B, L>, L extends Label> DiffNode<L> diffU
8593
var dst = new VariationTreeAdapter<L>(after);
8694

8795
MappingStore matching = matcher.match(src, dst);
88-
Assert.assertTrue(matching.has(src, dst));
96+
97+
// The following algorithm assumes that the root nodes are matched so we ensure that this is
98+
// the case here by establishing that mapping if necessary.
99+
ensureMapping(matching, src, dst);
89100

90101
removeUnmapped(matching, src);
91102
for (var child : dst.getChildren()) {
@@ -182,7 +193,10 @@ public static <L extends Label> DiffNode<L> improveMatching(DiffNode<L> tree, Ma
182193
MappingStore matching = new MappingStore(src, dst);
183194
extractMatching(src, dst, matching);
184195
matcher.match(src, dst, matching);
185-
Assert.assertTrue(matching.has(src, dst));
196+
197+
// The following algorithm assumes that the root nodes are matched so we ensure that this is
198+
// the case here by establishing that mapping if necessary.
199+
ensureMapping(matching, src, dst);
186200

187201
for (var srcNode : src.preOrder()) {
188202
var dstNode = matching.getDstForSrc(srcNode);
@@ -299,4 +313,20 @@ private static <L extends Label> void extractMatching(
299313
}
300314
}
301315
}
316+
317+
/**
318+
* Add a mapping between {@code src} and {@code dst}.
319+
* In case {@code src} or {@code dst} are mapped to some other nodes, these mappings are
320+
* removed.
321+
*/
322+
private static void ensureMapping(MappingStore matching, Tree src, Tree dst) {
323+
if (matching.isSrcMapped(src)) {
324+
matching.removeMapping(src, matching.getDstForSrc(src));
325+
}
326+
if (matching.isDstMapped(dst)) {
327+
matching.removeMapping(dst, matching.getSrcForDst(dst));
328+
}
329+
330+
matching.addMapping(src, dst);
331+
}
302332
}

src/test/java/TreeDiffingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static Stream<TestCase> createMatchingTestCases() throws IOException {
8585
public void createMatchingTestCase(TestCase testCase) throws IOException, DiffParseException {
8686
VariationTree<DiffLinesLabel> beforeEdit = parseVariationTree(testCase.beforeEdit());
8787
VariationTree<DiffLinesLabel> afterEdit = parseVariationTree(testCase.afterEdit());
88-
assertExpectedVariationDiffs(testCase, GumTreeDiff.diffUsingMatching(beforeEdit, afterEdit));
88+
assertExpectedVariationDiffs(testCase, GumTreeDiff.diffUsingMatching(beforeEdit, afterEdit, testCase.matcher()));
8989
}
9090

9191
private static Stream<TestCase> improveMatchingTestCases() throws IOException {

src/test/resources/tree-diffing/createMatching/07_change-distiller-theta_expected.lg

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/test/resources/tree-diffing/createMatching/07_classic-gumtree-theta_expected.lg

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/test/resources/tree-diffing/createMatching/07_gumtree-partition-id_expected.lg

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/test/resources/tree-diffing/createMatching/07_gumtree-simple-id-theta_expected.lg

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
v 80 NON;IF;(old: -1, diff: 0, new: -1);(old: -1, diff: 0, new: -1);True
2-
v 136 REM;IF;(old: 1, diff: 1, new: -1);(old: 5, diff: 1, new: -1);defined(CONFIG_A);#ifdef CONFIG_A
2+
v 144 NON;IF;(old: 1, diff: 1, new: 1);(old: 5, diff: 1, new: 5);defined(CONFIG_A);#ifdef CONFIG_A
33
v 200 REM;IF;(old: 2, diff: 2, new: -1);(old: 4, diff: 2, new: -1);defined(CONFIG_B);#ifdef CONFIG_B
44
v 275 NON;ARTIFACT;(old: 3, diff: 3, new: 3);(old: 4, diff: 3, new: 4);;Line
55
v 320 ADD;IF;(old: -1, diff: 4, new: 1);(old: -1, diff: 4, new: 5);defined(CONFIG_B);#ifdef CONFIG_B
6-
v 384 ADD;IF;(old: -1, diff: 5, new: 2);(old: -1, diff: 5, new: 4);defined(CONFIG_A);#ifdef CONFIG_A
7-
e 136 80 b;0,-1
6+
e 144 80 b;0,-1
87
e 320 80 a;-1,0
9-
e 200 136 b;0,-1
8+
e 200 144 b;0,-1
9+
e 275 144 a;-1,0
1010
e 275 200 b;0,-1
11-
e 384 320 a;-1,0
12-
e 275 384 a;-1,0
11+
e 144 320 a;-1,0

src/test/resources/tree-diffing/createMatching/07_theta_expected.lg

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
v 80 NON;IF;(old: -1, diff: 0, new: -1);(old: -1, diff: 0, new: -1);True
22
v 136 REM;IF;(old: 1, diff: 1, new: -1);(old: 5, diff: 1, new: -1);defined(CONFIG_A);#ifdef CONFIG_A
33
v 200 REM;IF;(old: 2, diff: 2, new: -1);(old: 4, diff: 2, new: -1);defined(CONFIG_B);#ifdef CONFIG_B
4-
v 275 NON;ARTIFACT;(old: 3, diff: 3, new: 3);(old: 4, diff: 3, new: 4);;Line
4+
v 267 REM;ARTIFACT;(old: 3, diff: 3, new: -1);(old: 4, diff: 3, new: -1);;Line
55
v 320 ADD;IF;(old: -1, diff: 4, new: 1);(old: -1, diff: 4, new: 5);defined(CONFIG_B);#ifdef CONFIG_B
66
v 384 ADD;IF;(old: -1, diff: 5, new: 2);(old: -1, diff: 5, new: 4);defined(CONFIG_A);#ifdef CONFIG_A
7+
v 451 ADD;ARTIFACT;(old: -1, diff: 6, new: 3);(old: -1, diff: 6, new: 4);;Line
78
e 136 80 b;0,-1
89
e 320 80 a;-1,0
910
e 200 136 b;0,-1
10-
e 275 200 b;0,-1
11+
e 267 200 b;0,-1
1112
e 384 320 a;-1,0
12-
e 275 384 a;-1,0
13+
e 451 384 a;-1,0

src/test/resources/tree-diffing/createMatching/08_change-distiller-theta_expected.lg

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)