Skip to content

Commit 56abb9c

Browse files
authored
Merge pull request #132 from maximilian-glumann/issue104-propositional-formula-parser-test
Add PropositionalFormulaParserTest
2 parents 0cb2cca + 4ab8d21 commit 56abb9c

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import org.junit.jupiter.params.ParameterizedTest;
2+
import org.junit.jupiter.params.provider.MethodSource;
3+
import org.prop4j.And;
4+
import org.prop4j.Literal;
5+
import org.prop4j.Node;
6+
import org.prop4j.Or;
7+
import org.variantsync.diffdetective.feature.PropositionalFormulaParser;
8+
9+
import java.util.List;
10+
11+
import static org.variantsync.diffdetective.util.Assert.assertEquals;
12+
13+
/**
14+
* Class containing tests of the parsing behaviour for the default implementation of PropositionalFormulaParser.
15+
* Goal: Special characters that occur in the output of a DiffLineFormulaExtractor must not confuse the parsing process of the PropositionalFormulaParser.
16+
* It is not designed to extensively test the functionality of the PropositionalFormulaParser itself as this is expected to be done by FeatureIDE already.
17+
*
18+
* @author Maximilian Glumann
19+
*/
20+
public class PropositionalFormulaParserTest {
21+
private record TestCase(String formula, Node expected) {
22+
}
23+
24+
/**
25+
* These test cases are based on a subset of the CPPParserTest test cases.
26+
* It is not necessary to keep all test cases from CPPParserTest as most of them result in a single but long Literal anyway.
27+
*/
28+
private static List<TestCase> testCases() {
29+
return List.of(
30+
new TestCase("A", new Literal("A")),
31+
new TestCase("!(A)", new Literal("A", false)),
32+
new TestCase("!A", new Literal("A", false)),
33+
34+
new TestCase("A&&B", new And(new Literal("A"), new Literal("B"))),
35+
new TestCase("A||B", new Or(new Literal("A"), new Literal("B"))),
36+
new TestCase("A&&(B||C)", new And(new Literal("A"), new Or(new Literal("B"), new Literal("C")))),
37+
new TestCase("A&&B||C", new Or(new And(new Literal("A"), new Literal("B")), new Literal("C"))),
38+
39+
new TestCase("A__GEQ__B&&C__GT__D", new And(new Literal("A__GEQ__B"), new Literal("C__GT__D"))),
40+
new TestCase("DEFINED___LB__A__RB__&&__LB__B__MUL__2__RB____GT__C", new And(new Literal("DEFINED___LB__A__RB__"), new Literal("__LB__B__MUL__2__RB____GT__C"))),
41+
new TestCase("(STDC__EQ__1)&&(DEFINED___LB__LARGE__RB__||DEFINED___LB__COMPACT__RB__)", new And(new Literal("STDC__EQ__1"), new Or(new Literal("DEFINED___LB__LARGE__RB__"), new Literal("DEFINED___LB__COMPACT__RB__")))),
42+
new TestCase("APR_CHARSET_EBCDIC&&!(__LB____SQUOTE__Z__SQUOTE____SUB____SQUOTE__A__SQUOTE____RB____EQ__25)", new And(new Literal("APR_CHARSET_EBCDIC"), new Literal("__LB____SQUOTE__Z__SQUOTE____SUB____SQUOTE__A__SQUOTE____RB____EQ__25", false))),
43+
new TestCase("A&&(B__GT__C)", new And(new Literal("A"), new Literal("B__GT__C"))),
44+
new TestCase("A&&(__LB__B__ADD__1__RB____GT____LB__C__L_OR__D__RB__)", new And(new Literal("A"), new Literal("__LB__B__ADD__1__RB____GT____LB__C__L_OR__D__RB__"))),
45+
new TestCase("DEFINED_HAS_ATTRIBUTE_&&HAS_ATTRIBUTE___LB__nonnull__RB__", new And(new Literal("DEFINED_HAS_ATTRIBUTE_"), new Literal("HAS_ATTRIBUTE___LB__nonnull__RB__"))),
46+
new TestCase("HAS_BUILTIN___LB__nonnull__RB__&&A", new And(new Literal("HAS_BUILTIN___LB__nonnull__RB__"), new Literal("A"))),
47+
new TestCase("A||(DEFINED___LB__NAME__RB__&&(NAME__GEQ__199630))", new Or(new Literal("A"), new And(new Literal("DEFINED___LB__NAME__RB__"), new Literal("NAME__GEQ__199630")))),
48+
new TestCase("(DEFINED___LB__NAME__RB__&&(NAME__GEQ__199905)&&(NAME__LT__1991011))||(NAME__GEQ__300000)||DEFINED___LB__NAME__RB__", new Or(new And(new Literal("DEFINED___LB__NAME__RB__"), new And(new Literal("NAME__GEQ__199905"), new Literal("NAME__LT__1991011"))), new Or(new Literal("NAME__GEQ__300000"), new Literal("DEFINED___LB__NAME__RB__")))),
49+
new TestCase("1__GT____U_MINUS__42", new Literal("1__GT____U_MINUS__42")),
50+
new TestCase("1__GT____U_PLUS__42", new Literal("1__GT____U_PLUS__42")),
51+
new TestCase("42__GT____U_TILDE__A", new Literal("42__GT____U_TILDE__A")),
52+
new TestCase("A__ADD__B__GT__42", new Literal("A__ADD__B__GT__42")),
53+
new TestCase("A__LSHIFT__B", new Literal("A__LSHIFT__B")),
54+
new TestCase("A__THEN__B__COLON__C", new Literal("A__THEN__B__COLON__C")),
55+
new TestCase("A__MUL____LB__B__ADD__C__RB__", new Literal("A__MUL____LB__B__ADD__C__RB__")),
56+
new TestCase("(__LB____SQUOTE__Z__SQUOTE____SUB____SQUOTE__A__SQUOTE____RB____EQ__25)", new Literal("__LB____SQUOTE__Z__SQUOTE____SUB____SQUOTE__A__SQUOTE____RB____EQ__25")),
57+
new TestCase("(__LB__GNUTLS_VERSION_MAJOR__ADD____LB__GNUTLS_VERSION_MINOR__GT__0__L_OR__GNUTLS_VERSION_PATCH__GEQ__20__RB____RB____GT__3)", new Literal("__LB__GNUTLS_VERSION_MAJOR__ADD____LB__GNUTLS_VERSION_MINOR__GT__0__L_OR__GNUTLS_VERSION_PATCH__GEQ__20__RB____RB____GT__3")),
58+
new TestCase("(__LB__A__L_AND__B__RB____GT__C)", new Literal("__LB__A__L_AND__B__RB____GT__C")),
59+
new TestCase("A__EQ__B", new Literal("A__EQ__B")),
60+
new TestCase("(DEFINED_A)", new Literal("DEFINED_A")),
61+
new TestCase("MACRO___LB__A__B__RB____EQ__1", new Literal("MACRO___LB__A__B__RB____EQ__1")),
62+
new TestCase("ifndef", new Literal("ifndef")),
63+
new TestCase("__HAS_WARNING___LB____QUOTE____SUB__Wa__SUB__warning__QUOTE_____foo__RB__", new Literal("__HAS_WARNING___LB____QUOTE____SUB__Wa__SUB__warning__QUOTE_____foo__RB__"))
64+
);
65+
}
66+
67+
/**
68+
* Each test case compares the output of the default PropositionalFormularParser to the expected output.
69+
* This comparison is performed using the equivalence defined by org.prop4j.Node from FeatureIDE.
70+
* Therefore, nodes describing equivalent propositional formulas in different tree structures are not considered equal.
71+
* As long as FeatureIDE produces a deterministic and consistent tree structure in its output, these tests will succeed.
72+
* Because DiffDetective desires not only a correct but also a deterministic and consistent parser output,
73+
* it is intended that these tests also break, if FeatureIDE changes its parsing behaviour in the future.
74+
*/
75+
@ParameterizedTest
76+
@MethodSource("testCases")
77+
public void testCase(TestCase testCase) {
78+
assertEquals(
79+
testCase.expected,
80+
PropositionalFormulaParser.Default.parse(testCase.formula)
81+
);
82+
}
83+
}

0 commit comments

Comments
 (0)