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" |
| 6 | |
| 7 | int g_numFailed = 0; |
| 8 | |
| 9 | void printResult(const QString &test, bool shouldBe, const MatchResult &result) { |
| 10 | if (result.success != shouldBe) { |
| 11 | g_numFailed++; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame^] | 12 | qDebug() << "\nTEST FAILS:"; |
| 13 | qDebug() << "with context" << result.context; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | qDebug() << "MatchResult" << test << result.success; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame^] | 17 | |
| 18 | if (result.success != shouldBe) { |
| 19 | qDebug() << ""; |
| 20 | } |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | int testResults() { |
| 24 | if (g_numFailed == 0) { |
| 25 | qDebug() << "ALL TESTS SUCCEEDED"; |
| 26 | } else { |
| 27 | qDebug() << g_numFailed << "TESTS FAILED"; |
| 28 | } |
| 29 | |
| 30 | return g_numFailed; |
| 31 | } |
| 32 | |
| 33 | void runTests() { |
| 34 | printResult("a = a", true, match({Token('a')}, {Token('a')}, VarContext())); |
| 35 | |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame^] | 36 | printResult("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext())); |
| 37 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 38 | LTok sameTwo = {Token('s', "a"), Token('s', "a")}; |
| 39 | printResult("s.a s.a = aa", true, match({Token('a'), Token('a')}, sameTwo, VarContext())); |
| 40 | printResult("s.a s.a = ab", false, match({Token('a'), Token('b')}, sameTwo, VarContext())); |
| 41 | |
| 42 | LTok sameStartEnd = { |
| 43 | Token('s', "a"), |
| 44 | Token('e', "middle"), |
| 45 | Token('s', "a") |
| 46 | }; |
| 47 | printResult("s.a e.middle s.a = aea", true, |
| 48 | match({ |
| 49 | Token('a'), Token('e'), Token('a') |
| 50 | }, sameStartEnd, VarContext())); |
| 51 | |
| 52 | printResult("s.a e.middle s.a = aef Hi a", true, |
| 53 | match({ |
| 54 | Token('a'), Token('e'), Token('f'), Token("Hi"), Token('a') |
| 55 | }, sameStartEnd, VarContext())); |
| 56 | |
| 57 | printResult("s.a e.middle s.a = aef Hi c", false, |
| 58 | match({ |
| 59 | Token('a'), Token('e'), Token('f'), Token("Hi"), Token('c') |
| 60 | }, sameStartEnd, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame^] | 61 | |
| 62 | LTok parenthesized = { |
| 63 | Token(LTok({ |
| 64 | Token('s', "a") |
| 65 | })), |
| 66 | Token('e', "Middle"), |
| 67 | Token('s', "a"), |
| 68 | }; |
| 69 | LTok parenTest1 = { |
| 70 | Token(LTok({ |
| 71 | Token('y') |
| 72 | })), |
| 73 | Token('f'), |
| 74 | Token("MiddleStuff"), |
| 75 | Token('y') |
| 76 | }; |
| 77 | |
| 78 | printResult("(s.a) e.Middle s.a = (y)f MiddleStuff y", true, |
| 79 | match(parenTest1, parenthesized, VarContext())); |
| 80 | |
| 81 | printResult("(a) = (a)", true, |
| 82 | match({Token({Token('a')})}, {Token({Token('a')})}, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 83 | } |
| 84 | |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 85 | int main(int argc, char *argv[]) { |
| 86 | QCoreApplication a(argc, argv); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 87 | |
| 88 | runTests(); |
| 89 | |
| 90 | return testResults(); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 91 | } |