OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library analyzer.test.generated.scanner_test; | 5 import 'package:front_end/src/base/errors.dart'; |
6 | 6 import 'package:front_end/src/base/jenkins_smi_hash.dart'; |
7 import 'package:analyzer/dart/ast/token.dart'; | 7 import 'package:front_end/src/scanner/errors.dart'; |
8 import 'package:analyzer/error/error.dart'; | 8 import 'package:front_end/src/scanner/reader.dart'; |
9 import 'package:analyzer/error/listener.dart'; | 9 import 'package:front_end/src/scanner/scanner.dart'; |
10 import 'package:analyzer/src/dart/ast/token.dart'; | 10 import 'package:front_end/src/scanner/token.dart'; |
11 import 'package:analyzer/src/dart/scanner/reader.dart'; | |
12 import 'package:analyzer/src/dart/scanner/scanner.dart'; | |
13 import 'package:analyzer/src/generated/source.dart'; | |
14 import 'package:test/test.dart'; | 11 import 'package:test/test.dart'; |
15 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 12 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
16 | 13 |
17 import 'test_support.dart'; | |
18 | |
19 main() { | 14 main() { |
20 defineReflectiveSuite(() { | 15 defineReflectiveSuite(() { |
21 defineReflectiveTests(CharSequenceReaderTest); | 16 defineReflectiveTests(CharSequenceReaderTest); |
22 defineReflectiveTests(KeywordStateTest); | 17 defineReflectiveTests(KeywordStateTest); |
23 defineReflectiveTests(ScannerTest); | 18 defineReflectiveTests(ScannerTest); |
24 defineReflectiveTests(TokenTypeTest); | 19 defineReflectiveTests(TokenTypeTest); |
25 }); | 20 }); |
26 } | 21 } |
27 | 22 |
28 class CharacterRangeReaderTest extends EngineTestCase { | |
29 void test_advance() { | |
30 CharSequenceReader baseReader = new CharSequenceReader("xyzzy"); | |
31 CharacterRangeReader reader = new CharacterRangeReader(baseReader, 1, 4); | |
32 expect(reader.advance(), 0x79); | |
33 expect(reader.advance(), 0x80); | |
34 expect(reader.advance(), 0x80); | |
35 expect(reader.advance(), -1); | |
36 expect(reader.advance(), -1); | |
37 } | |
38 | |
39 void test_creation() { | |
40 CharSequenceReader baseReader = new CharSequenceReader("xyzzy"); | |
41 CharacterRangeReader reader = new CharacterRangeReader(baseReader, 1, 4); | |
42 expect(reader, isNotNull); | |
43 } | |
44 | |
45 void test_getOffset() { | |
46 CharSequenceReader baseReader = new CharSequenceReader("xyzzy"); | |
47 CharacterRangeReader reader = new CharacterRangeReader(baseReader, 1, 2); | |
48 expect(reader.offset, 1); | |
49 reader.advance(); | |
50 expect(reader.offset, 2); | |
51 reader.advance(); | |
52 expect(reader.offset, 2); | |
53 } | |
54 | |
55 void test_getString() { | |
56 CharSequenceReader baseReader = new CharSequenceReader("__xyzzy__"); | |
57 CharacterRangeReader reader = new CharacterRangeReader(baseReader, 2, 7); | |
58 reader.offset = 5; | |
59 expect(reader.getString(3, 0), "yzz"); | |
60 expect(reader.getString(4, 1), "zzy"); | |
61 } | |
62 | |
63 void test_peek() { | |
64 CharSequenceReader baseReader = new CharSequenceReader("xyzzy"); | |
65 CharacterRangeReader reader = new CharacterRangeReader(baseReader, 1, 3); | |
66 expect(reader.peek(), 0x79); | |
67 expect(reader.peek(), 0x79); | |
68 reader.advance(); | |
69 expect(reader.peek(), 0x80); | |
70 expect(reader.peek(), 0x80); | |
71 reader.advance(); | |
72 expect(reader.peek(), -1); | |
73 expect(reader.peek(), -1); | |
74 } | |
75 | |
76 void test_setOffset() { | |
77 CharSequenceReader baseReader = new CharSequenceReader("xyzzy"); | |
78 CharacterRangeReader reader = new CharacterRangeReader(baseReader, 1, 4); | |
79 reader.offset = 2; | |
80 expect(reader.offset, 2); | |
81 } | |
82 } | |
83 | |
84 @reflectiveTest | 23 @reflectiveTest |
85 class CharSequenceReaderTest { | 24 class CharSequenceReaderTest { |
86 void test_advance() { | 25 void test_advance() { |
87 CharSequenceReader reader = new CharSequenceReader("x"); | 26 CharSequenceReader reader = new CharSequenceReader("x"); |
88 expect(reader.advance(), 0x78); | 27 expect(reader.advance(), 0x78); |
89 expect(reader.advance(), -1); | 28 expect(reader.advance(), -1); |
90 expect(reader.advance(), -1); | 29 expect(reader.advance(), -1); |
91 } | 30 } |
92 | 31 |
93 void test_creation() { | 32 void test_creation() { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 | 199 |
261 void test_colon() { | 200 void test_colon() { |
262 _assertToken(TokenType.COLON, ":"); | 201 _assertToken(TokenType.COLON, ":"); |
263 } | 202 } |
264 | 203 |
265 void test_comma() { | 204 void test_comma() { |
266 _assertToken(TokenType.COMMA, ","); | 205 _assertToken(TokenType.COMMA, ","); |
267 } | 206 } |
268 | 207 |
269 void test_comment_disabled_multi() { | 208 void test_comment_disabled_multi() { |
270 Scanner scanner = new Scanner( | 209 Scanner scanner = |
271 null, | 210 new _TestScanner(new CharSequenceReader("/* comment */ ")); |
272 new CharSequenceReader("/* comment */ "), | |
273 AnalysisErrorListener.NULL_LISTENER); | |
274 scanner.preserveComments = false; | 211 scanner.preserveComments = false; |
275 Token token = scanner.tokenize(); | 212 Token token = scanner.tokenize(); |
276 expect(token, isNotNull); | 213 expect(token, isNotNull); |
277 expect(token.precedingComments, isNull); | 214 expect(token.precedingComments, isNull); |
278 } | 215 } |
279 | 216 |
280 void test_comment_generic_method_type_assign() { | 217 void test_comment_generic_method_type_assign() { |
281 _assertComment(TokenType.MULTI_LINE_COMMENT, "/*=comment*/"); | 218 _assertComment(TokenType.MULTI_LINE_COMMENT, "/*=comment*/"); |
282 _assertComment(TokenType.GENERIC_METHOD_TYPE_ASSIGN, "/*=comment*/", | 219 _assertComment(TokenType.GENERIC_METHOD_TYPE_ASSIGN, "/*=comment*/", |
283 genericMethodComments: true); | 220 genericMethodComments: true); |
284 } | 221 } |
285 | 222 |
286 void test_comment_generic_method_type_list() { | 223 void test_comment_generic_method_type_list() { |
287 _assertComment(TokenType.MULTI_LINE_COMMENT, "/*<comment>*/"); | 224 _assertComment(TokenType.MULTI_LINE_COMMENT, "/*<comment>*/"); |
288 _assertComment(TokenType.GENERIC_METHOD_TYPE_LIST, "/*<comment>*/", | 225 _assertComment(TokenType.GENERIC_METHOD_TYPE_LIST, "/*<comment>*/", |
289 genericMethodComments: true); | 226 genericMethodComments: true); |
290 } | 227 } |
291 | 228 |
292 void test_comment_multi() { | 229 void test_comment_multi() { |
293 _assertComment(TokenType.MULTI_LINE_COMMENT, "/* comment */"); | 230 _assertComment(TokenType.MULTI_LINE_COMMENT, "/* comment */"); |
294 } | 231 } |
295 | 232 |
296 void test_comment_multi_lineEnds() { | 233 void test_comment_multi_lineEnds() { |
297 String code = r''' | 234 String code = r''' |
298 /** | 235 /** |
299 * aa | 236 * aa |
300 * bbb | 237 * bbb |
301 * c | 238 * c |
302 */'''; | 239 */'''; |
303 GatheringErrorListener listener = new GatheringErrorListener(); | 240 _ErrorListener listener = new _ErrorListener(); |
304 Scanner scanner = new Scanner(null, new CharSequenceReader(code), listener); | 241 Scanner scanner = new _TestScanner(new CharSequenceReader(code), listener); |
305 scanner.tokenize(); | 242 scanner.tokenize(); |
306 expect( | 243 expect( |
307 scanner.lineStarts, | 244 scanner.lineStarts, |
308 equals(<int>[ | 245 equals(<int>[ |
309 code.indexOf('/**'), | 246 code.indexOf('/**'), |
310 code.indexOf(' * aa'), | 247 code.indexOf(' * aa'), |
311 code.indexOf(' * bbb'), | 248 code.indexOf(' * bbb'), |
312 code.indexOf(' * c'), | 249 code.indexOf(' * c'), |
313 code.indexOf(' */') | 250 code.indexOf(' */') |
314 ])); | 251 ])); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 | 330 |
394 void test_hexidecimal_missingDigit() { | 331 void test_hexidecimal_missingDigit() { |
395 _assertError(ScannerErrorCode.MISSING_HEX_DIGIT, 1, "0x"); | 332 _assertError(ScannerErrorCode.MISSING_HEX_DIGIT, 1, "0x"); |
396 } | 333 } |
397 | 334 |
398 void test_identifier() { | 335 void test_identifier() { |
399 _assertToken(TokenType.IDENTIFIER, "result"); | 336 _assertToken(TokenType.IDENTIFIER, "result"); |
400 } | 337 } |
401 | 338 |
402 void test_illegalChar_cyrillicLetter_middle() { | 339 void test_illegalChar_cyrillicLetter_middle() { |
403 _assertError(ScannerErrorCode.ILLEGAL_CHARACTER, 5, "Shche\u0433lov"); | 340 _assertError( |
| 341 ScannerErrorCode.ILLEGAL_CHARACTER, 5, "Shche\u0433lov", [0x433]); |
404 } | 342 } |
405 | 343 |
406 void test_illegalChar_cyrillicLetter_start() { | 344 void test_illegalChar_cyrillicLetter_start() { |
407 _assertError(ScannerErrorCode.ILLEGAL_CHARACTER, 0, "\u0429"); | 345 _assertError(ScannerErrorCode.ILLEGAL_CHARACTER, 0, "\u0429", [0x429]); |
408 } | 346 } |
409 | 347 |
410 void test_illegalChar_nbsp() { | 348 void test_illegalChar_nbsp() { |
411 _assertError(ScannerErrorCode.ILLEGAL_CHARACTER, 0, "\u00A0"); | 349 _assertError(ScannerErrorCode.ILLEGAL_CHARACTER, 0, "\u00A0", [0xa0]); |
412 } | 350 } |
413 | 351 |
414 void test_illegalChar_notLetter() { | 352 void test_illegalChar_notLetter() { |
415 _assertError(ScannerErrorCode.ILLEGAL_CHARACTER, 0, "\u0312"); | 353 _assertError(ScannerErrorCode.ILLEGAL_CHARACTER, 0, "\u0312", [0x312]); |
416 } | 354 } |
417 | 355 |
418 void test_index() { | 356 void test_index() { |
419 _assertToken(TokenType.INDEX, "[]"); | 357 _assertToken(TokenType.INDEX, "[]"); |
420 } | 358 } |
421 | 359 |
422 void test_index_eq() { | 360 void test_index_eq() { |
423 _assertToken(TokenType.INDEX_EQ, "[]="); | 361 _assertToken(TokenType.INDEX_EQ, "[]="); |
424 } | 362 } |
425 | 363 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 } | 554 } |
617 | 555 |
618 void test_keyword_while() { | 556 void test_keyword_while() { |
619 _assertKeywordToken("while"); | 557 _assertKeywordToken("while"); |
620 } | 558 } |
621 | 559 |
622 void test_keyword_with() { | 560 void test_keyword_with() { |
623 _assertKeywordToken("with"); | 561 _assertKeywordToken("with"); |
624 } | 562 } |
625 | 563 |
626 void test_lineInfo_multilineComment() { | |
627 String source = "/*\r *\r */"; | |
628 _assertLineInfo(source, [ | |
629 new ScannerTest_ExpectedLocation(0, 1, 1), | |
630 new ScannerTest_ExpectedLocation(4, 2, 2), | |
631 new ScannerTest_ExpectedLocation(source.length - 1, 3, 3) | |
632 ]); | |
633 } | |
634 | |
635 void test_lineInfo_multilineString() { | |
636 String source = "'''a\r\nbc\r\nd'''"; | |
637 _assertLineInfo(source, [ | |
638 new ScannerTest_ExpectedLocation(0, 1, 1), | |
639 new ScannerTest_ExpectedLocation(7, 2, 2), | |
640 new ScannerTest_ExpectedLocation(source.length - 1, 3, 4) | |
641 ]); | |
642 } | |
643 | |
644 void test_lineInfo_multilineString_raw() { | |
645 String source = "var a = r'''\nblah\n''';\n\nfoo"; | |
646 _assertLineInfo(source, [ | |
647 new ScannerTest_ExpectedLocation(0, 1, 1), | |
648 new ScannerTest_ExpectedLocation(14, 2, 2), | |
649 new ScannerTest_ExpectedLocation(source.length - 2, 5, 2) | |
650 ]); | |
651 } | |
652 | |
653 void test_lineInfo_simpleClass() { | |
654 String source = | |
655 "class Test {\r\n String s = '...';\r\n int get x => s.MISSING_GET
TER;\r\n}"; | |
656 _assertLineInfo(source, [ | |
657 new ScannerTest_ExpectedLocation(0, 1, 1), | |
658 new ScannerTest_ExpectedLocation(source.indexOf("MISSING_GETTER"), 3, 20), | |
659 new ScannerTest_ExpectedLocation(source.length - 1, 4, 1) | |
660 ]); | |
661 } | |
662 | |
663 void test_lineInfo_slashN() { | |
664 String source = "class Test {\n}"; | |
665 _assertLineInfo(source, [ | |
666 new ScannerTest_ExpectedLocation(0, 1, 1), | |
667 new ScannerTest_ExpectedLocation(source.indexOf("}"), 2, 1) | |
668 ]); | |
669 } | |
670 | |
671 void test_lt() { | 564 void test_lt() { |
672 _assertToken(TokenType.LT, "<"); | 565 _assertToken(TokenType.LT, "<"); |
673 } | 566 } |
674 | 567 |
675 void test_lt_eq() { | 568 void test_lt_eq() { |
676 _assertToken(TokenType.LT_EQ, "<="); | 569 _assertToken(TokenType.LT_EQ, "<="); |
677 } | 570 } |
678 | 571 |
679 void test_lt_lt() { | 572 void test_lt_lt() { |
680 _assertToken(TokenType.LT_LT, "<<"); | 573 _assertToken(TokenType.LT_LT, "<<"); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 void test_scriptTag_withSpace() { | 684 void test_scriptTag_withSpace() { |
792 _assertToken(TokenType.SCRIPT_TAG, "#! /bin/dart"); | 685 _assertToken(TokenType.SCRIPT_TAG, "#! /bin/dart"); |
793 } | 686 } |
794 | 687 |
795 void test_semicolon() { | 688 void test_semicolon() { |
796 _assertToken(TokenType.SEMICOLON, ";"); | 689 _assertToken(TokenType.SEMICOLON, ";"); |
797 } | 690 } |
798 | 691 |
799 void test_setSourceStart() { | 692 void test_setSourceStart() { |
800 int offsetDelta = 42; | 693 int offsetDelta = 42; |
801 GatheringErrorListener listener = new GatheringErrorListener(); | 694 _ErrorListener listener = new _ErrorListener(); |
802 Scanner scanner = | 695 Scanner scanner = |
803 new Scanner(null, new SubSequenceReader("a", offsetDelta), listener); | 696 new _TestScanner(new SubSequenceReader("a", offsetDelta), listener); |
804 scanner.setSourceStart(3, 9); | 697 scanner.setSourceStart(3, 9); |
805 scanner.tokenize(); | 698 scanner.tokenize(); |
806 List<int> lineStarts = scanner.lineStarts; | 699 List<int> lineStarts = scanner.lineStarts; |
807 expect(lineStarts, isNotNull); | 700 expect(lineStarts, isNotNull); |
808 expect(lineStarts.length, 3); | 701 expect(lineStarts.length, 3); |
809 expect(lineStarts[2], 33); | 702 expect(lineStarts[2], 33); |
810 } | 703 } |
811 | 704 |
812 void test_slash() { | 705 void test_slash() { |
813 _assertToken(TokenType.SLASH, "/"); | 706 _assertToken(TokenType.SLASH, "/"); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 | 955 |
1063 void test_tilde_slash() { | 956 void test_tilde_slash() { |
1064 _assertToken(TokenType.TILDE_SLASH, "~/"); | 957 _assertToken(TokenType.TILDE_SLASH, "~/"); |
1065 } | 958 } |
1066 | 959 |
1067 void test_tilde_slash_eq() { | 960 void test_tilde_slash_eq() { |
1068 _assertToken(TokenType.TILDE_SLASH_EQ, "~/="); | 961 _assertToken(TokenType.TILDE_SLASH_EQ, "~/="); |
1069 } | 962 } |
1070 | 963 |
1071 void test_unclosedPairInInterpolation() { | 964 void test_unclosedPairInInterpolation() { |
1072 GatheringErrorListener listener = new GatheringErrorListener(); | 965 _ErrorListener listener = new _ErrorListener(); |
1073 _scanWithListener("'\${(}'", listener); | 966 _scanWithListener("'\${(}'", listener); |
1074 } | 967 } |
1075 | 968 |
1076 void _assertComment(TokenType commentType, String source, | 969 void _assertComment(TokenType commentType, String source, |
1077 {bool genericMethodComments: false}) { | 970 {bool genericMethodComments: false}) { |
1078 // | 971 // |
1079 // Test without a trailing end-of-line marker | 972 // Test without a trailing end-of-line marker |
1080 // | 973 // |
1081 Token token = _scan(source, genericMethodComments: genericMethodComments); | 974 Token token = _scan(source, genericMethodComments: genericMethodComments); |
1082 expect(token, isNotNull); | 975 expect(token, isNotNull); |
(...skipping 20 matching lines...) Expand all Loading... |
1103 | 996 |
1104 /** | 997 /** |
1105 * Assert that scanning the given [source] produces an error with the given | 998 * Assert that scanning the given [source] produces an error with the given |
1106 * code. | 999 * code. |
1107 * | 1000 * |
1108 * [expectedError] the error that should be produced | 1001 * [expectedError] the error that should be produced |
1109 * [expectedOffset] the string offset that should be associated with the error | 1002 * [expectedOffset] the string offset that should be associated with the error |
1110 * [source] the source to be scanned to produce the error | 1003 * [source] the source to be scanned to produce the error |
1111 */ | 1004 */ |
1112 void _assertError( | 1005 void _assertError( |
1113 ScannerErrorCode expectedError, int expectedOffset, String source) { | 1006 ScannerErrorCode expectedError, int expectedOffset, String source, |
1114 GatheringErrorListener listener = new GatheringErrorListener(); | 1007 [List<Object> arguments]) { |
| 1008 _ErrorListener listener = new _ErrorListener(); |
1115 _scanWithListener(source, listener); | 1009 _scanWithListener(source, listener); |
1116 listener.assertErrors([ | 1010 listener.assertErrors( |
1117 new AnalysisError(null, expectedOffset, 1, expectedError, | 1011 [new _TestError(expectedOffset, 1, expectedError, arguments)]); |
1118 [source.codeUnitAt(expectedOffset)]) | |
1119 ]); | |
1120 } | 1012 } |
1121 | 1013 |
1122 /** | 1014 /** |
1123 * Assert that scanning the given [source] produces an error with the given | 1015 * Assert that scanning the given [source] produces an error with the given |
1124 * code, and also produces the given tokens. | 1016 * code, and also produces the given tokens. |
1125 * | 1017 * |
1126 * [expectedError] the error that should be produced | 1018 * [expectedError] the error that should be produced |
1127 * [expectedOffset] the string offset that should be associated with the error | 1019 * [expectedOffset] the string offset that should be associated with the error |
1128 * [source] the source to be scanned to produce the error | 1020 * [source] the source to be scanned to produce the error |
1129 * [expectedTokens] the tokens that are expected to be in the source | 1021 * [expectedTokens] the tokens that are expected to be in the source |
1130 */ | 1022 */ |
1131 void _assertErrorAndTokens(ScannerErrorCode expectedError, int expectedOffset, | 1023 void _assertErrorAndTokens(ScannerErrorCode expectedError, int expectedOffset, |
1132 String source, List<Token> expectedTokens) { | 1024 String source, List<Token> expectedTokens) { |
1133 GatheringErrorListener listener = new GatheringErrorListener(); | 1025 _ErrorListener listener = new _ErrorListener(); |
1134 Token token = _scanWithListener(source, listener); | 1026 Token token = _scanWithListener(source, listener); |
1135 listener.assertErrors([ | 1027 listener |
1136 new AnalysisError(null, expectedOffset, 1, expectedError, | 1028 .assertErrors([new _TestError(expectedOffset, 1, expectedError, null)]); |
1137 [source.codeUnitAt(expectedOffset)]) | |
1138 ]); | |
1139 _checkTokens(token, expectedTokens); | 1029 _checkTokens(token, expectedTokens); |
1140 } | 1030 } |
1141 | 1031 |
1142 /** | 1032 /** |
1143 * Assert that when scanned the given [source] contains a single keyword token | 1033 * Assert that when scanned the given [source] contains a single keyword token |
1144 * with the same lexeme as the original source. | 1034 * with the same lexeme as the original source. |
1145 */ | 1035 */ |
1146 void _assertKeywordToken(String source) { | 1036 void _assertKeywordToken(String source) { |
1147 Token token = _scan(source); | 1037 Token token = _scan(source); |
1148 expect(token, isNotNull); | 1038 expect(token, isNotNull); |
1149 expect(token.type, TokenType.KEYWORD); | 1039 expect(token.type, TokenType.KEYWORD); |
1150 expect(token.offset, 0); | 1040 expect(token.offset, 0); |
1151 expect(token.length, source.length); | 1041 expect(token.length, source.length); |
1152 expect(token.lexeme, source); | 1042 expect(token.lexeme, source); |
1153 Object value = token.value(); | 1043 Object value = token.value(); |
1154 expect(value is Keyword, isTrue); | 1044 expect(value is Keyword, isTrue); |
1155 expect((value as Keyword).syntax, source); | 1045 expect((value as Keyword).syntax, source); |
1156 token = _scan(" $source "); | 1046 token = _scan(" $source "); |
1157 expect(token, isNotNull); | 1047 expect(token, isNotNull); |
1158 expect(token.type, TokenType.KEYWORD); | 1048 expect(token.type, TokenType.KEYWORD); |
1159 expect(token.offset, 1); | 1049 expect(token.offset, 1); |
1160 expect(token.length, source.length); | 1050 expect(token.length, source.length); |
1161 expect(token.lexeme, source); | 1051 expect(token.lexeme, source); |
1162 value = token.value(); | 1052 value = token.value(); |
1163 expect(value is Keyword, isTrue); | 1053 expect(value is Keyword, isTrue); |
1164 expect((value as Keyword).syntax, source); | 1054 expect((value as Keyword).syntax, source); |
1165 expect(token.next.type, TokenType.EOF); | 1055 expect(token.next.type, TokenType.EOF); |
1166 } | 1056 } |
1167 | 1057 |
1168 void _assertLineInfo( | |
1169 String source, List<ScannerTest_ExpectedLocation> expectedLocations) { | |
1170 GatheringErrorListener listener = new GatheringErrorListener(); | |
1171 _scanWithListener(source, listener); | |
1172 listener.assertNoErrors(); | |
1173 LineInfo info = listener.getLineInfo(new TestSource()); | |
1174 expect(info, isNotNull); | |
1175 int count = expectedLocations.length; | |
1176 for (int i = 0; i < count; i++) { | |
1177 ScannerTest_ExpectedLocation expectedLocation = expectedLocations[i]; | |
1178 LineInfo_Location location = info.getLocation(expectedLocation._offset); | |
1179 expect(location.lineNumber, expectedLocation._lineNumber, | |
1180 reason: 'Line number in location $i'); | |
1181 expect(location.columnNumber, expectedLocation._columnNumber, | |
1182 reason: 'Column number in location $i'); | |
1183 } | |
1184 } | |
1185 | |
1186 /** | 1058 /** |
1187 * Assert that the token scanned from the given [source] has the | 1059 * Assert that the token scanned from the given [source] has the |
1188 * [expectedType]. | 1060 * [expectedType]. |
1189 */ | 1061 */ |
1190 Token _assertToken(TokenType expectedType, String source, | 1062 Token _assertToken(TokenType expectedType, String source, |
1191 {bool lazyAssignmentOperators: false}) { | 1063 {bool lazyAssignmentOperators: false}) { |
1192 Token originalToken = | 1064 Token originalToken = |
1193 _scan(source, lazyAssignmentOperators: lazyAssignmentOperators); | 1065 _scan(source, lazyAssignmentOperators: lazyAssignmentOperators); |
1194 expect(originalToken, isNotNull); | 1066 expect(originalToken, isNotNull); |
1195 expect(originalToken.type, expectedType); | 1067 expect(originalToken.type, expectedType); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1261 reason: "Wrong lexeme for token $i"); | 1133 reason: "Wrong lexeme for token $i"); |
1262 token = token.next; | 1134 token = token.next; |
1263 expect(token, isNotNull); | 1135 expect(token, isNotNull); |
1264 } | 1136 } |
1265 expect(token.type, TokenType.EOF); | 1137 expect(token.type, TokenType.EOF); |
1266 } | 1138 } |
1267 | 1139 |
1268 Token _scan(String source, | 1140 Token _scan(String source, |
1269 {bool genericMethodComments: false, | 1141 {bool genericMethodComments: false, |
1270 bool lazyAssignmentOperators: false}) { | 1142 bool lazyAssignmentOperators: false}) { |
1271 GatheringErrorListener listener = new GatheringErrorListener(); | 1143 _ErrorListener listener = new _ErrorListener(); |
1272 Token token = _scanWithListener(source, listener, | 1144 Token token = _scanWithListener(source, listener, |
1273 genericMethodComments: genericMethodComments, | 1145 genericMethodComments: genericMethodComments, |
1274 lazyAssignmentOperators: lazyAssignmentOperators); | 1146 lazyAssignmentOperators: lazyAssignmentOperators); |
1275 listener.assertNoErrors(); | 1147 listener.assertNoErrors(); |
1276 return token; | 1148 return token; |
1277 } | 1149 } |
1278 | 1150 |
1279 Token _scanWithListener(String source, GatheringErrorListener listener, | 1151 Token _scanWithListener(String source, _ErrorListener listener, |
1280 {bool genericMethodComments: false, | 1152 {bool genericMethodComments: false, |
1281 bool lazyAssignmentOperators: false}) { | 1153 bool lazyAssignmentOperators: false}) { |
1282 Scanner scanner = | 1154 Scanner scanner = |
1283 new Scanner(null, new CharSequenceReader(source), listener); | 1155 new _TestScanner(new CharSequenceReader(source), listener); |
1284 scanner.scanGenericMethodComments = genericMethodComments; | 1156 scanner.scanGenericMethodComments = genericMethodComments; |
1285 scanner.scanLazyAssignmentOperators = lazyAssignmentOperators; | 1157 scanner.scanLazyAssignmentOperators = lazyAssignmentOperators; |
1286 Token result = scanner.tokenize(); | 1158 return scanner.tokenize(); |
1287 listener.setLineInfo(new TestSource(), scanner.lineStarts); | |
1288 return result; | |
1289 } | |
1290 } | |
1291 | |
1292 /** | |
1293 * An `ExpectedLocation` encodes information about the expected location of a | |
1294 * given offset in source code. | |
1295 */ | |
1296 class ScannerTest_ExpectedLocation { | |
1297 final int _offset; | |
1298 | |
1299 final int _lineNumber; | |
1300 | |
1301 final int _columnNumber; | |
1302 | |
1303 ScannerTest_ExpectedLocation( | |
1304 this._offset, this._lineNumber, this._columnNumber); | |
1305 } | |
1306 | |
1307 /** | |
1308 * A `TokenStreamValidator` is used to validate the correct construction of a | |
1309 * stream of tokens. | |
1310 */ | |
1311 class TokenStreamValidator { | |
1312 /** | |
1313 * Validate that the stream of tokens that starts with the given [token] is | |
1314 * correct. | |
1315 */ | |
1316 void validate(Token token) { | |
1317 StringBuffer buffer = new StringBuffer(); | |
1318 _validateStream(buffer, token); | |
1319 if (buffer.length > 0) { | |
1320 fail(buffer.toString()); | |
1321 } | |
1322 } | |
1323 | |
1324 void _validateStream(StringBuffer buffer, Token token) { | |
1325 if (token == null) { | |
1326 return; | |
1327 } | |
1328 Token previousToken = null; | |
1329 int previousEnd = -1; | |
1330 Token currentToken = token; | |
1331 while (currentToken != null && currentToken.type != TokenType.EOF) { | |
1332 _validateStream(buffer, currentToken.precedingComments); | |
1333 TokenType type = currentToken.type; | |
1334 if (type == TokenType.OPEN_CURLY_BRACKET || | |
1335 type == TokenType.OPEN_PAREN || | |
1336 type == TokenType.OPEN_SQUARE_BRACKET || | |
1337 type == TokenType.STRING_INTERPOLATION_EXPRESSION) { | |
1338 if (currentToken is! BeginToken) { | |
1339 buffer.write("\r\nExpected BeginToken, found "); | |
1340 buffer.write(currentToken.runtimeType.toString()); | |
1341 buffer.write(" "); | |
1342 _writeToken(buffer, currentToken); | |
1343 } | |
1344 } | |
1345 int currentStart = currentToken.offset; | |
1346 int currentLength = currentToken.length; | |
1347 int currentEnd = currentStart + currentLength - 1; | |
1348 if (currentStart <= previousEnd) { | |
1349 buffer.write("\r\nInvalid token sequence: "); | |
1350 _writeToken(buffer, previousToken); | |
1351 buffer.write(" followed by "); | |
1352 _writeToken(buffer, currentToken); | |
1353 } | |
1354 previousEnd = currentEnd; | |
1355 previousToken = currentToken; | |
1356 currentToken = currentToken.next; | |
1357 } | |
1358 } | |
1359 | |
1360 void _writeToken(StringBuffer buffer, Token token) { | |
1361 buffer.write("["); | |
1362 buffer.write(token.type); | |
1363 buffer.write(", '"); | |
1364 buffer.write(token.lexeme); | |
1365 buffer.write("', "); | |
1366 buffer.write(token.offset); | |
1367 buffer.write(", "); | |
1368 buffer.write(token.length); | |
1369 buffer.write("]"); | |
1370 } | 1159 } |
1371 } | 1160 } |
1372 | 1161 |
1373 @reflectiveTest | 1162 @reflectiveTest |
1374 class TokenTypeTest extends EngineTestCase { | 1163 class TokenTypeTest { |
1375 void test_isOperator() { | 1164 void test_isOperator() { |
1376 expect(TokenType.AMPERSAND.isOperator, isTrue); | 1165 expect(TokenType.AMPERSAND.isOperator, isTrue); |
1377 expect(TokenType.AMPERSAND_AMPERSAND.isOperator, isTrue); | 1166 expect(TokenType.AMPERSAND_AMPERSAND.isOperator, isTrue); |
1378 expect(TokenType.AMPERSAND_EQ.isOperator, isTrue); | 1167 expect(TokenType.AMPERSAND_EQ.isOperator, isTrue); |
1379 expect(TokenType.BANG.isOperator, isTrue); | 1168 expect(TokenType.BANG.isOperator, isTrue); |
1380 expect(TokenType.BANG_EQ.isOperator, isTrue); | 1169 expect(TokenType.BANG_EQ.isOperator, isTrue); |
1381 expect(TokenType.BAR.isOperator, isTrue); | 1170 expect(TokenType.BAR.isOperator, isTrue); |
1382 expect(TokenType.BAR_BAR.isOperator, isTrue); | 1171 expect(TokenType.BAR_BAR.isOperator, isTrue); |
1383 expect(TokenType.BAR_EQ.isOperator, isTrue); | 1172 expect(TokenType.BAR_EQ.isOperator, isTrue); |
1384 expect(TokenType.CARET.isOperator, isTrue); | 1173 expect(TokenType.CARET.isOperator, isTrue); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1430 expect(TokenType.LT_LT.isUserDefinableOperator, isTrue); | 1219 expect(TokenType.LT_LT.isUserDefinableOperator, isTrue); |
1431 expect(TokenType.MINUS.isUserDefinableOperator, isTrue); | 1220 expect(TokenType.MINUS.isUserDefinableOperator, isTrue); |
1432 expect(TokenType.PERCENT.isUserDefinableOperator, isTrue); | 1221 expect(TokenType.PERCENT.isUserDefinableOperator, isTrue); |
1433 expect(TokenType.PLUS.isUserDefinableOperator, isTrue); | 1222 expect(TokenType.PLUS.isUserDefinableOperator, isTrue); |
1434 expect(TokenType.SLASH.isUserDefinableOperator, isTrue); | 1223 expect(TokenType.SLASH.isUserDefinableOperator, isTrue); |
1435 expect(TokenType.STAR.isUserDefinableOperator, isTrue); | 1224 expect(TokenType.STAR.isUserDefinableOperator, isTrue); |
1436 expect(TokenType.TILDE.isUserDefinableOperator, isTrue); | 1225 expect(TokenType.TILDE.isUserDefinableOperator, isTrue); |
1437 expect(TokenType.TILDE_SLASH.isUserDefinableOperator, isTrue); | 1226 expect(TokenType.TILDE_SLASH.isUserDefinableOperator, isTrue); |
1438 } | 1227 } |
1439 } | 1228 } |
| 1229 |
| 1230 class _ErrorListener { |
| 1231 final errors = <_TestError>[]; |
| 1232 |
| 1233 void assertErrors(List<_TestError> expectedErrors) { |
| 1234 expect(errors, unorderedEquals(expectedErrors)); |
| 1235 } |
| 1236 |
| 1237 void assertNoErrors() { |
| 1238 assertErrors([]); |
| 1239 } |
| 1240 } |
| 1241 |
| 1242 class _TestError { |
| 1243 final int offset; |
| 1244 final int length; |
| 1245 final ErrorCode errorCode; |
| 1246 final List<Object> arguments; |
| 1247 |
| 1248 _TestError(this.offset, this.length, this.errorCode, this.arguments); |
| 1249 |
| 1250 @override |
| 1251 get hashCode { |
| 1252 var h = new JenkinsSmiHash()..add(offset)..add(length)..add(errorCode); |
| 1253 if (arguments != null) { |
| 1254 for (Object argument in arguments) { |
| 1255 h.add(argument); |
| 1256 } |
| 1257 } |
| 1258 return h.hashCode; |
| 1259 } |
| 1260 |
| 1261 @override |
| 1262 operator ==(Object other) { |
| 1263 if (other is _TestError && |
| 1264 offset == other.offset && |
| 1265 length == other.length && |
| 1266 errorCode == other.errorCode) { |
| 1267 if (arguments == null) return other.arguments == null; |
| 1268 if (other.arguments == null) return false; |
| 1269 if (arguments.length != other.arguments.length) return false; |
| 1270 for (int i = 0; i < arguments.length; i++) { |
| 1271 if (arguments[i] != other.arguments[i]) return false; |
| 1272 } |
| 1273 return true; |
| 1274 } |
| 1275 return false; |
| 1276 } |
| 1277 |
| 1278 @override |
| 1279 toString() { |
| 1280 var end = offset + length; |
| 1281 var argString = arguments == null ? '' : '(${arguments.join(', ')})'; |
| 1282 return 'Error($offset..$end, $errorCode$argString)'; |
| 1283 } |
| 1284 } |
| 1285 |
| 1286 class _TestScanner extends Scanner { |
| 1287 final _ErrorListener listener; |
| 1288 |
| 1289 _TestScanner(CharacterReader reader, [this.listener]) : super(reader); |
| 1290 |
| 1291 @override |
| 1292 void reportError( |
| 1293 ScannerErrorCode errorCode, int offset, List<Object> arguments) { |
| 1294 if (listener != null) { |
| 1295 listener.errors.add(new _TestError(offset, 1, errorCode, arguments)); |
| 1296 } |
| 1297 } |
| 1298 } |
OLD | NEW |