blob: 25c4504963b5697fc19c6d1e3bc7b3c52a939a50 [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
swissChili682e7bc2021-12-07 09:04:54 -080028void 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
36void testParseAst(QString string)
swissChilic71acc62021-12-07 08:03:37 -080037{
38 Parser parser{string};
39
swissChili682e7bc2021-12-07 09:04:54 -080040 QList<AstNode> result = parser.parseMany<AstNode>();
swissChilic71acc62021-12-07 08:03:37 -080041
42 qDebug() << "\033[36mParse\033[0m" << string << result;
43}
44
swissChili8a581c62021-12-07 13:29:21 -080045void 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
swissChilic71acc62021-12-07 08:03:37 -080064int 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";
swissChili3e98c062021-12-04 22:07:38 -080073 }
74
75 return g_numFailed;
76}
77
swissChilic71acc62021-12-07 08:03:37 -080078void testAllMatches()
79{
80 testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext()));
swissChili3e98c062021-12-04 22:07:38 -080081
swissChilic71acc62021-12-07 08:03:37 -080082 testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext()));
swissChilid17b5a12021-12-05 20:46:42 -080083
swissChili3e98c062021-12-04 22:07:38 -080084 LTok sameTwo = {Token('s', "a"), Token('s', "a")};
swissChilic71acc62021-12-07 08:03:37 -080085 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()));
swissChili3e98c062021-12-04 22:07:38 -080087
88 LTok sameStartEnd = {
swissChilic71acc62021-12-07 08:03:37 -080089 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()));
swissChili3e98c062021-12-04 22:07:38 -080094
swissChilic71acc62021-12-07 08:03:37 -080095 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()));
swissChili3e98c062021-12-04 22:07:38 -080097
swissChilic71acc62021-12-07 08:03:37 -080098 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()));
swissChilid17b5a12021-12-05 20:46:42 -0800100
101 LTok parenthesized = {
swissChilic71acc62021-12-07 08:03:37 -0800102 Token(LTok({Token('s', "a")})),
103 Token('e', "Middle"),
104 Token('s', "a"),
swissChilid17b5a12021-12-05 20:46:42 -0800105 };
106 LTok parenTest1 = {
swissChilic71acc62021-12-07 08:03:37 -0800107 Token(LTok({Token('y')})),
108 Token('f'),
109 Token("MiddleStuff"),
110 Token('y')};
swissChilid17b5a12021-12-05 20:46:42 -0800111
swissChilic71acc62021-12-07 08:03:37 -0800112 testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true,
swissChilid17b5a12021-12-05 20:46:42 -0800113 match(parenTest1, parenthesized, VarContext()));
swissChili682e7bc2021-12-07 09:04:54 -0800114 // testMatch("(y)f Middle-stuff y", "(s.a) e.Middle s.a");
swissChilid17b5a12021-12-05 20:46:42 -0800115
swissChili682e7bc2021-12-07 09:04:54 -0800116 testMatch("(a)", "(a)");
swissChili3e98c062021-12-04 22:07:38 -0800117}
118
swissChilic71acc62021-12-07 08:03:37 -0800119void testAllParses()
120{
swissChili682e7bc2021-12-07 09:04:54 -0800121 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");
swissChili8a581c62021-12-07 13:29:21 -0800131 testParseAst("Hello; Goodbye");
132 testParseAst("Key = Value");
133}
134
135void 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>; } ");
swissChilic71acc62021-12-07 08:03:37 -0800139}
140
141int main(int argc, char *argv[])
142{
swissChili7babd922021-12-02 22:46:48 -0800143 QCoreApplication a(argc, argv);
swissChili3e98c062021-12-04 22:07:38 -0800144
swissChilic71acc62021-12-07 08:03:37 -0800145 testAllMatches();
146 qDebug() << "";
147 testAllParses();
swissChili8a581c62021-12-07 13:29:21 -0800148 qDebug() << "";
149 testAllFunctionDefs();
swissChili3e98c062021-12-04 22:07:38 -0800150
swissChilic71acc62021-12-07 08:03:37 -0800151 qDebug() << "";
swissChili3e98c062021-12-04 22:07:38 -0800152 return testResults();
swissChili7babd922021-12-02 22:46:48 -0800153}