blob: 8ef1fb4fc81c9315258be8e3fbe407e83fab744f [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
45int testResults()
46{
47 if (g_numFailed == 0)
48 {
49 qDebug() << "\033[32mALL TESTS SUCCEEDED\033[0m";
50 }
51 else
52 {
53 qDebug().nospace() << "\033[31m" << g_numFailed << " TESTS FAILED\033[0m";
swissChili3e98c062021-12-04 22:07:38 -080054 }
55
56 return g_numFailed;
57}
58
swissChilic71acc62021-12-07 08:03:37 -080059void testAllMatches()
60{
61 testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext()));
swissChili3e98c062021-12-04 22:07:38 -080062
swissChilic71acc62021-12-07 08:03:37 -080063 testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext()));
swissChilid17b5a12021-12-05 20:46:42 -080064
swissChili3e98c062021-12-04 22:07:38 -080065 LTok sameTwo = {Token('s', "a"), Token('s', "a")};
swissChilic71acc62021-12-07 08:03:37 -080066 testMatch("s.a s.a = aa", true, match({Token('a'), Token('a')}, sameTwo, VarContext()));
67 testMatch("s.a s.a = ab", false, match({Token('a'), Token('b')}, sameTwo, VarContext()));
swissChili3e98c062021-12-04 22:07:38 -080068
69 LTok sameStartEnd = {
swissChilic71acc62021-12-07 08:03:37 -080070 Token('s', "a"),
71 Token('e', "middle"),
72 Token('s', "a")};
73 testMatch("s.a e.middle s.a = aea", true,
74 match({Token('a'), Token('e'), Token('a')}, sameStartEnd, VarContext()));
swissChili3e98c062021-12-04 22:07:38 -080075
swissChilic71acc62021-12-07 08:03:37 -080076 testMatch("s.a e.middle s.a = aef Hi a", true,
77 match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('a')}, sameStartEnd, VarContext()));
swissChili3e98c062021-12-04 22:07:38 -080078
swissChilic71acc62021-12-07 08:03:37 -080079 testMatch("s.a e.middle s.a = aef Hi c", false,
80 match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('c')}, sameStartEnd, VarContext()));
swissChilid17b5a12021-12-05 20:46:42 -080081
82 LTok parenthesized = {
swissChilic71acc62021-12-07 08:03:37 -080083 Token(LTok({Token('s', "a")})),
84 Token('e', "Middle"),
85 Token('s', "a"),
swissChilid17b5a12021-12-05 20:46:42 -080086 };
87 LTok parenTest1 = {
swissChilic71acc62021-12-07 08:03:37 -080088 Token(LTok({Token('y')})),
89 Token('f'),
90 Token("MiddleStuff"),
91 Token('y')};
swissChilid17b5a12021-12-05 20:46:42 -080092
swissChilic71acc62021-12-07 08:03:37 -080093 testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true,
swissChilid17b5a12021-12-05 20:46:42 -080094 match(parenTest1, parenthesized, VarContext()));
swissChili682e7bc2021-12-07 09:04:54 -080095 // testMatch("(y)f Middle-stuff y", "(s.a) e.Middle s.a");
swissChilid17b5a12021-12-05 20:46:42 -080096
swissChili682e7bc2021-12-07 09:04:54 -080097 testMatch("(a)", "(a)");
swissChili3e98c062021-12-04 22:07:38 -080098}
99
swissChilic71acc62021-12-07 08:03:37 -0800100void testAllParses()
101{
swissChili682e7bc2021-12-07 09:04:54 -0800102 testParseAst("all symbols");
103 testParseAst("Identifier symbols Identifier");
104 testParseAst("s.A");
105 testParseAst("(s.A) Variable-quoted");
106 testParseAst("<Func-name a b (c)>");
107 testParseAst("<Prout hi>");
108 testParseAst("(Prout hi)");
109 testParseAst("(<Prout hi>)");
110 testParseAst("<If T Then (<Prout hi>) Else (<Prout sorry>)>");
111 testParseAst("(s.a) e.Middle s.a");
swissChilic71acc62021-12-07 08:03:37 -0800112}
113
114int main(int argc, char *argv[])
115{
swissChili7babd922021-12-02 22:46:48 -0800116 QCoreApplication a(argc, argv);
swissChili3e98c062021-12-04 22:07:38 -0800117
swissChilic71acc62021-12-07 08:03:37 -0800118 testAllMatches();
119 qDebug() << "";
120 testAllParses();
swissChili3e98c062021-12-04 22:07:38 -0800121
swissChilic71acc62021-12-07 08:03:37 -0800122 qDebug() << "";
swissChili3e98c062021-12-04 22:07:38 -0800123 return testResults();
swissChili7babd922021-12-02 22:46:48 -0800124}