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 | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 28 | void testParse(QString string) |
| 29 | { |
| 30 | Parser parser{string}; |
| 31 | |
| 32 | QList<AstNode> result = parser.parseMany(); |
| 33 | |
| 34 | qDebug() << "\033[36mParse\033[0m" << string << result; |
| 35 | } |
| 36 | |
| 37 | int testResults() |
| 38 | { |
| 39 | if (g_numFailed == 0) |
| 40 | { |
| 41 | qDebug() << "\033[32mALL TESTS SUCCEEDED\033[0m"; |
| 42 | } |
| 43 | else |
| 44 | { |
| 45 | qDebug().nospace() << "\033[31m" << g_numFailed << " TESTS FAILED\033[0m"; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return g_numFailed; |
| 49 | } |
| 50 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 51 | void testAllMatches() |
| 52 | { |
| 53 | testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 54 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 55 | testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 56 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 57 | LTok sameTwo = {Token('s', "a"), Token('s', "a")}; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 58 | testMatch("s.a s.a = aa", true, match({Token('a'), Token('a')}, sameTwo, VarContext())); |
| 59 | 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] | 60 | |
| 61 | LTok sameStartEnd = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 62 | Token('s', "a"), |
| 63 | Token('e', "middle"), |
| 64 | Token('s', "a")}; |
| 65 | testMatch("s.a e.middle s.a = aea", true, |
| 66 | match({Token('a'), Token('e'), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 67 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 68 | testMatch("s.a e.middle s.a = aef Hi a", true, |
| 69 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 70 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 71 | testMatch("s.a e.middle s.a = aef Hi c", false, |
| 72 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('c')}, sameStartEnd, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 73 | |
| 74 | LTok parenthesized = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 75 | Token(LTok({Token('s', "a")})), |
| 76 | Token('e', "Middle"), |
| 77 | Token('s', "a"), |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 78 | }; |
| 79 | LTok parenTest1 = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 80 | Token(LTok({Token('y')})), |
| 81 | Token('f'), |
| 82 | Token("MiddleStuff"), |
| 83 | Token('y')}; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 84 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 85 | testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true, |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 86 | match(parenTest1, parenthesized, VarContext())); |
| 87 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 88 | testMatch("(a) = (a)", true, |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 89 | match({Token({Token('a')})}, {Token({Token('a')})}, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 90 | } |
| 91 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 92 | void testAllParses() |
| 93 | { |
| 94 | testParse("all symbols"); |
| 95 | testParse("Identifier symbols Identifier"); |
| 96 | testParse("s.A"); |
| 97 | testParse("(s.A) Variable-quoted"); |
| 98 | testParse("<Func-name a b (c)>"); |
| 99 | testParse("<Prout hi>"); |
| 100 | testParse("(Prout hi)"); |
| 101 | testParse("(<Prout hi>)"); |
| 102 | testParse("<If T Then (<Prout hi>) Else (<Prout sorry>)>"); |
| 103 | } |
| 104 | |
| 105 | int main(int argc, char *argv[]) |
| 106 | { |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 107 | QCoreApplication a(argc, argv); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 108 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 109 | testAllMatches(); |
| 110 | qDebug() << ""; |
| 111 | testAllParses(); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 112 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 113 | qDebug() << ""; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 114 | return testResults(); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 115 | } |