|
| 1 | +import org.junit.jupiter.params.ParameterizedTest; |
| 2 | +import org.junit.jupiter.params.provider.MethodSource; |
| 3 | +import org.variantsync.diffdetective.error.UnparseableFormulaException; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +// Test cases for a parser of https://www.slashdev.ca/javapp/ |
| 8 | +public class JPPParserTest { |
| 9 | + private record TestCase(String formula, String expected) { |
| 10 | + } |
| 11 | + |
| 12 | + private record ThrowingTestCase(String formula) { |
| 13 | + } |
| 14 | + |
| 15 | + private static List<JPPParserTest.TestCase> testCases() { |
| 16 | + return List.of( |
| 17 | + /// #if expression |
| 18 | + // expression := <operand> <operator> <operand> | [!]defined(name) |
| 19 | + // expression := operand == operand |
| 20 | + new JPPParserTest.TestCase("//#if 1 == -42", "1__EQ____U_MINUS__42"), |
| 21 | + // expression := operand != operand |
| 22 | + new JPPParserTest.TestCase("// #if 1 != 0", "1__NEQ__0"), |
| 23 | + // expression := operand <= operand |
| 24 | + new JPPParserTest.TestCase("//#if -1 <= 0", "__U_MINUS__1__LEQ__0"), |
| 25 | + // expression := operand < operand |
| 26 | + new JPPParserTest.TestCase("//#if \"str\" < 0", "__QUOTE__str__QUOTE____LT__0"), |
| 27 | + // expression := operand >= operand |
| 28 | + new JPPParserTest.TestCase("// #if \"str\" >= \"str\"", "__QUOTE__str__QUOTE____GEQ____QUOTE__str__QUOTE__"), |
| 29 | + // expression := operand > operand |
| 30 | + new JPPParserTest.TestCase("// #if 1.2 > 0", "1__DOT__2__GT__0"), |
| 31 | + // expression := defined(name) |
| 32 | + new JPPParserTest.TestCase("//#if defined(property)", "DEFINED_property"), |
| 33 | + // expression := !defined(name) |
| 34 | + new JPPParserTest.TestCase("//#if !defined(property)", "__U_NOT__DEFINED_property"), |
| 35 | + // operand := ${property} |
| 36 | + new JPPParserTest.TestCase("//#if ${os_version} == 4.1", "os_version__EQ__4__DOT__1"), |
| 37 | + |
| 38 | + /// #if expression and expression |
| 39 | + new JPPParserTest.TestCase("//#if 1 > 2 and defined( FEAT_A )", "1__GT__2&&DEFINED_FEAT_A"), |
| 40 | + |
| 41 | + /// #if expression or expression |
| 42 | + new JPPParserTest.TestCase("//#if !defined(left) or defined(right)", "__U_NOT__DEFINED_left||DEFINED_right"), |
| 43 | + |
| 44 | + /// #if expression and expression or expression |
| 45 | + new JPPParserTest.TestCase("//#if ${os_version} == 4.1 and 1 > -42 or defined(ALL)", "os_version__EQ__4__DOT__1&&1__GT____U_MINUS__42||DEFINED_ALL") |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + private static List<JPPParserTest.ThrowingTestCase> throwingTestCases() { |
| 50 | + return List.of( |
| 51 | + // Invalid macro |
| 52 | + new JPPParserTest.ThrowingTestCase(""), |
| 53 | + new JPPParserTest.ThrowingTestCase("#"), |
| 54 | + new JPPParserTest.ThrowingTestCase("ifdef A"), |
| 55 | + new JPPParserTest.ThrowingTestCase("#error A"), |
| 56 | + new JPPParserTest.ThrowingTestCase("#iferror A"), |
| 57 | + |
| 58 | + // Empty formula |
| 59 | + new JPPParserTest.ThrowingTestCase("//#if"), |
| 60 | + new JPPParserTest.ThrowingTestCase("#if defined()"), |
| 61 | + new JPPParserTest.ThrowingTestCase("#if ${} > 0"), |
| 62 | + |
| 63 | + // incomplete expressions |
| 64 | + new JPPParserTest.ThrowingTestCase("#if 1 >"), |
| 65 | + new JPPParserTest.ThrowingTestCase("#if == 2"), |
| 66 | + new JPPParserTest.ThrowingTestCase("#if ${version} > ") |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @ParameterizedTest |
| 71 | + @MethodSource("testCases") |
| 72 | + public void testCase(JPPParserTest.TestCase testCase) throws UnparseableFormulaException { |
| 73 | +// assertEquals( |
| 74 | +// testCase.expected, |
| 75 | +// // TODO: |
| 76 | +// ); |
| 77 | + } |
| 78 | + |
| 79 | + @ParameterizedTest |
| 80 | + @MethodSource("throwingTestCases") |
| 81 | + public void throwingTestCase(JPPParserTest.ThrowingTestCase testCase) { |
| 82 | + // assertThrows(UnparseableFormulaException.class, () -> //TODO |
| 83 | + // ); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments