blob: fc9d5475e852f811f7c15bc6ec6ca0b009e79760 [file] [log] [blame]
swissChili7babd922021-12-02 22:46:48 -08001#include <QCoreApplication>
2#include <QDebug>
3
swissChili3e98c062021-12-04 22:07:38 -08004#include "Matcher.h"
5#include "Token.h"
swissChilic71acc62021-12-07 08:03:37 -08006#include "AstNode.h"
7#include "Parser.h"
swissChili3e98c062021-12-04 22:07:38 -08008
9int g_numFailed = 0;
10
swissChilic71acc62021-12-07 08:03:37 -080011void testMatch(const QString &test, bool shouldBe, const MatchResult &result)
12{
13 if (result.success != shouldBe)
14 {
swissChili3e98c062021-12-04 22:07:38 -080015 g_numFailed++;
swissChilic71acc62021-12-07 08:03:37 -080016 qDebug() << "\n\033[31mTEST FAILS:\033[0m";
swissChilid17b5a12021-12-05 20:46:42 -080017 qDebug() << "with context" << result.context;
swissChili3e98c062021-12-04 22:07:38 -080018 }
19
swissChilic71acc62021-12-07 08:03:37 -080020 qDebug() << "\033[36mMatchResult\033[0m" << test << result.success;
swissChilid17b5a12021-12-05 20:46:42 -080021
swissChilic71acc62021-12-07 08:03:37 -080022 if (result.success != shouldBe)
23 {
swissChilid17b5a12021-12-05 20:46:42 -080024 qDebug() << "";
25 }
swissChili3e98c062021-12-04 22:07:38 -080026}
27
swissChilic71acc62021-12-07 08:03:37 -080028void 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
37int 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";
swissChili3e98c062021-12-04 22:07:38 -080046 }
47
48 return g_numFailed;
49}
50
swissChilic71acc62021-12-07 08:03:37 -080051void testAllMatches()
52{
53 testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext()));
swissChili3e98c062021-12-04 22:07:38 -080054
swissChilic71acc62021-12-07 08:03:37 -080055 testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext()));
swissChilid17b5a12021-12-05 20:46:42 -080056
swissChili3e98c062021-12-04 22:07:38 -080057 LTok sameTwo = {Token('s', "a"), Token('s', "a")};
swissChilic71acc62021-12-07 08:03:37 -080058 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()));
swissChili3e98c062021-12-04 22:07:38 -080060
61 LTok sameStartEnd = {
swissChilic71acc62021-12-07 08:03:37 -080062 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()));
swissChili3e98c062021-12-04 22:07:38 -080067
swissChilic71acc62021-12-07 08:03:37 -080068 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()));
swissChili3e98c062021-12-04 22:07:38 -080070
swissChilic71acc62021-12-07 08:03:37 -080071 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()));
swissChilid17b5a12021-12-05 20:46:42 -080073
74 LTok parenthesized = {
swissChilic71acc62021-12-07 08:03:37 -080075 Token(LTok({Token('s', "a")})),
76 Token('e', "Middle"),
77 Token('s', "a"),
swissChilid17b5a12021-12-05 20:46:42 -080078 };
79 LTok parenTest1 = {
swissChilic71acc62021-12-07 08:03:37 -080080 Token(LTok({Token('y')})),
81 Token('f'),
82 Token("MiddleStuff"),
83 Token('y')};
swissChilid17b5a12021-12-05 20:46:42 -080084
swissChilic71acc62021-12-07 08:03:37 -080085 testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true,
swissChilid17b5a12021-12-05 20:46:42 -080086 match(parenTest1, parenthesized, VarContext()));
87
swissChilic71acc62021-12-07 08:03:37 -080088 testMatch("(a) = (a)", true,
swissChilid17b5a12021-12-05 20:46:42 -080089 match({Token({Token('a')})}, {Token({Token('a')})}, VarContext()));
swissChili3e98c062021-12-04 22:07:38 -080090}
91
swissChilic71acc62021-12-07 08:03:37 -080092void 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
105int main(int argc, char *argv[])
106{
swissChili7babd922021-12-02 22:46:48 -0800107 QCoreApplication a(argc, argv);
swissChili3e98c062021-12-04 22:07:38 -0800108
swissChilic71acc62021-12-07 08:03:37 -0800109 testAllMatches();
110 qDebug() << "";
111 testAllParses();
swissChili3e98c062021-12-04 22:07:38 -0800112
swissChilic71acc62021-12-07 08:03:37 -0800113 qDebug() << "";
swissChili3e98c062021-12-04 22:07:38 -0800114 return testResults();
swissChili7babd922021-12-02 22:46:48 -0800115}