OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 class TokenizerTest { | |
6 static void main() { | |
7 print('start TokenizerTest'); | |
8 testSourceLocations(); | |
9 testBasics(); | |
10 testIncomplete(); | |
11 testInterpolation(); | |
12 print('finished TokenizerTest'); | |
13 } | |
14 | |
15 static void checkLocation(source, position, expectedLine, expectedCol) { | |
16 final loc = source.getLocationMessage('TEST', position, position, false); | |
17 final expectedLoc = source.filename + ':$expectedLine:$expectedCol: TEST'; | |
18 Expect.equals(expectedLoc, loc); | |
19 } | |
20 | |
21 static void testSourceLocations() { | |
22 var filename = 'test.dart'; | |
23 var source = new SourceFile(filename, '012\n45\n7\n9'); | |
24 checkLocation(source, 0, 1, 1); | |
25 checkLocation(source, 1, 1, 2); | |
26 checkLocation(source, 2, 1, 3); | |
27 checkLocation(source, 3, 1, 4); | |
28 | |
29 checkLocation(source, 4, 2, 1); | |
30 checkLocation(source, 5, 2, 2); | |
31 checkLocation(source, 6, 2, 3); | |
32 | |
33 checkLocation(source, 7, 3, 1); | |
34 checkLocation(source, 8, 3, 2); | |
35 | |
36 checkLocation(source, 9, 4, 1); | |
37 checkLocation(source, 10, 4, 2); | |
38 } | |
39 | |
40 static void testBasics() { | |
41 tokenTest('==> =>> +++', | |
42 ['==', '>', '=>', '>', '++', '+']); | |
43 | |
44 tokenTest('0123q 0123 0xabcg 3.14 1e10', | |
45 ['0123q', '0123', '0xabc', 'g', '3.14', '1e10'], | |
46 [TokenKind.ERROR, TokenKind.INTEGER, TokenKind.HEX_INTEGER, | |
47 TokenKind.IDENTIFIER, TokenKind.DOUBLE, TokenKind.DOUBLE]); | |
48 | |
49 tokenTest('\$"foo" @"bar" \$abc @\'foo\' \$\'bar\'', | |
50 ['\$"foo"', '@"bar"', '\$abc', "@'foo'", "\$'bar'"], | |
51 [TokenKind.STRING, TokenKind.STRING, TokenKind.IDENTIFIER, | |
52 TokenKind.STRING, TokenKind.STRING]); | |
53 | |
54 | |
55 tokenTest('"hello \\"world" // comment', | |
56 ['"hello \\"world"', '// comment'], | |
57 [TokenKind.STRING, TokenKind.COMMENT]); | |
58 | |
59 tokenTest('a_34xy _32x a.b.c', | |
60 ['a_34xy', '_32x', 'a', '.', 'b', '.', 'c']); | |
61 | |
62 tokenTest('forclass for+ class in while', | |
63 ['forclass', 'for', '+', 'class', 'in', 'while'], | |
64 [TokenKind.IDENTIFIER, TokenKind.FOR, TokenKind.ADD, | |
65 TokenKind.CLASS, TokenKind.IN, TokenKind.WHILE]); | |
66 } | |
67 | |
68 static void testIncomplete() { | |
69 tokenTest('"hello', ['"hello'], | |
70 [TokenKind.INCOMPLETE_STRING]); | |
71 tokenTest('"""hello', ['"""hello'], | |
72 [TokenKind.INCOMPLETE_MULTILINE_STRING_DQ]); | |
73 tokenTest("'''hello", ["'''hello"], | |
74 [TokenKind.INCOMPLETE_MULTILINE_STRING_SQ]); | |
75 tokenTest('/* Comment * * /', ['/* Comment * * /'], | |
76 [TokenKind.INCOMPLETE_COMMENT]); | |
77 | |
78 tokenTest('@', ['@'], [TokenKind.ERROR]); | |
79 } | |
80 | |
81 static void testInterpolation() { | |
82 tokenTest("foo''", ["foo", "''"], | |
83 [TokenKind.IDENTIFIER, TokenKind.STRING]); | |
84 tokenTest("'hello'", ["'hello'"], | |
85 [TokenKind.STRING]); | |
86 tokenTest("'hello \$foo more'", ["'hello \$", "foo", " more'"], | |
87 [TokenKind.INCOMPLETE_STRING, TokenKind.IDENTIFIER, TokenKind.STRING]); | |
88 | |
89 tokenTest("'\${2 + x} = \${y('bar')}'", | |
90 ["'\$", "{", "2", "+", "x", "}", " = \$", | |
91 "{", "y", "(", "'bar'", ")", "}", "'"]); | |
92 | |
93 tokenTest('"hello \$foo"', ['"hello \$', "foo", '"'], | |
94 [TokenKind.INCOMPLETE_STRING, TokenKind.IDENTIFIER, TokenKind.STRING]); | |
95 | |
96 | |
97 tokenTest("'hello \$foo'", ["'hello \$", "foo", "'"], | |
98 [TokenKind.INCOMPLETE_STRING, TokenKind.IDENTIFIER, TokenKind.STRING]); | |
99 | |
100 } | |
101 | |
102 static void tokenTest(String code, List<String> expected, | |
103 [List<int> kinds=null]) { | |
104 final source = new SourceFile('test.dart', code); | |
105 final tokenizer = new Tokenizer(source, false); | |
106 for (int i=0; i < expected.length; i++) { | |
107 final token = tokenizer.next(); | |
108 //print('${token.text} ${TokenKind.kindToString(token.kind)}'); | |
109 Expect.equals(expected[i], token.text); | |
110 if (kinds != null) { | |
111 Expect.equals(kinds[i], token.kind); | |
112 } | |
113 } | |
114 var t = tokenizer.next(); | |
115 Expect.equals(TokenKind.END_OF_FILE, tokenizer.next().kind); | |
116 } | |
117 } | |
OLD | NEW |