swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 1 | #include <QCoreApplication> |
| 2 | #include <QDebug> |
| 3 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 4 | #include "Matcher.h" |
| 5 | #include "Token.h" |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 6 | #include "AstNode.h" |
| 7 | #include "Parser.h" |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 8 | |
| 9 | int g_numFailed = 0; |
| 10 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 11 | void testMatch(const QString &test, bool shouldBe, const MatchResult &result) |
| 12 | { |
| 13 | if (result.success != shouldBe) |
| 14 | { |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 15 | g_numFailed++; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 16 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 17 | qDebug() << "with context" << result.context; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 18 | } |
| 19 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 20 | qDebug() << "\033[36mMatchResult\033[0m" << test << result.success; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 21 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 22 | if (result.success != shouldBe) |
| 23 | { |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 24 | qDebug() << ""; |
| 25 | } |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 26 | } |
| 27 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 28 | void testMatch(QString data, QString pattern, bool shouldBe = true) |
| 29 | { |
| 30 | Parser dataParser(data), |
| 31 | patternParser(pattern); |
| 32 | |
| 33 | testMatch(pattern + " = " + data, shouldBe, match(dataParser.parseMany<Token>(), patternParser.parseMany<Token>(), VarContext())); |
| 34 | } |
| 35 | |
| 36 | void testParseAst(QString string) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 37 | { |
| 38 | Parser parser{string}; |
| 39 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 40 | QList<AstNode> result = parser.parseMany<AstNode>(); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 41 | |
| 42 | qDebug() << "\033[36mParse\033[0m" << string << result; |
| 43 | } |
| 44 | |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame^] | 45 | void testParseFunc(QString string) |
| 46 | { |
| 47 | Parser parser{string}; |
| 48 | |
| 49 | Function func; |
| 50 | |
| 51 | if (!parser.parseFunctionDefinition(&func)) |
| 52 | { |
| 53 | g_numFailed++; |
| 54 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 55 | qDebug() << string; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | qDebug() << "\033[36mFunction\033[0m"; |
| 60 | qDebug().noquote() << func; |
| 61 | } |
| 62 | } |
| 63 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 64 | int testResults() |
| 65 | { |
| 66 | if (g_numFailed == 0) |
| 67 | { |
| 68 | qDebug() << "\033[32mALL TESTS SUCCEEDED\033[0m"; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | qDebug().nospace() << "\033[31m" << g_numFailed << " TESTS FAILED\033[0m"; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return g_numFailed; |
| 76 | } |
| 77 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 78 | void testAllMatches() |
| 79 | { |
| 80 | testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 81 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 82 | testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 83 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 84 | LTok sameTwo = {Token('s', "a"), Token('s', "a")}; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 85 | testMatch("s.a s.a = aa", true, match({Token('a'), Token('a')}, sameTwo, VarContext())); |
| 86 | testMatch("s.a s.a = ab", false, match({Token('a'), Token('b')}, sameTwo, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 87 | |
| 88 | LTok sameStartEnd = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 89 | Token('s', "a"), |
| 90 | Token('e', "middle"), |
| 91 | Token('s', "a")}; |
| 92 | testMatch("s.a e.middle s.a = aea", true, |
| 93 | match({Token('a'), Token('e'), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 94 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 95 | testMatch("s.a e.middle s.a = aef Hi a", true, |
| 96 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 97 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 98 | testMatch("s.a e.middle s.a = aef Hi c", false, |
| 99 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('c')}, sameStartEnd, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 100 | |
| 101 | LTok parenthesized = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 102 | Token(LTok({Token('s', "a")})), |
| 103 | Token('e', "Middle"), |
| 104 | Token('s', "a"), |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 105 | }; |
| 106 | LTok parenTest1 = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 107 | Token(LTok({Token('y')})), |
| 108 | Token('f'), |
| 109 | Token("MiddleStuff"), |
| 110 | Token('y')}; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 111 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 112 | testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true, |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 113 | match(parenTest1, parenthesized, VarContext())); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 114 | // testMatch("(y)f Middle-stuff y", "(s.a) e.Middle s.a"); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 115 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 116 | testMatch("(a)", "(a)"); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 117 | } |
| 118 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 119 | void testAllParses() |
| 120 | { |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 121 | testParseAst("all symbols"); |
| 122 | testParseAst("Identifier symbols Identifier"); |
| 123 | testParseAst("s.A"); |
| 124 | testParseAst("(s.A) Variable-quoted"); |
| 125 | testParseAst("<Func-name a b (c)>"); |
| 126 | testParseAst("<Prout hi>"); |
| 127 | testParseAst("(Prout hi)"); |
| 128 | testParseAst("(<Prout hi>)"); |
| 129 | testParseAst("<If T Then (<Prout hi>) Else (<Prout sorry>)>"); |
| 130 | testParseAst("(s.a) e.Middle s.a"); |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame^] | 131 | testParseAst("Hello; Goodbye"); |
| 132 | testParseAst("Key = Value"); |
| 133 | } |
| 134 | |
| 135 | void testAllFunctionDefs() |
| 136 | { |
| 137 | testParseFunc("Test { = HI; }"); |
| 138 | testParseFunc("Palindrome { = T; s.A = T; s.A s.A = T; s.A e.Middle s.A = <Palindrome e.Middle>; } "); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | int main(int argc, char *argv[]) |
| 142 | { |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 143 | QCoreApplication a(argc, argv); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 144 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 145 | testAllMatches(); |
| 146 | qDebug() << ""; |
| 147 | testAllParses(); |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame^] | 148 | qDebug() << ""; |
| 149 | testAllFunctionDefs(); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 150 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 151 | qDebug() << ""; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 152 | return testResults(); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 153 | } |