blob: ebf886fe33c7603cb442cf272f9f54077445030c [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"
6
7int g_numFailed = 0;
8
9void printResult(const QString &test, bool shouldBe, const MatchResult &result) {
10 if (result.success != shouldBe) {
11 g_numFailed++;
swissChilid17b5a12021-12-05 20:46:42 -080012 qDebug() << "\nTEST FAILS:";
13 qDebug() << "with context" << result.context;
swissChili3e98c062021-12-04 22:07:38 -080014 }
15
16 qDebug() << "MatchResult" << test << result.success;
swissChilid17b5a12021-12-05 20:46:42 -080017
18 if (result.success != shouldBe) {
19 qDebug() << "";
20 }
swissChili3e98c062021-12-04 22:07:38 -080021}
22
23int 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
33void runTests() {
34 printResult("a = a", true, match({Token('a')}, {Token('a')}, VarContext()));
35
swissChilid17b5a12021-12-05 20:46:42 -080036 printResult("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext()));
37
swissChili3e98c062021-12-04 22:07:38 -080038 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()));
swissChilid17b5a12021-12-05 20:46:42 -080061
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()));
swissChili3e98c062021-12-04 22:07:38 -080083}
84
swissChili7babd922021-12-02 22:46:48 -080085int main(int argc, char *argv[]) {
86 QCoreApplication a(argc, argv);
swissChili3e98c062021-12-04 22:07:38 -080087
88 runTests();
89
90 return testResults();
swissChili7babd922021-12-02 22:46:48 -080091}