-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.test.cpp
136 lines (125 loc) · 4.54 KB
/
lexer.test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Using Catch2 unit testing library
// Let Catch2 define a main function
#define CATCH_CONFIG_MAIN
#include "../include/catch.hpp"
#include "lexer.hpp"
TEST_CASE("Test Next Token") {
std::string input = R"(let five = 5;
let ten = 10;
let add = fn(x, y) {
x + y;
};
let result = add(five, ten);
!-/*5;
5 < 10 > 5;
if (5 < 10) {
return true;
} else {
return false;
}
10 == 10;
10 != 9;
"foobar"
"foo bar"
[1, 2];
{"foo": "bar"}
)";
auto test_input = Lexer(input);
Token tests[] = {
Token{TokenType::LET, "let"},
Token{TokenType::IDENT, "five"},
Token{TokenType::ASSIGN, "="},
Token{TokenType::INT, "5"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::LET, "let"},
Token{TokenType::IDENT, "ten"},
Token{TokenType::ASSIGN, "="},
Token{TokenType::INT, "10"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::LET, "let"},
Token{TokenType::IDENT, "add"},
Token{TokenType::ASSIGN, "="},
Token{TokenType::FUNCTION, "fn"},
Token{TokenType::LPAREN, "("},
Token{TokenType::IDENT, "x"},
Token{TokenType::COMMA, ","},
Token{TokenType::IDENT, "y"},
Token{TokenType::RPAREN, ")"},
Token{TokenType::LBRACE, "{"},
Token{TokenType::IDENT, "x"},
Token{TokenType::PLUS, "+"},
Token{TokenType::IDENT, "y"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::RBRACE, "}"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::LET, "let"},
Token{TokenType::IDENT, "result"},
Token{TokenType::ASSIGN, "="},
Token{TokenType::IDENT, "add"},
Token{TokenType::LPAREN, "("},
Token{TokenType::IDENT, "five"},
Token{TokenType::COMMA, ","},
Token{TokenType::IDENT, "ten"},
Token{TokenType::RPAREN, ")"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::BANG, "!"},
Token{TokenType::MINUS, "-"},
Token{TokenType::SLASH, "/"},
Token{TokenType::ASTERISK, "*"},
Token{TokenType::INT, "5"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::INT, "5"},
Token{TokenType::LT, "<"},
Token{TokenType::INT, "10"},
Token{TokenType::GT, ">"},
Token{TokenType::INT, "5"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::IF, "if"},
Token{TokenType::LPAREN, "("},
Token{TokenType::INT, "5"},
Token{TokenType::LT, "<"},
Token{TokenType::INT, "10"},
Token{TokenType::RPAREN, ")"},
Token{TokenType::LBRACE, "{"},
Token{TokenType::RETURN, "return"},
Token{TokenType::TRUE, "true"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::RBRACE, "}"},
Token{TokenType::ELSE, "else"},
Token{TokenType::LBRACE, "{"},
Token{TokenType::RETURN, "return"},
Token{TokenType::FALSE, "false"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::RBRACE, "}"},
Token{TokenType::INT, "10"},
Token{TokenType::EQ, "=="},
Token{TokenType::INT, "10"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::INT, "10"},
Token{TokenType::NOT_EQ, "!="},
Token{TokenType::INT, "9"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::STRING, "foobar"},
Token{TokenType::STRING, "foo bar"},
Token{TokenType::LBRACKET, "["},
Token{TokenType::INT, "1"},
Token{TokenType::COMMA, ","},
Token{TokenType::INT, "2"},
Token{TokenType::RBRACKET, "]"},
Token{TokenType::SEMICOLON, ";"},
Token{TokenType::LBRACE, "{"},
Token{TokenType::STRING, "foo"},
Token{TokenType::COLON, ":"},
Token{TokenType::STRING, "bar"},
Token{TokenType::RBRACE, "}"},
Token{TokenType::ENDOFFILE, ""},
};
for (const Token &t: tests) {
auto tok = test_input.next_token();
if (!(tok == t)) {
std::cerr << "tokentype wrong. expected='" << tokentype_literal(t.type) << "(" << t.literal << ")', got='"
<< tokentype_literal(tok.type) << "(" << tok.literal << ")'" << std::endl;
}
REQUIRE(tok == t);
}
}