Debian Codegraph @

parser/lexer_test.go

raw symbols SCIP
1 package parser
2
3 import (
4 "testing"
5
6 "github.com/robertkrimen/otto/file"
7 "github.com/robertkrimen/otto/terst"
8 "github.com/robertkrimen/otto/token"
9 )
10
11 var (
12 tt = terst.Terst
13 is = terst.Is
14 )
15
16 func TestLexer(t *testing.T) {
17 tt(t, func() {
18 setup := func(src string) *parser {
19 parser := newParser("", src, 1, nil)
20 return parser
21 }
22
23 test := func(src string, test ...interface{}) {
24 parser := setup(src)
25 for len(test) > 0 {
26 tkn, literal, idx := parser.scan()
27 if len(test) > 0 {
28 is(tkn, test[0].(token.Token))
29 test = test[1:]
30 }
31 if len(test) > 0 {
32 is(literal, test[0].(string))
33 test = test[1:]
34 }
35 if len(test) > 0 {
36 // FIXME terst, Fix this so that cast to file.Idx is not necessary?
37 is(idx, file.Idx(test[0].(int)))
38 test = test[1:]
39 }
40 }
41 }
42
43 test("",
44 token.EOF, "", 1,
45 )
46
47 test("1",
48 token.NUMBER, "1", 1,
49 token.EOF, "", 2,
50 )
51
52 test(".0",
53 token.NUMBER, ".0", 1,
54 token.EOF, "", 3,
55 )
56
57 test("abc",
58 token.IDENTIFIER, "abc", 1,
59 token.EOF, "", 4,
60 )
61
62 test("abc(1)",
63 token.IDENTIFIER, "abc", 1,
64 token.LEFT_PARENTHESIS, "", 4,
65 token.NUMBER, "1", 5,
66 token.RIGHT_PARENTHESIS, "", 6,
67 token.EOF, "", 7,
68 )
69
70 test(".",
71 token.PERIOD, "", 1,
72 token.EOF, "", 2,
73 )
74
75 test("===.",
76 token.STRICT_EQUAL, "", 1,
77 token.PERIOD, "", 4,
78 token.EOF, "", 5,
79 )
80
81 test(">>>=.0",
82 token.UNSIGNED_SHIFT_RIGHT_ASSIGN, "", 1,
83 token.NUMBER, ".0", 5,
84 token.EOF, "", 7,
85 )
86
87 test(">>>=0.0.",
88 token.UNSIGNED_SHIFT_RIGHT_ASSIGN, "", 1,
89 token.NUMBER, "0.0", 5,
90 token.PERIOD, "", 8,
91 token.EOF, "", 9,
92 )
93
94 test("\"abc\"",
95 token.STRING, "\"abc\"", 1,
96 token.EOF, "", 6,
97 )
98
99 test("abc = //",
100 token.IDENTIFIER, "abc", 1,
101 token.ASSIGN, "", 5,
102 token.EOF, "", 9,
103 )
104
105 test("abc = /*test*/",
106 token.IDENTIFIER, "abc", 1,
107 token.ASSIGN, "", 5,
108 token.EOF, "", 15,
109 )
110
111 test("abc = 1 / 2",
112 token.IDENTIFIER, "abc", 1,
113 token.ASSIGN, "", 5,
114 token.NUMBER, "1", 7,
115 token.SLASH, "", 9,
116 token.NUMBER, "2", 11,
117 token.EOF, "", 12,
118 )
119
120 test("xyzzy = 'Nothing happens.'",
121 token.IDENTIFIER, "xyzzy", 1,
122 token.ASSIGN, "", 7,
123 token.STRING, "'Nothing happens.'", 9,
124 token.EOF, "", 27,
125 )
126
127 test("abc = !false",
128 token.IDENTIFIER, "abc", 1,
129 token.ASSIGN, "", 5,
130 token.NOT, "", 7,
131 token.BOOLEAN, "false", 8,
132 token.EOF, "", 13,
133 )
134
135 test("abc = !!true",
136 token.IDENTIFIER, "abc", 1,
137 token.ASSIGN, "", 5,
138 token.NOT, "", 7,
139 token.NOT, "", 8,
140 token.BOOLEAN, "true", 9,
141 token.EOF, "", 13,
142 )
143
144 test("abc *= 1",
145 token.IDENTIFIER, "abc", 1,
146 token.MULTIPLY_ASSIGN, "", 5,
147 token.NUMBER, "1", 8,
148 token.EOF, "", 9,
149 )
150
151 test("if 1 else",
152 token.IF, "if", 1,
153 token.NUMBER, "1", 4,
154 token.ELSE, "else", 6,
155 token.EOF, "", 10,
156 )
157
158 test("null",
159 token.NULL, "null", 1,
160 token.EOF, "", 5,
161 )
162
163 test(`"\u007a\x79\u000a\x78"`,
164 token.STRING, "\"\\u007a\\x79\\u000a\\x78\"", 1,
165 token.EOF, "", 23,
166 )
167
168 test(`"[First line \
169 Second line \
170 Third line\
171 . ]"
172 `,
173 token.STRING, "\"[First line \\\nSecond line \\\n Third line\\\n. ]\"", 1,
174 token.EOF, "", 53,
175 )
176
177 test("/",
178 token.SLASH, "", 1,
179 token.EOF, "", 2,
180 )
181
182 test("var abc = \"abc\uFFFFabc\"",
183 token.VAR, "var", 1,
184 token.IDENTIFIER, "abc", 5,
185 token.ASSIGN, "", 9,
186 token.STRING, "\"abc\uFFFFabc\"", 11,
187 token.EOF, "", 22,
188 )
189
190 test(`'\t' === '\r'`,
191 token.STRING, "'\\t'", 1,
192 token.STRICT_EQUAL, "", 6,
193 token.STRING, "'\\r'", 10,
194 token.EOF, "", 14,
195 )
196
197 test(`var \u0024 = 1`,
198 token.VAR, "var", 1,
199 token.IDENTIFIER, "$", 5,
200 token.ASSIGN, "", 12,
201 token.NUMBER, "1", 14,
202 token.EOF, "", 15,
203 )
204
205 test("10e10000",
206 token.NUMBER, "10e10000", 1,
207 token.EOF, "", 9,
208 )
209
210 test(`var if var class`,
211 token.VAR, "var", 1,
212 token.IF, "if", 5,
213 token.VAR, "var", 8,
214 token.KEYWORD, "class", 12,
215 token.EOF, "", 17,
216 )
217
218 test(`-0`,
219 token.MINUS, "", 1,
220 token.NUMBER, "0", 2,
221 token.EOF, "", 3,
222 )
223
224 test(`.01`,
225 token.NUMBER, ".01", 1,
226 token.EOF, "", 4,
227 )
228
229 test(`.01e+2`,
230 token.NUMBER, ".01e+2", 1,
231 token.EOF, "", 7,
232 )
233
234 test(";",
235 token.SEMICOLON, "", 1,
236 token.EOF, "", 2,
237 )
238
239 test(";;",
240 token.SEMICOLON, "", 1,
241 token.SEMICOLON, "", 2,
242 token.EOF, "", 3,
243 )
244
245 test("//",
246 token.EOF, "", 3,
247 )
248
249 test(";;//test",
250 token.SEMICOLON, "", 1,
251 token.SEMICOLON, "", 2,
252 token.EOF, "", 9,
253 )
254
255 test("1",
256 token.NUMBER, "1", 1,
257 )
258
259 test("12 123",
260 token.NUMBER, "12", 1,
261 token.NUMBER, "123", 4,
262 )
263
264 test("1.2 12.3",
265 token.NUMBER, "1.2", 1,
266 token.NUMBER, "12.3", 5,
267 )
268
269 test("/ /=",
270 token.SLASH, "", 1,
271 token.QUOTIENT_ASSIGN, "", 3,
272 )
273
274 test(`"abc"`,
275 token.STRING, `"abc"`, 1,
276 )
277
278 test(`'abc'`,
279 token.STRING, `'abc'`, 1,
280 )
281
282 test("++",
283 token.INCREMENT, "", 1,
284 )
285
286 test(">",
287 token.GREATER, "", 1,
288 )
289
290 test(">=",
291 token.GREATER_OR_EQUAL, "", 1,
292 )
293
294 test(">>",
295 token.SHIFT_RIGHT, "", 1,
296 )
297
298 test(">>=",
299 token.SHIFT_RIGHT_ASSIGN, "", 1,
300 )
301
302 test(">>>",
303 token.UNSIGNED_SHIFT_RIGHT, "", 1,
304 )
305
306 test(">>>=",
307 token.UNSIGNED_SHIFT_RIGHT_ASSIGN, "", 1,
308 )
309
310 test("1 \"abc\"",
311 token.NUMBER, "1", 1,
312 token.STRING, "\"abc\"", 3,
313 )
314
315 test(",",
316 token.COMMA, "", 1,
317 )
318
319 test("1, \"abc\"",
320 token.NUMBER, "1", 1,
321 token.COMMA, "", 2,
322 token.STRING, "\"abc\"", 4,
323 )
324
325 test("new abc(1, 3.14159);",
326 token.NEW, "new", 1,
327 token.IDENTIFIER, "abc", 5,
328 token.LEFT_PARENTHESIS, "", 8,
329 token.NUMBER, "1", 9,
330 token.COMMA, "", 10,
331 token.NUMBER, "3.14159", 12,
332 token.RIGHT_PARENTHESIS, "", 19,
333 token.SEMICOLON, "", 20,
334 )
335
336 test("1 == \"1\"",
337 token.NUMBER, "1", 1,
338 token.EQUAL, "", 3,
339 token.STRING, "\"1\"", 6,
340 )
341
342 test("1\n[]\n",
343 token.NUMBER, "1", 1,
344 token.LEFT_BRACKET, "", 3,
345 token.RIGHT_BRACKET, "", 4,
346 )
347
348 test("1\ufeff[]\ufeff",
349 token.NUMBER, "1", 1,
350 token.LEFT_BRACKET, "", 5,
351 token.RIGHT_BRACKET, "", 6,
352 )
353
354 // Identifier from Unicode Nl
355 test("\u16ee",
356 token.IDENTIFIER, "ᛮ", 1,
357 )
358
359 // Identifier from Unicode Other_ID_Start
360 test("\u212e",
361 token.IDENTIFIER, "℮", 1,
362 )
363
364 // Using char from ID_Continue after valid start char
365 test("a\u0300",
366 token.IDENTIFIER, "à", 1,
367 )
368
369 // ILLEGAL
370
371 test(`3ea`,
372 token.ILLEGAL, "3e", 1,
373 token.IDENTIFIER, "a", 3,
374 token.EOF, "", 4,
375 )
376
377 test(`3in`,
378 token.ILLEGAL, "3", 1,
379 token.IN, "in", 2,
380 token.EOF, "", 4,
381 )
382
383 test("\"Hello\nWorld\"",
384 token.ILLEGAL, "", 1,
385 token.IDENTIFIER, "World", 8,
386 token.ILLEGAL, "", 13,
387 token.EOF, "", 14,
388 )
389
390 test("\u203f = 10",
391 token.ILLEGAL, "", 1,
392 token.ASSIGN, "", 5,
393 token.NUMBER, "10", 7,
394 token.EOF, "", 9,
395 )
396
397 test(`"\x0G"`,
398 token.STRING, "\"\\x0G\"", 1,
399 token.EOF, "", 7,
400 )
401
402 // Starting identifier with ID_Continue char from Nm
403 test("\u0300",
404 token.ILLEGAL,
405 )
406
407 // Starting identifier with Pattern_Syntax
408 test("'",
409 token.ILLEGAL,
410 )
411 })
412 }