Skip to content

Commit 02f7340

Browse files
eugen-shulimovibbem
authored andcommitted
feat: create an unparser for variation trees
1 parent b694ce5 commit 02f7340

4 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.variantsync.diffdetective.variation;
2+
3+
import java.util.ArrayList;
4+
import java.util.Stack;
5+
import org.variantsync.diffdetective.variation.tree.VariationTree;
6+
import org.variantsync.diffdetective.variation.tree.VariationTreeNode;
7+
8+
public class VariationUnparser {
9+
public static String variationTreeUnparser(VariationTree<? extends Label> tree) {
10+
StringBuilder result = new StringBuilder();
11+
Stack<VariationTreeNode<? extends Label>> stack = new Stack<>();
12+
for (int i = tree.root().getChildren().size() - 1; i >= 0; i--) {
13+
stack.push(tree.root().getChildren().get(i));
14+
}
15+
while (!stack.empty()) {
16+
VariationTreeNode<? extends Label> node = stack.pop();
17+
for (String line : node.getLabel().getLines()) {
18+
result.append(line);
19+
result.append("\n");
20+
}
21+
if (node.isIf()) {
22+
ArrayList<String> list = new ArrayList<>();
23+
list.add(node.getEndIf());
24+
stack.push(new VariationTreeNode<>(NodeType.ARTIFACT, null, null,
25+
DiffLinesLabel.withInvalidLineNumbers(list)));
26+
}
27+
for (int i = node.getChildren().size() - 1; i >= 0; i--) {
28+
stack.push(node.getChildren().get(i));
29+
}
30+
}
31+
return result.substring(0, result.length() - 1);
32+
}
33+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3+
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.util.stream.Stream;
7+
8+
import org.junit.jupiter.api.Disabled;
9+
import org.junit.jupiter.api.Test;
10+
import org.variantsync.diffdetective.variation.DiffLinesLabel;
11+
import org.variantsync.diffdetective.variation.tree.VariationTree;
12+
import org.variantsync.diffdetective.variation.VariationUnparser;
13+
14+
public class VariationUnparserTest {
15+
private final static Path testDir = Constants.RESOURCE_DIR.resolve("unparser");
16+
17+
private final static String testCaseSuffix = ".txt";
18+
19+
protected static Stream<Path> findTestCases(Path dir) {
20+
try {
21+
return Files
22+
.list(dir)
23+
.filter(filename -> filename.getFileName().toString().endsWith(testCaseSuffix));
24+
} catch (Exception e) {
25+
e.printStackTrace();
26+
}
27+
return null;
28+
}
29+
30+
@Disabled
31+
@Test
32+
public void tests() {
33+
findTestCases(testDir).forEach(this::test);
34+
}
35+
36+
@Disabled
37+
@Test
38+
public void teststest() {
39+
Path path = testDir.resolve("test2.txt");
40+
String temp = "b";
41+
VariationTree<DiffLinesLabel> tree = null;
42+
try {
43+
tree = VariationTree.fromFile(path);
44+
temp = VariationUnparser.variationTreeUnparser(tree);
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
assertNotNull(tree.root().getChildren().get(0).getEndIf());
49+
}
50+
51+
public void test(Path basename) {
52+
testCase(basename);
53+
}
54+
55+
public static void testCase(Path testCasePath) {
56+
String temp = "";
57+
try {
58+
temp = Files.readString(testCasePath);
59+
} catch (Exception e) {
60+
e.printStackTrace();
61+
}
62+
temp = temp.replaceAll("\\r\\n", "\n");
63+
String unparse = parseUnparse(testCasePath);
64+
assertEquals(temp, unparse);
65+
}
66+
67+
public static String parseUnparse(Path path) {
68+
String temp = "b";
69+
try {
70+
VariationTree<DiffLinesLabel> tree = VariationTree.fromFile(path);
71+
temp = VariationUnparser.variationTreeUnparser(tree);
72+
} catch (Exception e) {
73+
e.printStackTrace();
74+
}
75+
return temp;
76+
}
77+
78+
}

src/test/resources/unparser/test1.txt

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#ifdef C
2+
boob()
3+
#endif

0 commit comments

Comments
 (0)