Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: packages/analyzer/test/generated/parser_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 library engine.parser_test;
6
7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/element.dart';
9 import 'package:analyzer/src/generated/engine.dart';
10 import 'package:analyzer/src/generated/error.dart';
11 import 'package:analyzer/src/generated/incremental_scanner.dart';
12 import 'package:analyzer/src/generated/parser.dart';
13 import 'package:analyzer/src/generated/scanner.dart';
14 import 'package:analyzer/src/generated/source.dart' show Source;
15 import 'package:analyzer/src/generated/testing/ast_factory.dart';
16 import 'package:analyzer/src/generated/testing/element_factory.dart';
17 import 'package:analyzer/src/generated/testing/token_factory.dart';
18 import 'package:analyzer/src/generated/utilities_dart.dart';
19 import 'package:unittest/unittest.dart';
20
21 import '../reflective_tests.dart';
22 import '../utils.dart';
23 import 'test_support.dart';
24
25 main() {
26 initializeTestEnvironment();
27 runReflectiveTests(ComplexParserTest);
28 runReflectiveTests(ErrorParserTest);
29 runReflectiveTests(IncrementalParserTest);
30 runReflectiveTests(NonErrorParserTest);
31 runReflectiveTests(RecoveryParserTest);
32 runReflectiveTests(ResolutionCopierTest);
33 runReflectiveTests(SimpleParserTest);
34 }
35
36 class AnalysisErrorListener_SimpleParserTest_computeStringValue
37 implements AnalysisErrorListener {
38 @override
39 void onError(AnalysisError event) {
40 fail(
41 "Unexpected compilation error: ${event.message} (${event.offset}, ${even t.length})");
42 }
43 }
44
45 /**
46 * Instances of the class `AstValidator` are used to validate the correct constr uction of an
47 * AST structure.
48 */
49 class AstValidator extends UnifyingAstVisitor<Object> {
50 /**
51 * A list containing the errors found while traversing the AST structure.
52 */
53 List<String> _errors = new List<String>();
54
55 /**
56 * Assert that no errors were found while traversing any of the AST structures that have been
57 * visited.
58 */
59 void assertValid() {
60 if (!_errors.isEmpty) {
61 StringBuffer buffer = new StringBuffer();
62 buffer.write("Invalid AST structure:");
63 for (String message in _errors) {
64 buffer.write("\r\n ");
65 buffer.write(message);
66 }
67 fail(buffer.toString());
68 }
69 }
70
71 @override
72 Object visitNode(AstNode node) {
73 _validate(node);
74 return super.visitNode(node);
75 }
76
77 /**
78 * Validate that the given AST node is correctly constructed.
79 *
80 * @param node the AST node being validated
81 */
82 void _validate(AstNode node) {
83 AstNode parent = node.parent;
84 if (node is CompilationUnit) {
85 if (parent != null) {
86 _errors.add("Compilation units should not have a parent");
87 }
88 } else {
89 if (parent == null) {
90 _errors.add("No parent for ${node.runtimeType}");
91 }
92 }
93 if (node.beginToken == null) {
94 _errors.add("No begin token for ${node.runtimeType}");
95 }
96 if (node.endToken == null) {
97 _errors.add("No end token for ${node.runtimeType}");
98 }
99 int nodeStart = node.offset;
100 int nodeLength = node.length;
101 if (nodeStart < 0 || nodeLength < 0) {
102 _errors.add("No source info for ${node.runtimeType}");
103 }
104 if (parent != null) {
105 int nodeEnd = nodeStart + nodeLength;
106 int parentStart = parent.offset;
107 int parentEnd = parentStart + parent.length;
108 if (nodeStart < parentStart) {
109 _errors.add(
110 "Invalid source start ($nodeStart) for ${node.runtimeType} inside ${ parent.runtimeType} ($parentStart)");
111 }
112 if (nodeEnd > parentEnd) {
113 _errors.add(
114 "Invalid source end ($nodeEnd) for ${node.runtimeType} inside ${pare nt.runtimeType} ($parentStart)");
115 }
116 }
117 }
118 }
119
120 /**
121 * The class `ComplexParserTest` defines parser tests that test the parsing of m ore complex
122 * code fragments or the interactions between multiple parsing methods. For exam ple, tests to ensure
123 * that the precedence of operations is being handled correctly should be define d in this class.
124 *
125 * Simpler tests should be defined in the class [SimpleParserTest].
126 */
127 @reflectiveTest
128 class ComplexParserTest extends ParserTestCase {
129 void test_additiveExpression_normal() {
130 BinaryExpression expression = parseExpression("x + y - z");
131 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
132 BinaryExpression, expression.leftOperand);
133 }
134
135 void test_additiveExpression_noSpaces() {
136 BinaryExpression expression = parseExpression("i+1");
137 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
138 SimpleIdentifier, expression.leftOperand);
139 EngineTestCase.assertInstanceOf((obj) => obj is IntegerLiteral,
140 IntegerLiteral, expression.rightOperand);
141 }
142
143 void test_additiveExpression_precedence_multiplicative_left() {
144 BinaryExpression expression = parseExpression("x * y + z");
145 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
146 BinaryExpression, expression.leftOperand);
147 }
148
149 void test_additiveExpression_precedence_multiplicative_left_withSuper() {
150 BinaryExpression expression = parseExpression("super * y - z");
151 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
152 BinaryExpression, expression.leftOperand);
153 }
154
155 void test_additiveExpression_precedence_multiplicative_right() {
156 BinaryExpression expression = parseExpression("x + y * z");
157 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
158 BinaryExpression, expression.rightOperand);
159 }
160
161 void test_additiveExpression_super() {
162 BinaryExpression expression = parseExpression("super + y - z");
163 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
164 BinaryExpression, expression.leftOperand);
165 }
166
167 void test_assignableExpression_arguments_normal_chain() {
168 PropertyAccess propertyAccess1 = parseExpression("a(b)(c).d(e).f");
169 expect(propertyAccess1.propertyName.name, "f");
170 //
171 // a(b)(c).d(e)
172 //
173 MethodInvocation invocation2 = EngineTestCase.assertInstanceOf(
174 (obj) => obj is MethodInvocation,
175 MethodInvocation,
176 propertyAccess1.target);
177 expect(invocation2.methodName.name, "d");
178 expect(invocation2.typeArguments, isNull);
179 ArgumentList argumentList2 = invocation2.argumentList;
180 expect(argumentList2, isNotNull);
181 expect(argumentList2.arguments, hasLength(1));
182 //
183 // a(b)(c)
184 //
185 FunctionExpressionInvocation invocation3 = EngineTestCase.assertInstanceOf(
186 (obj) => obj is FunctionExpressionInvocation,
187 FunctionExpressionInvocation,
188 invocation2.target);
189 expect(invocation3.typeArguments, isNull);
190 ArgumentList argumentList3 = invocation3.argumentList;
191 expect(argumentList3, isNotNull);
192 expect(argumentList3.arguments, hasLength(1));
193 //
194 // a(b)
195 //
196 MethodInvocation invocation4 = EngineTestCase.assertInstanceOf(
197 (obj) => obj is MethodInvocation,
198 MethodInvocation,
199 invocation3.function);
200 expect(invocation4.methodName.name, "a");
201 expect(invocation4.typeArguments, isNull);
202 ArgumentList argumentList4 = invocation4.argumentList;
203 expect(argumentList4, isNotNull);
204 expect(argumentList4.arguments, hasLength(1));
205 }
206
207 void test_assignableExpression_arguments_normal_chain_typeArguments() {
208 enableGenericMethods = true;
209 PropertyAccess propertyAccess1 = parseExpression("a<E>(b)<F>(c).d<G>(e).f");
210 expect(propertyAccess1.propertyName.name, "f");
211 //
212 // a<E>(b)<F>(c).d>G?(e)
213 //
214 MethodInvocation invocation2 = EngineTestCase.assertInstanceOf(
215 (obj) => obj is MethodInvocation,
216 MethodInvocation,
217 propertyAccess1.target);
218 expect(invocation2.methodName.name, "d");
219 expect(invocation2.typeArguments, isNotNull);
220 ArgumentList argumentList2 = invocation2.argumentList;
221 expect(argumentList2, isNotNull);
222 expect(argumentList2.arguments, hasLength(1));
223 //
224 // a<E>(b)<F>(c)
225 //
226 FunctionExpressionInvocation invocation3 = EngineTestCase.assertInstanceOf(
227 (obj) => obj is FunctionExpressionInvocation,
228 FunctionExpressionInvocation,
229 invocation2.target);
230 expect(invocation3.typeArguments, isNotNull);
231 ArgumentList argumentList3 = invocation3.argumentList;
232 expect(argumentList3, isNotNull);
233 expect(argumentList3.arguments, hasLength(1));
234 //
235 // a(b)
236 //
237 MethodInvocation invocation4 = EngineTestCase.assertInstanceOf(
238 (obj) => obj is MethodInvocation,
239 MethodInvocation,
240 invocation3.function);
241 expect(invocation4.methodName.name, "a");
242 expect(invocation4.typeArguments, isNotNull);
243 ArgumentList argumentList4 = invocation4.argumentList;
244 expect(argumentList4, isNotNull);
245 expect(argumentList4.arguments, hasLength(1));
246 }
247
248 void test_assignmentExpression_compound() {
249 AssignmentExpression expression = parseExpression("x = y = 0");
250 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
251 SimpleIdentifier, expression.leftHandSide);
252 EngineTestCase.assertInstanceOf((obj) => obj is AssignmentExpression,
253 AssignmentExpression, expression.rightHandSide);
254 }
255
256 void test_assignmentExpression_indexExpression() {
257 AssignmentExpression expression = parseExpression("x[1] = 0");
258 EngineTestCase.assertInstanceOf((obj) => obj is IndexExpression,
259 IndexExpression, expression.leftHandSide);
260 EngineTestCase.assertInstanceOf((obj) => obj is IntegerLiteral,
261 IntegerLiteral, expression.rightHandSide);
262 }
263
264 void test_assignmentExpression_prefixedIdentifier() {
265 AssignmentExpression expression = parseExpression("x.y = 0");
266 EngineTestCase.assertInstanceOf((obj) => obj is PrefixedIdentifier,
267 PrefixedIdentifier, expression.leftHandSide);
268 EngineTestCase.assertInstanceOf((obj) => obj is IntegerLiteral,
269 IntegerLiteral, expression.rightHandSide);
270 }
271
272 void test_assignmentExpression_propertyAccess() {
273 AssignmentExpression expression = parseExpression("super.y = 0");
274 EngineTestCase.assertInstanceOf((obj) => obj is PropertyAccess,
275 PropertyAccess, expression.leftHandSide);
276 EngineTestCase.assertInstanceOf((obj) => obj is IntegerLiteral,
277 IntegerLiteral, expression.rightHandSide);
278 }
279
280 void test_bitwiseAndExpression_normal() {
281 BinaryExpression expression = parseExpression("x & y & z");
282 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
283 BinaryExpression, expression.leftOperand);
284 }
285
286 void test_bitwiseAndExpression_precedence_equality_left() {
287 BinaryExpression expression = parseExpression("x == y && z");
288 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
289 BinaryExpression, expression.leftOperand);
290 }
291
292 void test_bitwiseAndExpression_precedence_equality_right() {
293 BinaryExpression expression = parseExpression("x && y == z");
294 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
295 BinaryExpression, expression.rightOperand);
296 }
297
298 void test_bitwiseAndExpression_super() {
299 BinaryExpression expression = parseExpression("super & y & z");
300 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
301 BinaryExpression, expression.leftOperand);
302 }
303
304 void test_bitwiseOrExpression_normal() {
305 BinaryExpression expression = parseExpression("x | y | z");
306 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
307 BinaryExpression, expression.leftOperand);
308 }
309
310 void test_bitwiseOrExpression_precedence_xor_left() {
311 BinaryExpression expression = parseExpression("x ^ y | z");
312 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
313 BinaryExpression, expression.leftOperand);
314 }
315
316 void test_bitwiseOrExpression_precedence_xor_right() {
317 BinaryExpression expression = parseExpression("x | y ^ z");
318 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
319 BinaryExpression, expression.rightOperand);
320 }
321
322 void test_bitwiseOrExpression_super() {
323 BinaryExpression expression = parseExpression("super | y | z");
324 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
325 BinaryExpression, expression.leftOperand);
326 }
327
328 void test_bitwiseXorExpression_normal() {
329 BinaryExpression expression = parseExpression("x ^ y ^ z");
330 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
331 BinaryExpression, expression.leftOperand);
332 }
333
334 void test_bitwiseXorExpression_precedence_and_left() {
335 BinaryExpression expression = parseExpression("x & y ^ z");
336 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
337 BinaryExpression, expression.leftOperand);
338 }
339
340 void test_bitwiseXorExpression_precedence_and_right() {
341 BinaryExpression expression = parseExpression("x ^ y & z");
342 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
343 BinaryExpression, expression.rightOperand);
344 }
345
346 void test_bitwiseXorExpression_super() {
347 BinaryExpression expression = parseExpression("super ^ y ^ z");
348 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
349 BinaryExpression, expression.leftOperand);
350 }
351
352 void test_cascade_withAssignment() {
353 CascadeExpression cascade =
354 parseExpression("new Map()..[3] = 4 ..[0] = 11;");
355 Expression target = cascade.target;
356 for (Expression section in cascade.cascadeSections) {
357 EngineTestCase.assertInstanceOf(
358 (obj) => obj is AssignmentExpression, AssignmentExpression, section);
359 Expression lhs = (section as AssignmentExpression).leftHandSide;
360 EngineTestCase.assertInstanceOf(
361 (obj) => obj is IndexExpression, IndexExpression, lhs);
362 IndexExpression index = lhs as IndexExpression;
363 expect(index.isCascaded, isTrue);
364 expect(index.realTarget, same(target));
365 }
366 }
367
368 void test_conditionalExpression_precedence_ifNullExpression() {
369 ConditionalExpression expression = parseExpression('a ?? b ? y : z');
370 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
371 BinaryExpression, expression.condition);
372 }
373
374 void test_conditionalExpression_precedence_logicalOrExpression() {
375 ConditionalExpression expression = parseExpression("a | b ? y : z");
376 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
377 BinaryExpression, expression.condition);
378 }
379
380 void test_constructor_initializer_withParenthesizedExpression() {
381 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r'''
382 class C {
383 C() :
384 this.a = (b == null ? c : d) {
385 }
386 }''');
387 NodeList<CompilationUnitMember> declarations = unit.declarations;
388 expect(declarations, hasLength(1));
389 }
390
391 void test_equalityExpression_normal() {
392 BinaryExpression expression = parseExpression(
393 "x == y != z", [ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND]);
394 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
395 BinaryExpression, expression.leftOperand);
396 }
397
398 void test_equalityExpression_precedence_relational_left() {
399 BinaryExpression expression = parseExpression("x is y == z");
400 EngineTestCase.assertInstanceOf(
401 (obj) => obj is IsExpression, IsExpression, expression.leftOperand);
402 }
403
404 void test_equalityExpression_precedence_relational_right() {
405 BinaryExpression expression = parseExpression("x == y is z");
406 EngineTestCase.assertInstanceOf(
407 (obj) => obj is IsExpression, IsExpression, expression.rightOperand);
408 }
409
410 void test_equalityExpression_super() {
411 BinaryExpression expression = parseExpression("super == y != z",
412 [ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND]);
413 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
414 BinaryExpression, expression.leftOperand);
415 }
416
417 void test_ifNullExpression() {
418 BinaryExpression expression = parseExpression('x ?? y ?? z');
419 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
420 BinaryExpression, expression.leftOperand);
421 }
422
423 void test_ifNullExpression_precedence_logicalOr_left() {
424 BinaryExpression expression = parseExpression('x || y ?? z');
425 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
426 BinaryExpression, expression.leftOperand);
427 }
428
429 void test_ifNullExpression_precendce_logicalOr_right() {
430 BinaryExpression expression = parseExpression('x ?? y || z');
431 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
432 BinaryExpression, expression.rightOperand);
433 }
434
435 void test_logicalAndExpression() {
436 BinaryExpression expression = parseExpression("x && y && z");
437 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
438 BinaryExpression, expression.leftOperand);
439 }
440
441 void test_logicalAndExpression_precedence_bitwiseOr_left() {
442 BinaryExpression expression = parseExpression("x | y < z");
443 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
444 BinaryExpression, expression.leftOperand);
445 }
446
447 void test_logicalAndExpression_precedence_bitwiseOr_right() {
448 BinaryExpression expression = parseExpression("x < y | z");
449 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
450 BinaryExpression, expression.rightOperand);
451 }
452
453 void test_logicalOrExpression() {
454 BinaryExpression expression = parseExpression("x || y || z");
455 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
456 BinaryExpression, expression.leftOperand);
457 }
458
459 void test_logicalOrExpression_precedence_logicalAnd_left() {
460 BinaryExpression expression = parseExpression("x && y || z");
461 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
462 BinaryExpression, expression.leftOperand);
463 }
464
465 void test_logicalOrExpression_precedence_logicalAnd_right() {
466 BinaryExpression expression = parseExpression("x || y && z");
467 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
468 BinaryExpression, expression.rightOperand);
469 }
470
471 void test_multipleLabels_statement() {
472 LabeledStatement statement =
473 ParserTestCase.parseStatement("a: b: c: return x;");
474 expect(statement.labels, hasLength(3));
475 EngineTestCase.assertInstanceOf(
476 (obj) => obj is ReturnStatement, ReturnStatement, statement.statement);
477 }
478
479 void test_multiplicativeExpression_normal() {
480 BinaryExpression expression = parseExpression("x * y / z");
481 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
482 BinaryExpression, expression.leftOperand);
483 }
484
485 void test_multiplicativeExpression_precedence_unary_left() {
486 BinaryExpression expression = parseExpression("-x * y");
487 EngineTestCase.assertInstanceOf((obj) => obj is PrefixExpression,
488 PrefixExpression, expression.leftOperand);
489 }
490
491 void test_multiplicativeExpression_precedence_unary_right() {
492 BinaryExpression expression = parseExpression("x * -y");
493 EngineTestCase.assertInstanceOf((obj) => obj is PrefixExpression,
494 PrefixExpression, expression.rightOperand);
495 }
496
497 void test_multiplicativeExpression_super() {
498 BinaryExpression expression = parseExpression("super * y / z");
499 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
500 BinaryExpression, expression.leftOperand);
501 }
502
503 void test_relationalExpression_precedence_shift_right() {
504 IsExpression expression = parseExpression("x << y is z");
505 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
506 BinaryExpression, expression.expression);
507 }
508
509 void test_shiftExpression_normal() {
510 BinaryExpression expression = parseExpression("x >> 4 << 3");
511 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
512 BinaryExpression, expression.leftOperand);
513 }
514
515 void test_shiftExpression_precedence_additive_left() {
516 BinaryExpression expression = parseExpression("x + y << z");
517 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
518 BinaryExpression, expression.leftOperand);
519 }
520
521 void test_shiftExpression_precedence_additive_right() {
522 BinaryExpression expression = parseExpression("x << y + z");
523 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
524 BinaryExpression, expression.rightOperand);
525 }
526
527 void test_shiftExpression_super() {
528 BinaryExpression expression = parseExpression("super >> 4 << 3");
529 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
530 BinaryExpression, expression.leftOperand);
531 }
532 }
533
534 /**
535 * The class `ErrorParserTest` defines parser tests that test the parsing of cod e to ensure
536 * that errors are correctly reported, and in some cases, not reported.
537 */
538 @reflectiveTest
539 class ErrorParserTest extends ParserTestCase {
540 void fail_expectedListOrMapLiteral() {
541 // It isn't clear that this test can ever pass. The parser is currently
542 // create a synthetic list literal in this case, but isSynthetic() isn't
543 // overridden for ListLiteral. The problem is that the synthetic list
544 // literals that are being created are not always zero length (because they
545 // could have type parameters), which violates the contract of
546 // isSynthetic().
547 TypedLiteral literal = parse3("parseListOrMapLiteral", <Object>[null], "1",
548 [ParserErrorCode.EXPECTED_LIST_OR_MAP_LITERAL]);
549 expect(literal.isSynthetic, isTrue);
550 }
551
552 void fail_illegalAssignmentToNonAssignable_superAssigned() {
553 // TODO(brianwilkerson) When this test starts to pass, remove the test
554 // test_illegalAssignmentToNonAssignable_superAssigned.
555 parseExpression(
556 "super = x;", [ParserErrorCode.ILLEGAL_ASSIGNMENT_TO_NON_ASSIGNABLE]);
557 }
558
559 void fail_invalidCommentReference__new_nonIdentifier() {
560 // This test fails because the method parseCommentReference returns null.
561 parse3("parseCommentReference", <Object>["new 42", 0], "",
562 [ParserErrorCode.INVALID_COMMENT_REFERENCE]);
563 }
564
565 void fail_invalidCommentReference__new_tooMuch() {
566 parse3("parseCommentReference", <Object>["new a.b.c.d", 0], "",
567 [ParserErrorCode.INVALID_COMMENT_REFERENCE]);
568 }
569
570 void fail_invalidCommentReference__nonNew_nonIdentifier() {
571 // This test fails because the method parseCommentReference returns null.
572 parse3("parseCommentReference", <Object>["42", 0], "",
573 [ParserErrorCode.INVALID_COMMENT_REFERENCE]);
574 }
575
576 void fail_invalidCommentReference__nonNew_tooMuch() {
577 parse3("parseCommentReference", <Object>["a.b.c.d", 0], "",
578 [ParserErrorCode.INVALID_COMMENT_REFERENCE]);
579 }
580
581 void fail_missingClosingParenthesis() {
582 // It is possible that it is not possible to generate this error (that it's
583 // being reported in code that cannot actually be reached), but that hasn't
584 // been proven yet.
585 parse4("parseFormalParameterList", "(int a, int b ;",
586 [ParserErrorCode.MISSING_CLOSING_PARENTHESIS]);
587 }
588
589 void fail_missingFunctionParameters_local_nonVoid_block() {
590 // The parser does not recognize this as a function declaration, so it tries
591 // to parse it as an expression statement. It isn't clear what the best
592 // error message is in this case.
593 ParserTestCase.parseStatement(
594 "int f { return x;}", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
595 }
596
597 void fail_missingFunctionParameters_local_nonVoid_expression() {
598 // The parser does not recognize this as a function declaration, so it tries
599 // to parse it as an expression statement. It isn't clear what the best
600 // error message is in this case.
601 ParserTestCase.parseStatement(
602 "int f => x;", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
603 }
604
605 void fail_namedFunctionExpression() {
606 Expression expression = parse4("parsePrimaryExpression", "f() {}",
607 [ParserErrorCode.NAMED_FUNCTION_EXPRESSION]);
608 EngineTestCase.assertInstanceOf(
609 (obj) => obj is FunctionExpression, FunctionExpression, expression);
610 }
611
612 void fail_unexpectedToken_invalidPostfixExpression() {
613 // Note: this might not be the right error to produce, but some error should
614 // be produced
615 parseExpression("f()++", [ParserErrorCode.UNEXPECTED_TOKEN]);
616 }
617
618 void fail_varAndType_local() {
619 // This is currently reporting EXPECTED_TOKEN for a missing semicolon, but
620 // this would be a better error message.
621 ParserTestCase.parseStatement("var int x;", [ParserErrorCode.VAR_AND_TYPE]);
622 }
623
624 void fail_varAndType_parameter() {
625 // This is currently reporting EXPECTED_TOKEN for a missing semicolon, but
626 // this would be a better error message.
627 parse4("parseFormalParameterList", "(var int x)",
628 [ParserErrorCode.VAR_AND_TYPE]);
629 }
630
631 void test_abstractClassMember_constructor() {
632 parse3("parseClassMember", <Object>["C"], "abstract C.c();",
633 [ParserErrorCode.ABSTRACT_CLASS_MEMBER]);
634 }
635
636 void test_abstractClassMember_field() {
637 parse3("parseClassMember", <Object>["C"], "abstract C f;",
638 [ParserErrorCode.ABSTRACT_CLASS_MEMBER]);
639 }
640
641 void test_abstractClassMember_getter() {
642 parse3("parseClassMember", <Object>["C"], "abstract get m;",
643 [ParserErrorCode.ABSTRACT_CLASS_MEMBER]);
644 }
645
646 void test_abstractClassMember_method() {
647 parse3("parseClassMember", <Object>["C"], "abstract m();",
648 [ParserErrorCode.ABSTRACT_CLASS_MEMBER]);
649 }
650
651 void test_abstractClassMember_setter() {
652 parse3("parseClassMember", <Object>["C"], "abstract set m(v);",
653 [ParserErrorCode.ABSTRACT_CLASS_MEMBER]);
654 }
655
656 void test_abstractEnum() {
657 ParserTestCase.parseCompilationUnit(
658 "abstract enum E {ONE}", [ParserErrorCode.ABSTRACT_ENUM]);
659 }
660
661 void test_abstractTopLevelFunction_function() {
662 ParserTestCase.parseCompilationUnit(
663 "abstract f(v) {}", [ParserErrorCode.ABSTRACT_TOP_LEVEL_FUNCTION]);
664 }
665
666 void test_abstractTopLevelFunction_getter() {
667 ParserTestCase.parseCompilationUnit(
668 "abstract get m {}", [ParserErrorCode.ABSTRACT_TOP_LEVEL_FUNCTION]);
669 }
670
671 void test_abstractTopLevelFunction_setter() {
672 ParserTestCase.parseCompilationUnit(
673 "abstract set m(v) {}", [ParserErrorCode.ABSTRACT_TOP_LEVEL_FUNCTION]);
674 }
675
676 void test_abstractTopLevelVariable() {
677 ParserTestCase.parseCompilationUnit(
678 "abstract C f;", [ParserErrorCode.ABSTRACT_TOP_LEVEL_VARIABLE]);
679 }
680
681 void test_abstractTypeDef() {
682 ParserTestCase.parseCompilationUnit(
683 "abstract typedef F();", [ParserErrorCode.ABSTRACT_TYPEDEF]);
684 }
685
686 void test_annotationOnEnumConstant_first() {
687 ParserTestCase.parseCompilationUnit("enum E { @override C }",
688 [ParserErrorCode.ANNOTATION_ON_ENUM_CONSTANT]);
689 }
690
691 void test_annotationOnEnumConstant_middle() {
692 ParserTestCase.parseCompilationUnit("enum E { C, @override D, E }",
693 [ParserErrorCode.ANNOTATION_ON_ENUM_CONSTANT]);
694 }
695
696 void test_assertDoesNotTakeAssignment() {
697 parse4("parseAssertStatement", "assert(b = true);",
698 [ParserErrorCode.ASSERT_DOES_NOT_TAKE_ASSIGNMENT]);
699 }
700
701 void test_assertDoesNotTakeCascades() {
702 parse4("parseAssertStatement", "assert(new A()..m());",
703 [ParserErrorCode.ASSERT_DOES_NOT_TAKE_CASCADE]);
704 }
705
706 void test_assertDoesNotTakeRethrow() {
707 parse4("parseAssertStatement", "assert(rethrow);",
708 [ParserErrorCode.ASSERT_DOES_NOT_TAKE_RETHROW]);
709 }
710
711 void test_assertDoesNotTakeThrow() {
712 parse4("parseAssertStatement", "assert(throw x);",
713 [ParserErrorCode.ASSERT_DOES_NOT_TAKE_THROW]);
714 }
715
716 void test_breakOutsideOfLoop_breakInDoStatement() {
717 parse4("parseDoStatement", "do {break;} while (x);");
718 }
719
720 void test_breakOutsideOfLoop_breakInForStatement() {
721 parse4("parseForStatement", "for (; x;) {break;}");
722 }
723
724 void test_breakOutsideOfLoop_breakInIfStatement() {
725 parse4("parseIfStatement", "if (x) {break;}",
726 [ParserErrorCode.BREAK_OUTSIDE_OF_LOOP]);
727 }
728
729 void test_breakOutsideOfLoop_breakInSwitchStatement() {
730 parse4("parseSwitchStatement", "switch (x) {case 1: break;}");
731 }
732
733 void test_breakOutsideOfLoop_breakInWhileStatement() {
734 parse4("parseWhileStatement", "while (x) {break;}");
735 }
736
737 void test_breakOutsideOfLoop_functionExpression_inALoop() {
738 ParserTestCase.parseStatement(
739 "for(; x;) {() {break;};}", [ParserErrorCode.BREAK_OUTSIDE_OF_LOOP]);
740 }
741
742 void test_breakOutsideOfLoop_functionExpression_withALoop() {
743 ParserTestCase.parseStatement("() {for (; x;) {break;}};");
744 }
745
746 void test_classInClass_abstract() {
747 ParserTestCase.parseCompilationUnit(
748 "class C { abstract class B {} }", [ParserErrorCode.CLASS_IN_CLASS]);
749 }
750
751 void test_classInClass_nonAbstract() {
752 ParserTestCase.parseCompilationUnit(
753 "class C { class B {} }", [ParserErrorCode.CLASS_IN_CLASS]);
754 }
755
756 void test_classTypeAlias_abstractAfterEq() {
757 // This syntax has been removed from the language in favor of
758 // "abstract class A = B with C;" (issue 18098).
759 parse3(
760 "parseCompilationUnitMember",
761 <Object>[emptyCommentAndMetadata()],
762 "class A = abstract B with C;",
763 [ParserErrorCode.EXPECTED_TOKEN, ParserErrorCode.EXPECTED_TOKEN]);
764 }
765
766 void test_colonInPlaceOfIn() {
767 ParserTestCase.parseStatement(
768 "for (var x : list) {}", [ParserErrorCode.COLON_IN_PLACE_OF_IN]);
769 }
770
771 void test_constAndFinal() {
772 parse3("parseClassMember", <Object>["C"], "const final int x;",
773 [ParserErrorCode.CONST_AND_FINAL]);
774 }
775
776 void test_constAndVar() {
777 parse3("parseClassMember", <Object>["C"], "const var x;",
778 [ParserErrorCode.CONST_AND_VAR]);
779 }
780
781 void test_constClass() {
782 ParserTestCase.parseCompilationUnit(
783 "const class C {}", [ParserErrorCode.CONST_CLASS]);
784 }
785
786 void test_constConstructorWithBody() {
787 parse3("parseClassMember", <Object>["C"], "const C() {}",
788 [ParserErrorCode.CONST_CONSTRUCTOR_WITH_BODY]);
789 }
790
791 void test_constEnum() {
792 ParserTestCase.parseCompilationUnit(
793 "const enum E {ONE}", [ParserErrorCode.CONST_ENUM]);
794 }
795
796 void test_constFactory() {
797 parse3("parseClassMember", <Object>["C"], "const factory C() {}",
798 [ParserErrorCode.CONST_FACTORY]);
799 }
800
801 void test_constMethod() {
802 parse3("parseClassMember", <Object>["C"], "const int m() {}",
803 [ParserErrorCode.CONST_METHOD]);
804 }
805
806 void test_constructorWithReturnType() {
807 parse3("parseClassMember", <Object>["C"], "C C() {}",
808 [ParserErrorCode.CONSTRUCTOR_WITH_RETURN_TYPE]);
809 }
810
811 void test_constructorWithReturnType_var() {
812 parse3("parseClassMember", <Object>["C"], "var C() {}",
813 [ParserErrorCode.CONSTRUCTOR_WITH_RETURN_TYPE]);
814 }
815
816 void test_constTypedef() {
817 ParserTestCase.parseCompilationUnit(
818 "const typedef F();", [ParserErrorCode.CONST_TYPEDEF]);
819 }
820
821 void test_continueOutsideOfLoop_continueInDoStatement() {
822 parse4("parseDoStatement", "do {continue;} while (x);");
823 }
824
825 void test_continueOutsideOfLoop_continueInForStatement() {
826 parse4("parseForStatement", "for (; x;) {continue;}");
827 }
828
829 void test_continueOutsideOfLoop_continueInIfStatement() {
830 parse4("parseIfStatement", "if (x) {continue;}",
831 [ParserErrorCode.CONTINUE_OUTSIDE_OF_LOOP]);
832 }
833
834 void test_continueOutsideOfLoop_continueInSwitchStatement() {
835 parse4("parseSwitchStatement", "switch (x) {case 1: continue a;}");
836 }
837
838 void test_continueOutsideOfLoop_continueInWhileStatement() {
839 parse4("parseWhileStatement", "while (x) {continue;}");
840 }
841
842 void test_continueOutsideOfLoop_functionExpression_inALoop() {
843 ParserTestCase.parseStatement("for(; x;) {() {continue;};}",
844 [ParserErrorCode.CONTINUE_OUTSIDE_OF_LOOP]);
845 }
846
847 void test_continueOutsideOfLoop_functionExpression_withALoop() {
848 ParserTestCase.parseStatement("() {for (; x;) {continue;}};");
849 }
850
851 void test_continueWithoutLabelInCase_error() {
852 parse4("parseSwitchStatement", "switch (x) {case 1: continue;}",
853 [ParserErrorCode.CONTINUE_WITHOUT_LABEL_IN_CASE]);
854 }
855
856 void test_continueWithoutLabelInCase_noError() {
857 parse4("parseSwitchStatement", "switch (x) {case 1: continue a;}");
858 }
859
860 void test_continueWithoutLabelInCase_noError_switchInLoop() {
861 parse4(
862 "parseWhileStatement", "while (a) { switch (b) {default: continue;}}");
863 }
864
865 void test_deprecatedClassTypeAlias() {
866 ParserTestCase.parseCompilationUnit(
867 "typedef C = S with M;", [ParserErrorCode.DEPRECATED_CLASS_TYPE_ALIAS]);
868 }
869
870 void test_deprecatedClassTypeAlias_withGeneric() {
871 ParserTestCase.parseCompilationUnit("typedef C<T> = S<T> with M;",
872 [ParserErrorCode.DEPRECATED_CLASS_TYPE_ALIAS]);
873 }
874
875 void test_directiveAfterDeclaration_classBeforeDirective() {
876 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
877 "class Foo{} library l;",
878 [ParserErrorCode.DIRECTIVE_AFTER_DECLARATION]);
879 expect(unit, isNotNull);
880 }
881
882 void test_directiveAfterDeclaration_classBetweenDirectives() {
883 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
884 "library l;\nclass Foo{}\npart 'a.dart';",
885 [ParserErrorCode.DIRECTIVE_AFTER_DECLARATION]);
886 expect(unit, isNotNull);
887 }
888
889 void test_duplicatedModifier_const() {
890 parse3("parseClassMember", <Object>["C"], "const const m;",
891 [ParserErrorCode.DUPLICATED_MODIFIER]);
892 }
893
894 void test_duplicatedModifier_external() {
895 parse3("parseClassMember", <Object>["C"], "external external f();",
896 [ParserErrorCode.DUPLICATED_MODIFIER]);
897 }
898
899 void test_duplicatedModifier_factory() {
900 parse3("parseClassMember", <Object>["C"], "factory factory C() {}",
901 [ParserErrorCode.DUPLICATED_MODIFIER]);
902 }
903
904 void test_duplicatedModifier_final() {
905 parse3("parseClassMember", <Object>["C"], "final final m;",
906 [ParserErrorCode.DUPLICATED_MODIFIER]);
907 }
908
909 void test_duplicatedModifier_static() {
910 parse3("parseClassMember", <Object>["C"], "static static var m;",
911 [ParserErrorCode.DUPLICATED_MODIFIER]);
912 }
913
914 void test_duplicatedModifier_var() {
915 parse3("parseClassMember", <Object>["C"], "var var m;",
916 [ParserErrorCode.DUPLICATED_MODIFIER]);
917 }
918
919 void test_duplicateLabelInSwitchStatement() {
920 parse4(
921 "parseSwitchStatement",
922 "switch (e) {l1: case 0: break; l1: case 1: break;}",
923 [ParserErrorCode.DUPLICATE_LABEL_IN_SWITCH_STATEMENT]);
924 }
925
926 void test_emptyEnumBody() {
927 parse3("parseEnumDeclaration", <Object>[emptyCommentAndMetadata()],
928 "enum E {}", [ParserErrorCode.EMPTY_ENUM_BODY]);
929 }
930
931 void test_enumInClass() {
932 ParserTestCase.parseCompilationUnit(
933 r'''
934 class Foo {
935 enum Bar {
936 Bar1, Bar2, Bar3
937 }
938 }
939 ''',
940 [ParserErrorCode.ENUM_IN_CLASS]);
941 }
942
943 void test_equalityCannotBeEqualityOperand_eq_eq() {
944 parseExpression(
945 "1 == 2 == 3", [ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND]);
946 }
947
948 void test_equalityCannotBeEqualityOperand_eq_neq() {
949 parseExpression(
950 "1 == 2 != 3", [ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND]);
951 }
952
953 void test_equalityCannotBeEqualityOperand_neq_eq() {
954 parseExpression(
955 "1 != 2 == 3", [ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND]);
956 }
957
958 void test_expectedCaseOrDefault() {
959 parse4("parseSwitchStatement", "switch (e) {break;}",
960 [ParserErrorCode.EXPECTED_CASE_OR_DEFAULT]);
961 }
962
963 void test_expectedClassMember_inClass_afterType() {
964 parse3("parseClassMember", <Object>["C"], "heart 2 heart",
965 [ParserErrorCode.EXPECTED_CLASS_MEMBER]);
966 }
967
968 void test_expectedClassMember_inClass_beforeType() {
969 parse3("parseClassMember", <Object>["C"], "4 score",
970 [ParserErrorCode.EXPECTED_CLASS_MEMBER]);
971 }
972
973 void test_expectedExecutable_inClass_afterVoid() {
974 parse3("parseClassMember", <Object>["C"], "void 2 void",
975 [ParserErrorCode.EXPECTED_EXECUTABLE]);
976 }
977
978 void test_expectedExecutable_topLevel_afterType() {
979 parse3("parseCompilationUnitMember", <Object>[emptyCommentAndMetadata()],
980 "heart 2 heart", [ParserErrorCode.EXPECTED_EXECUTABLE]);
981 }
982
983 void test_expectedExecutable_topLevel_afterVoid() {
984 parse3("parseCompilationUnitMember", <Object>[emptyCommentAndMetadata()],
985 "void 2 void", [ParserErrorCode.EXPECTED_EXECUTABLE]);
986 }
987
988 void test_expectedExecutable_topLevel_beforeType() {
989 parse3("parseCompilationUnitMember", <Object>[emptyCommentAndMetadata()],
990 "4 score", [ParserErrorCode.EXPECTED_EXECUTABLE]);
991 }
992
993 void test_expectedExecutable_topLevel_eof() {
994 parse2(
995 "parseCompilationUnitMember",
996 <Object>[emptyCommentAndMetadata()],
997 "x",
998 [new AnalysisError(null, 0, 1, ParserErrorCode.EXPECTED_EXECUTABLE)]);
999 }
1000
1001 void test_expectedInterpolationIdentifier() {
1002 parse4(
1003 "parseStringLiteral", "'\$x\$'", [ParserErrorCode.MISSING_IDENTIFIER]);
1004 }
1005
1006 void test_expectedInterpolationIdentifier_emptyString() {
1007 // The scanner inserts an empty string token between the two $'s; we need to
1008 // make sure that the MISSING_IDENTIFIER error that is generated has a
1009 // nonzero width so that it will show up in the editor UI.
1010 parse2("parseStringLiteral", <Object>[], "'\$\$foo'",
1011 [new AnalysisError(null, 2, 1, ParserErrorCode.MISSING_IDENTIFIER)]);
1012 }
1013
1014 void test_expectedStringLiteral() {
1015 StringLiteral expression = parse4(
1016 "parseStringLiteral", "1", [ParserErrorCode.EXPECTED_STRING_LITERAL]);
1017 expect(expression.isSynthetic, isTrue);
1018 }
1019
1020 void test_expectedToken_commaMissingInArgumentList() {
1021 parse4("parseArgumentList", "(x, y z)", [ParserErrorCode.EXPECTED_TOKEN]);
1022 }
1023
1024 void test_expectedToken_parseStatement_afterVoid() {
1025 ParserTestCase.parseStatement("void}",
1026 [ParserErrorCode.EXPECTED_TOKEN, ParserErrorCode.MISSING_IDENTIFIER]);
1027 }
1028
1029 void test_expectedToken_semicolonAfterClass() {
1030 Token token = TokenFactory.tokenFromKeyword(Keyword.CLASS);
1031 parse3(
1032 "parseClassTypeAlias",
1033 <Object>[emptyCommentAndMetadata(), null, token],
1034 "A = B with C",
1035 [ParserErrorCode.EXPECTED_TOKEN]);
1036 }
1037
1038 void test_expectedToken_semicolonMissingAfterExport() {
1039 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1040 "export '' class A {}", [ParserErrorCode.EXPECTED_TOKEN]);
1041 ExportDirective directive = unit.directives[0] as ExportDirective;
1042 Token semicolon = directive.semicolon;
1043 expect(semicolon, isNotNull);
1044 expect(semicolon.isSynthetic, isTrue);
1045 }
1046
1047 void test_expectedToken_semicolonMissingAfterExpression() {
1048 ParserTestCase.parseStatement("x", [ParserErrorCode.EXPECTED_TOKEN]);
1049 }
1050
1051 void test_expectedToken_semicolonMissingAfterImport() {
1052 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1053 "import '' class A {}", [ParserErrorCode.EXPECTED_TOKEN]);
1054 ImportDirective directive = unit.directives[0] as ImportDirective;
1055 Token semicolon = directive.semicolon;
1056 expect(semicolon, isNotNull);
1057 expect(semicolon.isSynthetic, isTrue);
1058 }
1059
1060 void test_expectedToken_whileMissingInDoStatement() {
1061 ParserTestCase.parseStatement(
1062 "do {} (x);", [ParserErrorCode.EXPECTED_TOKEN]);
1063 }
1064
1065 void test_expectedTypeName_is() {
1066 parseExpression("x is", [ParserErrorCode.EXPECTED_TYPE_NAME]);
1067 }
1068
1069 void test_exportDirectiveAfterPartDirective() {
1070 ParserTestCase.parseCompilationUnit("part 'a.dart'; export 'b.dart';",
1071 [ParserErrorCode.EXPORT_DIRECTIVE_AFTER_PART_DIRECTIVE]);
1072 }
1073
1074 void test_externalAfterConst() {
1075 parse3("parseClassMember", <Object>["C"], "const external C();",
1076 [ParserErrorCode.EXTERNAL_AFTER_CONST]);
1077 }
1078
1079 void test_externalAfterFactory() {
1080 parse3("parseClassMember", <Object>["C"], "factory external C();",
1081 [ParserErrorCode.EXTERNAL_AFTER_FACTORY]);
1082 }
1083
1084 void test_externalAfterStatic() {
1085 parse3("parseClassMember", <Object>["C"], "static external int m();",
1086 [ParserErrorCode.EXTERNAL_AFTER_STATIC]);
1087 }
1088
1089 void test_externalClass() {
1090 ParserTestCase.parseCompilationUnit(
1091 "external class C {}", [ParserErrorCode.EXTERNAL_CLASS]);
1092 }
1093
1094 void test_externalConstructorWithBody_factory() {
1095 parse3("parseClassMember", <Object>["C"], "external factory C() {}",
1096 [ParserErrorCode.EXTERNAL_CONSTRUCTOR_WITH_BODY]);
1097 }
1098
1099 void test_externalConstructorWithBody_named() {
1100 parse3("parseClassMember", <Object>["C"], "external C.c() {}",
1101 [ParserErrorCode.EXTERNAL_CONSTRUCTOR_WITH_BODY]);
1102 }
1103
1104 void test_externalEnum() {
1105 ParserTestCase.parseCompilationUnit(
1106 "external enum E {ONE}", [ParserErrorCode.EXTERNAL_ENUM]);
1107 }
1108
1109 void test_externalField_const() {
1110 parse3("parseClassMember", <Object>["C"], "external const A f;",
1111 [ParserErrorCode.EXTERNAL_FIELD]);
1112 }
1113
1114 void test_externalField_final() {
1115 parse3("parseClassMember", <Object>["C"], "external final A f;",
1116 [ParserErrorCode.EXTERNAL_FIELD]);
1117 }
1118
1119 void test_externalField_static() {
1120 parse3("parseClassMember", <Object>["C"], "external static A f;",
1121 [ParserErrorCode.EXTERNAL_FIELD]);
1122 }
1123
1124 void test_externalField_typed() {
1125 parse3("parseClassMember", <Object>["C"], "external A f;",
1126 [ParserErrorCode.EXTERNAL_FIELD]);
1127 }
1128
1129 void test_externalField_untyped() {
1130 parse3("parseClassMember", <Object>["C"], "external var f;",
1131 [ParserErrorCode.EXTERNAL_FIELD]);
1132 }
1133
1134 void test_externalGetterWithBody() {
1135 parse3("parseClassMember", <Object>["C"], "external int get x {}",
1136 [ParserErrorCode.EXTERNAL_GETTER_WITH_BODY]);
1137 }
1138
1139 void test_externalMethodWithBody() {
1140 parse3("parseClassMember", <Object>["C"], "external m() {}",
1141 [ParserErrorCode.EXTERNAL_METHOD_WITH_BODY]);
1142 }
1143
1144 void test_externalOperatorWithBody() {
1145 parse3(
1146 "parseClassMember",
1147 <Object>["C"],
1148 "external operator +(int value) {}",
1149 [ParserErrorCode.EXTERNAL_OPERATOR_WITH_BODY]);
1150 }
1151
1152 void test_externalSetterWithBody() {
1153 parse3("parseClassMember", <Object>["C"], "external set x(int value) {}",
1154 [ParserErrorCode.EXTERNAL_SETTER_WITH_BODY]);
1155 }
1156
1157 void test_externalTypedef() {
1158 ParserTestCase.parseCompilationUnit(
1159 "external typedef F();", [ParserErrorCode.EXTERNAL_TYPEDEF]);
1160 }
1161
1162 void test_factoryTopLevelDeclaration_class() {
1163 ParserTestCase.parseCompilationUnit(
1164 "factory class C {}", [ParserErrorCode.FACTORY_TOP_LEVEL_DECLARATION]);
1165 }
1166
1167 void test_factoryTopLevelDeclaration_typedef() {
1168 ParserTestCase.parseCompilationUnit("factory typedef F();",
1169 [ParserErrorCode.FACTORY_TOP_LEVEL_DECLARATION]);
1170 }
1171
1172 void test_factoryWithInitializers() {
1173 parse3("parseClassMember", <Object>["C"], "factory C() : x = 3 {}",
1174 [ParserErrorCode.FACTORY_WITH_INITIALIZERS]);
1175 }
1176
1177 void test_factoryWithoutBody() {
1178 parse3("parseClassMember", <Object>["C"], "factory C();",
1179 [ParserErrorCode.FACTORY_WITHOUT_BODY]);
1180 }
1181
1182 void test_fieldInitializerOutsideConstructor() {
1183 parse3("parseClassMember", <Object>["C"], "void m(this.x);",
1184 [ParserErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR]);
1185 }
1186
1187 void test_finalAndVar() {
1188 parse3("parseClassMember", <Object>["C"], "final var x;",
1189 [ParserErrorCode.FINAL_AND_VAR]);
1190 }
1191
1192 void test_finalClass() {
1193 ParserTestCase.parseCompilationUnit(
1194 "final class C {}", [ParserErrorCode.FINAL_CLASS]);
1195 }
1196
1197 void test_finalConstructor() {
1198 parse3("parseClassMember", <Object>["C"], "final C() {}",
1199 [ParserErrorCode.FINAL_CONSTRUCTOR]);
1200 }
1201
1202 void test_finalEnum() {
1203 ParserTestCase.parseCompilationUnit(
1204 "final enum E {ONE}", [ParserErrorCode.FINAL_ENUM]);
1205 }
1206
1207 void test_finalMethod() {
1208 parse3("parseClassMember", <Object>["C"], "final int m() {}",
1209 [ParserErrorCode.FINAL_METHOD]);
1210 }
1211
1212 void test_finalTypedef() {
1213 ParserTestCase.parseCompilationUnit(
1214 "final typedef F();", [ParserErrorCode.FINAL_TYPEDEF]);
1215 }
1216
1217 void test_functionTypedParameter_const() {
1218 ParserTestCase.parseCompilationUnit(
1219 "void f(const x()) {}", [ParserErrorCode.FUNCTION_TYPED_PARAMETER_VAR]);
1220 }
1221
1222 void test_functionTypedParameter_final() {
1223 ParserTestCase.parseCompilationUnit(
1224 "void f(final x()) {}", [ParserErrorCode.FUNCTION_TYPED_PARAMETER_VAR]);
1225 }
1226
1227 void test_functionTypedParameter_var() {
1228 ParserTestCase.parseCompilationUnit(
1229 "void f(var x()) {}", [ParserErrorCode.FUNCTION_TYPED_PARAMETER_VAR]);
1230 }
1231
1232 void test_getterInFunction_block_noReturnType() {
1233 FunctionDeclarationStatement statement = ParserTestCase.parseStatement(
1234 "get x { return _x; }", [ParserErrorCode.GETTER_IN_FUNCTION]);
1235 expect(statement.functionDeclaration.functionExpression.parameters, isNull);
1236 }
1237
1238 void test_getterInFunction_block_returnType() {
1239 ParserTestCase.parseStatement(
1240 "int get x { return _x; }", [ParserErrorCode.GETTER_IN_FUNCTION]);
1241 }
1242
1243 void test_getterInFunction_expression_noReturnType() {
1244 ParserTestCase.parseStatement(
1245 "get x => _x;", [ParserErrorCode.GETTER_IN_FUNCTION]);
1246 }
1247
1248 void test_getterInFunction_expression_returnType() {
1249 ParserTestCase.parseStatement(
1250 "int get x => _x;", [ParserErrorCode.GETTER_IN_FUNCTION]);
1251 }
1252
1253 void test_getterWithParameters() {
1254 parse3("parseClassMember", <Object>["C"], "int get x() {}",
1255 [ParserErrorCode.GETTER_WITH_PARAMETERS]);
1256 }
1257
1258 void test_illegalAssignmentToNonAssignable_postfix_minusMinus_literal() {
1259 parseExpression(
1260 "0--", [ParserErrorCode.ILLEGAL_ASSIGNMENT_TO_NON_ASSIGNABLE]);
1261 }
1262
1263 void test_illegalAssignmentToNonAssignable_postfix_plusPlus_literal() {
1264 parseExpression(
1265 "0++", [ParserErrorCode.ILLEGAL_ASSIGNMENT_TO_NON_ASSIGNABLE]);
1266 }
1267
1268 void test_illegalAssignmentToNonAssignable_postfix_plusPlus_parethesized() {
1269 parseExpression(
1270 "(x)++", [ParserErrorCode.ILLEGAL_ASSIGNMENT_TO_NON_ASSIGNABLE]);
1271 }
1272
1273 void test_illegalAssignmentToNonAssignable_primarySelectorPostfix() {
1274 parseExpression(
1275 "x(y)(z)++", [ParserErrorCode.ILLEGAL_ASSIGNMENT_TO_NON_ASSIGNABLE]);
1276 }
1277
1278 void test_illegalAssignmentToNonAssignable_superAssigned() {
1279 // TODO(brianwilkerson) When the test
1280 // fail_illegalAssignmentToNonAssignable_superAssigned starts to pass,
1281 // remove this test (there should only be one error generated, but we're
1282 // keeping this test until that time so that we can catch other forms of
1283 // regressions).
1284 parseExpression("super = x;", [
1285 ParserErrorCode.MISSING_ASSIGNABLE_SELECTOR,
1286 ParserErrorCode.ILLEGAL_ASSIGNMENT_TO_NON_ASSIGNABLE
1287 ]);
1288 }
1289
1290 void test_implementsBeforeExtends() {
1291 ParserTestCase.parseCompilationUnit("class A implements B extends C {}",
1292 [ParserErrorCode.IMPLEMENTS_BEFORE_EXTENDS]);
1293 }
1294
1295 void test_implementsBeforeWith() {
1296 ParserTestCase.parseCompilationUnit(
1297 "class A extends B implements C with D {}",
1298 [ParserErrorCode.IMPLEMENTS_BEFORE_WITH]);
1299 }
1300
1301 void test_importDirectiveAfterPartDirective() {
1302 ParserTestCase.parseCompilationUnit("part 'a.dart'; import 'b.dart';",
1303 [ParserErrorCode.IMPORT_DIRECTIVE_AFTER_PART_DIRECTIVE]);
1304 }
1305
1306 void test_initializedVariableInForEach() {
1307 parse4("parseForStatement", "for (int a = 0 in foo) {}",
1308 [ParserErrorCode.INITIALIZED_VARIABLE_IN_FOR_EACH]);
1309 }
1310
1311 void test_invalidAwaitInFor() {
1312 parse4("parseForStatement", "await for (; ;) {}",
1313 [ParserErrorCode.INVALID_AWAIT_IN_FOR]);
1314 }
1315
1316 void test_invalidCodePoint() {
1317 parse4("parseStringLiteral", "'\\uD900'",
1318 [ParserErrorCode.INVALID_CODE_POINT]);
1319 }
1320
1321 void test_invalidHexEscape_invalidDigit() {
1322 parse4(
1323 "parseStringLiteral", "'\\x0 a'", [ParserErrorCode.INVALID_HEX_ESCAPE]);
1324 }
1325
1326 void test_invalidHexEscape_tooFewDigits() {
1327 parse4(
1328 "parseStringLiteral", "'\\x0'", [ParserErrorCode.INVALID_HEX_ESCAPE]);
1329 }
1330
1331 void test_invalidInterpolationIdentifier_startWithDigit() {
1332 parse4("parseStringLiteral", "'\$1'", [ParserErrorCode.MISSING_IDENTIFIER]);
1333 }
1334
1335 void test_invalidOperator() {
1336 parse3("parseClassMember", <Object>["C"], "void operator ===(x) {}",
1337 [ParserErrorCode.INVALID_OPERATOR]);
1338 }
1339
1340 void test_invalidOperatorAfterSuper_assignableExpression() {
1341 parse3('parseAssignableExpression', <Object>[false], 'super?.v',
1342 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]);
1343 }
1344
1345 void test_invalidOperatorAfterSuper_primaryExpression() {
1346 parse4('parsePrimaryExpression', 'super?.v',
1347 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]);
1348 }
1349
1350 void test_invalidOperatorForSuper() {
1351 parse4("parseUnaryExpression", "++super",
1352 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]);
1353 }
1354
1355 void test_invalidStarAfterAsync() {
1356 parse3("parseFunctionBody", <Object>[false, null, false], "async* => 0;",
1357 [ParserErrorCode.INVALID_STAR_AFTER_ASYNC]);
1358 }
1359
1360 void test_invalidSync() {
1361 parse3("parseFunctionBody", <Object>[false, null, false], "sync* => 0;",
1362 [ParserErrorCode.INVALID_SYNC]);
1363 }
1364
1365 void test_invalidUnicodeEscape_incomplete_noDigits() {
1366 parse4("parseStringLiteral", "'\\u{'",
1367 [ParserErrorCode.INVALID_UNICODE_ESCAPE]);
1368 }
1369
1370 void test_invalidUnicodeEscape_incomplete_someDigits() {
1371 parse4("parseStringLiteral", "'\\u{0A'",
1372 [ParserErrorCode.INVALID_UNICODE_ESCAPE]);
1373 }
1374
1375 void test_invalidUnicodeEscape_invalidDigit() {
1376 parse4("parseStringLiteral", "'\\u0 a'",
1377 [ParserErrorCode.INVALID_UNICODE_ESCAPE]);
1378 }
1379
1380 void test_invalidUnicodeEscape_tooFewDigits_fixed() {
1381 parse4("parseStringLiteral", "'\\u04'",
1382 [ParserErrorCode.INVALID_UNICODE_ESCAPE]);
1383 }
1384
1385 void test_invalidUnicodeEscape_tooFewDigits_variable() {
1386 parse4("parseStringLiteral", "'\\u{}'",
1387 [ParserErrorCode.INVALID_UNICODE_ESCAPE]);
1388 }
1389
1390 void test_invalidUnicodeEscape_tooManyDigits_variable() {
1391 parse4("parseStringLiteral", "'\\u{12345678}'", [
1392 ParserErrorCode.INVALID_UNICODE_ESCAPE,
1393 ParserErrorCode.INVALID_CODE_POINT
1394 ]);
1395 }
1396
1397 void test_libraryDirectiveNotFirst() {
1398 ParserTestCase.parseCompilationUnit("import 'x.dart'; library l;",
1399 [ParserErrorCode.LIBRARY_DIRECTIVE_NOT_FIRST]);
1400 }
1401
1402 void test_libraryDirectiveNotFirst_afterPart() {
1403 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1404 "part 'a.dart';\nlibrary l;",
1405 [ParserErrorCode.LIBRARY_DIRECTIVE_NOT_FIRST]);
1406 expect(unit, isNotNull);
1407 }
1408
1409 void test_localFunctionDeclarationModifier_abstract() {
1410 ParserTestCase.parseStatement("abstract f() {}",
1411 [ParserErrorCode.LOCAL_FUNCTION_DECLARATION_MODIFIER]);
1412 }
1413
1414 void test_localFunctionDeclarationModifier_external() {
1415 ParserTestCase.parseStatement("external f() {}",
1416 [ParserErrorCode.LOCAL_FUNCTION_DECLARATION_MODIFIER]);
1417 }
1418
1419 void test_localFunctionDeclarationModifier_factory() {
1420 ParserTestCase.parseStatement("factory f() {}",
1421 [ParserErrorCode.LOCAL_FUNCTION_DECLARATION_MODIFIER]);
1422 }
1423
1424 void test_localFunctionDeclarationModifier_static() {
1425 ParserTestCase.parseStatement(
1426 "static f() {}", [ParserErrorCode.LOCAL_FUNCTION_DECLARATION_MODIFIER]);
1427 }
1428
1429 void test_missingAssignableSelector_identifiersAssigned() {
1430 parseExpression("x.y = y;");
1431 }
1432
1433 void test_missingAssignableSelector_prefix_minusMinus_literal() {
1434 parseExpression("--0", [ParserErrorCode.MISSING_ASSIGNABLE_SELECTOR]);
1435 }
1436
1437 void test_missingAssignableSelector_prefix_plusPlus_literal() {
1438 parseExpression("++0", [ParserErrorCode.MISSING_ASSIGNABLE_SELECTOR]);
1439 }
1440
1441 void test_missingAssignableSelector_selector() {
1442 parseExpression("x(y)(z).a++");
1443 }
1444
1445 void test_missingAssignableSelector_superPrimaryExpression() {
1446 SuperExpression expression = parse4("parsePrimaryExpression", "super",
1447 [ParserErrorCode.MISSING_ASSIGNABLE_SELECTOR]);
1448 expect(expression.superKeyword, isNotNull);
1449 }
1450
1451 void test_missingAssignableSelector_superPropertyAccessAssigned() {
1452 parseExpression("super.x = x;");
1453 }
1454
1455 void test_missingCatchOrFinally() {
1456 TryStatement statement = parse4("parseTryStatement", "try {}",
1457 [ParserErrorCode.MISSING_CATCH_OR_FINALLY]);
1458 expect(statement, isNotNull);
1459 }
1460
1461 void test_missingClassBody() {
1462 ParserTestCase.parseCompilationUnit(
1463 "class A class B {}", [ParserErrorCode.MISSING_CLASS_BODY]);
1464 }
1465
1466 void test_missingConstFinalVarOrType_static() {
1467 ParserTestCase.parseCompilationUnit("class A { static f; }",
1468 [ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE]);
1469 }
1470
1471 void test_missingConstFinalVarOrType_topLevel() {
1472 parse3("parseFinalConstVarOrType", <Object>[false], "a;",
1473 [ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE]);
1474 }
1475
1476 void test_missingEnumBody() {
1477 parse3("parseEnumDeclaration", <Object>[emptyCommentAndMetadata()],
1478 "enum E;", [ParserErrorCode.MISSING_ENUM_BODY]);
1479 }
1480
1481 void test_missingExpressionInThrow_withCascade() {
1482 parse4("parseThrowExpression", "throw;",
1483 [ParserErrorCode.MISSING_EXPRESSION_IN_THROW]);
1484 }
1485
1486 void test_missingExpressionInThrow_withoutCascade() {
1487 parse4("parseThrowExpressionWithoutCascade", "throw;",
1488 [ParserErrorCode.MISSING_EXPRESSION_IN_THROW]);
1489 }
1490
1491 void test_missingFunctionBody_emptyNotAllowed() {
1492 parse3(
1493 "parseFunctionBody",
1494 <Object>[false, ParserErrorCode.MISSING_FUNCTION_BODY, false],
1495 ";",
1496 [ParserErrorCode.MISSING_FUNCTION_BODY]);
1497 }
1498
1499 void test_missingFunctionBody_invalid() {
1500 parse3(
1501 "parseFunctionBody",
1502 <Object>[false, ParserErrorCode.MISSING_FUNCTION_BODY, false],
1503 "return 0;",
1504 [ParserErrorCode.MISSING_FUNCTION_BODY]);
1505 }
1506
1507 void test_missingFunctionParameters_local_void_block() {
1508 ParserTestCase.parseStatement(
1509 "void f { return x;}", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
1510 }
1511
1512 void test_missingFunctionParameters_local_void_expression() {
1513 ParserTestCase.parseStatement(
1514 "void f => x;", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
1515 }
1516
1517 void test_missingFunctionParameters_topLevel_nonVoid_block() {
1518 ParserTestCase.parseCompilationUnit(
1519 "int f { return x;}", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
1520 }
1521
1522 void test_missingFunctionParameters_topLevel_nonVoid_expression() {
1523 ParserTestCase.parseCompilationUnit(
1524 "int f => x;", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
1525 }
1526
1527 void test_missingFunctionParameters_topLevel_void_block() {
1528 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1529 "void f { return x;}", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
1530 FunctionDeclaration funct = unit.declarations[0];
1531 expect(funct.functionExpression.parameters, hasLength(0));
1532 }
1533
1534 void test_missingFunctionParameters_topLevel_void_expression() {
1535 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1536 "void f => x;", [ParserErrorCode.MISSING_FUNCTION_PARAMETERS]);
1537 FunctionDeclaration funct = unit.declarations[0];
1538 expect(funct.functionExpression.parameters, hasLength(0));
1539 }
1540
1541 void test_missingIdentifier_afterOperator() {
1542 parse4("parseMultiplicativeExpression", "1 *",
1543 [ParserErrorCode.MISSING_IDENTIFIER]);
1544 }
1545
1546 void test_missingIdentifier_beforeClosingCurly() {
1547 parse3("parseClassMember", <Object>["C"], "int}",
1548 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.EXPECTED_TOKEN]);
1549 }
1550
1551 void test_missingIdentifier_functionDeclaration_returnTypeWithoutName() {
1552 parse4("parseFunctionDeclarationStatement", "A<T> () {}",
1553 [ParserErrorCode.MISSING_IDENTIFIER]);
1554 }
1555
1556 void test_missingIdentifier_inEnum() {
1557 parse3("parseEnumDeclaration", <Object>[emptyCommentAndMetadata()],
1558 "enum E {, TWO}", [ParserErrorCode.MISSING_IDENTIFIER]);
1559 }
1560
1561 void test_missingIdentifier_inSymbol_afterPeriod() {
1562 parse4("parseSymbolLiteral", "#a.", [ParserErrorCode.MISSING_IDENTIFIER]);
1563 }
1564
1565 void test_missingIdentifier_inSymbol_first() {
1566 parse4("parseSymbolLiteral", "#", [ParserErrorCode.MISSING_IDENTIFIER]);
1567 }
1568
1569 void test_missingIdentifier_number() {
1570 SimpleIdentifier expression = parse4(
1571 "parseSimpleIdentifier", "1", [ParserErrorCode.MISSING_IDENTIFIER]);
1572 expect(expression.isSynthetic, isTrue);
1573 }
1574
1575 void test_missingKeywordOperator() {
1576 parse3("parseOperator", <Object>[emptyCommentAndMetadata(), null, null],
1577 "+(x) {}", [ParserErrorCode.MISSING_KEYWORD_OPERATOR]);
1578 }
1579
1580 void test_missingKeywordOperator_parseClassMember() {
1581 parse3("parseClassMember", <Object>["C"], "+() {}",
1582 [ParserErrorCode.MISSING_KEYWORD_OPERATOR]);
1583 }
1584
1585 void test_missingKeywordOperator_parseClassMember_afterTypeName() {
1586 parse3("parseClassMember", <Object>["C"], "int +() {}",
1587 [ParserErrorCode.MISSING_KEYWORD_OPERATOR]);
1588 }
1589
1590 void test_missingKeywordOperator_parseClassMember_afterVoid() {
1591 parse3("parseClassMember", <Object>["C"], "void +() {}",
1592 [ParserErrorCode.MISSING_KEYWORD_OPERATOR]);
1593 }
1594
1595 void test_missingMethodParameters_void_block() {
1596 MethodDeclaration method = parse3("parseClassMember", <Object>["C"],
1597 "void m {} }", [ParserErrorCode.MISSING_METHOD_PARAMETERS]);
1598 expect(method.parameters, hasLength(0));
1599 }
1600
1601 void test_missingMethodParameters_void_expression() {
1602 parse3("parseClassMember", <Object>["C"], "void m => null; }",
1603 [ParserErrorCode.MISSING_METHOD_PARAMETERS]);
1604 }
1605
1606 void test_missingNameInLibraryDirective() {
1607 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1608 "library;", [ParserErrorCode.MISSING_NAME_IN_LIBRARY_DIRECTIVE]);
1609 expect(unit, isNotNull);
1610 }
1611
1612 void test_missingNameInPartOfDirective() {
1613 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1614 "part of;", [ParserErrorCode.MISSING_NAME_IN_PART_OF_DIRECTIVE]);
1615 expect(unit, isNotNull);
1616 }
1617
1618 void test_missingPrefixInDeferredImport() {
1619 ParserTestCase.parseCompilationUnit("import 'foo.dart' deferred;",
1620 [ParserErrorCode.MISSING_PREFIX_IN_DEFERRED_IMPORT]);
1621 }
1622
1623 void test_missingStartAfterSync() {
1624 parse3("parseFunctionBody", <Object>[false, null, false], "sync {}",
1625 [ParserErrorCode.MISSING_STAR_AFTER_SYNC]);
1626 }
1627
1628 void test_missingStatement() {
1629 ParserTestCase.parseStatement("is", [ParserErrorCode.MISSING_STATEMENT]);
1630 }
1631
1632 void test_missingStatement_afterVoid() {
1633 ParserTestCase.parseStatement("void;", [ParserErrorCode.MISSING_STATEMENT]);
1634 }
1635
1636 void test_missingTerminatorForParameterGroup_named() {
1637 parse4("parseFormalParameterList", "(a, {b: 0)",
1638 [ParserErrorCode.MISSING_TERMINATOR_FOR_PARAMETER_GROUP]);
1639 }
1640
1641 void test_missingTerminatorForParameterGroup_optional() {
1642 parse4("parseFormalParameterList", "(a, [b = 0)",
1643 [ParserErrorCode.MISSING_TERMINATOR_FOR_PARAMETER_GROUP]);
1644 }
1645
1646 void test_missingTypedefParameters_nonVoid() {
1647 ParserTestCase.parseCompilationUnit(
1648 "typedef int F;", [ParserErrorCode.MISSING_TYPEDEF_PARAMETERS]);
1649 }
1650
1651 void test_missingTypedefParameters_typeParameters() {
1652 ParserTestCase.parseCompilationUnit(
1653 "typedef F<E>;", [ParserErrorCode.MISSING_TYPEDEF_PARAMETERS]);
1654 }
1655
1656 void test_missingTypedefParameters_void() {
1657 ParserTestCase.parseCompilationUnit(
1658 "typedef void F;", [ParserErrorCode.MISSING_TYPEDEF_PARAMETERS]);
1659 }
1660
1661 void test_missingVariableInForEach() {
1662 parse4("parseForStatement", "for (a < b in foo) {}",
1663 [ParserErrorCode.MISSING_VARIABLE_IN_FOR_EACH]);
1664 }
1665
1666 void test_mixedParameterGroups_namedPositional() {
1667 parse4("parseFormalParameterList", "(a, {b}, [c])",
1668 [ParserErrorCode.MIXED_PARAMETER_GROUPS]);
1669 }
1670
1671 void test_mixedParameterGroups_positionalNamed() {
1672 parse4("parseFormalParameterList", "(a, [b], {c})",
1673 [ParserErrorCode.MIXED_PARAMETER_GROUPS]);
1674 }
1675
1676 void test_mixin_application_lacks_with_clause() {
1677 ParserTestCase.parseCompilationUnit(
1678 "class Foo = Bar;", [ParserErrorCode.EXPECTED_TOKEN]);
1679 }
1680
1681 void test_multipleExtendsClauses() {
1682 ParserTestCase.parseCompilationUnit("class A extends B extends C {}",
1683 [ParserErrorCode.MULTIPLE_EXTENDS_CLAUSES]);
1684 }
1685
1686 void test_multipleImplementsClauses() {
1687 ParserTestCase.parseCompilationUnit("class A implements B implements C {}",
1688 [ParserErrorCode.MULTIPLE_IMPLEMENTS_CLAUSES]);
1689 }
1690
1691 void test_multipleLibraryDirectives() {
1692 ParserTestCase.parseCompilationUnit(
1693 "library l; library m;", [ParserErrorCode.MULTIPLE_LIBRARY_DIRECTIVES]);
1694 }
1695
1696 void test_multipleNamedParameterGroups() {
1697 parse4("parseFormalParameterList", "(a, {b}, {c})",
1698 [ParserErrorCode.MULTIPLE_NAMED_PARAMETER_GROUPS]);
1699 }
1700
1701 void test_multiplePartOfDirectives() {
1702 ParserTestCase.parseCompilationUnit(
1703 "part of l; part of m;", [ParserErrorCode.MULTIPLE_PART_OF_DIRECTIVES]);
1704 }
1705
1706 void test_multiplePositionalParameterGroups() {
1707 parse4("parseFormalParameterList", "(a, [b], [c])",
1708 [ParserErrorCode.MULTIPLE_POSITIONAL_PARAMETER_GROUPS]);
1709 }
1710
1711 void test_multipleVariablesInForEach() {
1712 parse4("parseForStatement", "for (int a, b in foo) {}",
1713 [ParserErrorCode.MULTIPLE_VARIABLES_IN_FOR_EACH]);
1714 }
1715
1716 void test_multipleWithClauses() {
1717 ParserTestCase.parseCompilationUnit("class A extends B with C with D {}",
1718 [ParserErrorCode.MULTIPLE_WITH_CLAUSES]);
1719 }
1720
1721 void test_namedParameterOutsideGroup() {
1722 parse4("parseFormalParameterList", "(a, b : 0)",
1723 [ParserErrorCode.NAMED_PARAMETER_OUTSIDE_GROUP]);
1724 }
1725
1726 void test_nonConstructorFactory_field() {
1727 parse3("parseClassMember", <Object>["C"], "factory int x;",
1728 [ParserErrorCode.NON_CONSTRUCTOR_FACTORY]);
1729 }
1730
1731 void test_nonConstructorFactory_method() {
1732 parse3("parseClassMember", <Object>["C"], "factory int m() {}",
1733 [ParserErrorCode.NON_CONSTRUCTOR_FACTORY]);
1734 }
1735
1736 void test_nonIdentifierLibraryName_library() {
1737 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1738 "library 'lib';", [ParserErrorCode.NON_IDENTIFIER_LIBRARY_NAME]);
1739 expect(unit, isNotNull);
1740 }
1741
1742 void test_nonIdentifierLibraryName_partOf() {
1743 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
1744 "part of 'lib';", [ParserErrorCode.NON_IDENTIFIER_LIBRARY_NAME]);
1745 expect(unit, isNotNull);
1746 }
1747
1748 void test_nonPartOfDirectiveInPart_after() {
1749 ParserTestCase.parseCompilationUnit("part of l; part 'f.dart';",
1750 [ParserErrorCode.NON_PART_OF_DIRECTIVE_IN_PART]);
1751 }
1752
1753 void test_nonPartOfDirectiveInPart_before() {
1754 ParserTestCase.parseCompilationUnit("part 'f.dart'; part of m;",
1755 [ParserErrorCode.NON_PART_OF_DIRECTIVE_IN_PART]);
1756 }
1757
1758 void test_nonUserDefinableOperator() {
1759 parse3("parseClassMember", <Object>["C"], "operator +=(int x) => x + 1;",
1760 [ParserErrorCode.NON_USER_DEFINABLE_OPERATOR]);
1761 }
1762
1763 void test_optionalAfterNormalParameters_named() {
1764 ParserTestCase.parseCompilationUnit(
1765 "f({a}, b) {}", [ParserErrorCode.NORMAL_BEFORE_OPTIONAL_PARAMETERS]);
1766 }
1767
1768 void test_optionalAfterNormalParameters_positional() {
1769 ParserTestCase.parseCompilationUnit(
1770 "f([a], b) {}", [ParserErrorCode.NORMAL_BEFORE_OPTIONAL_PARAMETERS]);
1771 }
1772
1773 void test_parseCascadeSection_missingIdentifier() {
1774 MethodInvocation methodInvocation = parse4(
1775 "parseCascadeSection", "..()", [ParserErrorCode.MISSING_IDENTIFIER]);
1776 expect(methodInvocation.target, isNull);
1777 expect(methodInvocation.methodName.name, "");
1778 expect(methodInvocation.typeArguments, isNull);
1779 expect(methodInvocation.argumentList.arguments, hasLength(0));
1780 }
1781
1782 void test_parseCascadeSection_missingIdentifier_typeArguments() {
1783 enableGenericMethods = true;
1784 MethodInvocation methodInvocation = parse4(
1785 "parseCascadeSection", "..<E>()", [ParserErrorCode.MISSING_IDENTIFIER]);
1786 expect(methodInvocation.target, isNull);
1787 expect(methodInvocation.methodName.name, "");
1788 expect(methodInvocation.typeArguments, isNotNull);
1789 expect(methodInvocation.argumentList.arguments, hasLength(0));
1790 }
1791
1792 void test_positionalAfterNamedArgument() {
1793 parse4("parseArgumentList", "(x: 1, 2)",
1794 [ParserErrorCode.POSITIONAL_AFTER_NAMED_ARGUMENT]);
1795 }
1796
1797 void test_positionalParameterOutsideGroup() {
1798 parse4("parseFormalParameterList", "(a, b = 0)",
1799 [ParserErrorCode.POSITIONAL_PARAMETER_OUTSIDE_GROUP]);
1800 }
1801
1802 void test_redirectionInNonFactoryConstructor() {
1803 parse3("parseClassMember", <Object>["C"], "C() = D;",
1804 [ParserErrorCode.REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR]);
1805 }
1806
1807 void test_setterInFunction_block() {
1808 ParserTestCase.parseStatement(
1809 "set x(v) {_x = v;}", [ParserErrorCode.SETTER_IN_FUNCTION]);
1810 }
1811
1812 void test_setterInFunction_expression() {
1813 ParserTestCase.parseStatement(
1814 "set x(v) => _x = v;", [ParserErrorCode.SETTER_IN_FUNCTION]);
1815 }
1816
1817 void test_staticAfterConst() {
1818 parse3("parseClassMember", <Object>["C"], "final static int f;",
1819 [ParserErrorCode.STATIC_AFTER_FINAL]);
1820 }
1821
1822 void test_staticAfterFinal() {
1823 parse3("parseClassMember", <Object>["C"], "const static int f;",
1824 [ParserErrorCode.STATIC_AFTER_CONST]);
1825 }
1826
1827 void test_staticAfterVar() {
1828 parse3("parseClassMember", <Object>["C"], "var static f;",
1829 [ParserErrorCode.STATIC_AFTER_VAR]);
1830 }
1831
1832 void test_staticConstructor() {
1833 parse3("parseClassMember", <Object>["C"], "static C.m() {}",
1834 [ParserErrorCode.STATIC_CONSTRUCTOR]);
1835 }
1836
1837 void test_staticGetterWithoutBody() {
1838 parse3("parseClassMember", <Object>["C"], "static get m;",
1839 [ParserErrorCode.STATIC_GETTER_WITHOUT_BODY]);
1840 }
1841
1842 void test_staticOperator_noReturnType() {
1843 parse3(
1844 "parseClassMember",
1845 <Object>["C"],
1846 "static operator +(int x) => x + 1;",
1847 [ParserErrorCode.STATIC_OPERATOR]);
1848 }
1849
1850 void test_staticOperator_returnType() {
1851 parse3(
1852 "parseClassMember",
1853 <Object>["C"],
1854 "static int operator +(int x) => x + 1;",
1855 [ParserErrorCode.STATIC_OPERATOR]);
1856 }
1857
1858 void test_staticSetterWithoutBody() {
1859 parse3("parseClassMember", <Object>["C"], "static set m(x);",
1860 [ParserErrorCode.STATIC_SETTER_WITHOUT_BODY]);
1861 }
1862
1863 void test_staticTopLevelDeclaration_class() {
1864 ParserTestCase.parseCompilationUnit(
1865 "static class C {}", [ParserErrorCode.STATIC_TOP_LEVEL_DECLARATION]);
1866 }
1867
1868 void test_staticTopLevelDeclaration_function() {
1869 ParserTestCase.parseCompilationUnit(
1870 "static f() {}", [ParserErrorCode.STATIC_TOP_LEVEL_DECLARATION]);
1871 }
1872
1873 void test_staticTopLevelDeclaration_typedef() {
1874 ParserTestCase.parseCompilationUnit(
1875 "static typedef F();", [ParserErrorCode.STATIC_TOP_LEVEL_DECLARATION]);
1876 }
1877
1878 void test_staticTopLevelDeclaration_variable() {
1879 ParserTestCase.parseCompilationUnit(
1880 "static var x;", [ParserErrorCode.STATIC_TOP_LEVEL_DECLARATION]);
1881 }
1882
1883 void test_switchHasCaseAfterDefaultCase() {
1884 parse4(
1885 "parseSwitchStatement",
1886 "switch (a) {default: return 0; case 1: return 1;}",
1887 [ParserErrorCode.SWITCH_HAS_CASE_AFTER_DEFAULT_CASE]);
1888 }
1889
1890 void test_switchHasCaseAfterDefaultCase_repeated() {
1891 parse4("parseSwitchStatement",
1892 "switch (a) {default: return 0; case 1: return 1; case 2: return 2;}", [
1893 ParserErrorCode.SWITCH_HAS_CASE_AFTER_DEFAULT_CASE,
1894 ParserErrorCode.SWITCH_HAS_CASE_AFTER_DEFAULT_CASE
1895 ]);
1896 }
1897
1898 void test_switchHasMultipleDefaultCases() {
1899 parse4(
1900 "parseSwitchStatement",
1901 "switch (a) {default: return 0; default: return 1;}",
1902 [ParserErrorCode.SWITCH_HAS_MULTIPLE_DEFAULT_CASES]);
1903 }
1904
1905 void test_switchHasMultipleDefaultCases_repeated() {
1906 parse4(
1907 "parseSwitchStatement",
1908 "switch (a) {default: return 0; default: return 1; default: return 2;}",
1909 [
1910 ParserErrorCode.SWITCH_HAS_MULTIPLE_DEFAULT_CASES,
1911 ParserErrorCode.SWITCH_HAS_MULTIPLE_DEFAULT_CASES
1912 ]);
1913 }
1914
1915 void test_topLevel_getter() {
1916 FunctionDeclaration funct = parse3("parseCompilationUnitMember",
1917 <Object>[emptyCommentAndMetadata()], "get x => 7;");
1918 expect(funct.functionExpression.parameters, isNull);
1919 }
1920
1921 void test_topLevelOperator_withoutType() {
1922 parse3(
1923 "parseCompilationUnitMember",
1924 <Object>[emptyCommentAndMetadata()],
1925 "operator +(bool x, bool y) => x | y;",
1926 [ParserErrorCode.TOP_LEVEL_OPERATOR]);
1927 }
1928
1929 void test_topLevelOperator_withType() {
1930 parse3(
1931 "parseCompilationUnitMember",
1932 <Object>[emptyCommentAndMetadata()],
1933 "bool operator +(bool x, bool y) => x | y;",
1934 [ParserErrorCode.TOP_LEVEL_OPERATOR]);
1935 }
1936
1937 void test_topLevelOperator_withVoid() {
1938 parse3(
1939 "parseCompilationUnitMember",
1940 <Object>[emptyCommentAndMetadata()],
1941 "void operator +(bool x, bool y) => x | y;",
1942 [ParserErrorCode.TOP_LEVEL_OPERATOR]);
1943 }
1944
1945 void test_topLevelVariable_withMetadata() {
1946 ParserTestCase.parseCompilationUnit("String @A string;", [
1947 ParserErrorCode.MISSING_IDENTIFIER,
1948 ParserErrorCode.EXPECTED_TOKEN,
1949 ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE
1950 ]);
1951 }
1952
1953 void test_typedefInClass_withoutReturnType() {
1954 ParserTestCase.parseCompilationUnit(
1955 "class C { typedef F(x); }", [ParserErrorCode.TYPEDEF_IN_CLASS]);
1956 }
1957
1958 void test_typedefInClass_withReturnType() {
1959 ParserTestCase.parseCompilationUnit("class C { typedef int F(int x); }",
1960 [ParserErrorCode.TYPEDEF_IN_CLASS]);
1961 }
1962
1963 void test_unexpectedTerminatorForParameterGroup_named() {
1964 parse4("parseFormalParameterList", "(a, b})",
1965 [ParserErrorCode.UNEXPECTED_TERMINATOR_FOR_PARAMETER_GROUP]);
1966 }
1967
1968 void test_unexpectedTerminatorForParameterGroup_optional() {
1969 parse4("parseFormalParameterList", "(a, b])",
1970 [ParserErrorCode.UNEXPECTED_TERMINATOR_FOR_PARAMETER_GROUP]);
1971 }
1972
1973 void test_unexpectedToken_endOfFieldDeclarationStatement() {
1974 ParserTestCase.parseStatement(
1975 "String s = (null));", [ParserErrorCode.UNEXPECTED_TOKEN]);
1976 }
1977
1978 void test_unexpectedToken_returnInExpressionFuntionBody() {
1979 ParserTestCase.parseCompilationUnit(
1980 "f() => return null;", [ParserErrorCode.UNEXPECTED_TOKEN]);
1981 }
1982
1983 void test_unexpectedToken_semicolonBetweenClassMembers() {
1984 parse3("parseClassDeclaration", <Object>[emptyCommentAndMetadata(), null],
1985 "class C { int x; ; int y;}", [ParserErrorCode.UNEXPECTED_TOKEN]);
1986 }
1987
1988 void test_unexpectedToken_semicolonBetweenCompilationUnitMembers() {
1989 ParserTestCase.parseCompilationUnit(
1990 "int x; ; int y;", [ParserErrorCode.UNEXPECTED_TOKEN]);
1991 }
1992
1993 void test_unterminatedString_at_eof() {
1994 // Although the "unterminated string" error message is produced by the
1995 // scanner, we need to verify that the parser can handle the tokens
1996 // produced by the scanner when an unterminated string is encountered.
1997 ParserTestCase.parseCompilationUnit(
1998 r'''
1999 void main() {
2000 var x = "''',
2001 [
2002 ScannerErrorCode.UNTERMINATED_STRING_LITERAL,
2003 ParserErrorCode.EXPECTED_TOKEN,
2004 ParserErrorCode.EXPECTED_TOKEN
2005 ]);
2006 }
2007
2008 void test_unterminatedString_at_eol() {
2009 // Although the "unterminated string" error message is produced by the
2010 // scanner, we need to verify that the parser can handle the tokens
2011 // produced by the scanner when an unterminated string is encountered.
2012 ParserTestCase.parseCompilationUnit(
2013 r'''
2014 void main() {
2015 var x = "
2016 ;
2017 }
2018 ''',
2019 [ScannerErrorCode.UNTERMINATED_STRING_LITERAL]);
2020 }
2021
2022 void test_unterminatedString_multiline_at_eof_3_quotes() {
2023 // Although the "unterminated string" error message is produced by the
2024 // scanner, we need to verify that the parser can handle the tokens
2025 // produced by the scanner when an unterminated string is encountered.
2026 ParserTestCase.parseCompilationUnit(
2027 r'''
2028 void main() {
2029 var x = """''',
2030 [
2031 ScannerErrorCode.UNTERMINATED_STRING_LITERAL,
2032 ParserErrorCode.EXPECTED_TOKEN,
2033 ParserErrorCode.EXPECTED_TOKEN
2034 ]);
2035 }
2036
2037 void test_unterminatedString_multiline_at_eof_4_quotes() {
2038 // Although the "unterminated string" error message is produced by the
2039 // scanner, we need to verify that the parser can handle the tokens
2040 // produced by the scanner when an unterminated string is encountered.
2041 ParserTestCase.parseCompilationUnit(
2042 r'''
2043 void main() {
2044 var x = """"''',
2045 [
2046 ScannerErrorCode.UNTERMINATED_STRING_LITERAL,
2047 ParserErrorCode.EXPECTED_TOKEN,
2048 ParserErrorCode.EXPECTED_TOKEN
2049 ]);
2050 }
2051
2052 void test_unterminatedString_multiline_at_eof_5_quotes() {
2053 // Although the "unterminated string" error message is produced by the
2054 // scanner, we need to verify that the parser can handle the tokens
2055 // produced by the scanner when an unterminated string is encountered.
2056 ParserTestCase.parseCompilationUnit(
2057 r'''
2058 void main() {
2059 var x = """""''',
2060 [
2061 ScannerErrorCode.UNTERMINATED_STRING_LITERAL,
2062 ParserErrorCode.EXPECTED_TOKEN,
2063 ParserErrorCode.EXPECTED_TOKEN
2064 ]);
2065 }
2066
2067 void test_useOfUnaryPlusOperator() {
2068 SimpleIdentifier expression = parse4(
2069 "parseUnaryExpression", "+x", [ParserErrorCode.MISSING_IDENTIFIER]);
2070 EngineTestCase.assertInstanceOf(
2071 (obj) => obj is SimpleIdentifier, SimpleIdentifier, expression);
2072 expect(expression.isSynthetic, isTrue);
2073 }
2074
2075 void test_varAndType_field() {
2076 ParserTestCase.parseCompilationUnit(
2077 "class C { var int x; }", [ParserErrorCode.VAR_AND_TYPE]);
2078 }
2079
2080 void test_varAndType_topLevelVariable() {
2081 ParserTestCase.parseCompilationUnit(
2082 "var int x;", [ParserErrorCode.VAR_AND_TYPE]);
2083 }
2084
2085 void test_varAsTypeName_as() {
2086 parseExpression("x as var", [ParserErrorCode.VAR_AS_TYPE_NAME]);
2087 }
2088
2089 void test_varClass() {
2090 ParserTestCase.parseCompilationUnit(
2091 "var class C {}", [ParserErrorCode.VAR_CLASS]);
2092 }
2093
2094 void test_varEnum() {
2095 ParserTestCase.parseCompilationUnit(
2096 "var enum E {ONE}", [ParserErrorCode.VAR_ENUM]);
2097 }
2098
2099 void test_varReturnType() {
2100 parse3("parseClassMember", <Object>["C"], "var m() {}",
2101 [ParserErrorCode.VAR_RETURN_TYPE]);
2102 }
2103
2104 void test_varTypedef() {
2105 ParserTestCase.parseCompilationUnit(
2106 "var typedef F();", [ParserErrorCode.VAR_TYPEDEF]);
2107 }
2108
2109 void test_voidParameter() {
2110 parse4("parseNormalFormalParameter", "void a)",
2111 [ParserErrorCode.VOID_PARAMETER]);
2112 }
2113
2114 void test_voidVariable_parseClassMember_initializer() {
2115 parse3("parseClassMember", <Object>["C"], "void x = 0;",
2116 [ParserErrorCode.VOID_VARIABLE]);
2117 }
2118
2119 void test_voidVariable_parseClassMember_noInitializer() {
2120 parse3("parseClassMember", <Object>["C"], "void x;",
2121 [ParserErrorCode.VOID_VARIABLE]);
2122 }
2123
2124 void test_voidVariable_parseCompilationUnit_initializer() {
2125 ParserTestCase.parseCompilationUnit(
2126 "void x = 0;", [ParserErrorCode.VOID_VARIABLE]);
2127 }
2128
2129 void test_voidVariable_parseCompilationUnit_noInitializer() {
2130 ParserTestCase.parseCompilationUnit(
2131 "void x;", [ParserErrorCode.VOID_VARIABLE]);
2132 }
2133
2134 void test_voidVariable_parseCompilationUnitMember_initializer() {
2135 parse3("parseCompilationUnitMember", <Object>[emptyCommentAndMetadata()],
2136 "void a = 0;", [ParserErrorCode.VOID_VARIABLE]);
2137 }
2138
2139 void test_voidVariable_parseCompilationUnitMember_noInitializer() {
2140 parse3("parseCompilationUnitMember", <Object>[emptyCommentAndMetadata()],
2141 "void a;", [ParserErrorCode.VOID_VARIABLE]);
2142 }
2143
2144 void test_voidVariable_statement_initializer() {
2145 ParserTestCase.parseStatement("void x = 0;", [
2146 ParserErrorCode.VOID_VARIABLE,
2147 ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE
2148 ]);
2149 }
2150
2151 void test_voidVariable_statement_noInitializer() {
2152 ParserTestCase.parseStatement("void x;", [
2153 ParserErrorCode.VOID_VARIABLE,
2154 ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE
2155 ]);
2156 }
2157
2158 void test_withBeforeExtends() {
2159 ParserTestCase.parseCompilationUnit(
2160 "class A with B extends C {}", [ParserErrorCode.WITH_BEFORE_EXTENDS]);
2161 }
2162
2163 void test_withWithoutExtends() {
2164 parse3("parseClassDeclaration", <Object>[emptyCommentAndMetadata(), null],
2165 "class A with B, C {}", [ParserErrorCode.WITH_WITHOUT_EXTENDS]);
2166 }
2167
2168 void test_wrongSeparatorForNamedParameter() {
2169 parse4("parseFormalParameterList", "(a, {b = 0})",
2170 [ParserErrorCode.WRONG_SEPARATOR_FOR_NAMED_PARAMETER]);
2171 }
2172
2173 void test_wrongSeparatorForPositionalParameter() {
2174 parse4("parseFormalParameterList", "(a, [b : 0])",
2175 [ParserErrorCode.WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER]);
2176 }
2177
2178 void test_wrongTerminatorForParameterGroup_named() {
2179 parse4("parseFormalParameterList", "(a, {b, c])",
2180 [ParserErrorCode.WRONG_TERMINATOR_FOR_PARAMETER_GROUP]);
2181 }
2182
2183 void test_wrongTerminatorForParameterGroup_optional() {
2184 parse4("parseFormalParameterList", "(a, [b, c})",
2185 [ParserErrorCode.WRONG_TERMINATOR_FOR_PARAMETER_GROUP]);
2186 }
2187 }
2188
2189 @reflectiveTest
2190 class IncrementalParserTest extends EngineTestCase {
2191 void fail_replace_identifier_with_functionLiteral_in_initializer_interp() {
2192 // TODO(paulberry, brianwilkerson): broken due to incremental scanning bugs
2193
2194 // Function literals are allowed inside interpolation expressions in
2195 // initializers.
2196 //
2197 // 'class A { var a; A(b) : a = "${b}";'
2198 // 'class A { var a; A(b) : a = "${() {}}";'
2199 _assertParse(r'class A { var a; A(b) : a = "${', 'b', '() {}', '}";');
2200 }
2201
2202 void test_delete_everything() {
2203 // "f() => a + b;"
2204 // ""
2205 _assertParse("", "f() => a + b;", "", "");
2206 }
2207
2208 void test_delete_identifier_beginning() {
2209 // "f() => abs + b;"
2210 // "f() => s + b;"
2211 _assertParse("f() => ", "ab", "", "s + b;");
2212 }
2213
2214 void test_delete_identifier_end() {
2215 // "f() => abs + b;"
2216 // "f() => a + b;"
2217 _assertParse("f() => a", "bs", "", " + b;");
2218 }
2219
2220 void test_delete_identifier_middle() {
2221 // "f() => abs + b;"
2222 // "f() => as + b;"
2223 _assertParse("f() => a", "b", "", "s + b;");
2224 }
2225
2226 void test_delete_mergeTokens() {
2227 // "f() => a + b + c;"
2228 // "f() => ac;"
2229 _assertParse("f() => a", " + b + ", "", "c;");
2230 }
2231
2232 void test_insert_afterIdentifier1() {
2233 // "f() => a + b;"
2234 // "f() => abs + b;"
2235 _assertParse("f() => a", "", "bs", " + b;");
2236 }
2237
2238 void test_insert_afterIdentifier2() {
2239 // "f() => a + b;"
2240 // "f() => a + bar;"
2241 _assertParse("f() => a + b", "", "ar", ";");
2242 }
2243
2244 void test_insert_beforeIdentifier1() {
2245 // "f() => a + b;"
2246 // "f() => xa + b;"
2247 _assertParse("f() => ", "", "x", "a + b;");
2248 }
2249
2250 void test_insert_beforeIdentifier2() {
2251 // "f() => a + b;"
2252 // "f() => a + xb;"
2253 _assertParse("f() => a + ", "", "x", "b;");
2254 }
2255
2256 void test_insert_convertOneFunctionToTwo() {
2257 // "f() {}"
2258 // "f() => 0; g() {}"
2259 _assertParse("f()", "", " => 0; g()", " {}");
2260 }
2261
2262 void test_insert_end() {
2263 // "class A {}"
2264 // "class A {} class B {}"
2265 _assertParse("class A {}", "", " class B {}", "");
2266 }
2267
2268 void test_insert_final_before_field_declaration() {
2269 _assertParse('class C { ', '', 'final ', 'int x; }');
2270 }
2271
2272 void test_insert_function_parameter() {
2273 _assertParse('class C { void f(', '', 'arg', ') {} }');
2274 }
2275
2276 void test_insert_identifier_inCombinator() {
2277 _assertParse("import 'foo.dart' show x", "", ", y", ";");
2278 }
2279
2280 void test_insert_insideClassBody() {
2281 // "class C {C(); }"
2282 // "class C { C(); }"
2283 _assertParse("class C {", "", " ", "C(); }");
2284 }
2285
2286 void test_insert_insideIdentifier() {
2287 // "f() => cob;"
2288 // "f() => cow.b;"
2289 _assertParse("f() => co", "", "w.", "b;");
2290 }
2291
2292 void test_insert_newIdentifier1() {
2293 // "f() => a; c;"
2294 // "f() => a; b c;"
2295 _assertParse("f() => a;", "", " b", " c;");
2296 }
2297
2298 void test_insert_newIdentifier2() {
2299 // "f() => a; c;"
2300 // "f() => a;b c;"
2301 _assertParse("f() => a;", "", "b", " c;");
2302 }
2303
2304 void test_insert_newIdentifier3() {
2305 // "/** A simple function. */ f() => a; c;"
2306 // "/** A simple function. */ f() => a; b c;"
2307 _assertParse("/** A simple function. */ f() => a;", "", " b", " c;");
2308 }
2309
2310 void test_insert_newIdentifier4() {
2311 // "/** An [A]. */ class A {} class B { m() { return 1; } }"
2312 // "/** An [A]. */ class A {} class B { m() { return 1 + 2; } }"
2313 _assertParse("/** An [A]. */ class A {} class B { m() { return 1", "",
2314 " + 2", "; } }");
2315 }
2316
2317 void test_insert_period() {
2318 // "f() => a + b;"
2319 // "f() => a + b.;"
2320 _assertParse("f() => a + b", "", ".", ";");
2321 }
2322
2323 void test_insert_period_betweenIdentifiers1() {
2324 // "f() => a b;"
2325 // "f() => a. b;"
2326 _assertParse("f() => a", "", ".", " b;");
2327 }
2328
2329 void test_insert_period_betweenIdentifiers2() {
2330 // "f() => a b;"
2331 // "f() => a .b;"
2332 _assertParse("f() => a ", "", ".", "b;");
2333 }
2334
2335 void test_insert_period_betweenIdentifiers3() {
2336 // "f() => a b;"
2337 // "f() => a . b;"
2338 _assertParse("f() => a ", "", ".", " b;");
2339 }
2340
2341 void test_insert_period_insideExistingIdentifier() {
2342 // "f() => ab;"
2343 // "f() => a.b;"
2344 _assertParse("f() => a", "", ".", "b;");
2345 }
2346
2347 void test_insert_periodAndIdentifier() {
2348 // "f() => a + b;"
2349 // "f() => a + b.x;"
2350 _assertParse("f() => a + b", "", ".x", ";");
2351 }
2352
2353 void test_insert_simpleToComplexExression() {
2354 // "/** An [A]. */ class A {} class B { m() => 1; }"
2355 // "/** An [A]. */ class A {} class B { m() => 1 + 2; }"
2356 _assertParse(
2357 "/** An [A]. */ class A {} class B { m() => 1", "", " + 2", "; }");
2358 }
2359
2360 void test_insert_statement_in_method_with_mismatched_braces() {
2361 _assertParse(
2362 '''
2363 class C {
2364 void f() {
2365 ''',
2366 '',
2367 'g();',
2368 '''
2369 if (b) {
2370
2371
2372 }
2373
2374 void g() {
2375 h((x) {});
2376 }
2377 }
2378 ''');
2379 }
2380
2381 void test_insert_whitespace_end() {
2382 // "f() => a + b;"
2383 // "f() => a + b; "
2384 _assertParse("f() => a + b;", "", " ", "");
2385 }
2386
2387 void test_insert_whitespace_end_multiple() {
2388 // "f() => a + b;"
2389 // "f() => a + b; "
2390 _assertParse("f() => a + b;", "", " ", "");
2391 }
2392
2393 void test_insert_whitespace_middle() {
2394 // "f() => a + b;"
2395 // "f() => a + b;"
2396 _assertParse("f() => a", "", " ", " + b;");
2397 }
2398
2399 void test_rename_class_withConstructor() {
2400 // "class C { C() {} }"
2401 // "class D { C() {} }"
2402 _assertParse('class ', 'C', 'D', ' { C() {} }');
2403 }
2404
2405 void test_replace_field_type_with_void() {
2406 // Note: this produces an error, but we still need the parser to produce a
2407 // consistent parse tree for it.
2408 _assertParse('class C { ', 'int', 'void', ' x; }');
2409 }
2410
2411 void test_replace_identifier_beginning() {
2412 // "f() => bell + b;"
2413 // "f() => fell + b;"
2414 _assertParse("f() => ", "b", "f", "ell + b;");
2415 }
2416
2417 void test_replace_identifier_end() {
2418 // "f() => bell + b;"
2419 // "f() => belt + b;"
2420 _assertParse("f() => bel", "l", "t", " + b;");
2421 }
2422
2423 void test_replace_identifier_middle() {
2424 // "f() => first + b;"
2425 // "f() => frost + b;"
2426 _assertParse("f() => f", "ir", "ro", "st + b;");
2427 }
2428
2429 void test_replace_identifier_with_functionLiteral_in_initializer() {
2430 // Function literals aren't allowed inside initializers; incremental parsing
2431 // needs to gather the appropriate context.
2432 //
2433 // "class A { var a; A(b) : a = b ? b : 0 { } }"
2434 // "class A { var a; A(b) : a = b ? () {} : 0 { } }"
2435 _assertParse(
2436 "class A { var a; A(b) : a = b ? ", "b", "() {}", " : 0 { } }");
2437 }
2438
2439 void test_replace_identifier_with_functionLiteral_in_initializer_index() {
2440 // Function literals are allowed inside index expressions in initializers.
2441 //
2442 // "class A { var a; A(b) : a = b[b];"
2443 // "class A { var a; A(b) : a = b[() {}];"
2444 _assertParse('class A { var a; A(b) : a = b[', 'b', '() {}', '];');
2445 }
2446
2447 void test_replace_identifier_with_functionLiteral_in_initializer_list() {
2448 // Function literals are allowed inside list literals in initializers.
2449 //
2450 // "class A { var a; A(b) : a = [b];"
2451 // "class A { var a; A(b) : a = [() {}];"
2452 _assertParse('class A { var a; A(b) : a = [', 'b', '() {}', '];');
2453 }
2454
2455 void test_replace_identifier_with_functionLiteral_in_initializer_map() {
2456 // Function literals are allowed inside map literals in initializers.
2457 //
2458 // "class A { var a; A(b) : a = {0: b};"
2459 // "class A { var a; A(b) : a = {0: () {}};"
2460 _assertParse('class A { var a; A(b) : a = {0: ', 'b', '() {}', '};');
2461 }
2462
2463 void test_replace_identifier_with_functionLiteral_in_initializer_parens() {
2464 // Function literals are allowed inside parentheses in initializers.
2465 //
2466 // "class A { var a; A(b) : a = (b);"
2467 // "class A { var a; A(b) : a = (() {});"
2468 _assertParse('class A { var a; A(b) : a = (', 'b', '() {}', ');');
2469 }
2470
2471 void test_replace_multiple_partialFirstAndLast() {
2472 // "f() => aa + bb;"
2473 // "f() => ab * ab;"
2474 _assertParse("f() => a", "a + b", "b * a", "b;");
2475 }
2476
2477 void test_replace_operator_oneForMany() {
2478 // "f() => a + b;"
2479 // "f() => a * c - b;"
2480 _assertParse("f() => a ", "+", "* c -", " b;");
2481 }
2482
2483 void test_replace_operator_oneForOne() {
2484 // "f() => a + b;"
2485 // "f() => a * b;"
2486 _assertParse("f() => a ", "+", "*", " b;");
2487 }
2488
2489 void test_split_combinator() {
2490 // "import 'foo.dart' show A;"
2491 // "import 'foo.dart' show B hide A;"
2492 _assertParse("import 'foo.dart' show ", "", "B hide ", "A;");
2493 }
2494
2495 /**
2496 * Given a description of the original and modified contents, perform an incre mental scan of the
2497 * two pieces of text.
2498 *
2499 * @param prefix the unchanged text before the edit region
2500 * @param removed the text that was removed from the original contents
2501 * @param added the text that was added to the modified contents
2502 * @param suffix the unchanged text after the edit region
2503 */
2504 void _assertParse(
2505 String prefix, String removed, String added, String suffix) {
2506 //
2507 // Compute the information needed to perform the test.
2508 //
2509 String originalContents = "$prefix$removed$suffix";
2510 String modifiedContents = "$prefix$added$suffix";
2511 int replaceStart = prefix.length;
2512 Source source = new TestSource();
2513 //
2514 // Parse the original contents.
2515 //
2516 GatheringErrorListener originalListener = new GatheringErrorListener();
2517 Scanner originalScanner = new Scanner(
2518 source, new CharSequenceReader(originalContents), originalListener);
2519 Token originalTokens = originalScanner.tokenize();
2520 expect(originalTokens, isNotNull);
2521 Parser originalParser = new Parser(source, originalListener);
2522 CompilationUnit originalUnit =
2523 originalParser.parseCompilationUnit(originalTokens);
2524 expect(originalUnit, isNotNull);
2525 //
2526 // Parse the modified contents.
2527 //
2528 GatheringErrorListener modifiedListener = new GatheringErrorListener();
2529 Scanner modifiedScanner = new Scanner(
2530 source, new CharSequenceReader(modifiedContents), modifiedListener);
2531 Token modifiedTokens = modifiedScanner.tokenize();
2532 expect(modifiedTokens, isNotNull);
2533 Parser modifiedParser = new Parser(source, modifiedListener);
2534 CompilationUnit modifiedUnit =
2535 modifiedParser.parseCompilationUnit(modifiedTokens);
2536 expect(modifiedUnit, isNotNull);
2537 //
2538 // Incrementally parse the modified contents.
2539 //
2540 GatheringErrorListener incrementalListener = new GatheringErrorListener();
2541 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
2542 IncrementalScanner incrementalScanner = new IncrementalScanner(source,
2543 new CharSequenceReader(modifiedContents), incrementalListener, options);
2544 Token incrementalTokens = incrementalScanner.rescan(
2545 originalTokens, replaceStart, removed.length, added.length);
2546 expect(incrementalTokens, isNotNull);
2547 IncrementalParser incrementalParser = new IncrementalParser(
2548 source, incrementalScanner.tokenMap, incrementalListener);
2549 CompilationUnit incrementalUnit = incrementalParser.reparse(
2550 originalUnit,
2551 incrementalScanner.leftToken,
2552 incrementalScanner.rightToken,
2553 replaceStart,
2554 prefix.length + removed.length);
2555 expect(incrementalUnit, isNotNull);
2556 //
2557 // Validate that the results of the incremental parse are the same as the
2558 // full parse of the modified source.
2559 //
2560 expect(AstComparator.equalNodes(modifiedUnit, incrementalUnit), isTrue);
2561 // TODO(brianwilkerson) Verify that the errors are correct?
2562 }
2563 }
2564
2565 @reflectiveTest
2566 class NonErrorParserTest extends ParserTestCase {
2567 void test_constFactory_external() {
2568 parse("parseClassMember", <Object>["C"], "external const factory C();");
2569 }
2570
2571 void test_staticMethod_notParsingFunctionBodies() {
2572 ParserTestCase.parseFunctionBodies = false;
2573 try {
2574 parse4("parseCompilationUnit", "class C { static void m() {} }");
2575 } finally {
2576 ParserTestCase.parseFunctionBodies = true;
2577 }
2578 }
2579 }
2580
2581 class ParserTestCase extends EngineTestCase {
2582 /**
2583 * An empty list of objects used as arguments to zero-argument methods.
2584 */
2585 static const List<Object> _EMPTY_ARGUMENTS = const <Object>[];
2586
2587 /**
2588 * A flag indicating whether parser is to parse function bodies.
2589 */
2590 static bool parseFunctionBodies = true;
2591
2592 /**
2593 * A flag indicating whether generic method support should be enabled for a
2594 * specific test.
2595 */
2596 bool enableGenericMethods = false;
2597
2598 /**
2599 * Return a CommentAndMetadata object with the given values that can be used f or testing.
2600 *
2601 * @param comment the comment to be wrapped in the object
2602 * @param annotations the annotations to be wrapped in the object
2603 * @return a CommentAndMetadata object that can be used for testing
2604 */
2605 CommentAndMetadata commentAndMetadata(Comment comment,
2606 [List<Annotation> annotations]) {
2607 return new CommentAndMetadata(comment, annotations);
2608 }
2609
2610 /**
2611 * Return an empty CommentAndMetadata object that can be used for testing.
2612 *
2613 * @return an empty CommentAndMetadata object that can be used for testing
2614 */
2615 CommentAndMetadata emptyCommentAndMetadata() =>
2616 new CommentAndMetadata(null, null);
2617
2618 /**
2619 * Invoke a method in [Parser]. The method is assumed to have the given number and type of
2620 * parameters and will be invoked with the given arguments.
2621 *
2622 * The given source is scanned and the parser is initialized to start with the first token in the
2623 * source before the method is invoked.
2624 *
2625 * @param methodName the name of the method that should be invoked
2626 * @param objects the values of the arguments to the method
2627 * @param source the source to be processed by the parse method
2628 * @param listener the error listener that will be used for both scanning and parsing
2629 * @return the result of invoking the method
2630 * @throws Exception if the method could not be invoked or throws an exception
2631 * @throws AssertionFailedError if the result is `null` or the errors produced while
2632 * scanning and parsing the source do not match the expected errors
2633 */
2634 Object invokeParserMethod(String methodName, List<Object> objects,
2635 String source, GatheringErrorListener listener) {
2636 //
2637 // Scan the source.
2638 //
2639 Scanner scanner =
2640 new Scanner(null, new CharSequenceReader(source), listener);
2641 Token tokenStream = scanner.tokenize();
2642 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2643 //
2644 // Parse the source.
2645 //
2646 Parser parser = createParser(listener);
2647 parser.parseGenericMethods = enableGenericMethods;
2648 parser.parseFunctionBodies = parseFunctionBodies;
2649 Object result =
2650 invokeParserMethodImpl(parser, methodName, objects, tokenStream);
2651 //
2652 // Partially test the results.
2653 //
2654 if (!listener.hasErrors) {
2655 expect(result, isNotNull);
2656 }
2657 return result;
2658 }
2659
2660 /**
2661 * Invoke a method in [Parser]. The method is assumed to have no arguments.
2662 *
2663 * The given source is scanned and the parser is initialized to start with the first token in the
2664 * source before the method is invoked.
2665 *
2666 * @param methodName the name of the method that should be invoked
2667 * @param source the source to be processed by the parse method
2668 * @param listener the error listener that will be used for both scanning and parsing
2669 * @return the result of invoking the method
2670 * @throws Exception if the method could not be invoked or throws an exception
2671 * @throws AssertionFailedError if the result is `null` or the errors produced while
2672 * scanning and parsing the source do not match the expected errors
2673 */
2674 Object invokeParserMethod2(
2675 String methodName, String source, GatheringErrorListener listener) =>
2676 invokeParserMethod(methodName, _EMPTY_ARGUMENTS, source, listener);
2677
2678 /**
2679 * Invoke a parse method in [Parser]. The method is assumed to have the given number and
2680 * type of parameters and will be invoked with the given arguments.
2681 *
2682 * The given source is scanned and the parser is initialized to start with the first token in the
2683 * source before the parse method is invoked.
2684 *
2685 * @param methodName the name of the parse method that should be invoked to pa rse the source
2686 * @param objects the values of the arguments to the method
2687 * @param source the source to be parsed by the parse method
2688 * @return the result of invoking the method
2689 * @throws Exception if the method could not be invoked or throws an exception
2690 * @throws AssertionFailedError if the result is `null` or if any errors are p roduced
2691 */
2692 Object parse(String methodName, List<Object> objects, String source) =>
2693 parse2(methodName, objects, source);
2694
2695 /**
2696 * Invoke a parse method in [Parser]. The method is assumed to have the given number and
2697 * type of parameters and will be invoked with the given arguments.
2698 *
2699 * The given source is scanned and the parser is initialized to start with the first token in the
2700 * source before the parse method is invoked.
2701 *
2702 * @param methodName the name of the parse method that should be invoked to pa rse the source
2703 * @param objects the values of the arguments to the method
2704 * @param source the source to be parsed by the parse method
2705 * @param errors the errors that should be generated
2706 * @return the result of invoking the method
2707 * @throws Exception if the method could not be invoked or throws an exception
2708 * @throws AssertionFailedError if the result is `null` or the errors produced while
2709 * scanning and parsing the source do not match the expected errors
2710 */
2711 Object parse2(String methodName, List<Object> objects, String source,
2712 [List<AnalysisError> errors = AnalysisError.NO_ERRORS]) {
2713 GatheringErrorListener listener = new GatheringErrorListener();
2714 Object result = invokeParserMethod(methodName, objects, source, listener);
2715 listener.assertErrors(errors);
2716 return result;
2717 }
2718
2719 /**
2720 * Invoke a parse method in [Parser]. The method is assumed to have the given number and
2721 * type of parameters and will be invoked with the given arguments.
2722 *
2723 * The given source is scanned and the parser is initialized to start with the first token in the
2724 * source before the parse method is invoked.
2725 *
2726 * @param methodName the name of the parse method that should be invoked to pa rse the source
2727 * @param objects the values of the arguments to the method
2728 * @param source the source to be parsed by the parse method
2729 * @param errorCodes the error codes of the errors that should be generated
2730 * @return the result of invoking the method
2731 * @throws Exception if the method could not be invoked or throws an exception
2732 * @throws AssertionFailedError if the result is `null` or the errors produced while
2733 * scanning and parsing the source do not match the expected errors
2734 */
2735 Object parse3(String methodName, List<Object> objects, String source,
2736 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) {
2737 GatheringErrorListener listener = new GatheringErrorListener();
2738 Object result = invokeParserMethod(methodName, objects, source, listener);
2739 listener.assertErrorsWithCodes(errorCodes);
2740 return result;
2741 }
2742
2743 /**
2744 * Invoke a parse method in [Parser]. The method is assumed to have no argumen ts.
2745 *
2746 * The given source is scanned and the parser is initialized to start with the first token in the
2747 * source before the parse method is invoked.
2748 *
2749 * @param methodName the name of the parse method that should be invoked to pa rse the source
2750 * @param source the source to be parsed by the parse method
2751 * @param errorCodes the error codes of the errors that should be generated
2752 * @return the result of invoking the method
2753 * @throws Exception if the method could not be invoked or throws an exception
2754 * @throws AssertionFailedError if the result is `null` or the errors produced while
2755 * scanning and parsing the source do not match the expected errors
2756 */
2757 Object parse4(String methodName, String source,
2758 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) =>
2759 parse3(methodName, _EMPTY_ARGUMENTS, source, errorCodes);
2760
2761 /**
2762 * Parse the given source as an expression.
2763 *
2764 * @param source the source to be parsed
2765 * @param errorCodes the error codes of the errors that are expected to be fou nd
2766 * @return the expression that was parsed
2767 * @throws Exception if the source could not be parsed, if the compilation err ors in the source do
2768 * not match those that are expected, or if the result would have be en `null`
2769 */
2770 Expression parseExpression(String source,
2771 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) {
2772 GatheringErrorListener listener = new GatheringErrorListener();
2773 Scanner scanner =
2774 new Scanner(null, new CharSequenceReader(source), listener);
2775 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2776 Token token = scanner.tokenize();
2777 Parser parser = createParser(listener);
2778 parser.parseGenericMethods = enableGenericMethods;
2779 Expression expression = parser.parseExpression(token);
2780 expect(expression, isNotNull);
2781 listener.assertErrorsWithCodes(errorCodes);
2782 return expression;
2783 }
2784
2785 @override
2786 void setUp() {
2787 super.setUp();
2788 parseFunctionBodies = true;
2789 }
2790
2791 /**
2792 * Create a parser.
2793 *
2794 * @param listener the listener to be passed to the parser
2795 * @return the parser that was created
2796 */
2797 static Parser createParser(GatheringErrorListener listener) {
2798 return new Parser(null, listener);
2799 }
2800
2801 /**
2802 * Parse the given source as a compilation unit.
2803 *
2804 * @param source the source to be parsed
2805 * @param errorCodes the error codes of the errors that are expected to be fou nd
2806 * @return the compilation unit that was parsed
2807 * @throws Exception if the source could not be parsed, if the compilation err ors in the source do
2808 * not match those that are expected, or if the result would have be en `null`
2809 */
2810 static CompilationUnit parseCompilationUnit(String source,
2811 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) {
2812 GatheringErrorListener listener = new GatheringErrorListener();
2813 Scanner scanner =
2814 new Scanner(null, new CharSequenceReader(source), listener);
2815 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2816 Token token = scanner.tokenize();
2817 Parser parser = createParser(listener);
2818 CompilationUnit unit = parser.parseCompilationUnit(token);
2819 expect(unit, isNotNull);
2820 listener.assertErrorsWithCodes(errorCodes);
2821 return unit;
2822 }
2823
2824 /**
2825 * Parse the given source as a statement.
2826 *
2827 * @param source the source to be parsed
2828 * @param errorCodes the error codes of the errors that are expected to be fou nd
2829 * @return the statement that was parsed
2830 * @throws Exception if the source could not be parsed, if the compilation err ors in the source do
2831 * not match those that are expected, or if the result would have be en `null`
2832 */
2833 static Statement parseStatement(String source,
2834 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) {
2835 GatheringErrorListener listener = new GatheringErrorListener();
2836 Scanner scanner =
2837 new Scanner(null, new CharSequenceReader(source), listener);
2838 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2839 Token token = scanner.tokenize();
2840 Parser parser = createParser(listener);
2841 Statement statement = parser.parseStatement(token);
2842 expect(statement, isNotNull);
2843 listener.assertErrorsWithCodes(errorCodes);
2844 return statement;
2845 }
2846
2847 /**
2848 * Parse the given source as a sequence of statements.
2849 *
2850 * @param source the source to be parsed
2851 * @param expectedCount the number of statements that are expected
2852 * @param errorCodes the error codes of the errors that are expected to be fou nd
2853 * @return the statements that were parsed
2854 * @throws Exception if the source could not be parsed, if the number of state ments does not match
2855 * the expected count, if the compilation errors in the source do no t match those that
2856 * are expected, or if the result would have been `null`
2857 */
2858 static List<Statement> parseStatements(String source, int expectedCount,
2859 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) {
2860 GatheringErrorListener listener = new GatheringErrorListener();
2861 Scanner scanner =
2862 new Scanner(null, new CharSequenceReader(source), listener);
2863 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2864 Token token = scanner.tokenize();
2865 Parser parser = createParser(listener);
2866 List<Statement> statements = parser.parseStatements(token);
2867 expect(statements, hasLength(expectedCount));
2868 listener.assertErrorsWithCodes(errorCodes);
2869 return statements;
2870 }
2871 }
2872
2873 /**
2874 * The class `RecoveryParserTest` defines parser tests that test the parsing of invalid code
2875 * sequences to ensure that the correct recovery steps are taken in the parser.
2876 */
2877 @reflectiveTest
2878 class RecoveryParserTest extends ParserTestCase {
2879 void fail_incomplete_returnType() {
2880 ParserTestCase.parseCompilationUnit(r'''
2881 Map<Symbol, convertStringToSymbolMap(Map<String, dynamic> map) {
2882 if (map == null) return null;
2883 Map<Symbol, dynamic> result = new Map<Symbol, dynamic>();
2884 map.forEach((name, value) {
2885 result[new Symbol(name)] = value;
2886 });
2887 return result;
2888 }''');
2889 }
2890
2891 void test_additiveExpression_missing_LHS() {
2892 BinaryExpression expression =
2893 parseExpression("+ y", [ParserErrorCode.MISSING_IDENTIFIER]);
2894 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2895 SimpleIdentifier, expression.leftOperand);
2896 expect(expression.leftOperand.isSynthetic, isTrue);
2897 }
2898
2899 void test_additiveExpression_missing_LHS_RHS() {
2900 BinaryExpression expression = parseExpression("+", [
2901 ParserErrorCode.MISSING_IDENTIFIER,
2902 ParserErrorCode.MISSING_IDENTIFIER
2903 ]);
2904 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2905 SimpleIdentifier, expression.leftOperand);
2906 expect(expression.leftOperand.isSynthetic, isTrue);
2907 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2908 SimpleIdentifier, expression.rightOperand);
2909 expect(expression.rightOperand.isSynthetic, isTrue);
2910 }
2911
2912 void test_additiveExpression_missing_RHS() {
2913 BinaryExpression expression =
2914 parseExpression("x +", [ParserErrorCode.MISSING_IDENTIFIER]);
2915 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2916 SimpleIdentifier, expression.rightOperand);
2917 expect(expression.rightOperand.isSynthetic, isTrue);
2918 }
2919
2920 void test_additiveExpression_missing_RHS_super() {
2921 BinaryExpression expression =
2922 parseExpression("super +", [ParserErrorCode.MISSING_IDENTIFIER]);
2923 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2924 SimpleIdentifier, expression.rightOperand);
2925 expect(expression.rightOperand.isSynthetic, isTrue);
2926 }
2927
2928 void test_additiveExpression_precedence_multiplicative_left() {
2929 BinaryExpression expression = parseExpression("* +", [
2930 ParserErrorCode.MISSING_IDENTIFIER,
2931 ParserErrorCode.MISSING_IDENTIFIER,
2932 ParserErrorCode.MISSING_IDENTIFIER
2933 ]);
2934 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
2935 BinaryExpression, expression.leftOperand);
2936 }
2937
2938 void test_additiveExpression_precedence_multiplicative_right() {
2939 BinaryExpression expression = parseExpression("+ *", [
2940 ParserErrorCode.MISSING_IDENTIFIER,
2941 ParserErrorCode.MISSING_IDENTIFIER,
2942 ParserErrorCode.MISSING_IDENTIFIER
2943 ]);
2944 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
2945 BinaryExpression, expression.rightOperand);
2946 }
2947
2948 void test_additiveExpression_super() {
2949 BinaryExpression expression = parseExpression("super + +", [
2950 ParserErrorCode.MISSING_IDENTIFIER,
2951 ParserErrorCode.MISSING_IDENTIFIER
2952 ]);
2953 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
2954 BinaryExpression, expression.leftOperand);
2955 }
2956
2957 void test_assignmentExpression_missing_compound1() {
2958 AssignmentExpression expression =
2959 parseExpression("= y = 0", [ParserErrorCode.MISSING_IDENTIFIER]);
2960 Expression syntheticExpression = expression.leftHandSide;
2961 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2962 SimpleIdentifier, syntheticExpression);
2963 expect(syntheticExpression.isSynthetic, isTrue);
2964 }
2965
2966 void test_assignmentExpression_missing_compound2() {
2967 AssignmentExpression expression =
2968 parseExpression("x = = 0", [ParserErrorCode.MISSING_IDENTIFIER]);
2969 Expression syntheticExpression =
2970 (expression.rightHandSide as AssignmentExpression).leftHandSide;
2971 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2972 SimpleIdentifier, syntheticExpression);
2973 expect(syntheticExpression.isSynthetic, isTrue);
2974 }
2975
2976 void test_assignmentExpression_missing_compound3() {
2977 AssignmentExpression expression =
2978 parseExpression("x = y =", [ParserErrorCode.MISSING_IDENTIFIER]);
2979 Expression syntheticExpression =
2980 (expression.rightHandSide as AssignmentExpression).rightHandSide;
2981 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2982 SimpleIdentifier, syntheticExpression);
2983 expect(syntheticExpression.isSynthetic, isTrue);
2984 }
2985
2986 void test_assignmentExpression_missing_LHS() {
2987 AssignmentExpression expression =
2988 parseExpression("= 0", [ParserErrorCode.MISSING_IDENTIFIER]);
2989 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2990 SimpleIdentifier, expression.leftHandSide);
2991 expect(expression.leftHandSide.isSynthetic, isTrue);
2992 }
2993
2994 void test_assignmentExpression_missing_RHS() {
2995 AssignmentExpression expression =
2996 parseExpression("x =", [ParserErrorCode.MISSING_IDENTIFIER]);
2997 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
2998 SimpleIdentifier, expression.leftHandSide);
2999 expect(expression.rightHandSide.isSynthetic, isTrue);
3000 }
3001
3002 void test_bitwiseAndExpression_missing_LHS() {
3003 BinaryExpression expression =
3004 parseExpression("& y", [ParserErrorCode.MISSING_IDENTIFIER]);
3005 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3006 SimpleIdentifier, expression.leftOperand);
3007 expect(expression.leftOperand.isSynthetic, isTrue);
3008 }
3009
3010 void test_bitwiseAndExpression_missing_LHS_RHS() {
3011 BinaryExpression expression = parseExpression("&", [
3012 ParserErrorCode.MISSING_IDENTIFIER,
3013 ParserErrorCode.MISSING_IDENTIFIER
3014 ]);
3015 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3016 SimpleIdentifier, expression.leftOperand);
3017 expect(expression.leftOperand.isSynthetic, isTrue);
3018 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3019 SimpleIdentifier, expression.rightOperand);
3020 expect(expression.rightOperand.isSynthetic, isTrue);
3021 }
3022
3023 void test_bitwiseAndExpression_missing_RHS() {
3024 BinaryExpression expression =
3025 parseExpression("x &", [ParserErrorCode.MISSING_IDENTIFIER]);
3026 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3027 SimpleIdentifier, expression.rightOperand);
3028 expect(expression.rightOperand.isSynthetic, isTrue);
3029 }
3030
3031 void test_bitwiseAndExpression_missing_RHS_super() {
3032 BinaryExpression expression =
3033 parseExpression("super &", [ParserErrorCode.MISSING_IDENTIFIER]);
3034 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3035 SimpleIdentifier, expression.rightOperand);
3036 expect(expression.rightOperand.isSynthetic, isTrue);
3037 }
3038
3039 void test_bitwiseAndExpression_precedence_equality_left() {
3040 BinaryExpression expression = parseExpression("== &&", [
3041 ParserErrorCode.MISSING_IDENTIFIER,
3042 ParserErrorCode.MISSING_IDENTIFIER,
3043 ParserErrorCode.MISSING_IDENTIFIER
3044 ]);
3045 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3046 BinaryExpression, expression.leftOperand);
3047 }
3048
3049 void test_bitwiseAndExpression_precedence_equality_right() {
3050 BinaryExpression expression = parseExpression("&& ==", [
3051 ParserErrorCode.MISSING_IDENTIFIER,
3052 ParserErrorCode.MISSING_IDENTIFIER,
3053 ParserErrorCode.MISSING_IDENTIFIER
3054 ]);
3055 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3056 BinaryExpression, expression.rightOperand);
3057 }
3058
3059 void test_bitwiseAndExpression_super() {
3060 BinaryExpression expression = parseExpression("super & &", [
3061 ParserErrorCode.MISSING_IDENTIFIER,
3062 ParserErrorCode.MISSING_IDENTIFIER
3063 ]);
3064 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3065 BinaryExpression, expression.leftOperand);
3066 }
3067
3068 void test_bitwiseOrExpression_missing_LHS() {
3069 BinaryExpression expression =
3070 parseExpression("| y", [ParserErrorCode.MISSING_IDENTIFIER]);
3071 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3072 SimpleIdentifier, expression.leftOperand);
3073 expect(expression.leftOperand.isSynthetic, isTrue);
3074 }
3075
3076 void test_bitwiseOrExpression_missing_LHS_RHS() {
3077 BinaryExpression expression = parseExpression("|", [
3078 ParserErrorCode.MISSING_IDENTIFIER,
3079 ParserErrorCode.MISSING_IDENTIFIER
3080 ]);
3081 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3082 SimpleIdentifier, expression.leftOperand);
3083 expect(expression.leftOperand.isSynthetic, isTrue);
3084 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3085 SimpleIdentifier, expression.rightOperand);
3086 expect(expression.rightOperand.isSynthetic, isTrue);
3087 }
3088
3089 void test_bitwiseOrExpression_missing_RHS() {
3090 BinaryExpression expression =
3091 parseExpression("x |", [ParserErrorCode.MISSING_IDENTIFIER]);
3092 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3093 SimpleIdentifier, expression.rightOperand);
3094 expect(expression.rightOperand.isSynthetic, isTrue);
3095 }
3096
3097 void test_bitwiseOrExpression_missing_RHS_super() {
3098 BinaryExpression expression =
3099 parseExpression("super |", [ParserErrorCode.MISSING_IDENTIFIER]);
3100 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3101 SimpleIdentifier, expression.rightOperand);
3102 expect(expression.rightOperand.isSynthetic, isTrue);
3103 }
3104
3105 void test_bitwiseOrExpression_precedence_xor_left() {
3106 BinaryExpression expression = parseExpression("^ |", [
3107 ParserErrorCode.MISSING_IDENTIFIER,
3108 ParserErrorCode.MISSING_IDENTIFIER,
3109 ParserErrorCode.MISSING_IDENTIFIER
3110 ]);
3111 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3112 BinaryExpression, expression.leftOperand);
3113 }
3114
3115 void test_bitwiseOrExpression_precedence_xor_right() {
3116 BinaryExpression expression = parseExpression("| ^", [
3117 ParserErrorCode.MISSING_IDENTIFIER,
3118 ParserErrorCode.MISSING_IDENTIFIER,
3119 ParserErrorCode.MISSING_IDENTIFIER
3120 ]);
3121 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3122 BinaryExpression, expression.rightOperand);
3123 }
3124
3125 void test_bitwiseOrExpression_super() {
3126 BinaryExpression expression = parseExpression("super | |", [
3127 ParserErrorCode.MISSING_IDENTIFIER,
3128 ParserErrorCode.MISSING_IDENTIFIER
3129 ]);
3130 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3131 BinaryExpression, expression.leftOperand);
3132 }
3133
3134 void test_bitwiseXorExpression_missing_LHS() {
3135 BinaryExpression expression =
3136 parseExpression("^ y", [ParserErrorCode.MISSING_IDENTIFIER]);
3137 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3138 SimpleIdentifier, expression.leftOperand);
3139 expect(expression.leftOperand.isSynthetic, isTrue);
3140 }
3141
3142 void test_bitwiseXorExpression_missing_LHS_RHS() {
3143 BinaryExpression expression = parseExpression("^", [
3144 ParserErrorCode.MISSING_IDENTIFIER,
3145 ParserErrorCode.MISSING_IDENTIFIER
3146 ]);
3147 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3148 SimpleIdentifier, expression.leftOperand);
3149 expect(expression.leftOperand.isSynthetic, isTrue);
3150 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3151 SimpleIdentifier, expression.rightOperand);
3152 expect(expression.rightOperand.isSynthetic, isTrue);
3153 }
3154
3155 void test_bitwiseXorExpression_missing_RHS() {
3156 BinaryExpression expression =
3157 parseExpression("x ^", [ParserErrorCode.MISSING_IDENTIFIER]);
3158 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3159 SimpleIdentifier, expression.rightOperand);
3160 expect(expression.rightOperand.isSynthetic, isTrue);
3161 }
3162
3163 void test_bitwiseXorExpression_missing_RHS_super() {
3164 BinaryExpression expression =
3165 parseExpression("super ^", [ParserErrorCode.MISSING_IDENTIFIER]);
3166 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3167 SimpleIdentifier, expression.rightOperand);
3168 expect(expression.rightOperand.isSynthetic, isTrue);
3169 }
3170
3171 void test_bitwiseXorExpression_precedence_and_left() {
3172 BinaryExpression expression = parseExpression("& ^", [
3173 ParserErrorCode.MISSING_IDENTIFIER,
3174 ParserErrorCode.MISSING_IDENTIFIER,
3175 ParserErrorCode.MISSING_IDENTIFIER
3176 ]);
3177 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3178 BinaryExpression, expression.leftOperand);
3179 }
3180
3181 void test_bitwiseXorExpression_precedence_and_right() {
3182 BinaryExpression expression = parseExpression("^ &", [
3183 ParserErrorCode.MISSING_IDENTIFIER,
3184 ParserErrorCode.MISSING_IDENTIFIER,
3185 ParserErrorCode.MISSING_IDENTIFIER
3186 ]);
3187 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3188 BinaryExpression, expression.rightOperand);
3189 }
3190
3191 void test_bitwiseXorExpression_super() {
3192 BinaryExpression expression = parseExpression("super ^ ^", [
3193 ParserErrorCode.MISSING_IDENTIFIER,
3194 ParserErrorCode.MISSING_IDENTIFIER
3195 ]);
3196 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3197 BinaryExpression, expression.leftOperand);
3198 }
3199
3200 void test_classTypeAlias_withBody() {
3201 ParserTestCase.parseCompilationUnit(
3202 r'''
3203 class A {}
3204 class B = Object with A {}''',
3205 [ParserErrorCode.EXPECTED_TOKEN]);
3206 }
3207
3208 void test_conditionalExpression_missingElse() {
3209 ConditionalExpression expression = parse4("parseConditionalExpression",
3210 "x ? y :", [ParserErrorCode.MISSING_IDENTIFIER]);
3211 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3212 SimpleIdentifier, expression.elseExpression);
3213 expect(expression.elseExpression.isSynthetic, isTrue);
3214 }
3215
3216 void test_conditionalExpression_missingThen() {
3217 ConditionalExpression expression = parse4("parseConditionalExpression",
3218 "x ? : z", [ParserErrorCode.MISSING_IDENTIFIER]);
3219 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3220 SimpleIdentifier, expression.thenExpression);
3221 expect(expression.thenExpression.isSynthetic, isTrue);
3222 }
3223
3224 void test_equalityExpression_missing_LHS() {
3225 BinaryExpression expression =
3226 parseExpression("== y", [ParserErrorCode.MISSING_IDENTIFIER]);
3227 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3228 SimpleIdentifier, expression.leftOperand);
3229 expect(expression.leftOperand.isSynthetic, isTrue);
3230 }
3231
3232 void test_equalityExpression_missing_LHS_RHS() {
3233 BinaryExpression expression = parseExpression("==", [
3234 ParserErrorCode.MISSING_IDENTIFIER,
3235 ParserErrorCode.MISSING_IDENTIFIER
3236 ]);
3237 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3238 SimpleIdentifier, expression.leftOperand);
3239 expect(expression.leftOperand.isSynthetic, isTrue);
3240 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3241 SimpleIdentifier, expression.rightOperand);
3242 expect(expression.rightOperand.isSynthetic, isTrue);
3243 }
3244
3245 void test_equalityExpression_missing_RHS() {
3246 BinaryExpression expression =
3247 parseExpression("x ==", [ParserErrorCode.MISSING_IDENTIFIER]);
3248 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3249 SimpleIdentifier, expression.rightOperand);
3250 expect(expression.rightOperand.isSynthetic, isTrue);
3251 }
3252
3253 void test_equalityExpression_missing_RHS_super() {
3254 BinaryExpression expression =
3255 parseExpression("super ==", [ParserErrorCode.MISSING_IDENTIFIER]);
3256 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3257 SimpleIdentifier, expression.rightOperand);
3258 expect(expression.rightOperand.isSynthetic, isTrue);
3259 }
3260
3261 void test_equalityExpression_precedence_relational_left() {
3262 BinaryExpression expression = parseExpression("is ==", [
3263 ParserErrorCode.EXPECTED_TYPE_NAME,
3264 ParserErrorCode.MISSING_IDENTIFIER,
3265 ParserErrorCode.MISSING_IDENTIFIER
3266 ]);
3267 EngineTestCase.assertInstanceOf(
3268 (obj) => obj is IsExpression, IsExpression, expression.leftOperand);
3269 }
3270
3271 void test_equalityExpression_precedence_relational_right() {
3272 BinaryExpression expression = parseExpression("== is", [
3273 ParserErrorCode.EXPECTED_TYPE_NAME,
3274 ParserErrorCode.MISSING_IDENTIFIER,
3275 ParserErrorCode.MISSING_IDENTIFIER
3276 ]);
3277 EngineTestCase.assertInstanceOf(
3278 (obj) => obj is IsExpression, IsExpression, expression.rightOperand);
3279 }
3280
3281 void test_equalityExpression_super() {
3282 BinaryExpression expression = parseExpression("super == ==", [
3283 ParserErrorCode.MISSING_IDENTIFIER,
3284 ParserErrorCode.MISSING_IDENTIFIER,
3285 ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND
3286 ]);
3287 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3288 BinaryExpression, expression.leftOperand);
3289 }
3290
3291 void test_expressionList_multiple_end() {
3292 List<Expression> result = parse4("parseExpressionList", ", 2, 3, 4",
3293 [ParserErrorCode.MISSING_IDENTIFIER]);
3294 expect(result, hasLength(4));
3295 Expression syntheticExpression = result[0];
3296 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3297 SimpleIdentifier, syntheticExpression);
3298 expect(syntheticExpression.isSynthetic, isTrue);
3299 }
3300
3301 void test_expressionList_multiple_middle() {
3302 List<Expression> result = parse4("parseExpressionList", "1, 2, , 4",
3303 [ParserErrorCode.MISSING_IDENTIFIER]);
3304 expect(result, hasLength(4));
3305 Expression syntheticExpression = result[2];
3306 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3307 SimpleIdentifier, syntheticExpression);
3308 expect(syntheticExpression.isSynthetic, isTrue);
3309 }
3310
3311 void test_expressionList_multiple_start() {
3312 List<Expression> result = parse4("parseExpressionList", "1, 2, 3,",
3313 [ParserErrorCode.MISSING_IDENTIFIER]);
3314 expect(result, hasLength(4));
3315 Expression syntheticExpression = result[3];
3316 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3317 SimpleIdentifier, syntheticExpression);
3318 expect(syntheticExpression.isSynthetic, isTrue);
3319 }
3320
3321 void test_functionExpression_in_ConstructorFieldInitializer() {
3322 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3323 "class A { A() : a = (){}; var v; }",
3324 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.UNEXPECTED_TOKEN]);
3325 // Make sure we recovered and parsed "var v" correctly
3326 ClassDeclaration declaration = unit.declarations[0] as ClassDeclaration;
3327 NodeList<ClassMember> members = declaration.members;
3328 ClassMember fieldDecl = members[1];
3329 EngineTestCase.assertInstanceOf(
3330 (obj) => obj is FieldDeclaration, FieldDeclaration, fieldDecl);
3331 NodeList<VariableDeclaration> vars =
3332 (fieldDecl as FieldDeclaration).fields.variables;
3333 expect(vars, hasLength(1));
3334 expect(vars[0].name.name, "v");
3335 }
3336
3337 void test_functionExpression_named() {
3338 parseExpression("m(f() => 0);", [ParserErrorCode.EXPECTED_TOKEN]);
3339 }
3340
3341 void test_declarationBeforeDirective() {
3342 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3343 "class foo { } import 'bar.dart';",
3344 [ParserErrorCode.DIRECTIVE_AFTER_DECLARATION]);
3345 expect(unit.directives, hasLength(1));
3346 expect(unit.declarations, hasLength(1));
3347 ClassDeclaration classDecl = unit.childEntities.first;
3348 expect(classDecl, isNotNull);
3349 expect(classDecl.name.name, 'foo');
3350 }
3351
3352 void test_importDirectivePartial_as() {
3353 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3354 "import 'b.dart' d as b;", [ParserErrorCode.UNEXPECTED_TOKEN]);
3355 ImportDirective importDirective = unit.childEntities.first;
3356 expect(importDirective.asKeyword, isNotNull);
3357 expect(unit.directives, hasLength(1));
3358 expect(unit.declarations, hasLength(0));
3359 }
3360
3361 void test_importDirectivePartial_hide() {
3362 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3363 "import 'b.dart' d hide foo;", [ParserErrorCode.UNEXPECTED_TOKEN]);
3364 ImportDirective importDirective = unit.childEntities.first;
3365 expect(importDirective.combinators, hasLength(1));
3366 expect(unit.directives, hasLength(1));
3367 expect(unit.declarations, hasLength(0));
3368 }
3369
3370 void test_importDirectivePartial_show() {
3371 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3372 "import 'b.dart' d show foo;", [ParserErrorCode.UNEXPECTED_TOKEN]);
3373 ImportDirective importDirective = unit.childEntities.first;
3374 expect(importDirective.combinators, hasLength(1));
3375 expect(unit.directives, hasLength(1));
3376 expect(unit.declarations, hasLength(0));
3377 }
3378
3379 void test_incomplete_conditionalExpression() {
3380 parseExpression("x ? 0",
3381 [ParserErrorCode.EXPECTED_TOKEN, ParserErrorCode.MISSING_IDENTIFIER]);
3382 }
3383
3384 void test_incomplete_constructorInitializers_empty() {
3385 parse3("parseClassMember", ["C"], "C() : {}",
3386 [ParserErrorCode.MISSING_INITIALIZER]);
3387 }
3388
3389 void test_incomplete_constructorInitializers_missingEquals() {
3390 ClassMember member = parse3("parseClassMember", ["C"], "C() : x(3) {}",
3391 [ParserErrorCode.MISSING_ASSIGNMENT_IN_INITIALIZER]);
3392 expect(member, new isInstanceOf<ConstructorDeclaration>());
3393 NodeList<ConstructorInitializer> initializers =
3394 (member as ConstructorDeclaration).initializers;
3395 expect(initializers, hasLength(1));
3396 ConstructorInitializer initializer = initializers[0];
3397 expect(initializer, new isInstanceOf<ConstructorFieldInitializer>());
3398 Expression expression =
3399 (initializer as ConstructorFieldInitializer).expression;
3400 expect(expression, isNotNull);
3401 expect(expression, new isInstanceOf<ParenthesizedExpression>());
3402 }
3403
3404 void test_incomplete_constructorInitializers_variable() {
3405 parse3("parseClassMember", ["C"], "C() : x {}",
3406 [ParserErrorCode.MISSING_ASSIGNMENT_IN_INITIALIZER]);
3407 }
3408
3409 void test_incomplete_topLevelFunction() {
3410 ParserTestCase.parseCompilationUnit(
3411 "foo();", [ParserErrorCode.MISSING_FUNCTION_BODY]);
3412 }
3413
3414 void test_incomplete_topLevelVariable() {
3415 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3416 "String", [ParserErrorCode.EXPECTED_EXECUTABLE]);
3417 NodeList<CompilationUnitMember> declarations = unit.declarations;
3418 expect(declarations, hasLength(1));
3419 CompilationUnitMember member = declarations[0];
3420 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration,
3421 TopLevelVariableDeclaration, member);
3422 NodeList<VariableDeclaration> variables =
3423 (member as TopLevelVariableDeclaration).variables.variables;
3424 expect(variables, hasLength(1));
3425 SimpleIdentifier name = variables[0].name;
3426 expect(name.isSynthetic, isTrue);
3427 }
3428
3429 void test_incomplete_topLevelVariable_const() {
3430 CompilationUnit unit = ParserTestCase.parseCompilationUnit("const ",
3431 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.EXPECTED_TOKEN]);
3432 NodeList<CompilationUnitMember> declarations = unit.declarations;
3433 expect(declarations, hasLength(1));
3434 CompilationUnitMember member = declarations[0];
3435 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration,
3436 TopLevelVariableDeclaration, member);
3437 NodeList<VariableDeclaration> variables =
3438 (member as TopLevelVariableDeclaration).variables.variables;
3439 expect(variables, hasLength(1));
3440 SimpleIdentifier name = variables[0].name;
3441 expect(name.isSynthetic, isTrue);
3442 }
3443
3444 void test_incomplete_topLevelVariable_final() {
3445 CompilationUnit unit = ParserTestCase.parseCompilationUnit("final ",
3446 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.EXPECTED_TOKEN]);
3447 NodeList<CompilationUnitMember> declarations = unit.declarations;
3448 expect(declarations, hasLength(1));
3449 CompilationUnitMember member = declarations[0];
3450 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration,
3451 TopLevelVariableDeclaration, member);
3452 NodeList<VariableDeclaration> variables =
3453 (member as TopLevelVariableDeclaration).variables.variables;
3454 expect(variables, hasLength(1));
3455 SimpleIdentifier name = variables[0].name;
3456 expect(name.isSynthetic, isTrue);
3457 }
3458
3459 void test_incomplete_topLevelVariable_var() {
3460 CompilationUnit unit = ParserTestCase.parseCompilationUnit("var ",
3461 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.EXPECTED_TOKEN]);
3462 NodeList<CompilationUnitMember> declarations = unit.declarations;
3463 expect(declarations, hasLength(1));
3464 CompilationUnitMember member = declarations[0];
3465 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration,
3466 TopLevelVariableDeclaration, member);
3467 NodeList<VariableDeclaration> variables =
3468 (member as TopLevelVariableDeclaration).variables.variables;
3469 expect(variables, hasLength(1));
3470 SimpleIdentifier name = variables[0].name;
3471 expect(name.isSynthetic, isTrue);
3472 }
3473
3474 void test_incompleteField_const() {
3475 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3476 r'''
3477 class C {
3478 const
3479 }''',
3480 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.EXPECTED_TOKEN]);
3481 NodeList<CompilationUnitMember> declarations = unit.declarations;
3482 expect(declarations, hasLength(1));
3483 CompilationUnitMember unitMember = declarations[0];
3484 EngineTestCase.assertInstanceOf(
3485 (obj) => obj is ClassDeclaration, ClassDeclaration, unitMember);
3486 NodeList<ClassMember> members = (unitMember as ClassDeclaration).members;
3487 expect(members, hasLength(1));
3488 ClassMember classMember = members[0];
3489 EngineTestCase.assertInstanceOf(
3490 (obj) => obj is FieldDeclaration, FieldDeclaration, classMember);
3491 VariableDeclarationList fieldList =
3492 (classMember as FieldDeclaration).fields;
3493 expect((fieldList.keyword as KeywordToken).keyword, Keyword.CONST);
3494 NodeList<VariableDeclaration> fields = fieldList.variables;
3495 expect(fields, hasLength(1));
3496 VariableDeclaration field = fields[0];
3497 expect(field.name.isSynthetic, isTrue);
3498 }
3499
3500 void test_incompleteField_final() {
3501 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3502 r'''
3503 class C {
3504 final
3505 }''',
3506 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.EXPECTED_TOKEN]);
3507 NodeList<CompilationUnitMember> declarations = unit.declarations;
3508 expect(declarations, hasLength(1));
3509 CompilationUnitMember unitMember = declarations[0];
3510 EngineTestCase.assertInstanceOf(
3511 (obj) => obj is ClassDeclaration, ClassDeclaration, unitMember);
3512 NodeList<ClassMember> members = (unitMember as ClassDeclaration).members;
3513 expect(members, hasLength(1));
3514 ClassMember classMember = members[0];
3515 EngineTestCase.assertInstanceOf(
3516 (obj) => obj is FieldDeclaration, FieldDeclaration, classMember);
3517 VariableDeclarationList fieldList =
3518 (classMember as FieldDeclaration).fields;
3519 expect((fieldList.keyword as KeywordToken).keyword, Keyword.FINAL);
3520 NodeList<VariableDeclaration> fields = fieldList.variables;
3521 expect(fields, hasLength(1));
3522 VariableDeclaration field = fields[0];
3523 expect(field.name.isSynthetic, isTrue);
3524 }
3525
3526 void test_incompleteField_var() {
3527 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3528 r'''
3529 class C {
3530 var
3531 }''',
3532 [ParserErrorCode.MISSING_IDENTIFIER, ParserErrorCode.EXPECTED_TOKEN]);
3533 NodeList<CompilationUnitMember> declarations = unit.declarations;
3534 expect(declarations, hasLength(1));
3535 CompilationUnitMember unitMember = declarations[0];
3536 EngineTestCase.assertInstanceOf(
3537 (obj) => obj is ClassDeclaration, ClassDeclaration, unitMember);
3538 NodeList<ClassMember> members = (unitMember as ClassDeclaration).members;
3539 expect(members, hasLength(1));
3540 ClassMember classMember = members[0];
3541 EngineTestCase.assertInstanceOf(
3542 (obj) => obj is FieldDeclaration, FieldDeclaration, classMember);
3543 VariableDeclarationList fieldList =
3544 (classMember as FieldDeclaration).fields;
3545 expect((fieldList.keyword as KeywordToken).keyword, Keyword.VAR);
3546 NodeList<VariableDeclaration> fields = fieldList.variables;
3547 expect(fields, hasLength(1));
3548 VariableDeclaration field = fields[0];
3549 expect(field.name.isSynthetic, isTrue);
3550 }
3551
3552 void test_invalidFunctionBodyModifier() {
3553 ParserTestCase.parseCompilationUnit(
3554 "f() sync {}", [ParserErrorCode.MISSING_STAR_AFTER_SYNC]);
3555 }
3556
3557 void test_isExpression_noType() {
3558 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3559 "class Bar<T extends Foo> {m(x){if (x is ) return;if (x is !)}}", [
3560 ParserErrorCode.EXPECTED_TYPE_NAME,
3561 ParserErrorCode.EXPECTED_TYPE_NAME,
3562 ParserErrorCode.MISSING_STATEMENT
3563 ]);
3564 ClassDeclaration declaration = unit.declarations[0] as ClassDeclaration;
3565 MethodDeclaration method = declaration.members[0] as MethodDeclaration;
3566 BlockFunctionBody body = method.body as BlockFunctionBody;
3567 IfStatement ifStatement = body.block.statements[1] as IfStatement;
3568 IsExpression expression = ifStatement.condition as IsExpression;
3569 expect(expression.expression, isNotNull);
3570 expect(expression.isOperator, isNotNull);
3571 expect(expression.notOperator, isNotNull);
3572 TypeName type = expression.type;
3573 expect(type, isNotNull);
3574 expect(type.name.isSynthetic, isTrue);
3575 EngineTestCase.assertInstanceOf((obj) => obj is EmptyStatement,
3576 EmptyStatement, ifStatement.thenStatement);
3577 }
3578
3579 void test_keywordInPlaceOfIdentifier() {
3580 // TODO(brianwilkerson) We could do better with this.
3581 ParserTestCase.parseCompilationUnit("do() {}", [
3582 ParserErrorCode.EXPECTED_EXECUTABLE,
3583 ParserErrorCode.UNEXPECTED_TOKEN
3584 ]);
3585 }
3586
3587 void test_logicalAndExpression_missing_LHS() {
3588 BinaryExpression expression =
3589 parseExpression("&& y", [ParserErrorCode.MISSING_IDENTIFIER]);
3590 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3591 SimpleIdentifier, expression.leftOperand);
3592 expect(expression.leftOperand.isSynthetic, isTrue);
3593 }
3594
3595 void test_logicalAndExpression_missing_LHS_RHS() {
3596 BinaryExpression expression = parseExpression("&&", [
3597 ParserErrorCode.MISSING_IDENTIFIER,
3598 ParserErrorCode.MISSING_IDENTIFIER
3599 ]);
3600 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3601 SimpleIdentifier, expression.leftOperand);
3602 expect(expression.leftOperand.isSynthetic, isTrue);
3603 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3604 SimpleIdentifier, expression.rightOperand);
3605 expect(expression.rightOperand.isSynthetic, isTrue);
3606 }
3607
3608 void test_logicalAndExpression_missing_RHS() {
3609 BinaryExpression expression =
3610 parseExpression("x &&", [ParserErrorCode.MISSING_IDENTIFIER]);
3611 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3612 SimpleIdentifier, expression.rightOperand);
3613 expect(expression.rightOperand.isSynthetic, isTrue);
3614 }
3615
3616 void test_logicalAndExpression_precedence_bitwiseOr_left() {
3617 BinaryExpression expression = parseExpression("| &&", [
3618 ParserErrorCode.MISSING_IDENTIFIER,
3619 ParserErrorCode.MISSING_IDENTIFIER,
3620 ParserErrorCode.MISSING_IDENTIFIER
3621 ]);
3622 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3623 BinaryExpression, expression.leftOperand);
3624 }
3625
3626 void test_logicalAndExpression_precedence_bitwiseOr_right() {
3627 BinaryExpression expression = parseExpression("&& |", [
3628 ParserErrorCode.MISSING_IDENTIFIER,
3629 ParserErrorCode.MISSING_IDENTIFIER,
3630 ParserErrorCode.MISSING_IDENTIFIER
3631 ]);
3632 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3633 BinaryExpression, expression.rightOperand);
3634 }
3635
3636 void test_logicalOrExpression_missing_LHS() {
3637 BinaryExpression expression =
3638 parseExpression("|| y", [ParserErrorCode.MISSING_IDENTIFIER]);
3639 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3640 SimpleIdentifier, expression.leftOperand);
3641 expect(expression.leftOperand.isSynthetic, isTrue);
3642 }
3643
3644 void test_logicalOrExpression_missing_LHS_RHS() {
3645 BinaryExpression expression = parseExpression("||", [
3646 ParserErrorCode.MISSING_IDENTIFIER,
3647 ParserErrorCode.MISSING_IDENTIFIER
3648 ]);
3649 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3650 SimpleIdentifier, expression.leftOperand);
3651 expect(expression.leftOperand.isSynthetic, isTrue);
3652 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3653 SimpleIdentifier, expression.rightOperand);
3654 expect(expression.rightOperand.isSynthetic, isTrue);
3655 }
3656
3657 void test_logicalOrExpression_missing_RHS() {
3658 BinaryExpression expression =
3659 parseExpression("x ||", [ParserErrorCode.MISSING_IDENTIFIER]);
3660 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3661 SimpleIdentifier, expression.rightOperand);
3662 expect(expression.rightOperand.isSynthetic, isTrue);
3663 }
3664
3665 void test_logicalOrExpression_precedence_logicalAnd_left() {
3666 BinaryExpression expression = parseExpression("&& ||", [
3667 ParserErrorCode.MISSING_IDENTIFIER,
3668 ParserErrorCode.MISSING_IDENTIFIER,
3669 ParserErrorCode.MISSING_IDENTIFIER
3670 ]);
3671 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3672 BinaryExpression, expression.leftOperand);
3673 }
3674
3675 void test_logicalOrExpression_precedence_logicalAnd_right() {
3676 BinaryExpression expression = parseExpression("|| &&", [
3677 ParserErrorCode.MISSING_IDENTIFIER,
3678 ParserErrorCode.MISSING_IDENTIFIER,
3679 ParserErrorCode.MISSING_IDENTIFIER
3680 ]);
3681 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3682 BinaryExpression, expression.rightOperand);
3683 }
3684
3685 void test_missing_commaInArgumentList() {
3686 parseExpression("f(x: 1 y: 2)", [ParserErrorCode.EXPECTED_TOKEN]);
3687 }
3688
3689 void test_missingGet() {
3690 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3691 r'''
3692 class C {
3693 int length {}
3694 void foo() {}
3695 }''',
3696 [ParserErrorCode.MISSING_GET]);
3697 expect(unit, isNotNull);
3698 ClassDeclaration classDeclaration =
3699 unit.declarations[0] as ClassDeclaration;
3700 NodeList<ClassMember> members = classDeclaration.members;
3701 expect(members, hasLength(2));
3702 EngineTestCase.assertInstanceOf(
3703 (obj) => obj is MethodDeclaration, MethodDeclaration, members[0]);
3704 ClassMember member = members[1];
3705 EngineTestCase.assertInstanceOf(
3706 (obj) => obj is MethodDeclaration, MethodDeclaration, member);
3707 expect((member as MethodDeclaration).name.name, "foo");
3708 }
3709
3710 void test_missingIdentifier_afterAnnotation() {
3711 MethodDeclaration method = parse3("parseClassMember", <Object>["C"],
3712 "@override }", [ParserErrorCode.EXPECTED_CLASS_MEMBER]);
3713 expect(method.documentationComment, isNull);
3714 NodeList<Annotation> metadata = method.metadata;
3715 expect(metadata, hasLength(1));
3716 expect(metadata[0].name.name, "override");
3717 }
3718
3719 void test_multiplicativeExpression_missing_LHS() {
3720 BinaryExpression expression =
3721 parseExpression("* y", [ParserErrorCode.MISSING_IDENTIFIER]);
3722 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3723 SimpleIdentifier, expression.leftOperand);
3724 expect(expression.leftOperand.isSynthetic, isTrue);
3725 }
3726
3727 void test_multiplicativeExpression_missing_LHS_RHS() {
3728 BinaryExpression expression = parseExpression("*", [
3729 ParserErrorCode.MISSING_IDENTIFIER,
3730 ParserErrorCode.MISSING_IDENTIFIER
3731 ]);
3732 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3733 SimpleIdentifier, expression.leftOperand);
3734 expect(expression.leftOperand.isSynthetic, isTrue);
3735 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3736 SimpleIdentifier, expression.rightOperand);
3737 expect(expression.rightOperand.isSynthetic, isTrue);
3738 }
3739
3740 void test_multiplicativeExpression_missing_RHS() {
3741 BinaryExpression expression =
3742 parseExpression("x *", [ParserErrorCode.MISSING_IDENTIFIER]);
3743 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3744 SimpleIdentifier, expression.rightOperand);
3745 expect(expression.rightOperand.isSynthetic, isTrue);
3746 }
3747
3748 void test_multiplicativeExpression_missing_RHS_super() {
3749 BinaryExpression expression =
3750 parseExpression("super *", [ParserErrorCode.MISSING_IDENTIFIER]);
3751 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3752 SimpleIdentifier, expression.rightOperand);
3753 expect(expression.rightOperand.isSynthetic, isTrue);
3754 }
3755
3756 void test_multiplicativeExpression_precedence_unary_left() {
3757 BinaryExpression expression =
3758 parseExpression("-x *", [ParserErrorCode.MISSING_IDENTIFIER]);
3759 EngineTestCase.assertInstanceOf((obj) => obj is PrefixExpression,
3760 PrefixExpression, expression.leftOperand);
3761 }
3762
3763 void test_multiplicativeExpression_precedence_unary_right() {
3764 BinaryExpression expression =
3765 parseExpression("* -y", [ParserErrorCode.MISSING_IDENTIFIER]);
3766 EngineTestCase.assertInstanceOf((obj) => obj is PrefixExpression,
3767 PrefixExpression, expression.rightOperand);
3768 }
3769
3770 void test_multiplicativeExpression_super() {
3771 BinaryExpression expression = parseExpression("super == ==", [
3772 ParserErrorCode.MISSING_IDENTIFIER,
3773 ParserErrorCode.MISSING_IDENTIFIER,
3774 ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND
3775 ]);
3776 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3777 BinaryExpression, expression.leftOperand);
3778 }
3779
3780 void test_nonStringLiteralUri_import() {
3781 ParserTestCase.parseCompilationUnit("import dart:io; class C {}",
3782 [ParserErrorCode.NON_STRING_LITERAL_AS_URI]);
3783 }
3784
3785 void test_prefixExpression_missing_operand_minus() {
3786 PrefixExpression expression =
3787 parseExpression("-", [ParserErrorCode.MISSING_IDENTIFIER]);
3788 EngineTestCase.assertInstanceOf(
3789 (obj) => obj is SimpleIdentifier, SimpleIdentifier, expression.operand);
3790 expect(expression.operand.isSynthetic, isTrue);
3791 expect(expression.operator.type, TokenType.MINUS);
3792 }
3793
3794 void test_primaryExpression_argumentDefinitionTest() {
3795 Expression expression = parse4(
3796 "parsePrimaryExpression", "?a", [ParserErrorCode.UNEXPECTED_TOKEN]);
3797 EngineTestCase.assertInstanceOf(
3798 (obj) => obj is SimpleIdentifier, SimpleIdentifier, expression);
3799 }
3800
3801 void test_relationalExpression_missing_LHS() {
3802 IsExpression expression =
3803 parseExpression("is y", [ParserErrorCode.MISSING_IDENTIFIER]);
3804 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3805 SimpleIdentifier, expression.expression);
3806 expect(expression.expression.isSynthetic, isTrue);
3807 }
3808
3809 void test_relationalExpression_missing_LHS_RHS() {
3810 IsExpression expression = parseExpression("is", [
3811 ParserErrorCode.EXPECTED_TYPE_NAME,
3812 ParserErrorCode.MISSING_IDENTIFIER
3813 ]);
3814 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3815 SimpleIdentifier, expression.expression);
3816 expect(expression.expression.isSynthetic, isTrue);
3817 EngineTestCase.assertInstanceOf(
3818 (obj) => obj is TypeName, TypeName, expression.type);
3819 expect(expression.type.isSynthetic, isTrue);
3820 }
3821
3822 void test_relationalExpression_missing_RHS() {
3823 IsExpression expression =
3824 parseExpression("x is", [ParserErrorCode.EXPECTED_TYPE_NAME]);
3825 EngineTestCase.assertInstanceOf(
3826 (obj) => obj is TypeName, TypeName, expression.type);
3827 expect(expression.type.isSynthetic, isTrue);
3828 }
3829
3830 void test_relationalExpression_precedence_shift_right() {
3831 IsExpression expression = parseExpression("<< is", [
3832 ParserErrorCode.EXPECTED_TYPE_NAME,
3833 ParserErrorCode.MISSING_IDENTIFIER,
3834 ParserErrorCode.MISSING_IDENTIFIER
3835 ]);
3836 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3837 BinaryExpression, expression.expression);
3838 }
3839
3840 void test_shiftExpression_missing_LHS() {
3841 BinaryExpression expression =
3842 parseExpression("<< y", [ParserErrorCode.MISSING_IDENTIFIER]);
3843 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3844 SimpleIdentifier, expression.leftOperand);
3845 expect(expression.leftOperand.isSynthetic, isTrue);
3846 }
3847
3848 void test_shiftExpression_missing_LHS_RHS() {
3849 BinaryExpression expression = parseExpression("<<", [
3850 ParserErrorCode.MISSING_IDENTIFIER,
3851 ParserErrorCode.MISSING_IDENTIFIER
3852 ]);
3853 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3854 SimpleIdentifier, expression.leftOperand);
3855 expect(expression.leftOperand.isSynthetic, isTrue);
3856 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3857 SimpleIdentifier, expression.rightOperand);
3858 expect(expression.rightOperand.isSynthetic, isTrue);
3859 }
3860
3861 void test_shiftExpression_missing_RHS() {
3862 BinaryExpression expression =
3863 parseExpression("x <<", [ParserErrorCode.MISSING_IDENTIFIER]);
3864 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3865 SimpleIdentifier, expression.rightOperand);
3866 expect(expression.rightOperand.isSynthetic, isTrue);
3867 }
3868
3869 void test_shiftExpression_missing_RHS_super() {
3870 BinaryExpression expression =
3871 parseExpression("super <<", [ParserErrorCode.MISSING_IDENTIFIER]);
3872 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3873 SimpleIdentifier, expression.rightOperand);
3874 expect(expression.rightOperand.isSynthetic, isTrue);
3875 }
3876
3877 void test_shiftExpression_precedence_unary_left() {
3878 BinaryExpression expression = parseExpression("+ <<", [
3879 ParserErrorCode.MISSING_IDENTIFIER,
3880 ParserErrorCode.MISSING_IDENTIFIER,
3881 ParserErrorCode.MISSING_IDENTIFIER
3882 ]);
3883 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3884 BinaryExpression, expression.leftOperand);
3885 }
3886
3887 void test_shiftExpression_precedence_unary_right() {
3888 BinaryExpression expression = parseExpression("<< +", [
3889 ParserErrorCode.MISSING_IDENTIFIER,
3890 ParserErrorCode.MISSING_IDENTIFIER,
3891 ParserErrorCode.MISSING_IDENTIFIER
3892 ]);
3893 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3894 BinaryExpression, expression.rightOperand);
3895 }
3896
3897 void test_shiftExpression_super() {
3898 BinaryExpression expression = parseExpression("super << <<", [
3899 ParserErrorCode.MISSING_IDENTIFIER,
3900 ParserErrorCode.MISSING_IDENTIFIER
3901 ]);
3902 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
3903 BinaryExpression, expression.leftOperand);
3904 }
3905
3906 void test_typedef_eof() {
3907 CompilationUnit unit = ParserTestCase.parseCompilationUnit("typedef n", [
3908 ParserErrorCode.EXPECTED_TOKEN,
3909 ParserErrorCode.MISSING_TYPEDEF_PARAMETERS
3910 ]);
3911 NodeList<CompilationUnitMember> declarations = unit.declarations;
3912 expect(declarations, hasLength(1));
3913 CompilationUnitMember member = declarations[0];
3914 EngineTestCase.assertInstanceOf(
3915 (obj) => obj is FunctionTypeAlias, FunctionTypeAlias, member);
3916 }
3917
3918 void test_unaryPlus() {
3919 parseExpression("+2", [ParserErrorCode.MISSING_IDENTIFIER]);
3920 }
3921 }
3922
3923 @reflectiveTest
3924 class ResolutionCopierTest extends EngineTestCase {
3925 void test_visitAnnotation() {
3926 String annotationName = "proxy";
3927 Annotation fromNode =
3928 AstFactory.annotation(AstFactory.identifier3(annotationName));
3929 Element element = ElementFactory.topLevelVariableElement2(annotationName);
3930 fromNode.element = element;
3931 Annotation toNode =
3932 AstFactory.annotation(AstFactory.identifier3(annotationName));
3933 ResolutionCopier.copyResolutionData(fromNode, toNode);
3934 expect(toNode.element, same(element));
3935 }
3936
3937 void test_visitAsExpression() {
3938 AsExpression fromNode = AstFactory.asExpression(
3939 AstFactory.identifier3("x"), AstFactory.typeName4("A"));
3940 DartType propagatedType = ElementFactory.classElement2("A").type;
3941 fromNode.propagatedType = propagatedType;
3942 DartType staticType = ElementFactory.classElement2("B").type;
3943 fromNode.staticType = staticType;
3944 AsExpression toNode = AstFactory.asExpression(
3945 AstFactory.identifier3("x"), AstFactory.typeName4("A"));
3946 ResolutionCopier.copyResolutionData(fromNode, toNode);
3947 expect(toNode.propagatedType, same(propagatedType));
3948 expect(toNode.staticType, same(staticType));
3949 }
3950
3951 void test_visitAssignmentExpression() {
3952 AssignmentExpression fromNode = AstFactory.assignmentExpression(
3953 AstFactory.identifier3("a"),
3954 TokenType.PLUS_EQ,
3955 AstFactory.identifier3("b"));
3956 DartType propagatedType = ElementFactory.classElement2("C").type;
3957 MethodElement propagatedElement =
3958 ElementFactory.methodElement("+", propagatedType);
3959 fromNode.propagatedElement = propagatedElement;
3960 fromNode.propagatedType = propagatedType;
3961 DartType staticType = ElementFactory.classElement2("C").type;
3962 MethodElement staticElement = ElementFactory.methodElement("+", staticType);
3963 fromNode.staticElement = staticElement;
3964 fromNode.staticType = staticType;
3965 AssignmentExpression toNode = AstFactory.assignmentExpression(
3966 AstFactory.identifier3("a"),
3967 TokenType.PLUS_EQ,
3968 AstFactory.identifier3("b"));
3969 ResolutionCopier.copyResolutionData(fromNode, toNode);
3970 expect(toNode.propagatedElement, same(propagatedElement));
3971 expect(toNode.propagatedType, same(propagatedType));
3972 expect(toNode.staticElement, same(staticElement));
3973 expect(toNode.staticType, same(staticType));
3974 }
3975
3976 void test_visitBinaryExpression() {
3977 BinaryExpression fromNode = AstFactory.binaryExpression(
3978 AstFactory.identifier3("a"),
3979 TokenType.PLUS,
3980 AstFactory.identifier3("b"));
3981 DartType propagatedType = ElementFactory.classElement2("C").type;
3982 MethodElement propagatedElement =
3983 ElementFactory.methodElement("+", propagatedType);
3984 fromNode.propagatedElement = propagatedElement;
3985 fromNode.propagatedType = propagatedType;
3986 DartType staticType = ElementFactory.classElement2("C").type;
3987 MethodElement staticElement = ElementFactory.methodElement("+", staticType);
3988 fromNode.staticElement = staticElement;
3989 fromNode.staticType = staticType;
3990 BinaryExpression toNode = AstFactory.binaryExpression(
3991 AstFactory.identifier3("a"),
3992 TokenType.PLUS,
3993 AstFactory.identifier3("b"));
3994 ResolutionCopier.copyResolutionData(fromNode, toNode);
3995 expect(toNode.propagatedElement, same(propagatedElement));
3996 expect(toNode.propagatedType, same(propagatedType));
3997 expect(toNode.staticElement, same(staticElement));
3998 expect(toNode.staticType, same(staticType));
3999 }
4000
4001 void test_visitBooleanLiteral() {
4002 BooleanLiteral fromNode = AstFactory.booleanLiteral(true);
4003 DartType propagatedType = ElementFactory.classElement2("C").type;
4004 fromNode.propagatedType = propagatedType;
4005 DartType staticType = ElementFactory.classElement2("C").type;
4006 fromNode.staticType = staticType;
4007 BooleanLiteral toNode = AstFactory.booleanLiteral(true);
4008 ResolutionCopier.copyResolutionData(fromNode, toNode);
4009 expect(toNode.propagatedType, same(propagatedType));
4010 expect(toNode.staticType, same(staticType));
4011 }
4012
4013 void test_visitCascadeExpression() {
4014 CascadeExpression fromNode = AstFactory.cascadeExpression(
4015 AstFactory.identifier3("a"), [AstFactory.identifier3("b")]);
4016 DartType propagatedType = ElementFactory.classElement2("C").type;
4017 fromNode.propagatedType = propagatedType;
4018 DartType staticType = ElementFactory.classElement2("C").type;
4019 fromNode.staticType = staticType;
4020 CascadeExpression toNode = AstFactory.cascadeExpression(
4021 AstFactory.identifier3("a"), [AstFactory.identifier3("b")]);
4022 ResolutionCopier.copyResolutionData(fromNode, toNode);
4023 expect(toNode.propagatedType, same(propagatedType));
4024 expect(toNode.staticType, same(staticType));
4025 }
4026
4027 void test_visitCompilationUnit() {
4028 CompilationUnit fromNode = AstFactory.compilationUnit();
4029 CompilationUnitElement element =
4030 new CompilationUnitElementImpl("test.dart");
4031 fromNode.element = element;
4032 CompilationUnit toNode = AstFactory.compilationUnit();
4033 ResolutionCopier.copyResolutionData(fromNode, toNode);
4034 expect(toNode.element, same(element));
4035 }
4036
4037 void test_visitConditionalExpression() {
4038 ConditionalExpression fromNode = AstFactory.conditionalExpression(
4039 AstFactory.identifier3("c"),
4040 AstFactory.identifier3("a"),
4041 AstFactory.identifier3("b"));
4042 DartType propagatedType = ElementFactory.classElement2("C").type;
4043 fromNode.propagatedType = propagatedType;
4044 DartType staticType = ElementFactory.classElement2("C").type;
4045 fromNode.staticType = staticType;
4046 ConditionalExpression toNode = AstFactory.conditionalExpression(
4047 AstFactory.identifier3("c"),
4048 AstFactory.identifier3("a"),
4049 AstFactory.identifier3("b"));
4050 ResolutionCopier.copyResolutionData(fromNode, toNode);
4051 expect(toNode.propagatedType, same(propagatedType));
4052 expect(toNode.staticType, same(staticType));
4053 }
4054
4055 void test_visitConstructorDeclaration() {
4056 String className = "A";
4057 String constructorName = "c";
4058 ConstructorDeclaration fromNode = AstFactory.constructorDeclaration(
4059 AstFactory.identifier3(className),
4060 constructorName,
4061 AstFactory.formalParameterList(),
4062 null);
4063 ConstructorElement element = ElementFactory.constructorElement2(
4064 ElementFactory.classElement2(className), constructorName);
4065 fromNode.element = element;
4066 ConstructorDeclaration toNode = AstFactory.constructorDeclaration(
4067 AstFactory.identifier3(className),
4068 constructorName,
4069 AstFactory.formalParameterList(),
4070 null);
4071 ResolutionCopier.copyResolutionData(fromNode, toNode);
4072 expect(toNode.element, same(element));
4073 }
4074
4075 void test_visitConstructorName() {
4076 ConstructorName fromNode =
4077 AstFactory.constructorName(AstFactory.typeName4("A"), "c");
4078 ConstructorElement staticElement = ElementFactory.constructorElement2(
4079 ElementFactory.classElement2("A"), "c");
4080 fromNode.staticElement = staticElement;
4081 ConstructorName toNode =
4082 AstFactory.constructorName(AstFactory.typeName4("A"), "c");
4083 ResolutionCopier.copyResolutionData(fromNode, toNode);
4084 expect(toNode.staticElement, same(staticElement));
4085 }
4086
4087 void test_visitDoubleLiteral() {
4088 DoubleLiteral fromNode = AstFactory.doubleLiteral(1.0);
4089 DartType propagatedType = ElementFactory.classElement2("C").type;
4090 fromNode.propagatedType = propagatedType;
4091 DartType staticType = ElementFactory.classElement2("C").type;
4092 fromNode.staticType = staticType;
4093 DoubleLiteral toNode = AstFactory.doubleLiteral(1.0);
4094 ResolutionCopier.copyResolutionData(fromNode, toNode);
4095 expect(toNode.propagatedType, same(propagatedType));
4096 expect(toNode.staticType, same(staticType));
4097 }
4098
4099 void test_visitExportDirective() {
4100 ExportDirective fromNode = AstFactory.exportDirective2("dart:uri");
4101 ExportElement element = new ExportElementImpl(-1);
4102 fromNode.element = element;
4103 ExportDirective toNode = AstFactory.exportDirective2("dart:uri");
4104 ResolutionCopier.copyResolutionData(fromNode, toNode);
4105 expect(toNode.element, same(element));
4106 }
4107
4108 void test_visitFunctionExpression() {
4109 FunctionExpression fromNode = AstFactory.functionExpression2(
4110 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody());
4111 MethodElement element = ElementFactory.methodElement(
4112 "m", ElementFactory.classElement2("C").type);
4113 fromNode.element = element;
4114 DartType propagatedType = ElementFactory.classElement2("C").type;
4115 fromNode.propagatedType = propagatedType;
4116 DartType staticType = ElementFactory.classElement2("C").type;
4117 fromNode.staticType = staticType;
4118 FunctionExpression toNode = AstFactory.functionExpression2(
4119 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody());
4120 ResolutionCopier.copyResolutionData(fromNode, toNode);
4121 expect(toNode.element, same(element));
4122 expect(toNode.propagatedType, same(propagatedType));
4123 expect(toNode.staticType, same(staticType));
4124 }
4125
4126 void test_visitFunctionExpressionInvocation() {
4127 FunctionExpressionInvocation fromNode =
4128 AstFactory.functionExpressionInvocation(AstFactory.identifier3("f"));
4129 MethodElement propagatedElement = ElementFactory.methodElement(
4130 "m", ElementFactory.classElement2("C").type);
4131 fromNode.propagatedElement = propagatedElement;
4132 DartType propagatedType = ElementFactory.classElement2("C").type;
4133 fromNode.propagatedType = propagatedType;
4134 MethodElement staticElement = ElementFactory.methodElement(
4135 "m", ElementFactory.classElement2("C").type);
4136 fromNode.staticElement = staticElement;
4137 DartType staticType = ElementFactory.classElement2("C").type;
4138 fromNode.staticType = staticType;
4139 FunctionExpressionInvocation toNode =
4140 AstFactory.functionExpressionInvocation(AstFactory.identifier3("f"));
4141 ResolutionCopier.copyResolutionData(fromNode, toNode);
4142 expect(toNode.propagatedElement, same(propagatedElement));
4143 expect(toNode.propagatedType, same(propagatedType));
4144 expect(toNode.staticElement, same(staticElement));
4145 expect(toNode.staticType, same(staticType));
4146 }
4147
4148 void test_visitImportDirective() {
4149 ImportDirective fromNode = AstFactory.importDirective3("dart:uri", null);
4150 ImportElement element = new ImportElementImpl(0);
4151 fromNode.element = element;
4152 ImportDirective toNode = AstFactory.importDirective3("dart:uri", null);
4153 ResolutionCopier.copyResolutionData(fromNode, toNode);
4154 expect(toNode.element, same(element));
4155 }
4156
4157 void test_visitIndexExpression() {
4158 IndexExpression fromNode = AstFactory.indexExpression(
4159 AstFactory.identifier3("a"), AstFactory.integer(0));
4160 MethodElement propagatedElement = ElementFactory.methodElement(
4161 "m", ElementFactory.classElement2("C").type);
4162 MethodElement staticElement = ElementFactory.methodElement(
4163 "m", ElementFactory.classElement2("C").type);
4164 AuxiliaryElements auxiliaryElements =
4165 new AuxiliaryElements(staticElement, propagatedElement);
4166 fromNode.auxiliaryElements = auxiliaryElements;
4167 fromNode.propagatedElement = propagatedElement;
4168 DartType propagatedType = ElementFactory.classElement2("C").type;
4169 fromNode.propagatedType = propagatedType;
4170 fromNode.staticElement = staticElement;
4171 DartType staticType = ElementFactory.classElement2("C").type;
4172 fromNode.staticType = staticType;
4173 IndexExpression toNode = AstFactory.indexExpression(
4174 AstFactory.identifier3("a"), AstFactory.integer(0));
4175 ResolutionCopier.copyResolutionData(fromNode, toNode);
4176 expect(toNode.auxiliaryElements, same(auxiliaryElements));
4177 expect(toNode.propagatedElement, same(propagatedElement));
4178 expect(toNode.propagatedType, same(propagatedType));
4179 expect(toNode.staticElement, same(staticElement));
4180 expect(toNode.staticType, same(staticType));
4181 }
4182
4183 void test_visitInstanceCreationExpression() {
4184 InstanceCreationExpression fromNode = AstFactory
4185 .instanceCreationExpression2(Keyword.NEW, AstFactory.typeName4("C"));
4186 DartType propagatedType = ElementFactory.classElement2("C").type;
4187 fromNode.propagatedType = propagatedType;
4188 ConstructorElement staticElement = ElementFactory.constructorElement2(
4189 ElementFactory.classElement2("C"), null);
4190 fromNode.staticElement = staticElement;
4191 DartType staticType = ElementFactory.classElement2("C").type;
4192 fromNode.staticType = staticType;
4193 InstanceCreationExpression toNode = AstFactory.instanceCreationExpression2(
4194 Keyword.NEW, AstFactory.typeName4("C"));
4195 ResolutionCopier.copyResolutionData(fromNode, toNode);
4196 expect(toNode.propagatedType, same(propagatedType));
4197 expect(toNode.staticElement, same(staticElement));
4198 expect(toNode.staticType, same(staticType));
4199 }
4200
4201 void test_visitIntegerLiteral() {
4202 IntegerLiteral fromNode = AstFactory.integer(2);
4203 DartType propagatedType = ElementFactory.classElement2("C").type;
4204 fromNode.propagatedType = propagatedType;
4205 DartType staticType = ElementFactory.classElement2("C").type;
4206 fromNode.staticType = staticType;
4207 IntegerLiteral toNode = AstFactory.integer(2);
4208 ResolutionCopier.copyResolutionData(fromNode, toNode);
4209 expect(toNode.propagatedType, same(propagatedType));
4210 expect(toNode.staticType, same(staticType));
4211 }
4212
4213 void test_visitIsExpression() {
4214 IsExpression fromNode = AstFactory.isExpression(
4215 AstFactory.identifier3("x"), false, AstFactory.typeName4("A"));
4216 DartType propagatedType = ElementFactory.classElement2("C").type;
4217 fromNode.propagatedType = propagatedType;
4218 DartType staticType = ElementFactory.classElement2("C").type;
4219 fromNode.staticType = staticType;
4220 IsExpression toNode = AstFactory.isExpression(
4221 AstFactory.identifier3("x"), false, AstFactory.typeName4("A"));
4222 ResolutionCopier.copyResolutionData(fromNode, toNode);
4223 expect(toNode.propagatedType, same(propagatedType));
4224 expect(toNode.staticType, same(staticType));
4225 }
4226
4227 void test_visitLibraryIdentifier() {
4228 LibraryIdentifier fromNode =
4229 AstFactory.libraryIdentifier([AstFactory.identifier3("lib")]);
4230 DartType propagatedType = ElementFactory.classElement2("C").type;
4231 fromNode.propagatedType = propagatedType;
4232 DartType staticType = ElementFactory.classElement2("C").type;
4233 fromNode.staticType = staticType;
4234 LibraryIdentifier toNode =
4235 AstFactory.libraryIdentifier([AstFactory.identifier3("lib")]);
4236 ResolutionCopier.copyResolutionData(fromNode, toNode);
4237 expect(toNode.propagatedType, same(propagatedType));
4238 expect(toNode.staticType, same(staticType));
4239 }
4240
4241 void test_visitListLiteral() {
4242 ListLiteral fromNode = AstFactory.listLiteral();
4243 DartType propagatedType = ElementFactory.classElement2("C").type;
4244 fromNode.propagatedType = propagatedType;
4245 DartType staticType = ElementFactory.classElement2("C").type;
4246 fromNode.staticType = staticType;
4247 ListLiteral toNode = AstFactory.listLiteral();
4248 ResolutionCopier.copyResolutionData(fromNode, toNode);
4249 expect(toNode.propagatedType, same(propagatedType));
4250 expect(toNode.staticType, same(staticType));
4251 }
4252
4253 void test_visitMapLiteral() {
4254 MapLiteral fromNode = AstFactory.mapLiteral2();
4255 DartType propagatedType = ElementFactory.classElement2("C").type;
4256 fromNode.propagatedType = propagatedType;
4257 DartType staticType = ElementFactory.classElement2("C").type;
4258 fromNode.staticType = staticType;
4259 MapLiteral toNode = AstFactory.mapLiteral2();
4260 ResolutionCopier.copyResolutionData(fromNode, toNode);
4261 expect(toNode.propagatedType, same(propagatedType));
4262 expect(toNode.staticType, same(staticType));
4263 }
4264
4265 void test_visitMethodInvocation() {
4266 MethodInvocation fromNode = AstFactory.methodInvocation2("m");
4267 DartType propagatedType = ElementFactory.classElement2("C").type;
4268 fromNode.propagatedType = propagatedType;
4269 DartType staticType = ElementFactory.classElement2("C").type;
4270 fromNode.staticType = staticType;
4271 MethodInvocation toNode = AstFactory.methodInvocation2("m");
4272 ResolutionCopier.copyResolutionData(fromNode, toNode);
4273 expect(toNode.propagatedType, same(propagatedType));
4274 expect(toNode.staticType, same(staticType));
4275 }
4276
4277 void test_visitNamedExpression() {
4278 NamedExpression fromNode =
4279 AstFactory.namedExpression2("n", AstFactory.integer(0));
4280 DartType propagatedType = ElementFactory.classElement2("C").type;
4281 fromNode.propagatedType = propagatedType;
4282 DartType staticType = ElementFactory.classElement2("C").type;
4283 fromNode.staticType = staticType;
4284 NamedExpression toNode =
4285 AstFactory.namedExpression2("n", AstFactory.integer(0));
4286 ResolutionCopier.copyResolutionData(fromNode, toNode);
4287 expect(toNode.propagatedType, same(propagatedType));
4288 expect(toNode.staticType, same(staticType));
4289 }
4290
4291 void test_visitNullLiteral() {
4292 NullLiteral fromNode = AstFactory.nullLiteral();
4293 DartType propagatedType = ElementFactory.classElement2("C").type;
4294 fromNode.propagatedType = propagatedType;
4295 DartType staticType = ElementFactory.classElement2("C").type;
4296 fromNode.staticType = staticType;
4297 NullLiteral toNode = AstFactory.nullLiteral();
4298 ResolutionCopier.copyResolutionData(fromNode, toNode);
4299 expect(toNode.propagatedType, same(propagatedType));
4300 expect(toNode.staticType, same(staticType));
4301 }
4302
4303 void test_visitParenthesizedExpression() {
4304 ParenthesizedExpression fromNode =
4305 AstFactory.parenthesizedExpression(AstFactory.integer(0));
4306 DartType propagatedType = ElementFactory.classElement2("C").type;
4307 fromNode.propagatedType = propagatedType;
4308 DartType staticType = ElementFactory.classElement2("C").type;
4309 fromNode.staticType = staticType;
4310 ParenthesizedExpression toNode =
4311 AstFactory.parenthesizedExpression(AstFactory.integer(0));
4312 ResolutionCopier.copyResolutionData(fromNode, toNode);
4313 expect(toNode.propagatedType, same(propagatedType));
4314 expect(toNode.staticType, same(staticType));
4315 }
4316
4317 void test_visitPartDirective() {
4318 PartDirective fromNode = AstFactory.partDirective2("part.dart");
4319 LibraryElement element = new LibraryElementImpl.forNode(
4320 null, AstFactory.libraryIdentifier2(["lib"]));
4321 fromNode.element = element;
4322 PartDirective toNode = AstFactory.partDirective2("part.dart");
4323 ResolutionCopier.copyResolutionData(fromNode, toNode);
4324 expect(toNode.element, same(element));
4325 }
4326
4327 void test_visitPartOfDirective() {
4328 PartOfDirective fromNode =
4329 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["lib"]));
4330 LibraryElement element = new LibraryElementImpl.forNode(
4331 null, AstFactory.libraryIdentifier2(["lib"]));
4332 fromNode.element = element;
4333 PartOfDirective toNode =
4334 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["lib"]));
4335 ResolutionCopier.copyResolutionData(fromNode, toNode);
4336 expect(toNode.element, same(element));
4337 }
4338
4339 void test_visitPostfixExpression() {
4340 String variableName = "x";
4341 PostfixExpression fromNode = AstFactory.postfixExpression(
4342 AstFactory.identifier3(variableName), TokenType.PLUS_PLUS);
4343 MethodElement propagatedElement = ElementFactory.methodElement(
4344 "+", ElementFactory.classElement2("C").type);
4345 fromNode.propagatedElement = propagatedElement;
4346 DartType propagatedType = ElementFactory.classElement2("C").type;
4347 fromNode.propagatedType = propagatedType;
4348 MethodElement staticElement = ElementFactory.methodElement(
4349 "+", ElementFactory.classElement2("C").type);
4350 fromNode.staticElement = staticElement;
4351 DartType staticType = ElementFactory.classElement2("C").type;
4352 fromNode.staticType = staticType;
4353 PostfixExpression toNode = AstFactory.postfixExpression(
4354 AstFactory.identifier3(variableName), TokenType.PLUS_PLUS);
4355 ResolutionCopier.copyResolutionData(fromNode, toNode);
4356 expect(toNode.propagatedElement, same(propagatedElement));
4357 expect(toNode.propagatedType, same(propagatedType));
4358 expect(toNode.staticElement, same(staticElement));
4359 expect(toNode.staticType, same(staticType));
4360 }
4361
4362 void test_visitPrefixedIdentifier() {
4363 PrefixedIdentifier fromNode = AstFactory.identifier5("p", "f");
4364 DartType propagatedType = ElementFactory.classElement2("C").type;
4365 fromNode.propagatedType = propagatedType;
4366 DartType staticType = ElementFactory.classElement2("C").type;
4367 fromNode.staticType = staticType;
4368 PrefixedIdentifier toNode = AstFactory.identifier5("p", "f");
4369 ResolutionCopier.copyResolutionData(fromNode, toNode);
4370 expect(toNode.propagatedType, same(propagatedType));
4371 expect(toNode.staticType, same(staticType));
4372 }
4373
4374 void test_visitPrefixExpression() {
4375 PrefixExpression fromNode = AstFactory.prefixExpression(
4376 TokenType.PLUS_PLUS, AstFactory.identifier3("x"));
4377 MethodElement propagatedElement = ElementFactory.methodElement(
4378 "+", ElementFactory.classElement2("C").type);
4379 DartType propagatedType = ElementFactory.classElement2("C").type;
4380 fromNode.propagatedElement = propagatedElement;
4381 fromNode.propagatedType = propagatedType;
4382 DartType staticType = ElementFactory.classElement2("C").type;
4383 MethodElement staticElement = ElementFactory.methodElement(
4384 "+", ElementFactory.classElement2("C").type);
4385 fromNode.staticElement = staticElement;
4386 fromNode.staticType = staticType;
4387 PrefixExpression toNode = AstFactory.prefixExpression(
4388 TokenType.PLUS_PLUS, AstFactory.identifier3("x"));
4389 ResolutionCopier.copyResolutionData(fromNode, toNode);
4390 expect(toNode.propagatedElement, same(propagatedElement));
4391 expect(toNode.propagatedType, same(propagatedType));
4392 expect(toNode.staticElement, same(staticElement));
4393 expect(toNode.staticType, same(staticType));
4394 }
4395
4396 void test_visitPropertyAccess() {
4397 PropertyAccess fromNode =
4398 AstFactory.propertyAccess2(AstFactory.identifier3("x"), "y");
4399 DartType propagatedType = ElementFactory.classElement2("C").type;
4400 fromNode.propagatedType = propagatedType;
4401 DartType staticType = ElementFactory.classElement2("C").type;
4402 fromNode.staticType = staticType;
4403 PropertyAccess toNode =
4404 AstFactory.propertyAccess2(AstFactory.identifier3("x"), "y");
4405 ResolutionCopier.copyResolutionData(fromNode, toNode);
4406 expect(toNode.propagatedType, same(propagatedType));
4407 expect(toNode.staticType, same(staticType));
4408 }
4409
4410 void test_visitRedirectingConstructorInvocation() {
4411 RedirectingConstructorInvocation fromNode =
4412 AstFactory.redirectingConstructorInvocation();
4413 ConstructorElement staticElement = ElementFactory.constructorElement2(
4414 ElementFactory.classElement2("C"), null);
4415 fromNode.staticElement = staticElement;
4416 RedirectingConstructorInvocation toNode =
4417 AstFactory.redirectingConstructorInvocation();
4418 ResolutionCopier.copyResolutionData(fromNode, toNode);
4419 expect(toNode.staticElement, same(staticElement));
4420 }
4421
4422 void test_visitRethrowExpression() {
4423 RethrowExpression fromNode = AstFactory.rethrowExpression();
4424 DartType propagatedType = ElementFactory.classElement2("C").type;
4425 fromNode.propagatedType = propagatedType;
4426 DartType staticType = ElementFactory.classElement2("C").type;
4427 fromNode.staticType = staticType;
4428 RethrowExpression toNode = AstFactory.rethrowExpression();
4429 ResolutionCopier.copyResolutionData(fromNode, toNode);
4430 expect(toNode.propagatedType, same(propagatedType));
4431 expect(toNode.staticType, same(staticType));
4432 }
4433
4434 void test_visitSimpleIdentifier() {
4435 SimpleIdentifier fromNode = AstFactory.identifier3("x");
4436 MethodElement propagatedElement = ElementFactory.methodElement(
4437 "m", ElementFactory.classElement2("C").type);
4438 MethodElement staticElement = ElementFactory.methodElement(
4439 "m", ElementFactory.classElement2("C").type);
4440 AuxiliaryElements auxiliaryElements =
4441 new AuxiliaryElements(staticElement, propagatedElement);
4442 fromNode.auxiliaryElements = auxiliaryElements;
4443 fromNode.propagatedElement = propagatedElement;
4444 DartType propagatedType = ElementFactory.classElement2("C").type;
4445 fromNode.propagatedType = propagatedType;
4446 fromNode.staticElement = staticElement;
4447 DartType staticType = ElementFactory.classElement2("C").type;
4448 fromNode.staticType = staticType;
4449 SimpleIdentifier toNode = AstFactory.identifier3("x");
4450 ResolutionCopier.copyResolutionData(fromNode, toNode);
4451 expect(toNode.auxiliaryElements, same(auxiliaryElements));
4452 expect(toNode.propagatedElement, same(propagatedElement));
4453 expect(toNode.propagatedType, same(propagatedType));
4454 expect(toNode.staticElement, same(staticElement));
4455 expect(toNode.staticType, same(staticType));
4456 }
4457
4458 void test_visitSimpleStringLiteral() {
4459 SimpleStringLiteral fromNode = AstFactory.string2("abc");
4460 DartType propagatedType = ElementFactory.classElement2("C").type;
4461 fromNode.propagatedType = propagatedType;
4462 DartType staticType = ElementFactory.classElement2("C").type;
4463 fromNode.staticType = staticType;
4464 SimpleStringLiteral toNode = AstFactory.string2("abc");
4465 ResolutionCopier.copyResolutionData(fromNode, toNode);
4466 expect(toNode.propagatedType, same(propagatedType));
4467 expect(toNode.staticType, same(staticType));
4468 }
4469
4470 void test_visitStringInterpolation() {
4471 StringInterpolation fromNode =
4472 AstFactory.string([AstFactory.interpolationString("a", "'a'")]);
4473 DartType propagatedType = ElementFactory.classElement2("C").type;
4474 fromNode.propagatedType = propagatedType;
4475 DartType staticType = ElementFactory.classElement2("C").type;
4476 fromNode.staticType = staticType;
4477 StringInterpolation toNode =
4478 AstFactory.string([AstFactory.interpolationString("a", "'a'")]);
4479 ResolutionCopier.copyResolutionData(fromNode, toNode);
4480 expect(toNode.propagatedType, same(propagatedType));
4481 expect(toNode.staticType, same(staticType));
4482 }
4483
4484 void test_visitSuperConstructorInvocation() {
4485 SuperConstructorInvocation fromNode =
4486 AstFactory.superConstructorInvocation();
4487 ConstructorElement staticElement = ElementFactory.constructorElement2(
4488 ElementFactory.classElement2("C"), null);
4489 fromNode.staticElement = staticElement;
4490 SuperConstructorInvocation toNode = AstFactory.superConstructorInvocation();
4491 ResolutionCopier.copyResolutionData(fromNode, toNode);
4492 expect(toNode.staticElement, same(staticElement));
4493 }
4494
4495 void test_visitSuperExpression() {
4496 SuperExpression fromNode = AstFactory.superExpression();
4497 DartType propagatedType = ElementFactory.classElement2("C").type;
4498 fromNode.propagatedType = propagatedType;
4499 DartType staticType = ElementFactory.classElement2("C").type;
4500 fromNode.staticType = staticType;
4501 SuperExpression toNode = AstFactory.superExpression();
4502 ResolutionCopier.copyResolutionData(fromNode, toNode);
4503 expect(toNode.propagatedType, same(propagatedType));
4504 expect(toNode.staticType, same(staticType));
4505 }
4506
4507 void test_visitSymbolLiteral() {
4508 SymbolLiteral fromNode = AstFactory.symbolLiteral(["s"]);
4509 DartType propagatedType = ElementFactory.classElement2("C").type;
4510 fromNode.propagatedType = propagatedType;
4511 DartType staticType = ElementFactory.classElement2("C").type;
4512 fromNode.staticType = staticType;
4513 SymbolLiteral toNode = AstFactory.symbolLiteral(["s"]);
4514 ResolutionCopier.copyResolutionData(fromNode, toNode);
4515 expect(toNode.propagatedType, same(propagatedType));
4516 expect(toNode.staticType, same(staticType));
4517 }
4518
4519 void test_visitThisExpression() {
4520 ThisExpression fromNode = AstFactory.thisExpression();
4521 DartType propagatedType = ElementFactory.classElement2("C").type;
4522 fromNode.propagatedType = propagatedType;
4523 DartType staticType = ElementFactory.classElement2("C").type;
4524 fromNode.staticType = staticType;
4525 ThisExpression toNode = AstFactory.thisExpression();
4526 ResolutionCopier.copyResolutionData(fromNode, toNode);
4527 expect(toNode.propagatedType, same(propagatedType));
4528 expect(toNode.staticType, same(staticType));
4529 }
4530
4531 void test_visitThrowExpression() {
4532 ThrowExpression fromNode = AstFactory.throwExpression();
4533 DartType propagatedType = ElementFactory.classElement2("C").type;
4534 fromNode.propagatedType = propagatedType;
4535 DartType staticType = ElementFactory.classElement2("C").type;
4536 fromNode.staticType = staticType;
4537 ThrowExpression toNode = AstFactory.throwExpression();
4538 ResolutionCopier.copyResolutionData(fromNode, toNode);
4539 expect(toNode.propagatedType, same(propagatedType));
4540 expect(toNode.staticType, same(staticType));
4541 }
4542
4543 void test_visitTypeName() {
4544 TypeName fromNode = AstFactory.typeName4("C");
4545 DartType type = ElementFactory.classElement2("C").type;
4546 fromNode.type = type;
4547 TypeName toNode = AstFactory.typeName4("C");
4548 ResolutionCopier.copyResolutionData(fromNode, toNode);
4549 expect(toNode.type, same(type));
4550 }
4551 }
4552
4553 /**
4554 * The class `SimpleParserTest` defines parser tests that test individual parsin g method. The
4555 * code fragments should be as minimal as possible in order to test the method, but should not test
4556 * the interactions between the method under test and other methods.
4557 *
4558 * More complex tests should be defined in the class [ComplexParserTest].
4559 */
4560 @reflectiveTest
4561 class SimpleParserTest extends ParserTestCase {
4562 void fail_parseAwaitExpression_inSync() {
4563 // This test requires better error recovery than we currently have. In
4564 // particular, we need to be able to distinguish between an await expression
4565 // in the wrong context, and the use of 'await' as an identifier.
4566 MethodDeclaration method = parse(
4567 "parseClassMember", <Object>["C"], "m() { return await x + await y; }");
4568 FunctionBody body = method.body;
4569 EngineTestCase.assertInstanceOf(
4570 (obj) => obj is BlockFunctionBody, BlockFunctionBody, body);
4571 Statement statement = (body as BlockFunctionBody).block.statements[0];
4572 EngineTestCase.assertInstanceOf(
4573 (obj) => obj is ReturnStatement, ReturnStatement, statement);
4574 Expression expression = (statement as ReturnStatement).expression;
4575 EngineTestCase.assertInstanceOf(
4576 (obj) => obj is BinaryExpression, BinaryExpression, expression);
4577 EngineTestCase.assertInstanceOf((obj) => obj is AwaitExpression,
4578 AwaitExpression, (expression as BinaryExpression).leftOperand);
4579 EngineTestCase.assertInstanceOf((obj) => obj is AwaitExpression,
4580 AwaitExpression, (expression as BinaryExpression).rightOperand);
4581 }
4582
4583 void fail_parseCommentReference_this() {
4584 // This fails because we are returning null from the method and asserting
4585 // that the return value is not null.
4586 CommentReference reference =
4587 parse("parseCommentReference", <Object>["this", 5], "");
4588 SimpleIdentifier identifier = EngineTestCase.assertInstanceOf(
4589 (obj) => obj is SimpleIdentifier,
4590 SimpleIdentifier,
4591 reference.identifier);
4592 expect(identifier.token, isNotNull);
4593 expect(identifier.name, "a");
4594 expect(identifier.offset, 5);
4595 }
4596
4597 void test_computeStringValue_emptyInterpolationPrefix() {
4598 expect(_computeStringValue("'''", true, false), "");
4599 }
4600
4601 void test_computeStringValue_escape_b() {
4602 expect(_computeStringValue("'\\b'", true, true), "\b");
4603 }
4604
4605 void test_computeStringValue_escape_f() {
4606 expect(_computeStringValue("'\\f'", true, true), "\f");
4607 }
4608
4609 void test_computeStringValue_escape_n() {
4610 expect(_computeStringValue("'\\n'", true, true), "\n");
4611 }
4612
4613 void test_computeStringValue_escape_notSpecial() {
4614 expect(_computeStringValue("'\\:'", true, true), ":");
4615 }
4616
4617 void test_computeStringValue_escape_r() {
4618 expect(_computeStringValue("'\\r'", true, true), "\r");
4619 }
4620
4621 void test_computeStringValue_escape_t() {
4622 expect(_computeStringValue("'\\t'", true, true), "\t");
4623 }
4624
4625 void test_computeStringValue_escape_u_fixed() {
4626 expect(_computeStringValue("'\\u4321'", true, true), "\u4321");
4627 }
4628
4629 void test_computeStringValue_escape_u_variable() {
4630 expect(_computeStringValue("'\\u{123}'", true, true), "\u0123");
4631 }
4632
4633 void test_computeStringValue_escape_v() {
4634 expect(_computeStringValue("'\\v'", true, true), "\u000B");
4635 }
4636
4637 void test_computeStringValue_escape_x() {
4638 expect(_computeStringValue("'\\xFF'", true, true), "\u00FF");
4639 }
4640
4641 void test_computeStringValue_noEscape_single() {
4642 expect(_computeStringValue("'text'", true, true), "text");
4643 }
4644
4645 void test_computeStringValue_noEscape_triple() {
4646 expect(_computeStringValue("'''text'''", true, true), "text");
4647 }
4648
4649 void test_computeStringValue_raw_single() {
4650 expect(_computeStringValue("r'text'", true, true), "text");
4651 }
4652
4653 void test_computeStringValue_raw_triple() {
4654 expect(_computeStringValue("r'''text'''", true, true), "text");
4655 }
4656
4657 void test_computeStringValue_raw_withEscape() {
4658 expect(_computeStringValue("r'two\\nlines'", true, true), "two\\nlines");
4659 }
4660
4661 void test_computeStringValue_triple_internalQuote_first_empty() {
4662 expect(_computeStringValue("''''", true, false), "'");
4663 }
4664
4665 void test_computeStringValue_triple_internalQuote_first_nonEmpty() {
4666 expect(_computeStringValue("''''text", true, false), "'text");
4667 }
4668
4669 void test_computeStringValue_triple_internalQuote_last_empty() {
4670 expect(_computeStringValue("'''", false, true), "");
4671 }
4672
4673 void test_computeStringValue_triple_internalQuote_last_nonEmpty() {
4674 expect(_computeStringValue("text'''", false, true), "text");
4675 }
4676
4677 void test_constFactory() {
4678 parse("parseClassMember", <Object>["C"], "const factory C() = A;");
4679 }
4680
4681 void test_createSyntheticIdentifier() {
4682 SimpleIdentifier identifier = _createSyntheticIdentifier();
4683 expect(identifier.isSynthetic, isTrue);
4684 }
4685
4686 void test_createSyntheticStringLiteral() {
4687 SimpleStringLiteral literal = _createSyntheticStringLiteral();
4688 expect(literal.isSynthetic, isTrue);
4689 }
4690
4691 void test_function_literal_allowed_at_toplevel() {
4692 ParserTestCase.parseCompilationUnit("var x = () {};");
4693 }
4694
4695 void test_function_literal_allowed_in_ArgumentList_in_ConstructorFieldInitiali zer() {
4696 ParserTestCase.parseCompilationUnit("class C { C() : a = f(() {}); }");
4697 }
4698
4699 void test_function_literal_allowed_in_IndexExpression_in_ConstructorFieldIniti alizer() {
4700 ParserTestCase.parseCompilationUnit("class C { C() : a = x[() {}]; }");
4701 }
4702
4703 void test_function_literal_allowed_in_ListLiteral_in_ConstructorFieldInitializ er() {
4704 ParserTestCase.parseCompilationUnit("class C { C() : a = [() {}]; }");
4705 }
4706
4707 void test_function_literal_allowed_in_MapLiteral_in_ConstructorFieldInitialize r() {
4708 ParserTestCase
4709 .parseCompilationUnit("class C { C() : a = {'key': () {}}; }");
4710 }
4711
4712 void test_function_literal_allowed_in_ParenthesizedExpression_in_ConstructorFi eldInitializer() {
4713 ParserTestCase.parseCompilationUnit("class C { C() : a = (() {}); }");
4714 }
4715
4716 void test_function_literal_allowed_in_StringInterpolation_in_ConstructorFieldI nitializer() {
4717 ParserTestCase.parseCompilationUnit("class C { C() : a = \"\${(){}}\"; }");
4718 }
4719
4720 void test_import_as_show() {
4721 ParserTestCase.parseCompilationUnit("import 'dart:math' as M show E;");
4722 }
4723
4724 void test_import_show_hide() {
4725 ParserTestCase.parseCompilationUnit(
4726 "import 'import1_lib.dart' show hide, show hide ugly;");
4727 }
4728
4729 void test_isFunctionDeclaration_nameButNoReturn_block() {
4730 expect(_isFunctionDeclaration("f() {}"), isTrue);
4731 }
4732
4733 void test_isFunctionDeclaration_nameButNoReturn_expression() {
4734 expect(_isFunctionDeclaration("f() => e"), isTrue);
4735 }
4736
4737 void test_isFunctionDeclaration_nameButNoReturn_typeParameters_block() {
4738 enableGenericMethods = true;
4739 expect(_isFunctionDeclaration("f<E>() {}"), isTrue);
4740 }
4741
4742 void test_isFunctionDeclaration_nameButNoReturn_typeParameters_expression() {
4743 enableGenericMethods = true;
4744 expect(_isFunctionDeclaration("f<E>() => e"), isTrue);
4745 }
4746
4747 void test_isFunctionDeclaration_normalReturn_block() {
4748 expect(_isFunctionDeclaration("C f() {}"), isTrue);
4749 }
4750
4751 void test_isFunctionDeclaration_normalReturn_expression() {
4752 expect(_isFunctionDeclaration("C f() => e"), isTrue);
4753 }
4754
4755 void test_isFunctionDeclaration_normalReturn_typeParameters_block() {
4756 enableGenericMethods = true;
4757 expect(_isFunctionDeclaration("C f<E>() {}"), isTrue);
4758 }
4759
4760 void test_isFunctionDeclaration_normalReturn_typeParameters_expression() {
4761 enableGenericMethods = true;
4762 expect(_isFunctionDeclaration("C f<E>() => e"), isTrue);
4763 }
4764
4765 void test_isFunctionDeclaration_voidReturn_block() {
4766 expect(_isFunctionDeclaration("void f() {}"), isTrue);
4767 }
4768
4769 void test_isFunctionDeclaration_voidReturn_expression() {
4770 expect(_isFunctionDeclaration("void f() => e"), isTrue);
4771 }
4772
4773 void test_isFunctionDeclaration_voidReturn_typeParameters_block() {
4774 enableGenericMethods = true;
4775 expect(_isFunctionDeclaration("void f<E>() {}"), isTrue);
4776 }
4777
4778 void test_isFunctionDeclaration_voidReturn_typeParameters_expression() {
4779 enableGenericMethods = true;
4780 expect(_isFunctionDeclaration("void f<E>() => e"), isTrue);
4781 }
4782
4783 void test_isFunctionExpression_false_noBody() {
4784 expect(_isFunctionExpression("f();"), isFalse);
4785 }
4786
4787 void test_isFunctionExpression_false_notParameters() {
4788 expect(_isFunctionExpression("(a + b) {"), isFalse);
4789 }
4790
4791 void test_isFunctionExpression_noParameters_block() {
4792 expect(_isFunctionExpression("() {}"), isTrue);
4793 }
4794
4795 void test_isFunctionExpression_noParameters_expression() {
4796 expect(_isFunctionExpression("() => e"), isTrue);
4797 }
4798
4799 void test_isFunctionExpression_noParameters_typeParameters_block() {
4800 enableGenericMethods = true;
4801 expect(_isFunctionExpression("<E>() {}"), isTrue);
4802 }
4803
4804 void test_isFunctionExpression_noParameters_typeParameters_expression() {
4805 enableGenericMethods = true;
4806 expect(_isFunctionExpression("<E>() => e"), isTrue);
4807 }
4808
4809 void test_isFunctionExpression_parameter_final() {
4810 expect(_isFunctionExpression("(final a) {}"), isTrue);
4811 expect(_isFunctionExpression("(final a, b) {}"), isTrue);
4812 expect(_isFunctionExpression("(final a, final b) {}"), isTrue);
4813 }
4814
4815 void test_isFunctionExpression_parameter_final_typed() {
4816 expect(_isFunctionExpression("(final int a) {}"), isTrue);
4817 expect(_isFunctionExpression("(final prefix.List a) {}"), isTrue);
4818 expect(_isFunctionExpression("(final List<int> a) {}"), isTrue);
4819 expect(_isFunctionExpression("(final prefix.List<int> a) {}"), isTrue);
4820 }
4821
4822 void test_isFunctionExpression_parameter_multiple() {
4823 expect(_isFunctionExpression("(a, b) {}"), isTrue);
4824 }
4825
4826 void test_isFunctionExpression_parameter_named() {
4827 expect(_isFunctionExpression("({a}) {}"), isTrue);
4828 }
4829
4830 void test_isFunctionExpression_parameter_optional() {
4831 expect(_isFunctionExpression("([a]) {}"), isTrue);
4832 }
4833
4834 void test_isFunctionExpression_parameter_single() {
4835 expect(_isFunctionExpression("(a) {}"), isTrue);
4836 }
4837
4838 void test_isFunctionExpression_parameter_typed() {
4839 expect(_isFunctionExpression("(int a, int b) {}"), isTrue);
4840 }
4841
4842 void test_isInitializedVariableDeclaration_assignment() {
4843 expect(_isInitializedVariableDeclaration("a = null;"), isFalse);
4844 }
4845
4846 void test_isInitializedVariableDeclaration_comparison() {
4847 expect(_isInitializedVariableDeclaration("a < 0;"), isFalse);
4848 }
4849
4850 void test_isInitializedVariableDeclaration_conditional() {
4851 expect(_isInitializedVariableDeclaration("a == null ? init() : update();"),
4852 isFalse);
4853 }
4854
4855 void test_isInitializedVariableDeclaration_const_noType_initialized() {
4856 expect(_isInitializedVariableDeclaration("const a = 0;"), isTrue);
4857 }
4858
4859 void test_isInitializedVariableDeclaration_const_noType_uninitialized() {
4860 expect(_isInitializedVariableDeclaration("const a;"), isTrue);
4861 }
4862
4863 void test_isInitializedVariableDeclaration_const_simpleType_uninitialized() {
4864 expect(_isInitializedVariableDeclaration("const A a;"), isTrue);
4865 }
4866
4867 void test_isInitializedVariableDeclaration_final_noType_initialized() {
4868 expect(_isInitializedVariableDeclaration("final a = 0;"), isTrue);
4869 }
4870
4871 void test_isInitializedVariableDeclaration_final_noType_uninitialized() {
4872 expect(_isInitializedVariableDeclaration("final a;"), isTrue);
4873 }
4874
4875 void test_isInitializedVariableDeclaration_final_simpleType_initialized() {
4876 expect(_isInitializedVariableDeclaration("final A a = 0;"), isTrue);
4877 }
4878
4879 void test_isInitializedVariableDeclaration_functionDeclaration_typed() {
4880 expect(_isInitializedVariableDeclaration("A f() {};"), isFalse);
4881 }
4882
4883 void test_isInitializedVariableDeclaration_functionDeclaration_untyped() {
4884 expect(_isInitializedVariableDeclaration("f() {};"), isFalse);
4885 }
4886
4887 void test_isInitializedVariableDeclaration_noType_initialized() {
4888 expect(_isInitializedVariableDeclaration("var a = 0;"), isTrue);
4889 }
4890
4891 void test_isInitializedVariableDeclaration_noType_uninitialized() {
4892 expect(_isInitializedVariableDeclaration("var a;"), isTrue);
4893 }
4894
4895 void test_isInitializedVariableDeclaration_parameterizedType_initialized() {
4896 expect(_isInitializedVariableDeclaration("List<int> a = null;"), isTrue);
4897 }
4898
4899 void test_isInitializedVariableDeclaration_parameterizedType_uninitialized() {
4900 expect(_isInitializedVariableDeclaration("List<int> a;"), isTrue);
4901 }
4902
4903 void test_isInitializedVariableDeclaration_simpleType_initialized() {
4904 expect(_isInitializedVariableDeclaration("A a = 0;"), isTrue);
4905 }
4906
4907 void test_isInitializedVariableDeclaration_simpleType_uninitialized() {
4908 expect(_isInitializedVariableDeclaration("A a;"), isTrue);
4909 }
4910
4911 void test_isSwitchMember_case_labeled() {
4912 expect(_isSwitchMember("l1: l2: case"), isTrue);
4913 }
4914
4915 void test_isSwitchMember_case_unlabeled() {
4916 expect(_isSwitchMember("case"), isTrue);
4917 }
4918
4919 void test_isSwitchMember_default_labeled() {
4920 expect(_isSwitchMember("l1: l2: default"), isTrue);
4921 }
4922
4923 void test_isSwitchMember_default_unlabeled() {
4924 expect(_isSwitchMember("default"), isTrue);
4925 }
4926
4927 void test_isSwitchMember_false() {
4928 expect(_isSwitchMember("break;"), isFalse);
4929 }
4930
4931 void test_parseAdditiveExpression_normal() {
4932 BinaryExpression expression = parse4("parseAdditiveExpression", "x + y");
4933 expect(expression.leftOperand, isNotNull);
4934 expect(expression.operator, isNotNull);
4935 expect(expression.operator.type, TokenType.PLUS);
4936 expect(expression.rightOperand, isNotNull);
4937 }
4938
4939 void test_parseAdditiveExpression_super() {
4940 BinaryExpression expression =
4941 parse4("parseAdditiveExpression", "super + y");
4942 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
4943 SuperExpression, expression.leftOperand);
4944 expect(expression.operator, isNotNull);
4945 expect(expression.operator.type, TokenType.PLUS);
4946 expect(expression.rightOperand, isNotNull);
4947 }
4948
4949 void test_parseAnnotation_n1() {
4950 Annotation annotation = parse4("parseAnnotation", "@A");
4951 expect(annotation.atSign, isNotNull);
4952 expect(annotation.name, isNotNull);
4953 expect(annotation.period, isNull);
4954 expect(annotation.constructorName, isNull);
4955 expect(annotation.arguments, isNull);
4956 }
4957
4958 void test_parseAnnotation_n1_a() {
4959 Annotation annotation = parse4("parseAnnotation", "@A(x,y)");
4960 expect(annotation.atSign, isNotNull);
4961 expect(annotation.name, isNotNull);
4962 expect(annotation.period, isNull);
4963 expect(annotation.constructorName, isNull);
4964 expect(annotation.arguments, isNotNull);
4965 }
4966
4967 void test_parseAnnotation_n2() {
4968 Annotation annotation = parse4("parseAnnotation", "@A.B");
4969 expect(annotation.atSign, isNotNull);
4970 expect(annotation.name, isNotNull);
4971 expect(annotation.period, isNull);
4972 expect(annotation.constructorName, isNull);
4973 expect(annotation.arguments, isNull);
4974 }
4975
4976 void test_parseAnnotation_n2_a() {
4977 Annotation annotation = parse4("parseAnnotation", "@A.B(x,y)");
4978 expect(annotation.atSign, isNotNull);
4979 expect(annotation.name, isNotNull);
4980 expect(annotation.period, isNull);
4981 expect(annotation.constructorName, isNull);
4982 expect(annotation.arguments, isNotNull);
4983 }
4984
4985 void test_parseAnnotation_n3() {
4986 Annotation annotation = parse4("parseAnnotation", "@A.B.C");
4987 expect(annotation.atSign, isNotNull);
4988 expect(annotation.name, isNotNull);
4989 expect(annotation.period, isNotNull);
4990 expect(annotation.constructorName, isNotNull);
4991 expect(annotation.arguments, isNull);
4992 }
4993
4994 void test_parseAnnotation_n3_a() {
4995 Annotation annotation = parse4("parseAnnotation", "@A.B.C(x,y)");
4996 expect(annotation.atSign, isNotNull);
4997 expect(annotation.name, isNotNull);
4998 expect(annotation.period, isNotNull);
4999 expect(annotation.constructorName, isNotNull);
5000 expect(annotation.arguments, isNotNull);
5001 }
5002
5003 void test_parseArgument_named() {
5004 NamedExpression expression = parse4("parseArgument", "n: x");
5005 Label name = expression.name;
5006 expect(name, isNotNull);
5007 expect(name.label, isNotNull);
5008 expect(name.colon, isNotNull);
5009 expect(expression.expression, isNotNull);
5010 }
5011
5012 void test_parseArgument_unnamed() {
5013 String lexeme = "x";
5014 SimpleIdentifier identifier = parse4("parseArgument", lexeme);
5015 expect(identifier.name, lexeme);
5016 }
5017
5018 void test_parseArgumentList_empty() {
5019 ArgumentList argumentList = parse4("parseArgumentList", "()");
5020 NodeList<Expression> arguments = argumentList.arguments;
5021 expect(arguments, hasLength(0));
5022 }
5023
5024 void test_parseArgumentList_mixed() {
5025 ArgumentList argumentList =
5026 parse4("parseArgumentList", "(w, x, y: y, z: z)");
5027 NodeList<Expression> arguments = argumentList.arguments;
5028 expect(arguments, hasLength(4));
5029 }
5030
5031 void test_parseArgumentList_noNamed() {
5032 ArgumentList argumentList = parse4("parseArgumentList", "(x, y, z)");
5033 NodeList<Expression> arguments = argumentList.arguments;
5034 expect(arguments, hasLength(3));
5035 }
5036
5037 void test_parseArgumentList_onlyNamed() {
5038 ArgumentList argumentList = parse4("parseArgumentList", "(x: x, y: y)");
5039 NodeList<Expression> arguments = argumentList.arguments;
5040 expect(arguments, hasLength(2));
5041 }
5042
5043 void test_parseAssertStatement() {
5044 AssertStatement statement = parse4("parseAssertStatement", "assert (x);");
5045 expect(statement.assertKeyword, isNotNull);
5046 expect(statement.leftParenthesis, isNotNull);
5047 expect(statement.condition, isNotNull);
5048 expect(statement.rightParenthesis, isNotNull);
5049 expect(statement.semicolon, isNotNull);
5050 }
5051
5052 void test_parseAssignableExpression_expression_args_dot() {
5053 PropertyAccess propertyAccess =
5054 parse("parseAssignableExpression", <Object>[false], "(x)(y).z");
5055 FunctionExpressionInvocation invocation =
5056 propertyAccess.target as FunctionExpressionInvocation;
5057 expect(invocation.function, isNotNull);
5058 expect(invocation.typeArguments, isNull);
5059 ArgumentList argumentList = invocation.argumentList;
5060 expect(argumentList, isNotNull);
5061 expect(argumentList.arguments, hasLength(1));
5062 expect(propertyAccess.operator, isNotNull);
5063 expect(propertyAccess.propertyName, isNotNull);
5064 }
5065
5066 void test_parseAssignableExpression_expression_args_dot_typeParameters() {
5067 enableGenericMethods = true;
5068 PropertyAccess propertyAccess =
5069 parse("parseAssignableExpression", <Object>[false], "(x)<F>(y).z");
5070 FunctionExpressionInvocation invocation =
5071 propertyAccess.target as FunctionExpressionInvocation;
5072 expect(invocation.function, isNotNull);
5073 expect(invocation.typeArguments, isNotNull);
5074 ArgumentList argumentList = invocation.argumentList;
5075 expect(argumentList, isNotNull);
5076 expect(argumentList.arguments, hasLength(1));
5077 expect(propertyAccess.operator, isNotNull);
5078 expect(propertyAccess.propertyName, isNotNull);
5079 }
5080
5081 void test_parseAssignableExpression_expression_dot() {
5082 PropertyAccess propertyAccess =
5083 parse("parseAssignableExpression", <Object>[false], "(x).y");
5084 expect(propertyAccess.target, isNotNull);
5085 expect(propertyAccess.operator.type, TokenType.PERIOD);
5086 expect(propertyAccess.propertyName, isNotNull);
5087 }
5088
5089 void test_parseAssignableExpression_expression_index() {
5090 IndexExpression expression =
5091 parse("parseAssignableExpression", <Object>[false], "(x)[y]");
5092 expect(expression.target, isNotNull);
5093 expect(expression.leftBracket, isNotNull);
5094 expect(expression.index, isNotNull);
5095 expect(expression.rightBracket, isNotNull);
5096 }
5097
5098 void test_parseAssignableExpression_expression_question_dot() {
5099 PropertyAccess propertyAccess =
5100 parse("parseAssignableExpression", <Object>[false], "(x)?.y");
5101 expect(propertyAccess.target, isNotNull);
5102 expect(propertyAccess.operator.type, TokenType.QUESTION_PERIOD);
5103 expect(propertyAccess.propertyName, isNotNull);
5104 }
5105
5106 void test_parseAssignableExpression_identifier() {
5107 SimpleIdentifier identifier =
5108 parse("parseAssignableExpression", <Object>[false], "x");
5109 expect(identifier, isNotNull);
5110 }
5111
5112 void test_parseAssignableExpression_identifier_args_dot() {
5113 PropertyAccess propertyAccess =
5114 parse("parseAssignableExpression", <Object>[false], "x(y).z");
5115 MethodInvocation invocation = propertyAccess.target as MethodInvocation;
5116 expect(invocation.methodName.name, "x");
5117 expect(invocation.typeArguments, isNull);
5118 ArgumentList argumentList = invocation.argumentList;
5119 expect(argumentList, isNotNull);
5120 expect(argumentList.arguments, hasLength(1));
5121 expect(propertyAccess.operator, isNotNull);
5122 expect(propertyAccess.propertyName, isNotNull);
5123 }
5124
5125 void test_parseAssignableExpression_identifier_args_dot_typeParameters() {
5126 enableGenericMethods = true;
5127 PropertyAccess propertyAccess =
5128 parse("parseAssignableExpression", <Object>[false], "x<E>(y).z");
5129 MethodInvocation invocation = propertyAccess.target as MethodInvocation;
5130 expect(invocation.methodName.name, "x");
5131 expect(invocation.typeArguments, isNotNull);
5132 ArgumentList argumentList = invocation.argumentList;
5133 expect(argumentList, isNotNull);
5134 expect(argumentList.arguments, hasLength(1));
5135 expect(propertyAccess.operator, isNotNull);
5136 expect(propertyAccess.propertyName, isNotNull);
5137 }
5138
5139 void test_parseAssignableExpression_identifier_dot() {
5140 PropertyAccess propertyAccess =
5141 parse("parseAssignableExpression", <Object>[false], "x.y");
5142 expect(propertyAccess.target, isNotNull);
5143 expect(propertyAccess.operator, isNotNull);
5144 expect(propertyAccess.operator.type, TokenType.PERIOD);
5145 expect(propertyAccess.propertyName, isNotNull);
5146 }
5147
5148 void test_parseAssignableExpression_identifier_index() {
5149 IndexExpression expression =
5150 parse("parseAssignableExpression", <Object>[false], "x[y]");
5151 expect(expression.target, isNotNull);
5152 expect(expression.leftBracket, isNotNull);
5153 expect(expression.index, isNotNull);
5154 expect(expression.rightBracket, isNotNull);
5155 }
5156
5157 void test_parseAssignableExpression_identifier_question_dot() {
5158 PropertyAccess propertyAccess =
5159 parse("parseAssignableExpression", <Object>[false], "x?.y");
5160 expect(propertyAccess.target, isNotNull);
5161 expect(propertyAccess.operator.type, TokenType.QUESTION_PERIOD);
5162 expect(propertyAccess.propertyName, isNotNull);
5163 }
5164
5165 void test_parseAssignableExpression_super_dot() {
5166 PropertyAccess propertyAccess =
5167 parse("parseAssignableExpression", <Object>[false], "super.y");
5168 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
5169 SuperExpression, propertyAccess.target);
5170 expect(propertyAccess.operator, isNotNull);
5171 expect(propertyAccess.propertyName, isNotNull);
5172 }
5173
5174 void test_parseAssignableExpression_super_index() {
5175 IndexExpression expression =
5176 parse("parseAssignableExpression", <Object>[false], "super[y]");
5177 EngineTestCase.assertInstanceOf(
5178 (obj) => obj is SuperExpression, SuperExpression, expression.target);
5179 expect(expression.leftBracket, isNotNull);
5180 expect(expression.index, isNotNull);
5181 expect(expression.rightBracket, isNotNull);
5182 }
5183
5184 void test_parseAssignableSelector_dot() {
5185 PropertyAccess selector =
5186 parse("parseAssignableSelector", <Object>[null, true], ".x");
5187 expect(selector.operator.type, TokenType.PERIOD);
5188 expect(selector.propertyName, isNotNull);
5189 }
5190
5191 void test_parseAssignableSelector_index() {
5192 IndexExpression selector =
5193 parse("parseAssignableSelector", <Object>[null, true], "[x]");
5194 expect(selector.leftBracket, isNotNull);
5195 expect(selector.index, isNotNull);
5196 expect(selector.rightBracket, isNotNull);
5197 }
5198
5199 void test_parseAssignableSelector_none() {
5200 SimpleIdentifier selector = parse("parseAssignableSelector",
5201 <Object>[new SimpleIdentifier(null), true], ";");
5202 expect(selector, isNotNull);
5203 }
5204
5205 void test_parseAssignableSelector_question_dot() {
5206 PropertyAccess selector =
5207 parse("parseAssignableSelector", <Object>[null, true], "?.x");
5208 expect(selector.operator.type, TokenType.QUESTION_PERIOD);
5209 expect(selector.propertyName, isNotNull);
5210 }
5211
5212 void test_parseAwaitExpression() {
5213 AwaitExpression expression = parse4("parseAwaitExpression", "await x;");
5214 expect(expression.awaitKeyword, isNotNull);
5215 expect(expression.expression, isNotNull);
5216 }
5217
5218 void test_parseAwaitExpression_asStatement_inAsync() {
5219 MethodDeclaration method =
5220 parse("parseClassMember", <Object>["C"], "m() async { await x; }");
5221 FunctionBody body = method.body;
5222 EngineTestCase.assertInstanceOf(
5223 (obj) => obj is BlockFunctionBody, BlockFunctionBody, body);
5224 Statement statement = (body as BlockFunctionBody).block.statements[0];
5225 EngineTestCase.assertInstanceOf(
5226 (obj) => obj is ExpressionStatement, ExpressionStatement, statement);
5227 Expression expression = (statement as ExpressionStatement).expression;
5228 EngineTestCase.assertInstanceOf(
5229 (obj) => obj is AwaitExpression, AwaitExpression, expression);
5230 expect((expression as AwaitExpression).awaitKeyword, isNotNull);
5231 expect((expression as AwaitExpression).expression, isNotNull);
5232 }
5233
5234 void test_parseAwaitExpression_asStatement_inSync() {
5235 MethodDeclaration method =
5236 parse("parseClassMember", <Object>["C"], "m() { await x; }");
5237 FunctionBody body = method.body;
5238 EngineTestCase.assertInstanceOf(
5239 (obj) => obj is BlockFunctionBody, BlockFunctionBody, body);
5240 Statement statement = (body as BlockFunctionBody).block.statements[0];
5241 EngineTestCase.assertInstanceOf(
5242 (obj) => obj is VariableDeclarationStatement,
5243 VariableDeclarationStatement,
5244 statement);
5245 }
5246
5247 void test_parseBitwiseAndExpression_normal() {
5248 BinaryExpression expression = parse4("parseBitwiseAndExpression", "x & y");
5249 expect(expression.leftOperand, isNotNull);
5250 expect(expression.operator, isNotNull);
5251 expect(expression.operator.type, TokenType.AMPERSAND);
5252 expect(expression.rightOperand, isNotNull);
5253 }
5254
5255 void test_parseBitwiseAndExpression_super() {
5256 BinaryExpression expression =
5257 parse4("parseBitwiseAndExpression", "super & y");
5258 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
5259 SuperExpression, expression.leftOperand);
5260 expect(expression.operator, isNotNull);
5261 expect(expression.operator.type, TokenType.AMPERSAND);
5262 expect(expression.rightOperand, isNotNull);
5263 }
5264
5265 void test_parseBitwiseOrExpression_normal() {
5266 BinaryExpression expression = parse4("parseBitwiseOrExpression", "x | y");
5267 expect(expression.leftOperand, isNotNull);
5268 expect(expression.operator, isNotNull);
5269 expect(expression.operator.type, TokenType.BAR);
5270 expect(expression.rightOperand, isNotNull);
5271 }
5272
5273 void test_parseBitwiseOrExpression_super() {
5274 BinaryExpression expression =
5275 parse4("parseBitwiseOrExpression", "super | y");
5276 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
5277 SuperExpression, expression.leftOperand);
5278 expect(expression.operator, isNotNull);
5279 expect(expression.operator.type, TokenType.BAR);
5280 expect(expression.rightOperand, isNotNull);
5281 }
5282
5283 void test_parseBitwiseXorExpression_normal() {
5284 BinaryExpression expression = parse4("parseBitwiseXorExpression", "x ^ y");
5285 expect(expression.leftOperand, isNotNull);
5286 expect(expression.operator, isNotNull);
5287 expect(expression.operator.type, TokenType.CARET);
5288 expect(expression.rightOperand, isNotNull);
5289 }
5290
5291 void test_parseBitwiseXorExpression_super() {
5292 BinaryExpression expression =
5293 parse4("parseBitwiseXorExpression", "super ^ y");
5294 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
5295 SuperExpression, expression.leftOperand);
5296 expect(expression.operator, isNotNull);
5297 expect(expression.operator.type, TokenType.CARET);
5298 expect(expression.rightOperand, isNotNull);
5299 }
5300
5301 void test_parseBlock_empty() {
5302 Block block = parse4("parseBlock", "{}");
5303 expect(block.leftBracket, isNotNull);
5304 expect(block.statements, hasLength(0));
5305 expect(block.rightBracket, isNotNull);
5306 }
5307
5308 void test_parseBlock_nonEmpty() {
5309 Block block = parse4("parseBlock", "{;}");
5310 expect(block.leftBracket, isNotNull);
5311 expect(block.statements, hasLength(1));
5312 expect(block.rightBracket, isNotNull);
5313 }
5314
5315 void test_parseBreakStatement_label() {
5316 BreakStatement statement = parse4("parseBreakStatement", "break foo;");
5317 expect(statement.breakKeyword, isNotNull);
5318 expect(statement.label, isNotNull);
5319 expect(statement.semicolon, isNotNull);
5320 }
5321
5322 void test_parseBreakStatement_noLabel() {
5323 BreakStatement statement = parse4("parseBreakStatement", "break;",
5324 [ParserErrorCode.BREAK_OUTSIDE_OF_LOOP]);
5325 expect(statement.breakKeyword, isNotNull);
5326 expect(statement.label, isNull);
5327 expect(statement.semicolon, isNotNull);
5328 }
5329
5330 void test_parseCascadeSection_i() {
5331 IndexExpression section = parse4("parseCascadeSection", "..[i]");
5332 expect(section.target, isNull);
5333 expect(section.leftBracket, isNotNull);
5334 expect(section.index, isNotNull);
5335 expect(section.rightBracket, isNotNull);
5336 }
5337
5338 void test_parseCascadeSection_ia() {
5339 FunctionExpressionInvocation section =
5340 parse4("parseCascadeSection", "..[i](b)");
5341 EngineTestCase.assertInstanceOf(
5342 (obj) => obj is IndexExpression, IndexExpression, section.function);
5343 expect(section.typeArguments, isNull);
5344 expect(section.argumentList, isNotNull);
5345 }
5346
5347 void test_parseCascadeSection_ia_typeArguments() {
5348 enableGenericMethods = true;
5349 FunctionExpressionInvocation section =
5350 parse4("parseCascadeSection", "..[i]<E>(b)");
5351 EngineTestCase.assertInstanceOf(
5352 (obj) => obj is IndexExpression, IndexExpression, section.function);
5353 expect(section.typeArguments, isNotNull);
5354 expect(section.argumentList, isNotNull);
5355 }
5356
5357 void test_parseCascadeSection_ii() {
5358 MethodInvocation section = parse4("parseCascadeSection", "..a(b).c(d)");
5359 EngineTestCase.assertInstanceOf(
5360 (obj) => obj is MethodInvocation, MethodInvocation, section.target);
5361 expect(section.operator, isNotNull);
5362 expect(section.methodName, isNotNull);
5363 expect(section.typeArguments, isNull);
5364 expect(section.argumentList, isNotNull);
5365 expect(section.argumentList.arguments, hasLength(1));
5366 }
5367
5368 void test_parseCascadeSection_ii_typeArguments() {
5369 enableGenericMethods = true;
5370 MethodInvocation section =
5371 parse4("parseCascadeSection", "..a<E>(b).c<F>(d)");
5372 EngineTestCase.assertInstanceOf(
5373 (obj) => obj is MethodInvocation, MethodInvocation, section.target);
5374 expect(section.operator, isNotNull);
5375 expect(section.methodName, isNotNull);
5376 expect(section.typeArguments, isNotNull);
5377 expect(section.argumentList, isNotNull);
5378 expect(section.argumentList.arguments, hasLength(1));
5379 }
5380
5381 void test_parseCascadeSection_p() {
5382 PropertyAccess section = parse4("parseCascadeSection", "..a");
5383 expect(section.target, isNull);
5384 expect(section.operator, isNotNull);
5385 expect(section.propertyName, isNotNull);
5386 }
5387
5388 void test_parseCascadeSection_p_assign() {
5389 AssignmentExpression section = parse4("parseCascadeSection", "..a = 3");
5390 expect(section.leftHandSide, isNotNull);
5391 expect(section.operator, isNotNull);
5392 Expression rhs = section.rightHandSide;
5393 expect(rhs, isNotNull);
5394 }
5395
5396 void test_parseCascadeSection_p_assign_withCascade() {
5397 AssignmentExpression section =
5398 parse4("parseCascadeSection", "..a = 3..m()");
5399 expect(section.leftHandSide, isNotNull);
5400 expect(section.operator, isNotNull);
5401 Expression rhs = section.rightHandSide;
5402 EngineTestCase.assertInstanceOf(
5403 (obj) => obj is IntegerLiteral, IntegerLiteral, rhs);
5404 }
5405
5406 void test_parseCascadeSection_p_assign_withCascade_typeArguments() {
5407 enableGenericMethods = true;
5408 AssignmentExpression section =
5409 parse4("parseCascadeSection", "..a = 3..m<E>()");
5410 expect(section.leftHandSide, isNotNull);
5411 expect(section.operator, isNotNull);
5412 Expression rhs = section.rightHandSide;
5413 EngineTestCase.assertInstanceOf(
5414 (obj) => obj is IntegerLiteral, IntegerLiteral, rhs);
5415 }
5416
5417 void test_parseCascadeSection_p_builtIn() {
5418 PropertyAccess section = parse4("parseCascadeSection", "..as");
5419 expect(section.target, isNull);
5420 expect(section.operator, isNotNull);
5421 expect(section.propertyName, isNotNull);
5422 }
5423
5424 void test_parseCascadeSection_pa() {
5425 MethodInvocation section = parse4("parseCascadeSection", "..a(b)");
5426 expect(section.target, isNull);
5427 expect(section.operator, isNotNull);
5428 expect(section.methodName, isNotNull);
5429 expect(section.typeArguments, isNull);
5430 expect(section.argumentList, isNotNull);
5431 expect(section.argumentList.arguments, hasLength(1));
5432 }
5433
5434 void test_parseCascadeSection_pa_typeArguments() {
5435 enableGenericMethods = true;
5436 MethodInvocation section = parse4("parseCascadeSection", "..a<E>(b)");
5437 expect(section.target, isNull);
5438 expect(section.operator, isNotNull);
5439 expect(section.methodName, isNotNull);
5440 expect(section.typeArguments, isNotNull);
5441 expect(section.argumentList, isNotNull);
5442 expect(section.argumentList.arguments, hasLength(1));
5443 }
5444
5445 void test_parseCascadeSection_paa() {
5446 FunctionExpressionInvocation section =
5447 parse4("parseCascadeSection", "..a(b)(c)");
5448 EngineTestCase.assertInstanceOf(
5449 (obj) => obj is MethodInvocation, MethodInvocation, section.function);
5450 expect(section.typeArguments, isNull);
5451 expect(section.argumentList, isNotNull);
5452 expect(section.argumentList.arguments, hasLength(1));
5453 }
5454
5455 void test_parseCascadeSection_paa_typeArguments() {
5456 enableGenericMethods = true;
5457 FunctionExpressionInvocation section =
5458 parse4("parseCascadeSection", "..a<E>(b)<F>(c)");
5459 EngineTestCase.assertInstanceOf(
5460 (obj) => obj is MethodInvocation, MethodInvocation, section.function);
5461 expect(section.typeArguments, isNotNull);
5462 expect(section.argumentList, isNotNull);
5463 expect(section.argumentList.arguments, hasLength(1));
5464 }
5465
5466 void test_parseCascadeSection_paapaa() {
5467 FunctionExpressionInvocation section =
5468 parse4("parseCascadeSection", "..a(b)(c).d(e)(f)");
5469 EngineTestCase.assertInstanceOf(
5470 (obj) => obj is MethodInvocation, MethodInvocation, section.function);
5471 expect(section.typeArguments, isNull);
5472 expect(section.argumentList, isNotNull);
5473 expect(section.argumentList.arguments, hasLength(1));
5474 }
5475
5476 void test_parseCascadeSection_paapaa_typeArguments() {
5477 enableGenericMethods = true;
5478 FunctionExpressionInvocation section =
5479 parse4("parseCascadeSection", "..a<E>(b)<F>(c).d<G>(e)<H>(f)");
5480 EngineTestCase.assertInstanceOf(
5481 (obj) => obj is MethodInvocation, MethodInvocation, section.function);
5482 expect(section.typeArguments, isNotNull);
5483 expect(section.argumentList, isNotNull);
5484 expect(section.argumentList.arguments, hasLength(1));
5485 }
5486
5487 void test_parseCascadeSection_pap() {
5488 PropertyAccess section = parse4("parseCascadeSection", "..a(b).c");
5489 expect(section.target, isNotNull);
5490 expect(section.operator, isNotNull);
5491 expect(section.propertyName, isNotNull);
5492 }
5493
5494 void test_parseCascadeSection_pap_typeArguments() {
5495 enableGenericMethods = true;
5496 PropertyAccess section = parse4("parseCascadeSection", "..a<E>(b).c");
5497 expect(section.target, isNotNull);
5498 expect(section.operator, isNotNull);
5499 expect(section.propertyName, isNotNull);
5500 }
5501
5502 void test_parseClassDeclaration_abstract() {
5503 ClassDeclaration declaration = parse(
5504 "parseClassDeclaration",
5505 <Object>[
5506 emptyCommentAndMetadata(),
5507 TokenFactory.tokenFromKeyword(Keyword.ABSTRACT)
5508 ],
5509 "class A {}");
5510 expect(declaration.documentationComment, isNull);
5511 expect(declaration.abstractKeyword, isNotNull);
5512 expect(declaration.extendsClause, isNull);
5513 expect(declaration.implementsClause, isNull);
5514 expect(declaration.classKeyword, isNotNull);
5515 expect(declaration.leftBracket, isNotNull);
5516 expect(declaration.name, isNotNull);
5517 expect(declaration.members, hasLength(0));
5518 expect(declaration.rightBracket, isNotNull);
5519 expect(declaration.typeParameters, isNull);
5520 }
5521
5522 void test_parseClassDeclaration_empty() {
5523 ClassDeclaration declaration = parse("parseClassDeclaration",
5524 <Object>[emptyCommentAndMetadata(), null], "class A {}");
5525 expect(declaration.documentationComment, isNull);
5526 expect(declaration.abstractKeyword, isNull);
5527 expect(declaration.extendsClause, isNull);
5528 expect(declaration.implementsClause, isNull);
5529 expect(declaration.classKeyword, isNotNull);
5530 expect(declaration.leftBracket, isNotNull);
5531 expect(declaration.name, isNotNull);
5532 expect(declaration.members, hasLength(0));
5533 expect(declaration.rightBracket, isNotNull);
5534 expect(declaration.typeParameters, isNull);
5535 }
5536
5537 void test_parseClassDeclaration_extends() {
5538 ClassDeclaration declaration = parse("parseClassDeclaration",
5539 <Object>[emptyCommentAndMetadata(), null], "class A extends B {}");
5540 expect(declaration.documentationComment, isNull);
5541 expect(declaration.abstractKeyword, isNull);
5542 expect(declaration.extendsClause, isNotNull);
5543 expect(declaration.implementsClause, isNull);
5544 expect(declaration.classKeyword, isNotNull);
5545 expect(declaration.leftBracket, isNotNull);
5546 expect(declaration.name, isNotNull);
5547 expect(declaration.members, hasLength(0));
5548 expect(declaration.rightBracket, isNotNull);
5549 expect(declaration.typeParameters, isNull);
5550 }
5551
5552 void test_parseClassDeclaration_extendsAndImplements() {
5553 ClassDeclaration declaration = parse(
5554 "parseClassDeclaration",
5555 <Object>[emptyCommentAndMetadata(), null],
5556 "class A extends B implements C {}");
5557 expect(declaration.documentationComment, isNull);
5558 expect(declaration.abstractKeyword, isNull);
5559 expect(declaration.extendsClause, isNotNull);
5560 expect(declaration.implementsClause, isNotNull);
5561 expect(declaration.classKeyword, isNotNull);
5562 expect(declaration.leftBracket, isNotNull);
5563 expect(declaration.name, isNotNull);
5564 expect(declaration.members, hasLength(0));
5565 expect(declaration.rightBracket, isNotNull);
5566 expect(declaration.typeParameters, isNull);
5567 }
5568
5569 void test_parseClassDeclaration_extendsAndWith() {
5570 ClassDeclaration declaration = parse(
5571 "parseClassDeclaration",
5572 <Object>[emptyCommentAndMetadata(), null],
5573 "class A extends B with C {}");
5574 expect(declaration.documentationComment, isNull);
5575 expect(declaration.abstractKeyword, isNull);
5576 expect(declaration.classKeyword, isNotNull);
5577 expect(declaration.name, isNotNull);
5578 expect(declaration.typeParameters, isNull);
5579 expect(declaration.extendsClause, isNotNull);
5580 expect(declaration.withClause, isNotNull);
5581 expect(declaration.implementsClause, isNull);
5582 expect(declaration.leftBracket, isNotNull);
5583 expect(declaration.members, hasLength(0));
5584 expect(declaration.rightBracket, isNotNull);
5585 }
5586
5587 void test_parseClassDeclaration_extendsAndWithAndImplements() {
5588 ClassDeclaration declaration = parse(
5589 "parseClassDeclaration",
5590 <Object>[emptyCommentAndMetadata(), null],
5591 "class A extends B with C implements D {}");
5592 expect(declaration.documentationComment, isNull);
5593 expect(declaration.abstractKeyword, isNull);
5594 expect(declaration.classKeyword, isNotNull);
5595 expect(declaration.name, isNotNull);
5596 expect(declaration.typeParameters, isNull);
5597 expect(declaration.extendsClause, isNotNull);
5598 expect(declaration.withClause, isNotNull);
5599 expect(declaration.implementsClause, isNotNull);
5600 expect(declaration.leftBracket, isNotNull);
5601 expect(declaration.members, hasLength(0));
5602 expect(declaration.rightBracket, isNotNull);
5603 }
5604
5605 void test_parseClassDeclaration_implements() {
5606 ClassDeclaration declaration = parse("parseClassDeclaration",
5607 <Object>[emptyCommentAndMetadata(), null], "class A implements C {}");
5608 expect(declaration.documentationComment, isNull);
5609 expect(declaration.abstractKeyword, isNull);
5610 expect(declaration.extendsClause, isNull);
5611 expect(declaration.implementsClause, isNotNull);
5612 expect(declaration.classKeyword, isNotNull);
5613 expect(declaration.leftBracket, isNotNull);
5614 expect(declaration.name, isNotNull);
5615 expect(declaration.members, hasLength(0));
5616 expect(declaration.rightBracket, isNotNull);
5617 expect(declaration.typeParameters, isNull);
5618 }
5619
5620 void test_parseClassDeclaration_native() {
5621 ClassDeclaration declaration = parse(
5622 "parseClassDeclaration",
5623 <Object>[emptyCommentAndMetadata(), null],
5624 "class A native 'nativeValue' {}");
5625 NativeClause nativeClause = declaration.nativeClause;
5626 expect(nativeClause, isNotNull);
5627 expect(nativeClause.nativeKeyword, isNotNull);
5628 expect(nativeClause.name.stringValue, "nativeValue");
5629 expect(nativeClause.beginToken, same(nativeClause.nativeKeyword));
5630 expect(nativeClause.endToken, same(nativeClause.name.endToken));
5631 }
5632
5633 void test_parseClassDeclaration_nonEmpty() {
5634 ClassDeclaration declaration = parse("parseClassDeclaration",
5635 <Object>[emptyCommentAndMetadata(), null], "class A {var f;}");
5636 expect(declaration.documentationComment, isNull);
5637 expect(declaration.abstractKeyword, isNull);
5638 expect(declaration.extendsClause, isNull);
5639 expect(declaration.implementsClause, isNull);
5640 expect(declaration.classKeyword, isNotNull);
5641 expect(declaration.leftBracket, isNotNull);
5642 expect(declaration.name, isNotNull);
5643 expect(declaration.members, hasLength(1));
5644 expect(declaration.rightBracket, isNotNull);
5645 expect(declaration.typeParameters, isNull);
5646 }
5647
5648 void test_parseClassDeclaration_typeAlias_implementsC() {
5649 ClassTypeAlias typeAlias = parse(
5650 "parseClassDeclaration",
5651 <Object>[emptyCommentAndMetadata(), null],
5652 "class A = Object with B implements C;");
5653 expect(typeAlias.typedefKeyword, isNotNull);
5654 expect(typeAlias.name, isNotNull);
5655 expect(typeAlias.typeParameters, isNull);
5656 expect(typeAlias.withClause, isNotNull);
5657 expect(typeAlias.implementsClause, isNotNull);
5658 expect(typeAlias.implementsClause.implementsKeyword, isNotNull);
5659 expect(typeAlias.implementsClause.interfaces.length, 1);
5660 expect(typeAlias.semicolon, isNotNull);
5661 }
5662
5663 void test_parseClassDeclaration_typeAlias_withB() {
5664 ClassTypeAlias typeAlias = parse("parseClassDeclaration",
5665 <Object>[emptyCommentAndMetadata(), null], "class A = Object with B;");
5666 expect(typeAlias.typedefKeyword, isNotNull);
5667 expect(typeAlias.name, isNotNull);
5668 expect(typeAlias.typeParameters, isNull);
5669 expect(typeAlias.withClause, isNotNull);
5670 expect(typeAlias.withClause.withKeyword, isNotNull);
5671 expect(typeAlias.withClause.mixinTypes.length, 1);
5672 expect(typeAlias.implementsClause, isNull);
5673 expect(typeAlias.semicolon, isNotNull);
5674 }
5675
5676 void test_parseClassDeclaration_typeParameters() {
5677 ClassDeclaration declaration = parse("parseClassDeclaration",
5678 <Object>[emptyCommentAndMetadata(), null], "class A<B> {}");
5679 expect(declaration.documentationComment, isNull);
5680 expect(declaration.abstractKeyword, isNull);
5681 expect(declaration.extendsClause, isNull);
5682 expect(declaration.implementsClause, isNull);
5683 expect(declaration.classKeyword, isNotNull);
5684 expect(declaration.leftBracket, isNotNull);
5685 expect(declaration.name, isNotNull);
5686 expect(declaration.members, hasLength(0));
5687 expect(declaration.rightBracket, isNotNull);
5688 expect(declaration.typeParameters, isNotNull);
5689 expect(declaration.typeParameters.typeParameters, hasLength(1));
5690 }
5691
5692 void test_parseClassMember_constructor_withInitializers() {
5693 // TODO(brianwilkerson) Test other kinds of class members: fields, getters
5694 // and setters.
5695 ConstructorDeclaration constructor = parse("parseClassMember",
5696 <Object>["C"], "C(_, _\$, this.__) : _a = _ + _\$ {}");
5697 expect(constructor.body, isNotNull);
5698 expect(constructor.separator, isNotNull);
5699 expect(constructor.externalKeyword, isNull);
5700 expect(constructor.constKeyword, isNull);
5701 expect(constructor.factoryKeyword, isNull);
5702 expect(constructor.name, isNull);
5703 expect(constructor.parameters, isNotNull);
5704 expect(constructor.period, isNull);
5705 expect(constructor.returnType, isNotNull);
5706 expect(constructor.initializers, hasLength(1));
5707 }
5708
5709 void test_parseClassMember_field_instance_prefixedType() {
5710 FieldDeclaration field = parse("parseClassMember", <Object>["C"], "p.A f;");
5711 expect(field.documentationComment, isNull);
5712 expect(field.metadata, hasLength(0));
5713 expect(field.staticKeyword, isNull);
5714 VariableDeclarationList list = field.fields;
5715 expect(list, isNotNull);
5716 NodeList<VariableDeclaration> variables = list.variables;
5717 expect(variables, hasLength(1));
5718 VariableDeclaration variable = variables[0];
5719 expect(variable.name, isNotNull);
5720 }
5721
5722 void test_parseClassMember_field_namedGet() {
5723 FieldDeclaration field =
5724 parse("parseClassMember", <Object>["C"], "var get;");
5725 expect(field.documentationComment, isNull);
5726 expect(field.metadata, hasLength(0));
5727 expect(field.staticKeyword, isNull);
5728 VariableDeclarationList list = field.fields;
5729 expect(list, isNotNull);
5730 NodeList<VariableDeclaration> variables = list.variables;
5731 expect(variables, hasLength(1));
5732 VariableDeclaration variable = variables[0];
5733 expect(variable.name, isNotNull);
5734 }
5735
5736 void test_parseClassMember_field_namedOperator() {
5737 FieldDeclaration field =
5738 parse("parseClassMember", <Object>["C"], "var operator;");
5739 expect(field.documentationComment, isNull);
5740 expect(field.metadata, hasLength(0));
5741 expect(field.staticKeyword, isNull);
5742 VariableDeclarationList list = field.fields;
5743 expect(list, isNotNull);
5744 NodeList<VariableDeclaration> variables = list.variables;
5745 expect(variables, hasLength(1));
5746 VariableDeclaration variable = variables[0];
5747 expect(variable.name, isNotNull);
5748 }
5749
5750 void test_parseClassMember_field_namedOperator_withAssignment() {
5751 FieldDeclaration field =
5752 parse("parseClassMember", <Object>["C"], "var operator = (5);");
5753 expect(field.documentationComment, isNull);
5754 expect(field.metadata, hasLength(0));
5755 expect(field.staticKeyword, isNull);
5756 VariableDeclarationList list = field.fields;
5757 expect(list, isNotNull);
5758 NodeList<VariableDeclaration> variables = list.variables;
5759 expect(variables, hasLength(1));
5760 VariableDeclaration variable = variables[0];
5761 expect(variable.name, isNotNull);
5762 expect(variable.initializer, isNotNull);
5763 }
5764
5765 void test_parseClassMember_field_namedSet() {
5766 FieldDeclaration field =
5767 parse("parseClassMember", <Object>["C"], "var set;");
5768 expect(field.documentationComment, isNull);
5769 expect(field.metadata, hasLength(0));
5770 expect(field.staticKeyword, isNull);
5771 VariableDeclarationList list = field.fields;
5772 expect(list, isNotNull);
5773 NodeList<VariableDeclaration> variables = list.variables;
5774 expect(variables, hasLength(1));
5775 VariableDeclaration variable = variables[0];
5776 expect(variable.name, isNotNull);
5777 }
5778
5779 void test_parseClassMember_getter_void() {
5780 MethodDeclaration method =
5781 parse("parseClassMember", <Object>["C"], "void get g {}");
5782 expect(method.documentationComment, isNull);
5783 expect(method.externalKeyword, isNull);
5784 expect(method.modifierKeyword, isNull);
5785 expect(method.propertyKeyword, isNotNull);
5786 expect(method.returnType, isNotNull);
5787 expect(method.name, isNotNull);
5788 expect(method.operatorKeyword, isNull);
5789 expect(method.body, isNotNull);
5790 expect(method.parameters, isNull);
5791 }
5792
5793 void test_parseClassMember_method_external() {
5794 MethodDeclaration method =
5795 parse("parseClassMember", <Object>["C"], "external m();");
5796 expect(method.body, isNotNull);
5797 expect(method.documentationComment, isNull);
5798 expect(method.externalKeyword, isNotNull);
5799 expect(method.modifierKeyword, isNull);
5800 expect(method.name, isNotNull);
5801 expect(method.operatorKeyword, isNull);
5802 expect(method.typeParameters, isNull);
5803 expect(method.parameters, isNotNull);
5804 expect(method.propertyKeyword, isNull);
5805 expect(method.returnType, isNull);
5806 }
5807
5808 void test_parseClassMember_method_external_withTypeAndArgs() {
5809 MethodDeclaration method =
5810 parse("parseClassMember", <Object>["C"], "external int m(int a);");
5811 expect(method.body, isNotNull);
5812 expect(method.documentationComment, isNull);
5813 expect(method.externalKeyword, isNotNull);
5814 expect(method.modifierKeyword, isNull);
5815 expect(method.name, isNotNull);
5816 expect(method.operatorKeyword, isNull);
5817 expect(method.typeParameters, isNull);
5818 expect(method.parameters, isNotNull);
5819 expect(method.propertyKeyword, isNull);
5820 expect(method.returnType, isNotNull);
5821 }
5822
5823 void test_parseClassMember_method_generic_noReturnType() {
5824 enableGenericMethods = true;
5825 MethodDeclaration method =
5826 parse("parseClassMember", <Object>["C"], "m<T>() {}");
5827 expect(method.documentationComment, isNull);
5828 expect(method.externalKeyword, isNull);
5829 expect(method.modifierKeyword, isNull);
5830 expect(method.propertyKeyword, isNull);
5831 expect(method.returnType, isNull);
5832 expect(method.name, isNotNull);
5833 expect(method.operatorKeyword, isNull);
5834 expect(method.typeParameters, isNotNull);
5835 expect(method.parameters, isNotNull);
5836 expect(method.body, isNotNull);
5837 }
5838
5839 void test_parseClassMember_method_generic_returnType() {
5840 enableGenericMethods = true;
5841 MethodDeclaration method =
5842 parse("parseClassMember", <Object>["C"], "T m<T>() {}");
5843 expect(method.documentationComment, isNull);
5844 expect(method.externalKeyword, isNull);
5845 expect(method.modifierKeyword, isNull);
5846 expect(method.propertyKeyword, isNull);
5847 expect(method.returnType, isNotNull);
5848 expect(method.name, isNotNull);
5849 expect(method.operatorKeyword, isNull);
5850 expect(method.typeParameters, isNotNull);
5851 expect(method.parameters, isNotNull);
5852 expect(method.body, isNotNull);
5853 }
5854
5855 void test_parseClassMember_method_generic_void() {
5856 enableGenericMethods = true;
5857 MethodDeclaration method =
5858 parse("parseClassMember", <Object>["C"], "void m<T>() {}");
5859 expect(method.documentationComment, isNull);
5860 expect(method.externalKeyword, isNull);
5861 expect(method.modifierKeyword, isNull);
5862 expect(method.propertyKeyword, isNull);
5863 expect(method.returnType, isNotNull);
5864 expect(method.name, isNotNull);
5865 expect(method.operatorKeyword, isNull);
5866 expect(method.typeParameters, isNotNull);
5867 expect(method.parameters, isNotNull);
5868 expect(method.body, isNotNull);
5869 }
5870
5871 void test_parseClassMember_method_get_noType() {
5872 MethodDeclaration method =
5873 parse("parseClassMember", <Object>["C"], "get() {}");
5874 expect(method.documentationComment, isNull);
5875 expect(method.externalKeyword, isNull);
5876 expect(method.modifierKeyword, isNull);
5877 expect(method.propertyKeyword, isNull);
5878 expect(method.returnType, isNull);
5879 expect(method.name, isNotNull);
5880 expect(method.operatorKeyword, isNull);
5881 expect(method.typeParameters, isNull);
5882 expect(method.parameters, isNotNull);
5883 expect(method.body, isNotNull);
5884 }
5885
5886 void test_parseClassMember_method_get_type() {
5887 MethodDeclaration method =
5888 parse("parseClassMember", <Object>["C"], "int get() {}");
5889 expect(method.documentationComment, isNull);
5890 expect(method.externalKeyword, isNull);
5891 expect(method.modifierKeyword, isNull);
5892 expect(method.propertyKeyword, isNull);
5893 expect(method.returnType, isNotNull);
5894 expect(method.name, isNotNull);
5895 expect(method.operatorKeyword, isNull);
5896 expect(method.typeParameters, isNull);
5897 expect(method.parameters, isNotNull);
5898 expect(method.body, isNotNull);
5899 }
5900
5901 void test_parseClassMember_method_get_void() {
5902 MethodDeclaration method =
5903 parse("parseClassMember", <Object>["C"], "void get() {}");
5904 expect(method.documentationComment, isNull);
5905 expect(method.externalKeyword, isNull);
5906 expect(method.modifierKeyword, isNull);
5907 expect(method.propertyKeyword, isNull);
5908 expect(method.returnType, isNotNull);
5909 expect(method.name, isNotNull);
5910 expect(method.operatorKeyword, isNull);
5911 expect(method.typeParameters, isNull);
5912 expect(method.parameters, isNotNull);
5913 expect(method.body, isNotNull);
5914 }
5915
5916 void test_parseClassMember_method_operator_noType() {
5917 MethodDeclaration method =
5918 parse("parseClassMember", <Object>["C"], "operator() {}");
5919 expect(method.documentationComment, isNull);
5920 expect(method.externalKeyword, isNull);
5921 expect(method.modifierKeyword, isNull);
5922 expect(method.propertyKeyword, isNull);
5923 expect(method.returnType, isNull);
5924 expect(method.name, isNotNull);
5925 expect(method.operatorKeyword, isNull);
5926 expect(method.typeParameters, isNull);
5927 expect(method.parameters, isNotNull);
5928 expect(method.body, isNotNull);
5929 }
5930
5931 void test_parseClassMember_method_operator_type() {
5932 MethodDeclaration method =
5933 parse("parseClassMember", <Object>["C"], "int operator() {}");
5934 expect(method.documentationComment, isNull);
5935 expect(method.externalKeyword, isNull);
5936 expect(method.modifierKeyword, isNull);
5937 expect(method.propertyKeyword, isNull);
5938 expect(method.returnType, isNotNull);
5939 expect(method.name, isNotNull);
5940 expect(method.operatorKeyword, isNull);
5941 expect(method.typeParameters, isNull);
5942 expect(method.parameters, isNotNull);
5943 expect(method.body, isNotNull);
5944 }
5945
5946 void test_parseClassMember_method_operator_void() {
5947 MethodDeclaration method =
5948 parse("parseClassMember", <Object>["C"], "void operator() {}");
5949 expect(method.documentationComment, isNull);
5950 expect(method.externalKeyword, isNull);
5951 expect(method.modifierKeyword, isNull);
5952 expect(method.propertyKeyword, isNull);
5953 expect(method.returnType, isNotNull);
5954 expect(method.name, isNotNull);
5955 expect(method.operatorKeyword, isNull);
5956 expect(method.typeParameters, isNull);
5957 expect(method.parameters, isNotNull);
5958 expect(method.body, isNotNull);
5959 }
5960
5961 void test_parseClassMember_method_returnType_parameterized() {
5962 MethodDeclaration method =
5963 parse("parseClassMember", <Object>["C"], "p.A m() {}");
5964 expect(method.documentationComment, isNull);
5965 expect(method.externalKeyword, isNull);
5966 expect(method.modifierKeyword, isNull);
5967 expect(method.propertyKeyword, isNull);
5968 expect(method.returnType, isNotNull);
5969 expect(method.name, isNotNull);
5970 expect(method.operatorKeyword, isNull);
5971 expect(method.typeParameters, isNull);
5972 expect(method.parameters, isNotNull);
5973 expect(method.body, isNotNull);
5974 }
5975
5976 void test_parseClassMember_method_set_noType() {
5977 MethodDeclaration method =
5978 parse("parseClassMember", <Object>["C"], "set() {}");
5979 expect(method.documentationComment, isNull);
5980 expect(method.externalKeyword, isNull);
5981 expect(method.modifierKeyword, isNull);
5982 expect(method.propertyKeyword, isNull);
5983 expect(method.returnType, isNull);
5984 expect(method.name, isNotNull);
5985 expect(method.operatorKeyword, isNull);
5986 expect(method.typeParameters, isNull);
5987 expect(method.parameters, isNotNull);
5988 expect(method.body, isNotNull);
5989 }
5990
5991 void test_parseClassMember_method_set_type() {
5992 MethodDeclaration method =
5993 parse("parseClassMember", <Object>["C"], "int set() {}");
5994 expect(method.documentationComment, isNull);
5995 expect(method.externalKeyword, isNull);
5996 expect(method.modifierKeyword, isNull);
5997 expect(method.propertyKeyword, isNull);
5998 expect(method.returnType, isNotNull);
5999 expect(method.name, isNotNull);
6000 expect(method.operatorKeyword, isNull);
6001 expect(method.typeParameters, isNull);
6002 expect(method.parameters, isNotNull);
6003 expect(method.body, isNotNull);
6004 }
6005
6006 void test_parseClassMember_method_set_void() {
6007 MethodDeclaration method =
6008 parse("parseClassMember", <Object>["C"], "void set() {}");
6009 expect(method.documentationComment, isNull);
6010 expect(method.externalKeyword, isNull);
6011 expect(method.modifierKeyword, isNull);
6012 expect(method.propertyKeyword, isNull);
6013 expect(method.returnType, isNotNull);
6014 expect(method.name, isNotNull);
6015 expect(method.operatorKeyword, isNull);
6016 expect(method.typeParameters, isNull);
6017 expect(method.parameters, isNotNull);
6018 expect(method.body, isNotNull);
6019 }
6020
6021 void test_parseClassMember_operator_index() {
6022 MethodDeclaration method =
6023 parse("parseClassMember", <Object>["C"], "int operator [](int i) {}");
6024 expect(method.documentationComment, isNull);
6025 expect(method.externalKeyword, isNull);
6026 expect(method.modifierKeyword, isNull);
6027 expect(method.propertyKeyword, isNull);
6028 expect(method.returnType, isNotNull);
6029 expect(method.name, isNotNull);
6030 expect(method.operatorKeyword, isNotNull);
6031 expect(method.typeParameters, isNull);
6032 expect(method.parameters, isNotNull);
6033 expect(method.body, isNotNull);
6034 }
6035
6036 void test_parseClassMember_operator_indexAssign() {
6037 MethodDeclaration method =
6038 parse("parseClassMember", <Object>["C"], "int operator []=(int i) {}");
6039 expect(method.documentationComment, isNull);
6040 expect(method.externalKeyword, isNull);
6041 expect(method.modifierKeyword, isNull);
6042 expect(method.propertyKeyword, isNull);
6043 expect(method.returnType, isNotNull);
6044 expect(method.name, isNotNull);
6045 expect(method.operatorKeyword, isNotNull);
6046 expect(method.typeParameters, isNull);
6047 expect(method.parameters, isNotNull);
6048 expect(method.body, isNotNull);
6049 }
6050
6051 void test_parseClassMember_redirectingFactory_const() {
6052 ConstructorDeclaration constructor =
6053 parse("parseClassMember", <Object>["C"], "const factory C() = B;");
6054 expect(constructor.externalKeyword, isNull);
6055 expect(constructor.constKeyword, isNotNull);
6056 expect(constructor.factoryKeyword, isNotNull);
6057 expect(constructor.returnType, isNotNull);
6058 expect(constructor.period, isNull);
6059 expect(constructor.name, isNull);
6060 expect(constructor.parameters, isNotNull);
6061 expect(constructor.separator, isNotNull);
6062 expect(constructor.initializers, hasLength(0));
6063 expect(constructor.redirectedConstructor, isNotNull);
6064 expect(constructor.body, isNotNull);
6065 }
6066
6067 void test_parseClassMember_redirectingFactory_nonConst() {
6068 ConstructorDeclaration constructor =
6069 parse("parseClassMember", <Object>["C"], "factory C() = B;");
6070 expect(constructor.externalKeyword, isNull);
6071 expect(constructor.constKeyword, isNull);
6072 expect(constructor.factoryKeyword, isNotNull);
6073 expect(constructor.returnType, isNotNull);
6074 expect(constructor.period, isNull);
6075 expect(constructor.name, isNull);
6076 expect(constructor.parameters, isNotNull);
6077 expect(constructor.separator, isNotNull);
6078 expect(constructor.initializers, hasLength(0));
6079 expect(constructor.redirectedConstructor, isNotNull);
6080 expect(constructor.body, isNotNull);
6081 }
6082
6083 void test_parseClassTypeAlias_abstract() {
6084 Token classToken = TokenFactory.tokenFromKeyword(Keyword.CLASS);
6085 Token abstractToken = TokenFactory.tokenFromKeyword(Keyword.ABSTRACT);
6086 ClassTypeAlias classTypeAlias = parse(
6087 "parseClassTypeAlias",
6088 <Object>[emptyCommentAndMetadata(), abstractToken, classToken],
6089 "A = B with C;");
6090 expect(classTypeAlias.typedefKeyword, isNotNull);
6091 expect(classTypeAlias.name.name, "A");
6092 expect(classTypeAlias.equals, isNotNull);
6093 expect(classTypeAlias.abstractKeyword, isNotNull);
6094 expect(classTypeAlias.superclass.name.name, isNotNull, reason: "B");
6095 expect(classTypeAlias.withClause, isNotNull);
6096 expect(classTypeAlias.implementsClause, isNull);
6097 expect(classTypeAlias.semicolon, isNotNull);
6098 }
6099
6100 void test_parseClassTypeAlias_implements() {
6101 Token token = TokenFactory.tokenFromKeyword(Keyword.CLASS);
6102 ClassTypeAlias classTypeAlias = parse(
6103 "parseClassTypeAlias",
6104 <Object>[emptyCommentAndMetadata(), null, token],
6105 "A = B with C implements D;");
6106 expect(classTypeAlias.typedefKeyword, isNotNull);
6107 expect(classTypeAlias.name.name, "A");
6108 expect(classTypeAlias.equals, isNotNull);
6109 expect(classTypeAlias.abstractKeyword, isNull);
6110 expect(classTypeAlias.superclass.name.name, isNotNull, reason: "B");
6111 expect(classTypeAlias.withClause, isNotNull);
6112 expect(classTypeAlias.implementsClause, isNotNull);
6113 expect(classTypeAlias.semicolon, isNotNull);
6114 }
6115
6116 void test_parseClassTypeAlias_with() {
6117 Token token = TokenFactory.tokenFromKeyword(Keyword.CLASS);
6118 ClassTypeAlias classTypeAlias = parse("parseClassTypeAlias",
6119 <Object>[emptyCommentAndMetadata(), null, token], "A = B with C;");
6120 expect(classTypeAlias.typedefKeyword, isNotNull);
6121 expect(classTypeAlias.name.name, "A");
6122 expect(classTypeAlias.equals, isNotNull);
6123 expect(classTypeAlias.abstractKeyword, isNull);
6124 expect(classTypeAlias.superclass.name.name, isNotNull, reason: "B");
6125 expect(classTypeAlias.withClause, isNotNull);
6126 expect(classTypeAlias.implementsClause, isNull);
6127 expect(classTypeAlias.semicolon, isNotNull);
6128 }
6129
6130 void test_parseClassTypeAlias_with_implements() {
6131 Token token = TokenFactory.tokenFromKeyword(Keyword.CLASS);
6132 ClassTypeAlias classTypeAlias = parse(
6133 "parseClassTypeAlias",
6134 <Object>[emptyCommentAndMetadata(), null, token],
6135 "A = B with C implements D;");
6136 expect(classTypeAlias.typedefKeyword, isNotNull);
6137 expect(classTypeAlias.name.name, "A");
6138 expect(classTypeAlias.equals, isNotNull);
6139 expect(classTypeAlias.abstractKeyword, isNull);
6140 expect(classTypeAlias.superclass.name.name, isNotNull, reason: "B");
6141 expect(classTypeAlias.withClause, isNotNull);
6142 expect(classTypeAlias.implementsClause, isNotNull);
6143 expect(classTypeAlias.semicolon, isNotNull);
6144 }
6145
6146 void test_parseCombinator_hide() {
6147 HideCombinator combinator = parse4('parseCombinator', 'hide a;');
6148 expect(combinator, new isInstanceOf<HideCombinator>());
6149 expect(combinator.keyword, isNotNull);
6150 expect(combinator.hiddenNames, hasLength(1));
6151 }
6152
6153 void test_parseCombinator_show() {
6154 ShowCombinator combinator = parse4('parseCombinator', 'show a;');
6155 expect(combinator, new isInstanceOf<ShowCombinator>());
6156 expect(combinator.keyword, isNotNull);
6157 expect(combinator.shownNames, hasLength(1));
6158 }
6159
6160 void test_parseCombinators_h() {
6161 List<Combinator> combinators = parse4("parseCombinators", "hide a;");
6162 expect(combinators, hasLength(1));
6163 HideCombinator combinator = combinators[0] as HideCombinator;
6164 expect(combinator, isNotNull);
6165 expect(combinator.keyword, isNotNull);
6166 expect(combinator.hiddenNames, hasLength(1));
6167 }
6168
6169 void test_parseCombinators_hs() {
6170 List<Combinator> combinators = parse4("parseCombinators", "hide a show b;");
6171 expect(combinators, hasLength(2));
6172 HideCombinator hideCombinator = combinators[0] as HideCombinator;
6173 expect(hideCombinator, isNotNull);
6174 expect(hideCombinator.keyword, isNotNull);
6175 expect(hideCombinator.hiddenNames, hasLength(1));
6176 ShowCombinator showCombinator = combinators[1] as ShowCombinator;
6177 expect(showCombinator, isNotNull);
6178 expect(showCombinator.keyword, isNotNull);
6179 expect(showCombinator.shownNames, hasLength(1));
6180 }
6181
6182 void test_parseCombinators_hshs() {
6183 List<Combinator> combinators =
6184 parse4("parseCombinators", "hide a show b hide c show d;");
6185 expect(combinators, hasLength(4));
6186 }
6187
6188 void test_parseCombinators_s() {
6189 List<Combinator> combinators = parse4("parseCombinators", "show a;");
6190 expect(combinators, hasLength(1));
6191 ShowCombinator combinator = combinators[0] as ShowCombinator;
6192 expect(combinator, isNotNull);
6193 expect(combinator.keyword, isNotNull);
6194 expect(combinator.shownNames, hasLength(1));
6195 }
6196
6197 void test_parseCommentAndMetadata_c() {
6198 CommentAndMetadata commentAndMetadata =
6199 parse4("parseCommentAndMetadata", "/** 1 */ void");
6200 expect(commentAndMetadata.comment, isNotNull);
6201 expect(commentAndMetadata.metadata, hasLength(0));
6202 }
6203
6204 void test_parseCommentAndMetadata_cmc() {
6205 CommentAndMetadata commentAndMetadata =
6206 parse4("parseCommentAndMetadata", "/** 1 */ @A /** 2 */ void");
6207 expect(commentAndMetadata.comment, isNotNull);
6208 expect(commentAndMetadata.metadata, hasLength(1));
6209 }
6210
6211 void test_parseCommentAndMetadata_cmcm() {
6212 CommentAndMetadata commentAndMetadata =
6213 parse4("parseCommentAndMetadata", "/** 1 */ @A /** 2 */ @B void");
6214 expect(commentAndMetadata.comment, isNotNull);
6215 expect(commentAndMetadata.metadata, hasLength(2));
6216 }
6217
6218 void test_parseCommentAndMetadata_cmm() {
6219 CommentAndMetadata commentAndMetadata =
6220 parse4("parseCommentAndMetadata", "/** 1 */ @A @B void");
6221 expect(commentAndMetadata.comment, isNotNull);
6222 expect(commentAndMetadata.metadata, hasLength(2));
6223 }
6224
6225 void test_parseCommentAndMetadata_m() {
6226 CommentAndMetadata commentAndMetadata =
6227 parse4("parseCommentAndMetadata", "@A void");
6228 expect(commentAndMetadata.comment, isNull);
6229 expect(commentAndMetadata.metadata, hasLength(1));
6230 }
6231
6232 void test_parseCommentAndMetadata_mcm() {
6233 CommentAndMetadata commentAndMetadata =
6234 parse4("parseCommentAndMetadata", "@A /** 1 */ @B void");
6235 expect(commentAndMetadata.comment, isNotNull);
6236 expect(commentAndMetadata.metadata, hasLength(2));
6237 }
6238
6239 void test_parseCommentAndMetadata_mcmc() {
6240 CommentAndMetadata commentAndMetadata =
6241 parse4("parseCommentAndMetadata", "@A /** 1 */ @B /** 2 */ void");
6242 expect(commentAndMetadata.comment, isNotNull);
6243 expect(commentAndMetadata.metadata, hasLength(2));
6244 }
6245
6246 void test_parseCommentAndMetadata_mm() {
6247 CommentAndMetadata commentAndMetadata =
6248 parse4("parseCommentAndMetadata", "@A @B(x) void");
6249 expect(commentAndMetadata.comment, isNull);
6250 expect(commentAndMetadata.metadata, hasLength(2));
6251 }
6252
6253 void test_parseCommentAndMetadata_none() {
6254 CommentAndMetadata commentAndMetadata =
6255 parse4("parseCommentAndMetadata", "void");
6256 expect(commentAndMetadata.comment, isNull);
6257 expect(commentAndMetadata.metadata, hasLength(0));
6258 }
6259
6260 void test_parseCommentAndMetadata_singleLine() {
6261 CommentAndMetadata commentAndMetadata = parse4(
6262 "parseCommentAndMetadata",
6263 r'''
6264 /// 1
6265 /// 2
6266 void''');
6267 expect(commentAndMetadata.comment, isNotNull);
6268 expect(commentAndMetadata.metadata, hasLength(0));
6269 }
6270
6271 void test_parseCommentReference_new_prefixed() {
6272 CommentReference reference =
6273 parse("parseCommentReference", <Object>["new a.b", 7], "");
6274 PrefixedIdentifier prefixedIdentifier = EngineTestCase.assertInstanceOf(
6275 (obj) => obj is PrefixedIdentifier,
6276 PrefixedIdentifier,
6277 reference.identifier);
6278 SimpleIdentifier prefix = prefixedIdentifier.prefix;
6279 expect(prefix.token, isNotNull);
6280 expect(prefix.name, "a");
6281 expect(prefix.offset, 11);
6282 expect(prefixedIdentifier.period, isNotNull);
6283 SimpleIdentifier identifier = prefixedIdentifier.identifier;
6284 expect(identifier.token, isNotNull);
6285 expect(identifier.name, "b");
6286 expect(identifier.offset, 13);
6287 }
6288
6289 void test_parseCommentReference_new_simple() {
6290 CommentReference reference =
6291 parse("parseCommentReference", <Object>["new a", 5], "");
6292 SimpleIdentifier identifier = EngineTestCase.assertInstanceOf(
6293 (obj) => obj is SimpleIdentifier,
6294 SimpleIdentifier,
6295 reference.identifier);
6296 expect(identifier.token, isNotNull);
6297 expect(identifier.name, "a");
6298 expect(identifier.offset, 9);
6299 }
6300
6301 void test_parseCommentReference_prefixed() {
6302 CommentReference reference =
6303 parse("parseCommentReference", <Object>["a.b", 7], "");
6304 PrefixedIdentifier prefixedIdentifier = EngineTestCase.assertInstanceOf(
6305 (obj) => obj is PrefixedIdentifier,
6306 PrefixedIdentifier,
6307 reference.identifier);
6308 SimpleIdentifier prefix = prefixedIdentifier.prefix;
6309 expect(prefix.token, isNotNull);
6310 expect(prefix.name, "a");
6311 expect(prefix.offset, 7);
6312 expect(prefixedIdentifier.period, isNotNull);
6313 SimpleIdentifier identifier = prefixedIdentifier.identifier;
6314 expect(identifier.token, isNotNull);
6315 expect(identifier.name, "b");
6316 expect(identifier.offset, 9);
6317 }
6318
6319 void test_parseCommentReference_simple() {
6320 CommentReference reference =
6321 parse("parseCommentReference", <Object>["a", 5], "");
6322 SimpleIdentifier identifier = EngineTestCase.assertInstanceOf(
6323 (obj) => obj is SimpleIdentifier,
6324 SimpleIdentifier,
6325 reference.identifier);
6326 expect(identifier.token, isNotNull);
6327 expect(identifier.name, "a");
6328 expect(identifier.offset, 5);
6329 }
6330
6331 void test_parseCommentReference_synthetic() {
6332 CommentReference reference =
6333 parse("parseCommentReference", <Object>["", 5], "");
6334 SimpleIdentifier identifier = EngineTestCase.assertInstanceOf(
6335 (obj) => obj is SimpleIdentifier,
6336 SimpleIdentifier,
6337 reference.identifier);
6338 expect(identifier, isNotNull);
6339 expect(identifier.isSynthetic, isTrue);
6340 expect(identifier.token, isNotNull);
6341 expect(identifier.name, "");
6342 expect(identifier.offset, 5);
6343 }
6344
6345 void test_parseCommentReferences_multiLine() {
6346 DocumentationCommentToken token = new DocumentationCommentToken(
6347 TokenType.MULTI_LINE_COMMENT, "/** xxx [a] yyy [bb] zzz */", 3);
6348 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[token];
6349 List<CommentReference> references =
6350 parse("parseCommentReferences", <Object>[tokens], "");
6351 List<Token> tokenReferences = token.references;
6352 expect(references, hasLength(2));
6353 expect(tokenReferences, hasLength(2));
6354 {
6355 CommentReference reference = references[0];
6356 expect(reference, isNotNull);
6357 expect(reference.identifier, isNotNull);
6358 expect(reference.offset, 12);
6359 // the reference is recorded in the comment token
6360 Token referenceToken = tokenReferences[0];
6361 expect(referenceToken.offset, 12);
6362 expect(referenceToken.lexeme, 'a');
6363 }
6364 {
6365 CommentReference reference = references[1];
6366 expect(reference, isNotNull);
6367 expect(reference.identifier, isNotNull);
6368 expect(reference.offset, 20);
6369 // the reference is recorded in the comment token
6370 Token referenceToken = tokenReferences[1];
6371 expect(referenceToken.offset, 20);
6372 expect(referenceToken.lexeme, 'bb');
6373 }
6374 }
6375
6376 void test_parseCommentReferences_notClosed_noIdentifier() {
6377 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6378 new DocumentationCommentToken(
6379 TokenType.MULTI_LINE_COMMENT, "/** [ some text", 5)
6380 ];
6381 List<CommentReference> references =
6382 parse("parseCommentReferences", <Object>[tokens], "");
6383 expect(references, hasLength(1));
6384 CommentReference reference = references[0];
6385 expect(reference, isNotNull);
6386 expect(reference.identifier, isNotNull);
6387 expect(reference.identifier.isSynthetic, isTrue);
6388 expect(reference.identifier.name, "");
6389 }
6390
6391 void test_parseCommentReferences_notClosed_withIdentifier() {
6392 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6393 new DocumentationCommentToken(
6394 TokenType.MULTI_LINE_COMMENT, "/** [namePrefix some text", 5)
6395 ];
6396 List<CommentReference> references =
6397 parse("parseCommentReferences", <Object>[tokens], "");
6398 expect(references, hasLength(1));
6399 CommentReference reference = references[0];
6400 expect(reference, isNotNull);
6401 expect(reference.identifier, isNotNull);
6402 expect(reference.identifier.isSynthetic, isFalse);
6403 expect(reference.identifier.name, "namePrefix");
6404 }
6405
6406 void test_parseCommentReferences_singleLine() {
6407 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6408 new DocumentationCommentToken(
6409 TokenType.SINGLE_LINE_COMMENT, "/// xxx [a] yyy [b] zzz", 3),
6410 new DocumentationCommentToken(
6411 TokenType.SINGLE_LINE_COMMENT, "/// x [c]", 28)
6412 ];
6413 List<CommentReference> references =
6414 parse("parseCommentReferences", <Object>[tokens], "");
6415 expect(references, hasLength(3));
6416 CommentReference reference = references[0];
6417 expect(reference, isNotNull);
6418 expect(reference.identifier, isNotNull);
6419 expect(reference.offset, 12);
6420 reference = references[1];
6421 expect(reference, isNotNull);
6422 expect(reference.identifier, isNotNull);
6423 expect(reference.offset, 20);
6424 reference = references[2];
6425 expect(reference, isNotNull);
6426 expect(reference.identifier, isNotNull);
6427 expect(reference.offset, 35);
6428 }
6429
6430 void test_parseCommentReferences_skipCodeBlock_bracketed() {
6431 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6432 new DocumentationCommentToken(
6433 TokenType.MULTI_LINE_COMMENT, "/** [:xxx [a] yyy:] [b] zzz */", 3)
6434 ];
6435 List<CommentReference> references =
6436 parse("parseCommentReferences", <Object>[tokens], "");
6437 expect(references, hasLength(1));
6438 CommentReference reference = references[0];
6439 expect(reference, isNotNull);
6440 expect(reference.identifier, isNotNull);
6441 expect(reference.offset, 24);
6442 }
6443
6444 void test_parseCommentReferences_skipCodeBlock_spaces() {
6445 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6446 new DocumentationCommentToken(TokenType.MULTI_LINE_COMMENT,
6447 "/**\n * a[i]\n * xxx [i] zzz\n */", 3)
6448 ];
6449 List<CommentReference> references =
6450 parse("parseCommentReferences", <Object>[tokens], "");
6451 expect(references, hasLength(1));
6452 CommentReference reference = references[0];
6453 expect(reference, isNotNull);
6454 expect(reference.identifier, isNotNull);
6455 expect(reference.offset, 27);
6456 }
6457
6458 void test_parseCommentReferences_skipLinkDefinition() {
6459 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6460 new DocumentationCommentToken(TokenType.MULTI_LINE_COMMENT,
6461 "/** [a]: http://www.google.com (Google) [b] zzz */", 3)
6462 ];
6463 List<CommentReference> references =
6464 parse("parseCommentReferences", <Object>[tokens], "");
6465 expect(references, hasLength(1));
6466 CommentReference reference = references[0];
6467 expect(reference, isNotNull);
6468 expect(reference.identifier, isNotNull);
6469 expect(reference.offset, 44);
6470 }
6471
6472 void test_parseCommentReferences_skipLinked() {
6473 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6474 new DocumentationCommentToken(TokenType.MULTI_LINE_COMMENT,
6475 "/** [a](http://www.google.com) [b] zzz */", 3)
6476 ];
6477 List<CommentReference> references =
6478 parse("parseCommentReferences", <Object>[tokens], "");
6479 expect(references, hasLength(1));
6480 CommentReference reference = references[0];
6481 expect(reference, isNotNull);
6482 expect(reference.identifier, isNotNull);
6483 expect(reference.offset, 35);
6484 }
6485
6486 void test_parseCommentReferences_skipReferenceLink() {
6487 List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6488 new DocumentationCommentToken(
6489 TokenType.MULTI_LINE_COMMENT, "/** [a][c] [b] zzz */", 3)
6490 ];
6491 List<CommentReference> references =
6492 parse("parseCommentReferences", <Object>[tokens], "");
6493 expect(references, hasLength(1));
6494 CommentReference reference = references[0];
6495 expect(reference, isNotNull);
6496 expect(reference.identifier, isNotNull);
6497 expect(reference.offset, 15);
6498 }
6499
6500 void test_parseCompilationUnit_abstractAsPrefix_parameterized() {
6501 CompilationUnit unit = parse4("parseCompilationUnit",
6502 "abstract<dynamic> _abstract = new abstract.A();");
6503 expect(unit.scriptTag, isNull);
6504 expect(unit.directives, hasLength(0));
6505 expect(unit.declarations, hasLength(1));
6506 }
6507
6508 void test_parseCompilationUnit_builtIn_asFunctionName() {
6509 parse4("parseCompilationUnit", "abstract(x) => 0;");
6510 parse4("parseCompilationUnit", "as(x) => 0;");
6511 parse4("parseCompilationUnit", "dynamic(x) => 0;");
6512 parse4("parseCompilationUnit", "export(x) => 0;");
6513 parse4("parseCompilationUnit", "external(x) => 0;");
6514 parse4("parseCompilationUnit", "factory(x) => 0;");
6515 parse4("parseCompilationUnit", "get(x) => 0;");
6516 parse4("parseCompilationUnit", "implements(x) => 0;");
6517 parse4("parseCompilationUnit", "import(x) => 0;");
6518 parse4("parseCompilationUnit", "library(x) => 0;");
6519 parse4("parseCompilationUnit", "operator(x) => 0;");
6520 parse4("parseCompilationUnit", "part(x) => 0;");
6521 parse4("parseCompilationUnit", "set(x) => 0;");
6522 parse4("parseCompilationUnit", "static(x) => 0;");
6523 parse4("parseCompilationUnit", "typedef(x) => 0;");
6524 }
6525
6526 void test_parseCompilationUnit_directives_multiple() {
6527 CompilationUnit unit =
6528 parse4("parseCompilationUnit", "library l;\npart 'a.dart';");
6529 expect(unit.scriptTag, isNull);
6530 expect(unit.directives, hasLength(2));
6531 expect(unit.declarations, hasLength(0));
6532 }
6533
6534 void test_parseCompilationUnit_directives_single() {
6535 CompilationUnit unit = parse4("parseCompilationUnit", "library l;");
6536 expect(unit.scriptTag, isNull);
6537 expect(unit.directives, hasLength(1));
6538 expect(unit.declarations, hasLength(0));
6539 }
6540
6541 void test_parseCompilationUnit_empty() {
6542 CompilationUnit unit = parse4("parseCompilationUnit", "");
6543 expect(unit.scriptTag, isNull);
6544 expect(unit.directives, hasLength(0));
6545 expect(unit.declarations, hasLength(0));
6546 }
6547
6548 void test_parseCompilationUnit_exportAsPrefix() {
6549 CompilationUnit unit =
6550 parse4("parseCompilationUnit", "export.A _export = new export.A();");
6551 expect(unit.scriptTag, isNull);
6552 expect(unit.directives, hasLength(0));
6553 expect(unit.declarations, hasLength(1));
6554 }
6555
6556 void test_parseCompilationUnit_exportAsPrefix_parameterized() {
6557 CompilationUnit unit = parse4(
6558 "parseCompilationUnit", "export<dynamic> _export = new export.A();");
6559 expect(unit.scriptTag, isNull);
6560 expect(unit.directives, hasLength(0));
6561 expect(unit.declarations, hasLength(1));
6562 }
6563
6564 void test_parseCompilationUnit_operatorAsPrefix_parameterized() {
6565 CompilationUnit unit = parse4("parseCompilationUnit",
6566 "operator<dynamic> _operator = new operator.A();");
6567 expect(unit.scriptTag, isNull);
6568 expect(unit.directives, hasLength(0));
6569 expect(unit.declarations, hasLength(1));
6570 }
6571
6572 void test_parseCompilationUnit_script() {
6573 CompilationUnit unit = parse4("parseCompilationUnit", "#! /bin/dart");
6574 expect(unit.scriptTag, isNotNull);
6575 expect(unit.directives, hasLength(0));
6576 expect(unit.declarations, hasLength(0));
6577 }
6578
6579 void test_parseCompilationUnit_skipFunctionBody_withInterpolation() {
6580 ParserTestCase.parseFunctionBodies = false;
6581 CompilationUnit unit = parse4("parseCompilationUnit", "f() { '\${n}'; }");
6582 expect(unit.scriptTag, isNull);
6583 expect(unit.declarations, hasLength(1));
6584 }
6585
6586 void test_parseCompilationUnit_topLevelDeclaration() {
6587 CompilationUnit unit = parse4("parseCompilationUnit", "class A {}");
6588 expect(unit.scriptTag, isNull);
6589 expect(unit.directives, hasLength(0));
6590 expect(unit.declarations, hasLength(1));
6591 }
6592
6593 void test_parseCompilationUnit_typedefAsPrefix() {
6594 CompilationUnit unit =
6595 parse4("parseCompilationUnit", "typedef.A _typedef = new typedef.A();");
6596 expect(unit.scriptTag, isNull);
6597 expect(unit.directives, hasLength(0));
6598 expect(unit.declarations, hasLength(1));
6599 }
6600
6601 void test_parseCompilationUnitMember_abstractAsPrefix() {
6602 TopLevelVariableDeclaration declaration = parse(
6603 "parseCompilationUnitMember",
6604 <Object>[emptyCommentAndMetadata()],
6605 "abstract.A _abstract = new abstract.A();");
6606 expect(declaration.semicolon, isNotNull);
6607 expect(declaration.variables, isNotNull);
6608 }
6609
6610 void test_parseCompilationUnitMember_class() {
6611 ClassDeclaration declaration = parse("parseCompilationUnitMember",
6612 <Object>[emptyCommentAndMetadata()], "class A {}");
6613 expect(declaration.name.name, "A");
6614 expect(declaration.members, hasLength(0));
6615 }
6616
6617 void test_parseCompilationUnitMember_classTypeAlias() {
6618 ClassTypeAlias alias = parse("parseCompilationUnitMember",
6619 <Object>[emptyCommentAndMetadata()], "abstract class A = B with C;");
6620 expect(alias.name.name, "A");
6621 expect(alias.abstractKeyword, isNotNull);
6622 }
6623
6624 void test_parseCompilationUnitMember_constVariable() {
6625 TopLevelVariableDeclaration declaration = parse(
6626 "parseCompilationUnitMember",
6627 <Object>[emptyCommentAndMetadata()],
6628 "const int x = 0;");
6629 expect(declaration.semicolon, isNotNull);
6630 expect(declaration.variables, isNotNull);
6631 }
6632
6633 void test_parseCompilationUnitMember_finalVariable() {
6634 TopLevelVariableDeclaration declaration = parse(
6635 "parseCompilationUnitMember",
6636 <Object>[emptyCommentAndMetadata()],
6637 "final x = 0;");
6638 expect(declaration.semicolon, isNotNull);
6639 expect(declaration.variables, isNotNull);
6640 }
6641
6642 void test_parseCompilationUnitMember_function_external_noType() {
6643 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6644 <Object>[emptyCommentAndMetadata()], "external f();");
6645 expect(declaration.externalKeyword, isNotNull);
6646 expect(declaration.functionExpression, isNotNull);
6647 expect(declaration.propertyKeyword, isNull);
6648 }
6649
6650 void test_parseCompilationUnitMember_function_external_type() {
6651 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6652 <Object>[emptyCommentAndMetadata()], "external int f();");
6653 expect(declaration.externalKeyword, isNotNull);
6654 expect(declaration.functionExpression, isNotNull);
6655 expect(declaration.propertyKeyword, isNull);
6656 }
6657
6658 void test_parseCompilationUnitMember_function_noType() {
6659 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6660 <Object>[emptyCommentAndMetadata()], "f() {}");
6661 expect(declaration.functionExpression, isNotNull);
6662 expect(declaration.propertyKeyword, isNull);
6663 }
6664
6665 void test_parseCompilationUnitMember_function_type() {
6666 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6667 <Object>[emptyCommentAndMetadata()], "int f() {}");
6668 expect(declaration.functionExpression, isNotNull);
6669 expect(declaration.propertyKeyword, isNull);
6670 }
6671
6672 void test_parseCompilationUnitMember_function_void() {
6673 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6674 <Object>[emptyCommentAndMetadata()], "void f() {}");
6675 expect(declaration.returnType, isNotNull);
6676 }
6677
6678 void test_parseCompilationUnitMember_getter_external_noType() {
6679 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6680 <Object>[emptyCommentAndMetadata()], "external get p;");
6681 expect(declaration.externalKeyword, isNotNull);
6682 expect(declaration.functionExpression, isNotNull);
6683 expect(declaration.propertyKeyword, isNotNull);
6684 }
6685
6686 void test_parseCompilationUnitMember_getter_external_type() {
6687 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6688 <Object>[emptyCommentAndMetadata()], "external int get p;");
6689 expect(declaration.externalKeyword, isNotNull);
6690 expect(declaration.functionExpression, isNotNull);
6691 expect(declaration.propertyKeyword, isNotNull);
6692 }
6693
6694 void test_parseCompilationUnitMember_getter_noType() {
6695 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6696 <Object>[emptyCommentAndMetadata()], "get p => 0;");
6697 expect(declaration.functionExpression, isNotNull);
6698 expect(declaration.propertyKeyword, isNotNull);
6699 }
6700
6701 void test_parseCompilationUnitMember_getter_type() {
6702 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6703 <Object>[emptyCommentAndMetadata()], "int get p => 0;");
6704 expect(declaration.functionExpression, isNotNull);
6705 expect(declaration.propertyKeyword, isNotNull);
6706 }
6707
6708 void test_parseCompilationUnitMember_setter_external_noType() {
6709 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6710 <Object>[emptyCommentAndMetadata()], "external set p(v);");
6711 expect(declaration.externalKeyword, isNotNull);
6712 expect(declaration.functionExpression, isNotNull);
6713 expect(declaration.propertyKeyword, isNotNull);
6714 }
6715
6716 void test_parseCompilationUnitMember_setter_external_type() {
6717 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6718 <Object>[emptyCommentAndMetadata()], "external void set p(int v);");
6719 expect(declaration.externalKeyword, isNotNull);
6720 expect(declaration.functionExpression, isNotNull);
6721 expect(declaration.propertyKeyword, isNotNull);
6722 }
6723
6724 void test_parseCompilationUnitMember_setter_noType() {
6725 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6726 <Object>[emptyCommentAndMetadata()], "set p(v) {}");
6727 expect(declaration.functionExpression, isNotNull);
6728 expect(declaration.propertyKeyword, isNotNull);
6729 }
6730
6731 void test_parseCompilationUnitMember_setter_type() {
6732 FunctionDeclaration declaration = parse("parseCompilationUnitMember",
6733 <Object>[emptyCommentAndMetadata()], "void set p(int v) {}");
6734 expect(declaration.functionExpression, isNotNull);
6735 expect(declaration.propertyKeyword, isNotNull);
6736 expect(declaration.returnType, isNotNull);
6737 }
6738
6739 void test_parseCompilationUnitMember_typeAlias_abstract() {
6740 ClassTypeAlias typeAlias = parse("parseCompilationUnitMember",
6741 <Object>[emptyCommentAndMetadata()], "abstract class C = S with M;");
6742 expect(typeAlias.typedefKeyword, isNotNull);
6743 expect(typeAlias.name.name, "C");
6744 expect(typeAlias.typeParameters, isNull);
6745 expect(typeAlias.equals, isNotNull);
6746 expect(typeAlias.abstractKeyword, isNotNull);
6747 expect(typeAlias.superclass.name.name, "S");
6748 expect(typeAlias.withClause, isNotNull);
6749 expect(typeAlias.implementsClause, isNull);
6750 expect(typeAlias.semicolon, isNotNull);
6751 }
6752
6753 void test_parseCompilationUnitMember_typeAlias_generic() {
6754 ClassTypeAlias typeAlias = parse(
6755 "parseCompilationUnitMember",
6756 <Object>[emptyCommentAndMetadata()],
6757 "class C<E> = S<E> with M<E> implements I<E>;");
6758 expect(typeAlias.typedefKeyword, isNotNull);
6759 expect(typeAlias.name.name, "C");
6760 expect(typeAlias.typeParameters.typeParameters, hasLength(1));
6761 expect(typeAlias.equals, isNotNull);
6762 expect(typeAlias.abstractKeyword, isNull);
6763 expect(typeAlias.superclass.name.name, "S");
6764 expect(typeAlias.withClause, isNotNull);
6765 expect(typeAlias.implementsClause, isNotNull);
6766 expect(typeAlias.semicolon, isNotNull);
6767 }
6768
6769 void test_parseCompilationUnitMember_typeAlias_implements() {
6770 ClassTypeAlias typeAlias = parse(
6771 "parseCompilationUnitMember",
6772 <Object>[emptyCommentAndMetadata()],
6773 "class C = S with M implements I;");
6774 expect(typeAlias.typedefKeyword, isNotNull);
6775 expect(typeAlias.name.name, "C");
6776 expect(typeAlias.typeParameters, isNull);
6777 expect(typeAlias.equals, isNotNull);
6778 expect(typeAlias.abstractKeyword, isNull);
6779 expect(typeAlias.superclass.name.name, "S");
6780 expect(typeAlias.withClause, isNotNull);
6781 expect(typeAlias.implementsClause, isNotNull);
6782 expect(typeAlias.semicolon, isNotNull);
6783 }
6784
6785 void test_parseCompilationUnitMember_typeAlias_noImplements() {
6786 ClassTypeAlias typeAlias = parse("parseCompilationUnitMember",
6787 <Object>[emptyCommentAndMetadata()], "class C = S with M;");
6788 expect(typeAlias.typedefKeyword, isNotNull);
6789 expect(typeAlias.name.name, "C");
6790 expect(typeAlias.typeParameters, isNull);
6791 expect(typeAlias.equals, isNotNull);
6792 expect(typeAlias.abstractKeyword, isNull);
6793 expect(typeAlias.superclass.name.name, "S");
6794 expect(typeAlias.withClause, isNotNull);
6795 expect(typeAlias.implementsClause, isNull);
6796 expect(typeAlias.semicolon, isNotNull);
6797 }
6798
6799 void test_parseCompilationUnitMember_typedef() {
6800 FunctionTypeAlias typeAlias = parse("parseCompilationUnitMember",
6801 <Object>[emptyCommentAndMetadata()], "typedef F();");
6802 expect(typeAlias.name.name, "F");
6803 expect(typeAlias.parameters.parameters, hasLength(0));
6804 }
6805
6806 void test_parseCompilationUnitMember_variable() {
6807 TopLevelVariableDeclaration declaration = parse(
6808 "parseCompilationUnitMember",
6809 <Object>[emptyCommentAndMetadata()],
6810 "var x = 0;");
6811 expect(declaration.semicolon, isNotNull);
6812 expect(declaration.variables, isNotNull);
6813 }
6814
6815 void test_parseCompilationUnitMember_variableGet() {
6816 TopLevelVariableDeclaration declaration = parse(
6817 "parseCompilationUnitMember",
6818 <Object>[emptyCommentAndMetadata()],
6819 "String get = null;");
6820 expect(declaration.semicolon, isNotNull);
6821 expect(declaration.variables, isNotNull);
6822 }
6823
6824 void test_parseCompilationUnitMember_variableSet() {
6825 TopLevelVariableDeclaration declaration = parse(
6826 "parseCompilationUnitMember",
6827 <Object>[emptyCommentAndMetadata()],
6828 "String set = null;");
6829 expect(declaration.semicolon, isNotNull);
6830 expect(declaration.variables, isNotNull);
6831 }
6832
6833 void test_parseConditionalExpression() {
6834 ConditionalExpression expression =
6835 parse4("parseConditionalExpression", "x ? y : z");
6836 expect(expression.condition, isNotNull);
6837 expect(expression.question, isNotNull);
6838 expect(expression.thenExpression, isNotNull);
6839 expect(expression.colon, isNotNull);
6840 expect(expression.elseExpression, isNotNull);
6841 }
6842
6843 void test_parseConstExpression_instanceCreation() {
6844 InstanceCreationExpression expression =
6845 parse4("parseConstExpression", "const A()");
6846 expect(expression.keyword, isNotNull);
6847 ConstructorName name = expression.constructorName;
6848 expect(name, isNotNull);
6849 expect(name.type, isNotNull);
6850 expect(name.period, isNull);
6851 expect(name.name, isNull);
6852 expect(expression.argumentList, isNotNull);
6853 }
6854
6855 void test_parseConstExpression_listLiteral_typed() {
6856 ListLiteral literal = parse4("parseConstExpression", "const <A> []");
6857 expect(literal.constKeyword, isNotNull);
6858 expect(literal.typeArguments, isNotNull);
6859 expect(literal.leftBracket, isNotNull);
6860 expect(literal.elements, hasLength(0));
6861 expect(literal.rightBracket, isNotNull);
6862 }
6863
6864 void test_parseConstExpression_listLiteral_untyped() {
6865 ListLiteral literal = parse4("parseConstExpression", "const []");
6866 expect(literal.constKeyword, isNotNull);
6867 expect(literal.typeArguments, isNull);
6868 expect(literal.leftBracket, isNotNull);
6869 expect(literal.elements, hasLength(0));
6870 expect(literal.rightBracket, isNotNull);
6871 }
6872
6873 void test_parseConstExpression_mapLiteral_typed() {
6874 MapLiteral literal = parse4("parseConstExpression", "const <A, B> {}");
6875 expect(literal.leftBracket, isNotNull);
6876 expect(literal.entries, hasLength(0));
6877 expect(literal.rightBracket, isNotNull);
6878 expect(literal.typeArguments, isNotNull);
6879 }
6880
6881 void test_parseConstExpression_mapLiteral_untyped() {
6882 MapLiteral literal = parse4("parseConstExpression", "const {}");
6883 expect(literal.leftBracket, isNotNull);
6884 expect(literal.entries, hasLength(0));
6885 expect(literal.rightBracket, isNotNull);
6886 expect(literal.typeArguments, isNull);
6887 }
6888
6889 void test_parseConstructor() {
6890 // TODO(brianwilkerson) Implement tests for this method.
6891 // parse("parseConstructor", new Class[] {Parser.CommentAndMetadata.class,
6892 // Token.class, Token.class, SimpleIdentifier.class, Token.class,
6893 // SimpleIdentifier.class, FormalParameterList.class}, new Object[] {empt yCommentAndMetadata(),
6894 // null, null, null, null, null, null}, "");
6895 }
6896
6897 void test_parseConstructor_with_pseudo_function_literal() {
6898 // "(b) {}" should not be misinterpreted as a function literal even though
6899 // it looks like one.
6900 ClassMember classMember =
6901 parse("parseClassMember", <Object>["C"], "C() : a = (b) {}");
6902 EngineTestCase.assertInstanceOf((obj) => obj is ConstructorDeclaration,
6903 ConstructorDeclaration, classMember);
6904 ConstructorDeclaration constructor = classMember as ConstructorDeclaration;
6905 NodeList<ConstructorInitializer> initializers = constructor.initializers;
6906 expect(initializers, hasLength(1));
6907 ConstructorInitializer initializer = initializers[0];
6908 EngineTestCase.assertInstanceOf((obj) => obj is ConstructorFieldInitializer,
6909 ConstructorFieldInitializer, initializer);
6910 EngineTestCase.assertInstanceOf(
6911 (obj) => obj is ParenthesizedExpression,
6912 ParenthesizedExpression,
6913 (initializer as ConstructorFieldInitializer).expression);
6914 EngineTestCase.assertInstanceOf(
6915 (obj) => obj is BlockFunctionBody, BlockFunctionBody, constructor.body);
6916 }
6917
6918 void test_parseConstructorFieldInitializer_qualified() {
6919 ConstructorFieldInitializer invocation =
6920 parse4("parseConstructorFieldInitializer", "this.a = b");
6921 expect(invocation.equals, isNotNull);
6922 expect(invocation.expression, isNotNull);
6923 expect(invocation.fieldName, isNotNull);
6924 expect(invocation.thisKeyword, isNotNull);
6925 expect(invocation.period, isNotNull);
6926 }
6927
6928 void test_parseConstructorFieldInitializer_unqualified() {
6929 ConstructorFieldInitializer invocation =
6930 parse4("parseConstructorFieldInitializer", "a = b");
6931 expect(invocation.equals, isNotNull);
6932 expect(invocation.expression, isNotNull);
6933 expect(invocation.fieldName, isNotNull);
6934 expect(invocation.thisKeyword, isNull);
6935 expect(invocation.period, isNull);
6936 }
6937
6938 void test_parseConstructorName_named_noPrefix() {
6939 ConstructorName name = parse4("parseConstructorName", "A.n;");
6940 expect(name.type, isNotNull);
6941 expect(name.period, isNull);
6942 expect(name.name, isNull);
6943 }
6944
6945 void test_parseConstructorName_named_prefixed() {
6946 ConstructorName name = parse4("parseConstructorName", "p.A.n;");
6947 expect(name.type, isNotNull);
6948 expect(name.period, isNotNull);
6949 expect(name.name, isNotNull);
6950 }
6951
6952 void test_parseConstructorName_unnamed_noPrefix() {
6953 ConstructorName name = parse4("parseConstructorName", "A;");
6954 expect(name.type, isNotNull);
6955 expect(name.period, isNull);
6956 expect(name.name, isNull);
6957 }
6958
6959 void test_parseConstructorName_unnamed_prefixed() {
6960 ConstructorName name = parse4("parseConstructorName", "p.A;");
6961 expect(name.type, isNotNull);
6962 expect(name.period, isNull);
6963 expect(name.name, isNull);
6964 }
6965
6966 void test_parseContinueStatement_label() {
6967 ContinueStatement statement = parse4("parseContinueStatement",
6968 "continue foo;", [ParserErrorCode.CONTINUE_OUTSIDE_OF_LOOP]);
6969 expect(statement.continueKeyword, isNotNull);
6970 expect(statement.label, isNotNull);
6971 expect(statement.semicolon, isNotNull);
6972 }
6973
6974 void test_parseContinueStatement_noLabel() {
6975 ContinueStatement statement = parse4("parseContinueStatement", "continue;",
6976 [ParserErrorCode.CONTINUE_OUTSIDE_OF_LOOP]);
6977 expect(statement.continueKeyword, isNotNull);
6978 expect(statement.label, isNull);
6979 expect(statement.semicolon, isNotNull);
6980 }
6981
6982 void test_parseDirective_export() {
6983 ExportDirective directive = parse("parseDirective",
6984 <Object>[emptyCommentAndMetadata()], "export 'lib/lib.dart';");
6985 expect(directive.keyword, isNotNull);
6986 expect(directive.uri, isNotNull);
6987 expect(directive.combinators, hasLength(0));
6988 expect(directive.semicolon, isNotNull);
6989 }
6990
6991 void test_parseDirective_import() {
6992 ImportDirective directive = parse("parseDirective",
6993 <Object>[emptyCommentAndMetadata()], "import 'lib/lib.dart';");
6994 expect(directive.keyword, isNotNull);
6995 expect(directive.uri, isNotNull);
6996 expect(directive.asKeyword, isNull);
6997 expect(directive.prefix, isNull);
6998 expect(directive.combinators, hasLength(0));
6999 expect(directive.semicolon, isNotNull);
7000 }
7001
7002 void test_parseDirective_library() {
7003 LibraryDirective directive = parse(
7004 "parseDirective", <Object>[emptyCommentAndMetadata()], "library l;");
7005 expect(directive.libraryKeyword, isNotNull);
7006 expect(directive.name, isNotNull);
7007 expect(directive.semicolon, isNotNull);
7008 }
7009
7010 void test_parseDirective_part() {
7011 PartDirective directive = parse("parseDirective",
7012 <Object>[emptyCommentAndMetadata()], "part 'lib/lib.dart';");
7013 expect(directive.partKeyword, isNotNull);
7014 expect(directive.uri, isNotNull);
7015 expect(directive.semicolon, isNotNull);
7016 }
7017
7018 void test_parseDirective_partOf() {
7019 PartOfDirective directive = parse(
7020 "parseDirective", <Object>[emptyCommentAndMetadata()], "part of l;");
7021 expect(directive.partKeyword, isNotNull);
7022 expect(directive.ofKeyword, isNotNull);
7023 expect(directive.libraryName, isNotNull);
7024 expect(directive.semicolon, isNotNull);
7025 }
7026
7027 void test_parseDirectives_complete() {
7028 CompilationUnit unit =
7029 _parseDirectives("#! /bin/dart\nlibrary l;\nclass A {}");
7030 expect(unit.scriptTag, isNotNull);
7031 expect(unit.directives, hasLength(1));
7032 }
7033
7034 void test_parseDirectives_empty() {
7035 CompilationUnit unit = _parseDirectives("");
7036 expect(unit.scriptTag, isNull);
7037 expect(unit.directives, hasLength(0));
7038 }
7039
7040 void test_parseDirectives_mixed() {
7041 CompilationUnit unit =
7042 _parseDirectives("library l; class A {} part 'foo.dart';");
7043 expect(unit.scriptTag, isNull);
7044 expect(unit.directives, hasLength(1));
7045 }
7046
7047 void test_parseDirectives_multiple() {
7048 CompilationUnit unit = _parseDirectives("library l;\npart 'a.dart';");
7049 expect(unit.scriptTag, isNull);
7050 expect(unit.directives, hasLength(2));
7051 }
7052
7053 void test_parseDirectives_script() {
7054 CompilationUnit unit = _parseDirectives("#! /bin/dart");
7055 expect(unit.scriptTag, isNotNull);
7056 expect(unit.directives, hasLength(0));
7057 }
7058
7059 void test_parseDirectives_single() {
7060 CompilationUnit unit = _parseDirectives("library l;");
7061 expect(unit.scriptTag, isNull);
7062 expect(unit.directives, hasLength(1));
7063 }
7064
7065 void test_parseDirectives_topLevelDeclaration() {
7066 CompilationUnit unit = _parseDirectives("class A {}");
7067 expect(unit.scriptTag, isNull);
7068 expect(unit.directives, hasLength(0));
7069 }
7070
7071 void test_parseDocumentationComment_block() {
7072 Comment comment = parse4("parseDocumentationComment", "/** */ class");
7073 expect(comment.isBlock, isFalse);
7074 expect(comment.isDocumentation, isTrue);
7075 expect(comment.isEndOfLine, isFalse);
7076 }
7077
7078 void test_parseDocumentationComment_block_withReference() {
7079 Comment comment = parse4("parseDocumentationComment", "/** [a] */ class");
7080 expect(comment.isBlock, isFalse);
7081 expect(comment.isDocumentation, isTrue);
7082 expect(comment.isEndOfLine, isFalse);
7083 NodeList<CommentReference> references = comment.references;
7084 expect(references, hasLength(1));
7085 CommentReference reference = references[0];
7086 expect(reference, isNotNull);
7087 expect(reference.offset, 5);
7088 }
7089
7090 void test_parseDocumentationComment_endOfLine() {
7091 Comment comment = parse4("parseDocumentationComment", "/// \n/// \n class");
7092 expect(comment.isBlock, isFalse);
7093 expect(comment.isDocumentation, isTrue);
7094 expect(comment.isEndOfLine, isFalse);
7095 }
7096
7097 void test_parseDoStatement() {
7098 DoStatement statement = parse4("parseDoStatement", "do {} while (x);");
7099 expect(statement.doKeyword, isNotNull);
7100 expect(statement.body, isNotNull);
7101 expect(statement.whileKeyword, isNotNull);
7102 expect(statement.leftParenthesis, isNotNull);
7103 expect(statement.condition, isNotNull);
7104 expect(statement.rightParenthesis, isNotNull);
7105 expect(statement.semicolon, isNotNull);
7106 }
7107
7108 void test_parseEmptyStatement() {
7109 EmptyStatement statement = parse4("parseEmptyStatement", ";");
7110 expect(statement.semicolon, isNotNull);
7111 }
7112
7113 void test_parseEnumDeclaration_one() {
7114 EnumDeclaration declaration = parse("parseEnumDeclaration",
7115 <Object>[emptyCommentAndMetadata()], "enum E {ONE}");
7116 expect(declaration.documentationComment, isNull);
7117 expect(declaration.enumKeyword, isNotNull);
7118 expect(declaration.leftBracket, isNotNull);
7119 expect(declaration.name, isNotNull);
7120 expect(declaration.constants, hasLength(1));
7121 expect(declaration.rightBracket, isNotNull);
7122 }
7123
7124 void test_parseEnumDeclaration_trailingComma() {
7125 EnumDeclaration declaration = parse("parseEnumDeclaration",
7126 <Object>[emptyCommentAndMetadata()], "enum E {ONE,}");
7127 expect(declaration.documentationComment, isNull);
7128 expect(declaration.enumKeyword, isNotNull);
7129 expect(declaration.leftBracket, isNotNull);
7130 expect(declaration.name, isNotNull);
7131 expect(declaration.constants, hasLength(1));
7132 expect(declaration.rightBracket, isNotNull);
7133 }
7134
7135 void test_parseEnumDeclaration_two() {
7136 EnumDeclaration declaration = parse("parseEnumDeclaration",
7137 <Object>[emptyCommentAndMetadata()], "enum E {ONE, TWO}");
7138 expect(declaration.documentationComment, isNull);
7139 expect(declaration.enumKeyword, isNotNull);
7140 expect(declaration.leftBracket, isNotNull);
7141 expect(declaration.name, isNotNull);
7142 expect(declaration.constants, hasLength(2));
7143 expect(declaration.rightBracket, isNotNull);
7144 }
7145
7146 void test_parseEqualityExpression_normal() {
7147 BinaryExpression expression = parse4("parseEqualityExpression", "x == y");
7148 expect(expression.leftOperand, isNotNull);
7149 expect(expression.operator, isNotNull);
7150 expect(expression.operator.type, TokenType.EQ_EQ);
7151 expect(expression.rightOperand, isNotNull);
7152 }
7153
7154 void test_parseEqualityExpression_super() {
7155 BinaryExpression expression =
7156 parse4("parseEqualityExpression", "super == y");
7157 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
7158 SuperExpression, expression.leftOperand);
7159 expect(expression.operator, isNotNull);
7160 expect(expression.operator.type, TokenType.EQ_EQ);
7161 expect(expression.rightOperand, isNotNull);
7162 }
7163
7164 void test_parseExportDirective_hide() {
7165 ExportDirective directive = parse(
7166 "parseExportDirective",
7167 <Object>[emptyCommentAndMetadata()],
7168 "export 'lib/lib.dart' hide A, B;");
7169 expect(directive.keyword, isNotNull);
7170 expect(directive.uri, isNotNull);
7171 expect(directive.combinators, hasLength(1));
7172 expect(directive.semicolon, isNotNull);
7173 }
7174
7175 void test_parseExportDirective_hide_show() {
7176 ExportDirective directive = parse(
7177 "parseExportDirective",
7178 <Object>[emptyCommentAndMetadata()],
7179 "export 'lib/lib.dart' hide A show B;");
7180 expect(directive.keyword, isNotNull);
7181 expect(directive.uri, isNotNull);
7182 expect(directive.combinators, hasLength(2));
7183 expect(directive.semicolon, isNotNull);
7184 }
7185
7186 void test_parseExportDirective_noCombinator() {
7187 ExportDirective directive = parse("parseExportDirective",
7188 <Object>[emptyCommentAndMetadata()], "export 'lib/lib.dart';");
7189 expect(directive.keyword, isNotNull);
7190 expect(directive.uri, isNotNull);
7191 expect(directive.combinators, hasLength(0));
7192 expect(directive.semicolon, isNotNull);
7193 }
7194
7195 void test_parseExportDirective_show() {
7196 ExportDirective directive = parse(
7197 "parseExportDirective",
7198 <Object>[emptyCommentAndMetadata()],
7199 "export 'lib/lib.dart' show A, B;");
7200 expect(directive.keyword, isNotNull);
7201 expect(directive.uri, isNotNull);
7202 expect(directive.combinators, hasLength(1));
7203 expect(directive.semicolon, isNotNull);
7204 }
7205
7206 void test_parseExportDirective_show_hide() {
7207 ExportDirective directive = parse(
7208 "parseExportDirective",
7209 <Object>[emptyCommentAndMetadata()],
7210 "export 'lib/lib.dart' show B hide A;");
7211 expect(directive.keyword, isNotNull);
7212 expect(directive.uri, isNotNull);
7213 expect(directive.combinators, hasLength(2));
7214 expect(directive.semicolon, isNotNull);
7215 }
7216
7217 void test_parseExpression_assign() {
7218 // TODO(brianwilkerson) Implement more tests for this method.
7219 AssignmentExpression expression = parse4("parseExpression", "x = y");
7220 expect(expression.leftHandSide, isNotNull);
7221 expect(expression.operator, isNotNull);
7222 expect(expression.operator.type, TokenType.EQ);
7223 expect(expression.rightHandSide, isNotNull);
7224 }
7225
7226 void test_parseExpression_comparison() {
7227 BinaryExpression expression = parse4("parseExpression", "--a.b == c");
7228 expect(expression.leftOperand, isNotNull);
7229 expect(expression.operator, isNotNull);
7230 expect(expression.operator.type, TokenType.EQ_EQ);
7231 expect(expression.rightOperand, isNotNull);
7232 }
7233
7234 void test_parseExpression_function_async() {
7235 FunctionExpression expression = parseExpression("() async {}");
7236 expect(expression.body, isNotNull);
7237 expect(expression.body.isAsynchronous, isTrue);
7238 expect(expression.body.isGenerator, isFalse);
7239 expect(expression.parameters, isNotNull);
7240 }
7241
7242 void test_parseExpression_function_asyncStar() {
7243 FunctionExpression expression = parseExpression("() async* {}");
7244 expect(expression.body, isNotNull);
7245 expect(expression.body.isAsynchronous, isTrue);
7246 expect(expression.body.isGenerator, isTrue);
7247 expect(expression.parameters, isNotNull);
7248 }
7249
7250 void test_parseExpression_function_sync() {
7251 FunctionExpression expression = parseExpression("() {}");
7252 expect(expression.body, isNotNull);
7253 expect(expression.body.isAsynchronous, isFalse);
7254 expect(expression.body.isGenerator, isFalse);
7255 expect(expression.parameters, isNotNull);
7256 }
7257
7258 void test_parseExpression_function_syncStar() {
7259 FunctionExpression expression = parseExpression("() sync* {}");
7260 expect(expression.body, isNotNull);
7261 expect(expression.body.isAsynchronous, isFalse);
7262 expect(expression.body.isGenerator, isTrue);
7263 expect(expression.parameters, isNotNull);
7264 }
7265
7266 void test_parseExpression_invokeFunctionExpression() {
7267 FunctionExpressionInvocation invocation =
7268 parse4("parseExpression", "(a) {return a + a;} (3)");
7269 EngineTestCase.assertInstanceOf((obj) => obj is FunctionExpression,
7270 FunctionExpression, invocation.function);
7271 FunctionExpression expression = invocation.function as FunctionExpression;
7272 expect(expression.parameters, isNotNull);
7273 expect(expression.body, isNotNull);
7274 expect(invocation.typeArguments, isNull);
7275 ArgumentList list = invocation.argumentList;
7276 expect(list, isNotNull);
7277 expect(list.arguments, hasLength(1));
7278 }
7279
7280 void test_parseExpression_nonAwait() {
7281 MethodInvocation expression = parseExpression("await()");
7282 expect(expression.methodName.name, 'await');
7283 expect(expression.typeArguments, isNull);
7284 expect(expression.argumentList, isNotNull);
7285 }
7286
7287 void test_parseExpression_superMethodInvocation() {
7288 MethodInvocation invocation = parse4("parseExpression", "super.m()");
7289 expect(invocation.target, isNotNull);
7290 expect(invocation.methodName, isNotNull);
7291 expect(invocation.typeArguments, isNull);
7292 expect(invocation.argumentList, isNotNull);
7293 }
7294
7295 void test_parseExpression_superMethodInvocation_typeArguments() {
7296 enableGenericMethods = true;
7297 MethodInvocation invocation = parse4("parseExpression", "super.m<E>()");
7298 expect(invocation.target, isNotNull);
7299 expect(invocation.methodName, isNotNull);
7300 expect(invocation.typeArguments, isNotNull);
7301 expect(invocation.argumentList, isNotNull);
7302 }
7303
7304 void test_parseExpressionList_multiple() {
7305 List<Expression> result = parse4("parseExpressionList", "1, 2, 3");
7306 expect(result, hasLength(3));
7307 }
7308
7309 void test_parseExpressionList_single() {
7310 List<Expression> result = parse4("parseExpressionList", "1");
7311 expect(result, hasLength(1));
7312 }
7313
7314 void test_parseExpressionWithoutCascade_assign() {
7315 // TODO(brianwilkerson) Implement more tests for this method.
7316 AssignmentExpression expression =
7317 parse4("parseExpressionWithoutCascade", "x = y");
7318 expect(expression.leftHandSide, isNotNull);
7319 expect(expression.operator, isNotNull);
7320 expect(expression.operator.type, TokenType.EQ);
7321 expect(expression.rightHandSide, isNotNull);
7322 }
7323
7324 void test_parseExpressionWithoutCascade_comparison() {
7325 BinaryExpression expression =
7326 parse4("parseExpressionWithoutCascade", "--a.b == c");
7327 expect(expression.leftOperand, isNotNull);
7328 expect(expression.operator, isNotNull);
7329 expect(expression.operator.type, TokenType.EQ_EQ);
7330 expect(expression.rightOperand, isNotNull);
7331 }
7332
7333 void test_parseExpressionWithoutCascade_superMethodInvocation() {
7334 MethodInvocation invocation =
7335 parse4("parseExpressionWithoutCascade", "super.m()");
7336 expect(invocation.target, isNotNull);
7337 expect(invocation.methodName, isNotNull);
7338 expect(invocation.typeArguments, isNull);
7339 expect(invocation.argumentList, isNotNull);
7340 }
7341
7342 void test_parseExpressionWithoutCascade_superMethodInvocation_typeArguments() {
7343 enableGenericMethods = true;
7344 MethodInvocation invocation =
7345 parse4("parseExpressionWithoutCascade", "super.m<E>()");
7346 expect(invocation.target, isNotNull);
7347 expect(invocation.methodName, isNotNull);
7348 expect(invocation.typeArguments, isNotNull);
7349 expect(invocation.argumentList, isNotNull);
7350 }
7351
7352 void test_parseExtendsClause() {
7353 ExtendsClause clause = parse4("parseExtendsClause", "extends B");
7354 expect(clause.extendsKeyword, isNotNull);
7355 expect(clause.superclass, isNotNull);
7356 EngineTestCase.assertInstanceOf(
7357 (obj) => obj is TypeName, TypeName, clause.superclass);
7358 }
7359
7360 void test_parseFinalConstVarOrType_const_noType() {
7361 FinalConstVarOrType result =
7362 parse("parseFinalConstVarOrType", <Object>[false], "const");
7363 Token keyword = result.keyword;
7364 expect(keyword, isNotNull);
7365 expect(keyword.type, TokenType.KEYWORD);
7366 expect((keyword as KeywordToken).keyword, Keyword.CONST);
7367 expect(result.type, isNull);
7368 }
7369
7370 void test_parseFinalConstVarOrType_const_type() {
7371 FinalConstVarOrType result =
7372 parse("parseFinalConstVarOrType", <Object>[false], "const A a");
7373 Token keyword = result.keyword;
7374 expect(keyword, isNotNull);
7375 expect(keyword.type, TokenType.KEYWORD);
7376 expect((keyword as KeywordToken).keyword, Keyword.CONST);
7377 expect(result.type, isNotNull);
7378 }
7379
7380 void test_parseFinalConstVarOrType_final_noType() {
7381 FinalConstVarOrType result =
7382 parse("parseFinalConstVarOrType", <Object>[false], "final");
7383 Token keyword = result.keyword;
7384 expect(keyword, isNotNull);
7385 expect(keyword.type, TokenType.KEYWORD);
7386 expect((keyword as KeywordToken).keyword, Keyword.FINAL);
7387 expect(result.type, isNull);
7388 }
7389
7390 void test_parseFinalConstVarOrType_final_prefixedType() {
7391 FinalConstVarOrType result =
7392 parse("parseFinalConstVarOrType", <Object>[false], "final p.A a");
7393 Token keyword = result.keyword;
7394 expect(keyword, isNotNull);
7395 expect(keyword.type, TokenType.KEYWORD);
7396 expect((keyword as KeywordToken).keyword, Keyword.FINAL);
7397 expect(result.type, isNotNull);
7398 }
7399
7400 void test_parseFinalConstVarOrType_final_type() {
7401 FinalConstVarOrType result =
7402 parse("parseFinalConstVarOrType", <Object>[false], "final A a");
7403 Token keyword = result.keyword;
7404 expect(keyword, isNotNull);
7405 expect(keyword.type, TokenType.KEYWORD);
7406 expect((keyword as KeywordToken).keyword, Keyword.FINAL);
7407 expect(result.type, isNotNull);
7408 }
7409
7410 void test_parseFinalConstVarOrType_type_parameterized() {
7411 FinalConstVarOrType result =
7412 parse("parseFinalConstVarOrType", <Object>[false], "A<B> a");
7413 expect(result.keyword, isNull);
7414 expect(result.type, isNotNull);
7415 }
7416
7417 void test_parseFinalConstVarOrType_type_prefixed() {
7418 FinalConstVarOrType result =
7419 parse("parseFinalConstVarOrType", <Object>[false], "p.A a");
7420 expect(result.keyword, isNull);
7421 expect(result.type, isNotNull);
7422 }
7423
7424 void test_parseFinalConstVarOrType_type_prefixed_noIdentifier() {
7425 FinalConstVarOrType result =
7426 parse("parseFinalConstVarOrType", <Object>[false], "p.A,");
7427 expect(result.keyword, isNull);
7428 expect(result.type, isNotNull);
7429 }
7430
7431 void test_parseFinalConstVarOrType_type_prefixedAndParameterized() {
7432 FinalConstVarOrType result =
7433 parse("parseFinalConstVarOrType", <Object>[false], "p.A<B> a");
7434 expect(result.keyword, isNull);
7435 expect(result.type, isNotNull);
7436 }
7437
7438 void test_parseFinalConstVarOrType_type_simple() {
7439 FinalConstVarOrType result =
7440 parse("parseFinalConstVarOrType", <Object>[false], "A a");
7441 expect(result.keyword, isNull);
7442 expect(result.type, isNotNull);
7443 }
7444
7445 void test_parseFinalConstVarOrType_var() {
7446 FinalConstVarOrType result =
7447 parse("parseFinalConstVarOrType", <Object>[false], "var");
7448 Token keyword = result.keyword;
7449 expect(keyword, isNotNull);
7450 expect(keyword.type, TokenType.KEYWORD);
7451 expect((keyword as KeywordToken).keyword, Keyword.VAR);
7452 expect(result.type, isNull);
7453 }
7454
7455 void test_parseFinalConstVarOrType_void() {
7456 FinalConstVarOrType result =
7457 parse("parseFinalConstVarOrType", <Object>[false], "void f()");
7458 expect(result.keyword, isNull);
7459 expect(result.type, isNotNull);
7460 }
7461
7462 void test_parseFinalConstVarOrType_void_noIdentifier() {
7463 FinalConstVarOrType result =
7464 parse("parseFinalConstVarOrType", <Object>[false], "void,");
7465 expect(result.keyword, isNull);
7466 expect(result.type, isNotNull);
7467 }
7468
7469 void test_parseFormalParameter_final_withType_named() {
7470 ParameterKind kind = ParameterKind.NAMED;
7471 DefaultFormalParameter parameter =
7472 parse("parseFormalParameter", <Object>[kind], "final A a : null");
7473 SimpleFormalParameter simpleParameter =
7474 parameter.parameter as SimpleFormalParameter;
7475 expect(simpleParameter.identifier, isNotNull);
7476 expect(simpleParameter.keyword, isNotNull);
7477 expect(simpleParameter.type, isNotNull);
7478 expect(simpleParameter.kind, kind);
7479 expect(parameter.separator, isNotNull);
7480 expect(parameter.defaultValue, isNotNull);
7481 expect(parameter.kind, kind);
7482 }
7483
7484 void test_parseFormalParameter_final_withType_normal() {
7485 ParameterKind kind = ParameterKind.REQUIRED;
7486 SimpleFormalParameter parameter =
7487 parse("parseFormalParameter", <Object>[kind], "final A a");
7488 expect(parameter.identifier, isNotNull);
7489 expect(parameter.keyword, isNotNull);
7490 expect(parameter.type, isNotNull);
7491 expect(parameter.kind, kind);
7492 }
7493
7494 void test_parseFormalParameter_final_withType_positional() {
7495 ParameterKind kind = ParameterKind.POSITIONAL;
7496 DefaultFormalParameter parameter =
7497 parse("parseFormalParameter", <Object>[kind], "final A a = null");
7498 SimpleFormalParameter simpleParameter =
7499 parameter.parameter as SimpleFormalParameter;
7500 expect(simpleParameter.identifier, isNotNull);
7501 expect(simpleParameter.keyword, isNotNull);
7502 expect(simpleParameter.type, isNotNull);
7503 expect(simpleParameter.kind, kind);
7504 expect(parameter.separator, isNotNull);
7505 expect(parameter.defaultValue, isNotNull);
7506 expect(parameter.kind, kind);
7507 }
7508
7509 void test_parseFormalParameter_nonFinal_withType_named() {
7510 ParameterKind kind = ParameterKind.NAMED;
7511 DefaultFormalParameter parameter =
7512 parse("parseFormalParameter", <Object>[kind], "A a : null");
7513 SimpleFormalParameter simpleParameter =
7514 parameter.parameter as SimpleFormalParameter;
7515 expect(simpleParameter.identifier, isNotNull);
7516 expect(simpleParameter.keyword, isNull);
7517 expect(simpleParameter.type, isNotNull);
7518 expect(simpleParameter.kind, kind);
7519 expect(parameter.separator, isNotNull);
7520 expect(parameter.defaultValue, isNotNull);
7521 expect(parameter.kind, kind);
7522 }
7523
7524 void test_parseFormalParameter_nonFinal_withType_normal() {
7525 ParameterKind kind = ParameterKind.REQUIRED;
7526 SimpleFormalParameter parameter =
7527 parse("parseFormalParameter", <Object>[kind], "A a");
7528 expect(parameter.identifier, isNotNull);
7529 expect(parameter.keyword, isNull);
7530 expect(parameter.type, isNotNull);
7531 expect(parameter.kind, kind);
7532 }
7533
7534 void test_parseFormalParameter_nonFinal_withType_positional() {
7535 ParameterKind kind = ParameterKind.POSITIONAL;
7536 DefaultFormalParameter parameter =
7537 parse("parseFormalParameter", <Object>[kind], "A a = null");
7538 SimpleFormalParameter simpleParameter =
7539 parameter.parameter as SimpleFormalParameter;
7540 expect(simpleParameter.identifier, isNotNull);
7541 expect(simpleParameter.keyword, isNull);
7542 expect(simpleParameter.type, isNotNull);
7543 expect(simpleParameter.kind, kind);
7544 expect(parameter.separator, isNotNull);
7545 expect(parameter.defaultValue, isNotNull);
7546 expect(parameter.kind, kind);
7547 }
7548
7549 void test_parseFormalParameter_var() {
7550 ParameterKind kind = ParameterKind.REQUIRED;
7551 SimpleFormalParameter parameter =
7552 parse("parseFormalParameter", <Object>[kind], "var a");
7553 expect(parameter.identifier, isNotNull);
7554 expect(parameter.keyword, isNotNull);
7555 expect(parameter.type, isNull);
7556 expect(parameter.kind, kind);
7557 }
7558
7559 void test_parseFormalParameter_var_named() {
7560 ParameterKind kind = ParameterKind.NAMED;
7561 DefaultFormalParameter parameter =
7562 parse("parseFormalParameter", <Object>[kind], "var a : null");
7563 SimpleFormalParameter simpleParameter =
7564 parameter.parameter as SimpleFormalParameter;
7565 expect(simpleParameter.identifier, isNotNull);
7566 expect(simpleParameter.keyword, isNotNull);
7567 expect(simpleParameter.type, isNull);
7568 expect(simpleParameter.kind, kind);
7569 expect(parameter.separator, isNotNull);
7570 expect(parameter.defaultValue, isNotNull);
7571 expect(parameter.kind, kind);
7572 }
7573
7574 void test_parseFormalParameter_var_positional() {
7575 ParameterKind kind = ParameterKind.POSITIONAL;
7576 DefaultFormalParameter parameter =
7577 parse("parseFormalParameter", <Object>[kind], "var a = null");
7578 SimpleFormalParameter simpleParameter =
7579 parameter.parameter as SimpleFormalParameter;
7580 expect(simpleParameter.identifier, isNotNull);
7581 expect(simpleParameter.keyword, isNotNull);
7582 expect(simpleParameter.type, isNull);
7583 expect(simpleParameter.kind, kind);
7584 expect(parameter.separator, isNotNull);
7585 expect(parameter.defaultValue, isNotNull);
7586 expect(parameter.kind, kind);
7587 }
7588
7589 void test_parseFormalParameterList_empty() {
7590 FormalParameterList parameterList =
7591 parse4("parseFormalParameterList", "()");
7592 expect(parameterList.leftParenthesis, isNotNull);
7593 expect(parameterList.leftDelimiter, isNull);
7594 expect(parameterList.parameters, hasLength(0));
7595 expect(parameterList.rightDelimiter, isNull);
7596 expect(parameterList.rightParenthesis, isNotNull);
7597 }
7598
7599 void test_parseFormalParameterList_named_multiple() {
7600 FormalParameterList parameterList =
7601 parse4("parseFormalParameterList", "({A a : 1, B b, C c : 3})");
7602 expect(parameterList.leftParenthesis, isNotNull);
7603 expect(parameterList.leftDelimiter, isNotNull);
7604 expect(parameterList.parameters, hasLength(3));
7605 expect(parameterList.rightDelimiter, isNotNull);
7606 expect(parameterList.rightParenthesis, isNotNull);
7607 }
7608
7609 void test_parseFormalParameterList_named_single() {
7610 FormalParameterList parameterList =
7611 parse4("parseFormalParameterList", "({A a})");
7612 expect(parameterList.leftParenthesis, isNotNull);
7613 expect(parameterList.leftDelimiter, isNotNull);
7614 expect(parameterList.parameters, hasLength(1));
7615 expect(parameterList.rightDelimiter, isNotNull);
7616 expect(parameterList.rightParenthesis, isNotNull);
7617 }
7618
7619 void test_parseFormalParameterList_normal_multiple() {
7620 FormalParameterList parameterList =
7621 parse4("parseFormalParameterList", "(A a, B b, C c)");
7622 expect(parameterList.leftParenthesis, isNotNull);
7623 expect(parameterList.leftDelimiter, isNull);
7624 expect(parameterList.parameters, hasLength(3));
7625 expect(parameterList.rightDelimiter, isNull);
7626 expect(parameterList.rightParenthesis, isNotNull);
7627 }
7628
7629 void test_parseFormalParameterList_normal_named() {
7630 FormalParameterList parameterList =
7631 parse4("parseFormalParameterList", "(A a, {B b})");
7632 expect(parameterList.leftParenthesis, isNotNull);
7633 expect(parameterList.leftDelimiter, isNotNull);
7634 expect(parameterList.parameters, hasLength(2));
7635 expect(parameterList.rightDelimiter, isNotNull);
7636 expect(parameterList.rightParenthesis, isNotNull);
7637 }
7638
7639 void test_parseFormalParameterList_normal_positional() {
7640 FormalParameterList parameterList =
7641 parse4("parseFormalParameterList", "(A a, [B b])");
7642 expect(parameterList.leftParenthesis, isNotNull);
7643 expect(parameterList.leftDelimiter, isNotNull);
7644 expect(parameterList.parameters, hasLength(2));
7645 expect(parameterList.rightDelimiter, isNotNull);
7646 expect(parameterList.rightParenthesis, isNotNull);
7647 }
7648
7649 void test_parseFormalParameterList_normal_single() {
7650 FormalParameterList parameterList =
7651 parse4("parseFormalParameterList", "(A a)");
7652 expect(parameterList.leftParenthesis, isNotNull);
7653 expect(parameterList.leftDelimiter, isNull);
7654 expect(parameterList.parameters, hasLength(1));
7655 expect(parameterList.rightDelimiter, isNull);
7656 expect(parameterList.rightParenthesis, isNotNull);
7657 }
7658
7659 void test_parseFormalParameterList_positional_multiple() {
7660 FormalParameterList parameterList =
7661 parse4("parseFormalParameterList", "([A a = null, B b, C c = null])");
7662 expect(parameterList.leftParenthesis, isNotNull);
7663 expect(parameterList.leftDelimiter, isNotNull);
7664 expect(parameterList.parameters, hasLength(3));
7665 expect(parameterList.rightDelimiter, isNotNull);
7666 expect(parameterList.rightParenthesis, isNotNull);
7667 }
7668
7669 void test_parseFormalParameterList_positional_single() {
7670 FormalParameterList parameterList =
7671 parse4("parseFormalParameterList", "([A a = null])");
7672 expect(parameterList.leftParenthesis, isNotNull);
7673 expect(parameterList.leftDelimiter, isNotNull);
7674 expect(parameterList.parameters, hasLength(1));
7675 expect(parameterList.rightDelimiter, isNotNull);
7676 expect(parameterList.rightParenthesis, isNotNull);
7677 }
7678
7679 void test_parseFormalParameterList_prefixedType() {
7680 FormalParameterList parameterList =
7681 parse4("parseFormalParameterList", "(io.File f)");
7682 expect(parameterList.leftParenthesis, isNotNull);
7683 expect(parameterList.leftDelimiter, isNull);
7684 expect(parameterList.parameters, hasLength(1));
7685 expect(parameterList.parameters[0].toSource(), 'io.File f');
7686 expect(parameterList.rightDelimiter, isNull);
7687 expect(parameterList.rightParenthesis, isNotNull);
7688 }
7689
7690 void test_parseFormalParameterList_prefixedType_partial() {
7691 FormalParameterList parameterList = parse4(
7692 "parseFormalParameterList", "(io.)", [
7693 ParserErrorCode.MISSING_IDENTIFIER,
7694 ParserErrorCode.MISSING_IDENTIFIER
7695 ]);
7696 expect(parameterList.leftParenthesis, isNotNull);
7697 expect(parameterList.leftDelimiter, isNull);
7698 expect(parameterList.parameters, hasLength(1));
7699 expect(parameterList.parameters[0].toSource(), 'io. ');
7700 expect(parameterList.rightDelimiter, isNull);
7701 expect(parameterList.rightParenthesis, isNotNull);
7702 }
7703
7704 void test_parseFormalParameterList_prefixedType_partial2() {
7705 FormalParameterList parameterList = parse4(
7706 "parseFormalParameterList", "(io.,a)", [
7707 ParserErrorCode.MISSING_IDENTIFIER,
7708 ParserErrorCode.MISSING_IDENTIFIER
7709 ]);
7710 expect(parameterList.leftParenthesis, isNotNull);
7711 expect(parameterList.leftDelimiter, isNull);
7712 expect(parameterList.parameters, hasLength(2));
7713 expect(parameterList.parameters[0].toSource(), 'io. ');
7714 expect(parameterList.parameters[1].toSource(), 'a');
7715 expect(parameterList.rightDelimiter, isNull);
7716 expect(parameterList.rightParenthesis, isNotNull);
7717 }
7718
7719 void test_parseForStatement_each_await() {
7720 ForEachStatement statement =
7721 parse4("parseForStatement", "await for (element in list) {}");
7722 expect(statement.awaitKeyword, isNotNull);
7723 expect(statement.forKeyword, isNotNull);
7724 expect(statement.leftParenthesis, isNotNull);
7725 expect(statement.loopVariable, isNull);
7726 expect(statement.identifier, isNotNull);
7727 expect(statement.inKeyword, isNotNull);
7728 expect(statement.iterable, isNotNull);
7729 expect(statement.rightParenthesis, isNotNull);
7730 expect(statement.body, isNotNull);
7731 }
7732
7733 void test_parseForStatement_each_identifier() {
7734 ForEachStatement statement =
7735 parse4("parseForStatement", "for (element in list) {}");
7736 expect(statement.awaitKeyword, isNull);
7737 expect(statement.forKeyword, isNotNull);
7738 expect(statement.leftParenthesis, isNotNull);
7739 expect(statement.loopVariable, isNull);
7740 expect(statement.identifier, isNotNull);
7741 expect(statement.inKeyword, isNotNull);
7742 expect(statement.iterable, isNotNull);
7743 expect(statement.rightParenthesis, isNotNull);
7744 expect(statement.body, isNotNull);
7745 }
7746
7747 void test_parseForStatement_each_noType_metadata() {
7748 ForEachStatement statement =
7749 parse4("parseForStatement", "for (@A var element in list) {}");
7750 expect(statement.awaitKeyword, isNull);
7751 expect(statement.forKeyword, isNotNull);
7752 expect(statement.leftParenthesis, isNotNull);
7753 expect(statement.loopVariable, isNotNull);
7754 expect(statement.loopVariable.metadata, hasLength(1));
7755 expect(statement.identifier, isNull);
7756 expect(statement.inKeyword, isNotNull);
7757 expect(statement.iterable, isNotNull);
7758 expect(statement.rightParenthesis, isNotNull);
7759 expect(statement.body, isNotNull);
7760 }
7761
7762 void test_parseForStatement_each_type() {
7763 ForEachStatement statement =
7764 parse4("parseForStatement", "for (A element in list) {}");
7765 expect(statement.awaitKeyword, isNull);
7766 expect(statement.forKeyword, isNotNull);
7767 expect(statement.leftParenthesis, isNotNull);
7768 expect(statement.loopVariable, isNotNull);
7769 expect(statement.identifier, isNull);
7770 expect(statement.inKeyword, isNotNull);
7771 expect(statement.iterable, isNotNull);
7772 expect(statement.rightParenthesis, isNotNull);
7773 expect(statement.body, isNotNull);
7774 }
7775
7776 void test_parseForStatement_each_var() {
7777 ForEachStatement statement =
7778 parse4("parseForStatement", "for (var element in list) {}");
7779 expect(statement.awaitKeyword, isNull);
7780 expect(statement.forKeyword, isNotNull);
7781 expect(statement.leftParenthesis, isNotNull);
7782 expect(statement.loopVariable, isNotNull);
7783 expect(statement.identifier, isNull);
7784 expect(statement.inKeyword, isNotNull);
7785 expect(statement.iterable, isNotNull);
7786 expect(statement.rightParenthesis, isNotNull);
7787 expect(statement.body, isNotNull);
7788 }
7789
7790 void test_parseForStatement_loop_c() {
7791 ForStatement statement =
7792 parse4("parseForStatement", "for (; i < count;) {}");
7793 expect(statement.forKeyword, isNotNull);
7794 expect(statement.leftParenthesis, isNotNull);
7795 expect(statement.variables, isNull);
7796 expect(statement.initialization, isNull);
7797 expect(statement.leftSeparator, isNotNull);
7798 expect(statement.condition, isNotNull);
7799 expect(statement.rightSeparator, isNotNull);
7800 expect(statement.updaters, hasLength(0));
7801 expect(statement.rightParenthesis, isNotNull);
7802 expect(statement.body, isNotNull);
7803 }
7804
7805 void test_parseForStatement_loop_cu() {
7806 ForStatement statement =
7807 parse4("parseForStatement", "for (; i < count; i++) {}");
7808 expect(statement.forKeyword, isNotNull);
7809 expect(statement.leftParenthesis, isNotNull);
7810 expect(statement.variables, isNull);
7811 expect(statement.initialization, isNull);
7812 expect(statement.leftSeparator, isNotNull);
7813 expect(statement.condition, isNotNull);
7814 expect(statement.rightSeparator, isNotNull);
7815 expect(statement.updaters, hasLength(1));
7816 expect(statement.rightParenthesis, isNotNull);
7817 expect(statement.body, isNotNull);
7818 }
7819
7820 void test_parseForStatement_loop_ecu() {
7821 ForStatement statement =
7822 parse4("parseForStatement", "for (i--; i < count; i++) {}");
7823 expect(statement.forKeyword, isNotNull);
7824 expect(statement.leftParenthesis, isNotNull);
7825 expect(statement.variables, isNull);
7826 expect(statement.initialization, isNotNull);
7827 expect(statement.leftSeparator, isNotNull);
7828 expect(statement.condition, isNotNull);
7829 expect(statement.rightSeparator, isNotNull);
7830 expect(statement.updaters, hasLength(1));
7831 expect(statement.rightParenthesis, isNotNull);
7832 expect(statement.body, isNotNull);
7833 }
7834
7835 void test_parseForStatement_loop_i() {
7836 ForStatement statement =
7837 parse4("parseForStatement", "for (var i = 0;;) {}");
7838 expect(statement.forKeyword, isNotNull);
7839 expect(statement.leftParenthesis, isNotNull);
7840 VariableDeclarationList variables = statement.variables;
7841 expect(variables, isNotNull);
7842 expect(variables.metadata, hasLength(0));
7843 expect(variables.variables, hasLength(1));
7844 expect(statement.initialization, isNull);
7845 expect(statement.leftSeparator, isNotNull);
7846 expect(statement.condition, isNull);
7847 expect(statement.rightSeparator, isNotNull);
7848 expect(statement.updaters, hasLength(0));
7849 expect(statement.rightParenthesis, isNotNull);
7850 expect(statement.body, isNotNull);
7851 }
7852
7853 void test_parseForStatement_loop_i_withMetadata() {
7854 ForStatement statement =
7855 parse4("parseForStatement", "for (@A var i = 0;;) {}");
7856 expect(statement.forKeyword, isNotNull);
7857 expect(statement.leftParenthesis, isNotNull);
7858 VariableDeclarationList variables = statement.variables;
7859 expect(variables, isNotNull);
7860 expect(variables.metadata, hasLength(1));
7861 expect(variables.variables, hasLength(1));
7862 expect(statement.initialization, isNull);
7863 expect(statement.leftSeparator, isNotNull);
7864 expect(statement.condition, isNull);
7865 expect(statement.rightSeparator, isNotNull);
7866 expect(statement.updaters, hasLength(0));
7867 expect(statement.rightParenthesis, isNotNull);
7868 expect(statement.body, isNotNull);
7869 }
7870
7871 void test_parseForStatement_loop_ic() {
7872 ForStatement statement =
7873 parse4("parseForStatement", "for (var i = 0; i < count;) {}");
7874 expect(statement.forKeyword, isNotNull);
7875 expect(statement.leftParenthesis, isNotNull);
7876 VariableDeclarationList variables = statement.variables;
7877 expect(variables, isNotNull);
7878 expect(variables.variables, hasLength(1));
7879 expect(statement.initialization, isNull);
7880 expect(statement.leftSeparator, isNotNull);
7881 expect(statement.condition, isNotNull);
7882 expect(statement.rightSeparator, isNotNull);
7883 expect(statement.updaters, hasLength(0));
7884 expect(statement.rightParenthesis, isNotNull);
7885 expect(statement.body, isNotNull);
7886 }
7887
7888 void test_parseForStatement_loop_icu() {
7889 ForStatement statement =
7890 parse4("parseForStatement", "for (var i = 0; i < count; i++) {}");
7891 expect(statement.forKeyword, isNotNull);
7892 expect(statement.leftParenthesis, isNotNull);
7893 VariableDeclarationList variables = statement.variables;
7894 expect(variables, isNotNull);
7895 expect(variables.variables, hasLength(1));
7896 expect(statement.initialization, isNull);
7897 expect(statement.leftSeparator, isNotNull);
7898 expect(statement.condition, isNotNull);
7899 expect(statement.rightSeparator, isNotNull);
7900 expect(statement.updaters, hasLength(1));
7901 expect(statement.rightParenthesis, isNotNull);
7902 expect(statement.body, isNotNull);
7903 }
7904
7905 void test_parseForStatement_loop_iicuu() {
7906 ForStatement statement = parse4(
7907 "parseForStatement", "for (int i = 0, j = count; i < j; i++, j--) {}");
7908 expect(statement.forKeyword, isNotNull);
7909 expect(statement.leftParenthesis, isNotNull);
7910 VariableDeclarationList variables = statement.variables;
7911 expect(variables, isNotNull);
7912 expect(variables.variables, hasLength(2));
7913 expect(statement.initialization, isNull);
7914 expect(statement.leftSeparator, isNotNull);
7915 expect(statement.condition, isNotNull);
7916 expect(statement.rightSeparator, isNotNull);
7917 expect(statement.updaters, hasLength(2));
7918 expect(statement.rightParenthesis, isNotNull);
7919 expect(statement.body, isNotNull);
7920 }
7921
7922 void test_parseForStatement_loop_iu() {
7923 ForStatement statement =
7924 parse4("parseForStatement", "for (var i = 0;; i++) {}");
7925 expect(statement.forKeyword, isNotNull);
7926 expect(statement.leftParenthesis, isNotNull);
7927 VariableDeclarationList variables = statement.variables;
7928 expect(variables, isNotNull);
7929 expect(variables.variables, hasLength(1));
7930 expect(statement.initialization, isNull);
7931 expect(statement.leftSeparator, isNotNull);
7932 expect(statement.condition, isNull);
7933 expect(statement.rightSeparator, isNotNull);
7934 expect(statement.updaters, hasLength(1));
7935 expect(statement.rightParenthesis, isNotNull);
7936 expect(statement.body, isNotNull);
7937 }
7938
7939 void test_parseForStatement_loop_u() {
7940 ForStatement statement = parse4("parseForStatement", "for (;; i++) {}");
7941 expect(statement.forKeyword, isNotNull);
7942 expect(statement.leftParenthesis, isNotNull);
7943 expect(statement.variables, isNull);
7944 expect(statement.initialization, isNull);
7945 expect(statement.leftSeparator, isNotNull);
7946 expect(statement.condition, isNull);
7947 expect(statement.rightSeparator, isNotNull);
7948 expect(statement.updaters, hasLength(1));
7949 expect(statement.rightParenthesis, isNotNull);
7950 expect(statement.body, isNotNull);
7951 }
7952
7953 void test_parseFunctionBody_block() {
7954 BlockFunctionBody functionBody =
7955 parse("parseFunctionBody", <Object>[false, null, false], "{}");
7956 expect(functionBody.keyword, isNull);
7957 expect(functionBody.star, isNull);
7958 expect(functionBody.block, isNotNull);
7959 expect(functionBody.isAsynchronous, isFalse);
7960 expect(functionBody.isGenerator, isFalse);
7961 expect(functionBody.isSynchronous, isTrue);
7962 }
7963
7964 void test_parseFunctionBody_block_async() {
7965 BlockFunctionBody functionBody =
7966 parse("parseFunctionBody", <Object>[false, null, false], "async {}");
7967 expect(functionBody.keyword, isNotNull);
7968 expect(functionBody.keyword.lexeme, Parser.ASYNC);
7969 expect(functionBody.star, isNull);
7970 expect(functionBody.block, isNotNull);
7971 expect(functionBody.isAsynchronous, isTrue);
7972 expect(functionBody.isGenerator, isFalse);
7973 expect(functionBody.isSynchronous, isFalse);
7974 }
7975
7976 void test_parseFunctionBody_block_asyncGenerator() {
7977 BlockFunctionBody functionBody =
7978 parse("parseFunctionBody", <Object>[false, null, false], "async* {}");
7979 expect(functionBody.keyword, isNotNull);
7980 expect(functionBody.keyword.lexeme, Parser.ASYNC);
7981 expect(functionBody.star, isNotNull);
7982 expect(functionBody.block, isNotNull);
7983 expect(functionBody.isAsynchronous, isTrue);
7984 expect(functionBody.isGenerator, isTrue);
7985 expect(functionBody.isSynchronous, isFalse);
7986 }
7987
7988 void test_parseFunctionBody_block_syncGenerator() {
7989 BlockFunctionBody functionBody =
7990 parse("parseFunctionBody", <Object>[false, null, false], "sync* {}");
7991 expect(functionBody.keyword, isNotNull);
7992 expect(functionBody.keyword.lexeme, Parser.SYNC);
7993 expect(functionBody.star, isNotNull);
7994 expect(functionBody.block, isNotNull);
7995 expect(functionBody.isAsynchronous, isFalse);
7996 expect(functionBody.isGenerator, isTrue);
7997 expect(functionBody.isSynchronous, isTrue);
7998 }
7999
8000 void test_parseFunctionBody_empty() {
8001 EmptyFunctionBody functionBody =
8002 parse("parseFunctionBody", <Object>[true, null, false], ";");
8003 expect(functionBody.semicolon, isNotNull);
8004 }
8005
8006 void test_parseFunctionBody_expression() {
8007 ExpressionFunctionBody functionBody =
8008 parse("parseFunctionBody", <Object>[false, null, false], "=> y;");
8009 expect(functionBody.keyword, isNull);
8010 expect(functionBody.functionDefinition, isNotNull);
8011 expect(functionBody.expression, isNotNull);
8012 expect(functionBody.semicolon, isNotNull);
8013 expect(functionBody.isAsynchronous, isFalse);
8014 expect(functionBody.isGenerator, isFalse);
8015 expect(functionBody.isSynchronous, isTrue);
8016 }
8017
8018 void test_parseFunctionBody_expression_async() {
8019 ExpressionFunctionBody functionBody =
8020 parse("parseFunctionBody", <Object>[false, null, false], "async => y;");
8021 expect(functionBody.keyword, isNotNull);
8022 expect(functionBody.keyword.lexeme, Parser.ASYNC);
8023 expect(functionBody.functionDefinition, isNotNull);
8024 expect(functionBody.expression, isNotNull);
8025 expect(functionBody.semicolon, isNotNull);
8026 expect(functionBody.isAsynchronous, isTrue);
8027 expect(functionBody.isGenerator, isFalse);
8028 expect(functionBody.isSynchronous, isFalse);
8029 }
8030
8031 void test_parseFunctionBody_nativeFunctionBody() {
8032 NativeFunctionBody functionBody = parse(
8033 "parseFunctionBody", <Object>[false, null, false], "native 'str';");
8034 expect(functionBody.nativeKeyword, isNotNull);
8035 expect(functionBody.stringLiteral, isNotNull);
8036 expect(functionBody.semicolon, isNotNull);
8037 }
8038
8039 void test_parseFunctionBody_skip_block() {
8040 ParserTestCase.parseFunctionBodies = false;
8041 FunctionBody functionBody =
8042 parse("parseFunctionBody", <Object>[false, null, false], "{}");
8043 EngineTestCase.assertInstanceOf(
8044 (obj) => obj is EmptyFunctionBody, EmptyFunctionBody, functionBody);
8045 }
8046
8047 void test_parseFunctionBody_skip_block_invalid() {
8048 ParserTestCase.parseFunctionBodies = false;
8049 FunctionBody functionBody = parse3("parseFunctionBody",
8050 <Object>[false, null, false], "{", [ParserErrorCode.EXPECTED_TOKEN]);
8051 EngineTestCase.assertInstanceOf(
8052 (obj) => obj is EmptyFunctionBody, EmptyFunctionBody, functionBody);
8053 }
8054
8055 void test_parseFunctionBody_skip_blocks() {
8056 ParserTestCase.parseFunctionBodies = false;
8057 FunctionBody functionBody =
8058 parse("parseFunctionBody", <Object>[false, null, false], "{ {} }");
8059 EngineTestCase.assertInstanceOf(
8060 (obj) => obj is EmptyFunctionBody, EmptyFunctionBody, functionBody);
8061 }
8062
8063 void test_parseFunctionBody_skip_expression() {
8064 ParserTestCase.parseFunctionBodies = false;
8065 FunctionBody functionBody =
8066 parse("parseFunctionBody", <Object>[false, null, false], "=> y;");
8067 EngineTestCase.assertInstanceOf(
8068 (obj) => obj is EmptyFunctionBody, EmptyFunctionBody, functionBody);
8069 }
8070
8071 void test_parseFunctionDeclaration_function() {
8072 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8073 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
8074 FunctionDeclaration declaration = parse("parseFunctionDeclaration",
8075 <Object>[commentAndMetadata(comment), null, returnType], "f() {}");
8076 expect(declaration.documentationComment, comment);
8077 expect(declaration.returnType, returnType);
8078 expect(declaration.name, isNotNull);
8079 FunctionExpression expression = declaration.functionExpression;
8080 expect(expression, isNotNull);
8081 expect(expression.body, isNotNull);
8082 expect(expression.typeParameters, isNull);
8083 expect(expression.parameters, isNotNull);
8084 expect(declaration.propertyKeyword, isNull);
8085 }
8086
8087 void test_parseFunctionDeclaration_functionWithTypeParameters() {
8088 enableGenericMethods = true;
8089 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8090 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
8091 FunctionDeclaration declaration = parse("parseFunctionDeclaration",
8092 <Object>[commentAndMetadata(comment), null, returnType], "f<E>() {}");
8093 expect(declaration.documentationComment, comment);
8094 expect(declaration.returnType, returnType);
8095 expect(declaration.name, isNotNull);
8096 FunctionExpression expression = declaration.functionExpression;
8097 expect(expression, isNotNull);
8098 expect(expression.body, isNotNull);
8099 expect(expression.typeParameters, isNotNull);
8100 expect(expression.parameters, isNotNull);
8101 expect(declaration.propertyKeyword, isNull);
8102 }
8103
8104 void test_parseFunctionDeclaration_getter() {
8105 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8106 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
8107 FunctionDeclaration declaration = parse("parseFunctionDeclaration",
8108 <Object>[commentAndMetadata(comment), null, returnType], "get p => 0;");
8109 expect(declaration.documentationComment, comment);
8110 expect(declaration.returnType, returnType);
8111 expect(declaration.name, isNotNull);
8112 FunctionExpression expression = declaration.functionExpression;
8113 expect(expression, isNotNull);
8114 expect(expression.body, isNotNull);
8115 expect(expression.typeParameters, isNull);
8116 expect(expression.parameters, isNull);
8117 expect(declaration.propertyKeyword, isNotNull);
8118 }
8119
8120 void test_parseFunctionDeclaration_setter() {
8121 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8122 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
8123 FunctionDeclaration declaration = parse("parseFunctionDeclaration",
8124 <Object>[commentAndMetadata(comment), null, returnType], "set p(v) {}");
8125 expect(declaration.documentationComment, comment);
8126 expect(declaration.returnType, returnType);
8127 expect(declaration.name, isNotNull);
8128 FunctionExpression expression = declaration.functionExpression;
8129 expect(expression, isNotNull);
8130 expect(expression.body, isNotNull);
8131 expect(expression.typeParameters, isNull);
8132 expect(expression.parameters, isNotNull);
8133 expect(declaration.propertyKeyword, isNotNull);
8134 }
8135
8136 void test_parseFunctionDeclarationStatement() {
8137 FunctionDeclarationStatement statement =
8138 parse4("parseFunctionDeclarationStatement", "void f(int p) => p * 2;");
8139 expect(statement.functionDeclaration, isNotNull);
8140 }
8141
8142 void test_parseFunctionDeclarationStatement_typeParameters() {
8143 enableGenericMethods = true;
8144 FunctionDeclarationStatement statement =
8145 parse4("parseFunctionDeclarationStatement", "E f<E>(E p) => p * 2;");
8146 expect(statement.functionDeclaration, isNotNull);
8147 }
8148
8149 void test_parseFunctionExpression_body_inExpression() {
8150 FunctionExpression expression =
8151 parse4("parseFunctionExpression", "(int i) => i++");
8152 expect(expression.body, isNotNull);
8153 expect(expression.typeParameters, isNull);
8154 expect(expression.parameters, isNotNull);
8155 expect((expression.body as ExpressionFunctionBody).semicolon, isNull);
8156 }
8157
8158 void test_parseFunctionExpression_typeParameters() {
8159 enableGenericMethods = true;
8160 FunctionExpression expression =
8161 parse4("parseFunctionExpression", "<E>(E i) => i++");
8162 expect(expression.body, isNotNull);
8163 expect(expression.typeParameters, isNotNull);
8164 expect(expression.parameters, isNotNull);
8165 expect((expression.body as ExpressionFunctionBody).semicolon, isNull);
8166 }
8167
8168 void test_parseGetter_nonStatic() {
8169 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8170 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
8171 MethodDeclaration method = parse(
8172 "parseGetter",
8173 <Object>[commentAndMetadata(comment), null, null, returnType],
8174 "get a;");
8175 expect(method.body, isNotNull);
8176 expect(method.documentationComment, comment);
8177 expect(method.externalKeyword, isNull);
8178 expect(method.modifierKeyword, isNull);
8179 expect(method.name, isNotNull);
8180 expect(method.operatorKeyword, isNull);
8181 expect(method.parameters, isNull);
8182 expect(method.propertyKeyword, isNotNull);
8183 expect(method.returnType, returnType);
8184 }
8185
8186 void test_parseGetter_static() {
8187 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8188 Token staticKeyword = TokenFactory.tokenFromKeyword(Keyword.STATIC);
8189 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
8190 MethodDeclaration method = parse(
8191 "parseGetter",
8192 <Object>[commentAndMetadata(comment), null, staticKeyword, returnType],
8193 "get a => 42;");
8194 expect(method.body, isNotNull);
8195 expect(method.documentationComment, comment);
8196 expect(method.externalKeyword, isNull);
8197 expect(method.modifierKeyword, staticKeyword);
8198 expect(method.name, isNotNull);
8199 expect(method.operatorKeyword, isNull);
8200 expect(method.typeParameters, isNull);
8201 expect(method.parameters, isNull);
8202 expect(method.propertyKeyword, isNotNull);
8203 expect(method.returnType, returnType);
8204 }
8205
8206 void test_parseIdentifierList_multiple() {
8207 List<SimpleIdentifier> list = parse4("parseIdentifierList", "a, b, c");
8208 expect(list, hasLength(3));
8209 }
8210
8211 void test_parseIdentifierList_single() {
8212 List<SimpleIdentifier> list = parse4("parseIdentifierList", "a");
8213 expect(list, hasLength(1));
8214 }
8215
8216 void test_parseIfStatement_else_block() {
8217 IfStatement statement = parse4("parseIfStatement", "if (x) {} else {}");
8218 expect(statement.ifKeyword, isNotNull);
8219 expect(statement.leftParenthesis, isNotNull);
8220 expect(statement.condition, isNotNull);
8221 expect(statement.rightParenthesis, isNotNull);
8222 expect(statement.thenStatement, isNotNull);
8223 expect(statement.elseKeyword, isNotNull);
8224 expect(statement.elseStatement, isNotNull);
8225 }
8226
8227 void test_parseIfStatement_else_statement() {
8228 IfStatement statement =
8229 parse4("parseIfStatement", "if (x) f(x); else f(y);");
8230 expect(statement.ifKeyword, isNotNull);
8231 expect(statement.leftParenthesis, isNotNull);
8232 expect(statement.condition, isNotNull);
8233 expect(statement.rightParenthesis, isNotNull);
8234 expect(statement.thenStatement, isNotNull);
8235 expect(statement.elseKeyword, isNotNull);
8236 expect(statement.elseStatement, isNotNull);
8237 }
8238
8239 void test_parseIfStatement_noElse_block() {
8240 IfStatement statement = parse4("parseIfStatement", "if (x) {}");
8241 expect(statement.ifKeyword, isNotNull);
8242 expect(statement.leftParenthesis, isNotNull);
8243 expect(statement.condition, isNotNull);
8244 expect(statement.rightParenthesis, isNotNull);
8245 expect(statement.thenStatement, isNotNull);
8246 expect(statement.elseKeyword, isNull);
8247 expect(statement.elseStatement, isNull);
8248 }
8249
8250 void test_parseIfStatement_noElse_statement() {
8251 IfStatement statement = parse4("parseIfStatement", "if (x) f(x);");
8252 expect(statement.ifKeyword, isNotNull);
8253 expect(statement.leftParenthesis, isNotNull);
8254 expect(statement.condition, isNotNull);
8255 expect(statement.rightParenthesis, isNotNull);
8256 expect(statement.thenStatement, isNotNull);
8257 expect(statement.elseKeyword, isNull);
8258 expect(statement.elseStatement, isNull);
8259 }
8260
8261 void test_parseImplementsClause_multiple() {
8262 ImplementsClause clause =
8263 parse4("parseImplementsClause", "implements A, B, C");
8264 expect(clause.interfaces, hasLength(3));
8265 expect(clause.implementsKeyword, isNotNull);
8266 }
8267
8268 void test_parseImplementsClause_single() {
8269 ImplementsClause clause = parse4("parseImplementsClause", "implements A");
8270 expect(clause.interfaces, hasLength(1));
8271 expect(clause.implementsKeyword, isNotNull);
8272 }
8273
8274 void test_parseImportDirective_deferred() {
8275 ImportDirective directive = parse(
8276 "parseImportDirective",
8277 <Object>[emptyCommentAndMetadata()],
8278 "import 'lib/lib.dart' deferred as a;");
8279 expect(directive.keyword, isNotNull);
8280 expect(directive.uri, isNotNull);
8281 expect(directive.deferredKeyword, isNotNull);
8282 expect(directive.asKeyword, isNotNull);
8283 expect(directive.prefix, isNotNull);
8284 expect(directive.combinators, hasLength(0));
8285 expect(directive.semicolon, isNotNull);
8286 }
8287
8288 void test_parseImportDirective_hide() {
8289 ImportDirective directive = parse(
8290 "parseImportDirective",
8291 <Object>[emptyCommentAndMetadata()],
8292 "import 'lib/lib.dart' hide A, B;");
8293 expect(directive.keyword, isNotNull);
8294 expect(directive.uri, isNotNull);
8295 expect(directive.deferredKeyword, isNull);
8296 expect(directive.asKeyword, isNull);
8297 expect(directive.prefix, isNull);
8298 expect(directive.combinators, hasLength(1));
8299 expect(directive.semicolon, isNotNull);
8300 }
8301
8302 void test_parseImportDirective_noCombinator() {
8303 ImportDirective directive = parse("parseImportDirective",
8304 <Object>[emptyCommentAndMetadata()], "import 'lib/lib.dart';");
8305 expect(directive.keyword, isNotNull);
8306 expect(directive.uri, isNotNull);
8307 expect(directive.deferredKeyword, isNull);
8308 expect(directive.asKeyword, isNull);
8309 expect(directive.prefix, isNull);
8310 expect(directive.combinators, hasLength(0));
8311 expect(directive.semicolon, isNotNull);
8312 }
8313
8314 void test_parseImportDirective_prefix() {
8315 ImportDirective directive = parse("parseImportDirective",
8316 <Object>[emptyCommentAndMetadata()], "import 'lib/lib.dart' as a;");
8317 expect(directive.keyword, isNotNull);
8318 expect(directive.uri, isNotNull);
8319 expect(directive.deferredKeyword, isNull);
8320 expect(directive.asKeyword, isNotNull);
8321 expect(directive.prefix, isNotNull);
8322 expect(directive.combinators, hasLength(0));
8323 expect(directive.semicolon, isNotNull);
8324 }
8325
8326 void test_parseImportDirective_prefix_hide_show() {
8327 ImportDirective directive = parse(
8328 "parseImportDirective",
8329 <Object>[emptyCommentAndMetadata()],
8330 "import 'lib/lib.dart' as a hide A show B;");
8331 expect(directive.keyword, isNotNull);
8332 expect(directive.uri, isNotNull);
8333 expect(directive.deferredKeyword, isNull);
8334 expect(directive.asKeyword, isNotNull);
8335 expect(directive.prefix, isNotNull);
8336 expect(directive.combinators, hasLength(2));
8337 expect(directive.semicolon, isNotNull);
8338 }
8339
8340 void test_parseImportDirective_prefix_show_hide() {
8341 ImportDirective directive = parse(
8342 "parseImportDirective",
8343 <Object>[emptyCommentAndMetadata()],
8344 "import 'lib/lib.dart' as a show B hide A;");
8345 expect(directive.keyword, isNotNull);
8346 expect(directive.uri, isNotNull);
8347 expect(directive.deferredKeyword, isNull);
8348 expect(directive.asKeyword, isNotNull);
8349 expect(directive.prefix, isNotNull);
8350 expect(directive.combinators, hasLength(2));
8351 expect(directive.semicolon, isNotNull);
8352 }
8353
8354 void test_parseImportDirective_show() {
8355 ImportDirective directive = parse(
8356 "parseImportDirective",
8357 <Object>[emptyCommentAndMetadata()],
8358 "import 'lib/lib.dart' show A, B;");
8359 expect(directive.keyword, isNotNull);
8360 expect(directive.uri, isNotNull);
8361 expect(directive.deferredKeyword, isNull);
8362 expect(directive.asKeyword, isNull);
8363 expect(directive.prefix, isNull);
8364 expect(directive.combinators, hasLength(1));
8365 expect(directive.semicolon, isNotNull);
8366 }
8367
8368 void test_parseInitializedIdentifierList_type() {
8369 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8370 Token staticKeyword = TokenFactory.tokenFromKeyword(Keyword.STATIC);
8371 TypeName type = new TypeName(new SimpleIdentifier(null), null);
8372 FieldDeclaration declaration = parse(
8373 "parseInitializedIdentifierList",
8374 <Object>[commentAndMetadata(comment), staticKeyword, null, type],
8375 "a = 1, b, c = 3;");
8376 expect(declaration.documentationComment, comment);
8377 VariableDeclarationList fields = declaration.fields;
8378 expect(fields, isNotNull);
8379 expect(fields.keyword, isNull);
8380 expect(fields.type, type);
8381 expect(fields.variables, hasLength(3));
8382 expect(declaration.staticKeyword, staticKeyword);
8383 expect(declaration.semicolon, isNotNull);
8384 }
8385
8386 void test_parseInitializedIdentifierList_var() {
8387 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
8388 Token staticKeyword = TokenFactory.tokenFromKeyword(Keyword.STATIC);
8389 Token varKeyword = TokenFactory.tokenFromKeyword(Keyword.VAR);
8390 FieldDeclaration declaration = parse(
8391 "parseInitializedIdentifierList",
8392 <Object>[commentAndMetadata(comment), staticKeyword, varKeyword, null],
8393 "a = 1, b, c = 3;");
8394 expect(declaration.documentationComment, comment);
8395 VariableDeclarationList fields = declaration.fields;
8396 expect(fields, isNotNull);
8397 expect(fields.keyword, varKeyword);
8398 expect(fields.type, isNull);
8399 expect(fields.variables, hasLength(3));
8400 expect(declaration.staticKeyword, staticKeyword);
8401 expect(declaration.semicolon, isNotNull);
8402 }
8403
8404 void test_parseInstanceCreationExpression_qualifiedType() {
8405 Token token = TokenFactory.tokenFromKeyword(Keyword.NEW);
8406 InstanceCreationExpression expression =
8407 parse("parseInstanceCreationExpression", <Object>[token], "A.B()");
8408 expect(expression.keyword, token);
8409 ConstructorName name = expression.constructorName;
8410 expect(name, isNotNull);
8411 expect(name.type, isNotNull);
8412 expect(name.period, isNull);
8413 expect(name.name, isNull);
8414 expect(expression.argumentList, isNotNull);
8415 }
8416
8417 void test_parseInstanceCreationExpression_qualifiedType_named() {
8418 Token token = TokenFactory.tokenFromKeyword(Keyword.NEW);
8419 InstanceCreationExpression expression =
8420 parse("parseInstanceCreationExpression", <Object>[token], "A.B.c()");
8421 expect(expression.keyword, token);
8422 ConstructorName name = expression.constructorName;
8423 expect(name, isNotNull);
8424 expect(name.type, isNotNull);
8425 expect(name.period, isNotNull);
8426 expect(name.name, isNotNull);
8427 expect(expression.argumentList, isNotNull);
8428 }
8429
8430 void test_parseInstanceCreationExpression_type() {
8431 Token token = TokenFactory.tokenFromKeyword(Keyword.NEW);
8432 InstanceCreationExpression expression =
8433 parse("parseInstanceCreationExpression", <Object>[token], "A()");
8434 expect(expression.keyword, token);
8435 ConstructorName name = expression.constructorName;
8436 expect(name, isNotNull);
8437 expect(name.type, isNotNull);
8438 expect(name.period, isNull);
8439 expect(name.name, isNull);
8440 expect(expression.argumentList, isNotNull);
8441 }
8442
8443 void test_parseInstanceCreationExpression_type_named() {
8444 Token token = TokenFactory.tokenFromKeyword(Keyword.NEW);
8445 InstanceCreationExpression expression =
8446 parse("parseInstanceCreationExpression", <Object>[token], "A<B>.c()");
8447 expect(expression.keyword, token);
8448 ConstructorName name = expression.constructorName;
8449 expect(name, isNotNull);
8450 expect(name.type, isNotNull);
8451 expect(name.period, isNotNull);
8452 expect(name.name, isNotNull);
8453 expect(expression.argumentList, isNotNull);
8454 }
8455
8456 void test_parseLibraryDirective() {
8457 LibraryDirective directive = parse("parseLibraryDirective",
8458 <Object>[emptyCommentAndMetadata()], "library l;");
8459 expect(directive.libraryKeyword, isNotNull);
8460 expect(directive.name, isNotNull);
8461 expect(directive.semicolon, isNotNull);
8462 }
8463
8464 void test_parseLibraryIdentifier_multiple() {
8465 String name = "a.b.c";
8466 LibraryIdentifier identifier = parse4("parseLibraryIdentifier", name);
8467 expect(identifier.name, name);
8468 }
8469
8470 void test_parseLibraryIdentifier_single() {
8471 String name = "a";
8472 LibraryIdentifier identifier = parse4("parseLibraryIdentifier", name);
8473 expect(identifier.name, name);
8474 }
8475
8476 void test_parseListLiteral_empty_oneToken() {
8477 Token token = TokenFactory.tokenFromKeyword(Keyword.CONST);
8478 TypeArgumentList typeArguments = null;
8479 ListLiteral literal =
8480 parse("parseListLiteral", <Object>[token, typeArguments], "[]");
8481 expect(literal.constKeyword, token);
8482 expect(literal.typeArguments, typeArguments);
8483 expect(literal.leftBracket, isNotNull);
8484 expect(literal.elements, hasLength(0));
8485 expect(literal.rightBracket, isNotNull);
8486 }
8487
8488 void test_parseListLiteral_empty_oneToken_withComment() {
8489 Token constToken = null;
8490 TypeArgumentList typeArguments = null;
8491 ListLiteral literal = parse(
8492 "parseListLiteral", <Object>[constToken, typeArguments], "/* 0 */ []");
8493 expect(literal.constKeyword, constToken);
8494 expect(literal.typeArguments, typeArguments);
8495 Token leftBracket = literal.leftBracket;
8496 expect(leftBracket, isNotNull);
8497 expect(leftBracket.precedingComments, isNotNull);
8498 expect(literal.elements, hasLength(0));
8499 expect(literal.rightBracket, isNotNull);
8500 }
8501
8502 void test_parseListLiteral_empty_twoTokens() {
8503 Token token = TokenFactory.tokenFromKeyword(Keyword.CONST);
8504 TypeArgumentList typeArguments = null;
8505 ListLiteral literal =
8506 parse("parseListLiteral", <Object>[token, typeArguments], "[ ]");
8507 expect(literal.constKeyword, token);
8508 expect(literal.typeArguments, typeArguments);
8509 expect(literal.leftBracket, isNotNull);
8510 expect(literal.elements, hasLength(0));
8511 expect(literal.rightBracket, isNotNull);
8512 }
8513
8514 void test_parseListLiteral_multiple() {
8515 ListLiteral literal =
8516 parse("parseListLiteral", <Object>[null, null], "[1, 2, 3]");
8517 expect(literal.constKeyword, isNull);
8518 expect(literal.typeArguments, isNull);
8519 expect(literal.leftBracket, isNotNull);
8520 expect(literal.elements, hasLength(3));
8521 expect(literal.rightBracket, isNotNull);
8522 }
8523
8524 void test_parseListLiteral_single() {
8525 ListLiteral literal =
8526 parse("parseListLiteral", <Object>[null, null], "[1]");
8527 expect(literal.constKeyword, isNull);
8528 expect(literal.typeArguments, isNull);
8529 expect(literal.leftBracket, isNotNull);
8530 expect(literal.elements, hasLength(1));
8531 expect(literal.rightBracket, isNotNull);
8532 }
8533
8534 void test_parseListOrMapLiteral_list_noType() {
8535 ListLiteral literal = parse("parseListOrMapLiteral", <Object>[null], "[1]");
8536 expect(literal.constKeyword, isNull);
8537 expect(literal.typeArguments, isNull);
8538 expect(literal.leftBracket, isNotNull);
8539 expect(literal.elements, hasLength(1));
8540 expect(literal.rightBracket, isNotNull);
8541 }
8542
8543 void test_parseListOrMapLiteral_list_type() {
8544 ListLiteral literal =
8545 parse("parseListOrMapLiteral", <Object>[null], "<int> [1]");
8546 expect(literal.constKeyword, isNull);
8547 expect(literal.typeArguments, isNotNull);
8548 expect(literal.leftBracket, isNotNull);
8549 expect(literal.elements, hasLength(1));
8550 expect(literal.rightBracket, isNotNull);
8551 }
8552
8553 void test_parseListOrMapLiteral_map_noType() {
8554 MapLiteral literal =
8555 parse("parseListOrMapLiteral", <Object>[null], "{'1' : 1}");
8556 expect(literal.constKeyword, isNull);
8557 expect(literal.typeArguments, isNull);
8558 expect(literal.leftBracket, isNotNull);
8559 expect(literal.entries, hasLength(1));
8560 expect(literal.rightBracket, isNotNull);
8561 }
8562
8563 void test_parseListOrMapLiteral_map_type() {
8564 MapLiteral literal = parse(
8565 "parseListOrMapLiteral", <Object>[null], "<String, int> {'1' : 1}");
8566 expect(literal.constKeyword, isNull);
8567 expect(literal.typeArguments, isNotNull);
8568 expect(literal.leftBracket, isNotNull);
8569 expect(literal.entries, hasLength(1));
8570 expect(literal.rightBracket, isNotNull);
8571 }
8572
8573 void test_parseLogicalAndExpression() {
8574 BinaryExpression expression = parse4("parseLogicalAndExpression", "x && y");
8575 expect(expression.leftOperand, isNotNull);
8576 expect(expression.operator, isNotNull);
8577 expect(expression.operator.type, TokenType.AMPERSAND_AMPERSAND);
8578 expect(expression.rightOperand, isNotNull);
8579 }
8580
8581 void test_parseLogicalOrExpression() {
8582 BinaryExpression expression = parse4("parseLogicalOrExpression", "x || y");
8583 expect(expression.leftOperand, isNotNull);
8584 expect(expression.operator, isNotNull);
8585 expect(expression.operator.type, TokenType.BAR_BAR);
8586 expect(expression.rightOperand, isNotNull);
8587 }
8588
8589 void test_parseMapLiteral_empty() {
8590 Token token = TokenFactory.tokenFromKeyword(Keyword.CONST);
8591 TypeArgumentList typeArguments = AstFactory.typeArgumentList(
8592 [AstFactory.typeName4("String"), AstFactory.typeName4("int")]);
8593 MapLiteral literal =
8594 parse("parseMapLiteral", <Object>[token, typeArguments], "{}");
8595 expect(literal.constKeyword, token);
8596 expect(literal.typeArguments, typeArguments);
8597 expect(literal.leftBracket, isNotNull);
8598 expect(literal.entries, hasLength(0));
8599 expect(literal.rightBracket, isNotNull);
8600 }
8601
8602 void test_parseMapLiteral_multiple() {
8603 MapLiteral literal =
8604 parse("parseMapLiteral", <Object>[null, null], "{'a' : b, 'x' : y}");
8605 expect(literal.leftBracket, isNotNull);
8606 expect(literal.entries, hasLength(2));
8607 expect(literal.rightBracket, isNotNull);
8608 }
8609
8610 void test_parseMapLiteral_single() {
8611 MapLiteral literal =
8612 parse("parseMapLiteral", <Object>[null, null], "{'x' : y}");
8613 expect(literal.leftBracket, isNotNull);
8614 expect(literal.entries, hasLength(1));
8615 expect(literal.rightBracket, isNotNull);
8616 }
8617
8618 void test_parseMapLiteralEntry_complex() {
8619 MapLiteralEntry entry = parse4("parseMapLiteralEntry", "2 + 2 : y");
8620 expect(entry.key, isNotNull);
8621 expect(entry.separator, isNotNull);
8622 expect(entry.value, isNotNull);
8623 }
8624
8625 void test_parseMapLiteralEntry_int() {
8626 MapLiteralEntry entry = parse4("parseMapLiteralEntry", "0 : y");
8627 expect(entry.key, isNotNull);
8628 expect(entry.separator, isNotNull);
8629 expect(entry.value, isNotNull);
8630 }
8631
8632 void test_parseMapLiteralEntry_string() {
8633 MapLiteralEntry entry = parse4("parseMapLiteralEntry", "'x' : y");
8634 expect(entry.key, isNotNull);
8635 expect(entry.separator, isNotNull);
8636 expect(entry.value, isNotNull);
8637 }
8638
8639 void test_parseModifiers_abstract() {
8640 Modifiers modifiers = parse4("parseModifiers", "abstract A");
8641 expect(modifiers.abstractKeyword, isNotNull);
8642 }
8643
8644 void test_parseModifiers_const() {
8645 Modifiers modifiers = parse4("parseModifiers", "const A");
8646 expect(modifiers.constKeyword, isNotNull);
8647 }
8648
8649 void test_parseModifiers_external() {
8650 Modifiers modifiers = parse4("parseModifiers", "external A");
8651 expect(modifiers.externalKeyword, isNotNull);
8652 }
8653
8654 void test_parseModifiers_factory() {
8655 Modifiers modifiers = parse4("parseModifiers", "factory A");
8656 expect(modifiers.factoryKeyword, isNotNull);
8657 }
8658
8659 void test_parseModifiers_final() {
8660 Modifiers modifiers = parse4("parseModifiers", "final A");
8661 expect(modifiers.finalKeyword, isNotNull);
8662 }
8663
8664 void test_parseModifiers_static() {
8665 Modifiers modifiers = parse4("parseModifiers", "static A");
8666 expect(modifiers.staticKeyword, isNotNull);
8667 }
8668
8669 void test_parseModifiers_var() {
8670 Modifiers modifiers = parse4("parseModifiers", "var A");
8671 expect(modifiers.varKeyword, isNotNull);
8672 }
8673
8674 void test_parseMultiplicativeExpression_normal() {
8675 BinaryExpression expression =
8676 parse4("parseMultiplicativeExpression", "x * y");
8677 expect(expression.leftOperand, isNotNull);
8678 expect(expression.operator, isNotNull);
8679 expect(expression.operator.type, TokenType.STAR);
8680 expect(expression.rightOperand, isNotNull);
8681 }
8682
8683 void test_parseMultiplicativeExpression_super() {
8684 BinaryExpression expression =
8685 parse4("parseMultiplicativeExpression", "super * y");
8686 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
8687 SuperExpression, expression.leftOperand);
8688 expect(expression.operator, isNotNull);
8689 expect(expression.operator.type, TokenType.STAR);
8690 expect(expression.rightOperand, isNotNull);
8691 }
8692
8693 void test_parseNewExpression() {
8694 InstanceCreationExpression expression =
8695 parse4("parseNewExpression", "new A()");
8696 expect(expression.keyword, isNotNull);
8697 ConstructorName name = expression.constructorName;
8698 expect(name, isNotNull);
8699 expect(name.type, isNotNull);
8700 expect(name.period, isNull);
8701 expect(name.name, isNull);
8702 expect(expression.argumentList, isNotNull);
8703 }
8704
8705 void test_parseNonLabeledStatement_const_list_empty() {
8706 ExpressionStatement statement =
8707 parse4("parseNonLabeledStatement", "const [];");
8708 expect(statement.expression, isNotNull);
8709 }
8710
8711 void test_parseNonLabeledStatement_const_list_nonEmpty() {
8712 ExpressionStatement statement =
8713 parse4("parseNonLabeledStatement", "const [1, 2];");
8714 expect(statement.expression, isNotNull);
8715 }
8716
8717 void test_parseNonLabeledStatement_const_map_empty() {
8718 ExpressionStatement statement =
8719 parse4("parseNonLabeledStatement", "const {};");
8720 expect(statement.expression, isNotNull);
8721 }
8722
8723 void test_parseNonLabeledStatement_const_map_nonEmpty() {
8724 // TODO(brianwilkerson) Implement more tests for this method.
8725 ExpressionStatement statement =
8726 parse4("parseNonLabeledStatement", "const {'a' : 1};");
8727 expect(statement.expression, isNotNull);
8728 }
8729
8730 void test_parseNonLabeledStatement_const_object() {
8731 ExpressionStatement statement =
8732 parse4("parseNonLabeledStatement", "const A();");
8733 expect(statement.expression, isNotNull);
8734 }
8735
8736 void test_parseNonLabeledStatement_const_object_named_typeParameters() {
8737 ExpressionStatement statement =
8738 parse4("parseNonLabeledStatement", "const A<B>.c();");
8739 expect(statement.expression, isNotNull);
8740 }
8741
8742 void test_parseNonLabeledStatement_constructorInvocation() {
8743 ExpressionStatement statement =
8744 parse4("parseNonLabeledStatement", "new C().m();");
8745 expect(statement.expression, isNotNull);
8746 }
8747
8748 void test_parseNonLabeledStatement_false() {
8749 ExpressionStatement statement =
8750 parse4("parseNonLabeledStatement", "false;");
8751 expect(statement.expression, isNotNull);
8752 }
8753
8754 void test_parseNonLabeledStatement_functionDeclaration() {
8755 parse4("parseNonLabeledStatement", "f() {};");
8756 }
8757
8758 void test_parseNonLabeledStatement_functionDeclaration_arguments() {
8759 parse4("parseNonLabeledStatement", "f(void g()) {};");
8760 }
8761
8762 void test_parseNonLabeledStatement_functionExpressionIndex() {
8763 parse4("parseNonLabeledStatement", "() {}[0] = null;");
8764 }
8765
8766 void test_parseNonLabeledStatement_functionInvocation() {
8767 ExpressionStatement statement = parse4("parseNonLabeledStatement", "f();");
8768 expect(statement.expression, isNotNull);
8769 }
8770
8771 void test_parseNonLabeledStatement_invokeFunctionExpression() {
8772 ExpressionStatement statement =
8773 parse4("parseNonLabeledStatement", "(a) {return a + a;} (3);");
8774 EngineTestCase.assertInstanceOf(
8775 (obj) => obj is FunctionExpressionInvocation,
8776 FunctionExpressionInvocation,
8777 statement.expression);
8778 FunctionExpressionInvocation invocation =
8779 statement.expression as FunctionExpressionInvocation;
8780 EngineTestCase.assertInstanceOf((obj) => obj is FunctionExpression,
8781 FunctionExpression, invocation.function);
8782 FunctionExpression expression = invocation.function as FunctionExpression;
8783 expect(expression.parameters, isNotNull);
8784 expect(expression.body, isNotNull);
8785 expect(invocation.typeArguments, isNull);
8786 ArgumentList list = invocation.argumentList;
8787 expect(list, isNotNull);
8788 expect(list.arguments, hasLength(1));
8789 }
8790
8791 void test_parseNonLabeledStatement_null() {
8792 ExpressionStatement statement = parse4("parseNonLabeledStatement", "null;");
8793 expect(statement.expression, isNotNull);
8794 }
8795
8796 void test_parseNonLabeledStatement_startingWithBuiltInIdentifier() {
8797 ExpressionStatement statement =
8798 parse4("parseNonLabeledStatement", "library.getName();");
8799 expect(statement.expression, isNotNull);
8800 }
8801
8802 void test_parseNonLabeledStatement_true() {
8803 ExpressionStatement statement = parse4("parseNonLabeledStatement", "true;");
8804 expect(statement.expression, isNotNull);
8805 }
8806
8807 void test_parseNonLabeledStatement_typeCast() {
8808 ExpressionStatement statement =
8809 parse4("parseNonLabeledStatement", "double.NAN as num;");
8810 expect(statement.expression, isNotNull);
8811 }
8812
8813 void test_parseNormalFormalParameter_field_const_noType() {
8814 FieldFormalParameter parameter =
8815 parse4("parseNormalFormalParameter", "const this.a)");
8816 expect(parameter.keyword, isNotNull);
8817 expect(parameter.type, isNull);
8818 expect(parameter.identifier, isNotNull);
8819 expect(parameter.parameters, isNull);
8820 }
8821
8822 void test_parseNormalFormalParameter_field_const_type() {
8823 FieldFormalParameter parameter =
8824 parse4("parseNormalFormalParameter", "const A this.a)");
8825 expect(parameter.keyword, isNotNull);
8826 expect(parameter.type, isNotNull);
8827 expect(parameter.identifier, isNotNull);
8828 expect(parameter.parameters, isNull);
8829 }
8830
8831 void test_parseNormalFormalParameter_field_final_noType() {
8832 FieldFormalParameter parameter =
8833 parse4("parseNormalFormalParameter", "final this.a)");
8834 expect(parameter.keyword, isNotNull);
8835 expect(parameter.type, isNull);
8836 expect(parameter.identifier, isNotNull);
8837 expect(parameter.parameters, isNull);
8838 }
8839
8840 void test_parseNormalFormalParameter_field_final_type() {
8841 FieldFormalParameter parameter =
8842 parse4("parseNormalFormalParameter", "final A this.a)");
8843 expect(parameter.keyword, isNotNull);
8844 expect(parameter.type, isNotNull);
8845 expect(parameter.identifier, isNotNull);
8846 expect(parameter.parameters, isNull);
8847 }
8848
8849 void test_parseNormalFormalParameter_field_function_nested() {
8850 FieldFormalParameter parameter =
8851 parse4("parseNormalFormalParameter", "this.a(B b))");
8852 expect(parameter.keyword, isNull);
8853 expect(parameter.type, isNull);
8854 expect(parameter.identifier, isNotNull);
8855 FormalParameterList parameterList = parameter.parameters;
8856 expect(parameterList, isNotNull);
8857 expect(parameterList.parameters, hasLength(1));
8858 }
8859
8860 void test_parseNormalFormalParameter_field_function_noNested() {
8861 FieldFormalParameter parameter =
8862 parse4("parseNormalFormalParameter", "this.a())");
8863 expect(parameter.keyword, isNull);
8864 expect(parameter.type, isNull);
8865 expect(parameter.identifier, isNotNull);
8866 FormalParameterList parameterList = parameter.parameters;
8867 expect(parameterList, isNotNull);
8868 expect(parameterList.parameters, hasLength(0));
8869 }
8870
8871 void test_parseNormalFormalParameter_field_noType() {
8872 FieldFormalParameter parameter =
8873 parse4("parseNormalFormalParameter", "this.a)");
8874 expect(parameter.keyword, isNull);
8875 expect(parameter.type, isNull);
8876 expect(parameter.identifier, isNotNull);
8877 expect(parameter.parameters, isNull);
8878 }
8879
8880 void test_parseNormalFormalParameter_field_type() {
8881 FieldFormalParameter parameter =
8882 parse4("parseNormalFormalParameter", "A this.a)");
8883 expect(parameter.keyword, isNull);
8884 expect(parameter.type, isNotNull);
8885 expect(parameter.identifier, isNotNull);
8886 expect(parameter.parameters, isNull);
8887 }
8888
8889 void test_parseNormalFormalParameter_field_var() {
8890 FieldFormalParameter parameter =
8891 parse4("parseNormalFormalParameter", "var this.a)");
8892 expect(parameter.keyword, isNotNull);
8893 expect(parameter.type, isNull);
8894 expect(parameter.identifier, isNotNull);
8895 expect(parameter.parameters, isNull);
8896 }
8897
8898 void test_parseNormalFormalParameter_function_noType() {
8899 FunctionTypedFormalParameter parameter =
8900 parse4("parseNormalFormalParameter", "a())");
8901 expect(parameter.returnType, isNull);
8902 expect(parameter.identifier, isNotNull);
8903 expect(parameter.typeParameters, isNull);
8904 expect(parameter.parameters, isNotNull);
8905 }
8906
8907 void test_parseNormalFormalParameter_function_noType_typeParameters() {
8908 enableGenericMethods = true;
8909 FunctionTypedFormalParameter parameter =
8910 parse4("parseNormalFormalParameter", "a<E>())");
8911 expect(parameter.returnType, isNull);
8912 expect(parameter.identifier, isNotNull);
8913 expect(parameter.typeParameters, isNotNull);
8914 expect(parameter.parameters, isNotNull);
8915 }
8916
8917 void test_parseNormalFormalParameter_function_type() {
8918 FunctionTypedFormalParameter parameter =
8919 parse4("parseNormalFormalParameter", "A a())");
8920 expect(parameter.returnType, isNotNull);
8921 expect(parameter.identifier, isNotNull);
8922 expect(parameter.typeParameters, isNull);
8923 expect(parameter.parameters, isNotNull);
8924 }
8925
8926 void test_parseNormalFormalParameter_function_type_typeParameters() {
8927 enableGenericMethods = true;
8928 FunctionTypedFormalParameter parameter =
8929 parse4("parseNormalFormalParameter", "A a<E>())");
8930 expect(parameter.returnType, isNotNull);
8931 expect(parameter.identifier, isNotNull);
8932 expect(parameter.typeParameters, isNotNull);
8933 expect(parameter.parameters, isNotNull);
8934 }
8935
8936 void test_parseNormalFormalParameter_function_void() {
8937 FunctionTypedFormalParameter parameter =
8938 parse4("parseNormalFormalParameter", "void a())");
8939 expect(parameter.returnType, isNotNull);
8940 expect(parameter.identifier, isNotNull);
8941 expect(parameter.typeParameters, isNull);
8942 expect(parameter.parameters, isNotNull);
8943 }
8944
8945 void test_parseNormalFormalParameter_function_void_typeParameters() {
8946 enableGenericMethods = true;
8947 FunctionTypedFormalParameter parameter =
8948 parse4("parseNormalFormalParameter", "void a<E>())");
8949 expect(parameter.returnType, isNotNull);
8950 expect(parameter.identifier, isNotNull);
8951 expect(parameter.typeParameters, isNotNull);
8952 expect(parameter.parameters, isNotNull);
8953 }
8954
8955 void test_parseNormalFormalParameter_simple_const_noType() {
8956 SimpleFormalParameter parameter =
8957 parse4("parseNormalFormalParameter", "const a)");
8958 expect(parameter.keyword, isNotNull);
8959 expect(parameter.type, isNull);
8960 expect(parameter.identifier, isNotNull);
8961 }
8962
8963 void test_parseNormalFormalParameter_simple_const_type() {
8964 SimpleFormalParameter parameter =
8965 parse4("parseNormalFormalParameter", "const A a)");
8966 expect(parameter.keyword, isNotNull);
8967 expect(parameter.type, isNotNull);
8968 expect(parameter.identifier, isNotNull);
8969 }
8970
8971 void test_parseNormalFormalParameter_simple_final_noType() {
8972 SimpleFormalParameter parameter =
8973 parse4("parseNormalFormalParameter", "final a)");
8974 expect(parameter.keyword, isNotNull);
8975 expect(parameter.type, isNull);
8976 expect(parameter.identifier, isNotNull);
8977 }
8978
8979 void test_parseNormalFormalParameter_simple_final_type() {
8980 SimpleFormalParameter parameter =
8981 parse4("parseNormalFormalParameter", "final A a)");
8982 expect(parameter.keyword, isNotNull);
8983 expect(parameter.type, isNotNull);
8984 expect(parameter.identifier, isNotNull);
8985 }
8986
8987 void test_parseNormalFormalParameter_simple_noType() {
8988 SimpleFormalParameter parameter =
8989 parse4("parseNormalFormalParameter", "a)");
8990 expect(parameter.keyword, isNull);
8991 expect(parameter.type, isNull);
8992 expect(parameter.identifier, isNotNull);
8993 }
8994
8995 void test_parseNormalFormalParameter_simple_type() {
8996 SimpleFormalParameter parameter =
8997 parse4("parseNormalFormalParameter", "A a)");
8998 expect(parameter.keyword, isNull);
8999 expect(parameter.type, isNotNull);
9000 expect(parameter.identifier, isNotNull);
9001 }
9002
9003 void test_parseOperator() {
9004 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
9005 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
9006 MethodDeclaration method = parse(
9007 "parseOperator",
9008 <Object>[commentAndMetadata(comment), null, returnType],
9009 "operator +(A a);");
9010 expect(method.body, isNotNull);
9011 expect(method.documentationComment, comment);
9012 expect(method.externalKeyword, isNull);
9013 expect(method.modifierKeyword, isNull);
9014 expect(method.name, isNotNull);
9015 expect(method.operatorKeyword, isNotNull);
9016 expect(method.typeParameters, isNull);
9017 expect(method.parameters, isNotNull);
9018 expect(method.propertyKeyword, isNull);
9019 expect(method.returnType, returnType);
9020 }
9021
9022 void test_parseOptionalReturnType() {
9023 // TODO(brianwilkerson) Implement tests for this method.
9024 }
9025
9026 void test_parsePartDirective_part() {
9027 PartDirective directive = parse("parsePartDirective",
9028 <Object>[emptyCommentAndMetadata()], "part 'lib/lib.dart';");
9029 expect(directive.partKeyword, isNotNull);
9030 expect(directive.uri, isNotNull);
9031 expect(directive.semicolon, isNotNull);
9032 }
9033
9034 void test_parsePartDirective_partOf() {
9035 PartOfDirective directive = parse("parsePartDirective",
9036 <Object>[emptyCommentAndMetadata()], "part of l;");
9037 expect(directive.partKeyword, isNotNull);
9038 expect(directive.ofKeyword, isNotNull);
9039 expect(directive.libraryName, isNotNull);
9040 expect(directive.semicolon, isNotNull);
9041 }
9042
9043 void test_parsePostfixExpression_decrement() {
9044 PostfixExpression expression = parse4("parsePostfixExpression", "i--");
9045 expect(expression.operand, isNotNull);
9046 expect(expression.operator, isNotNull);
9047 expect(expression.operator.type, TokenType.MINUS_MINUS);
9048 }
9049
9050 void test_parsePostfixExpression_increment() {
9051 PostfixExpression expression = parse4("parsePostfixExpression", "i++");
9052 expect(expression.operand, isNotNull);
9053 expect(expression.operator, isNotNull);
9054 expect(expression.operator.type, TokenType.PLUS_PLUS);
9055 }
9056
9057 void test_parsePostfixExpression_none_indexExpression() {
9058 IndexExpression expression = parse4("parsePostfixExpression", "a[0]");
9059 expect(expression.target, isNotNull);
9060 expect(expression.index, isNotNull);
9061 }
9062
9063 void test_parsePostfixExpression_none_methodInvocation() {
9064 MethodInvocation expression = parse4("parsePostfixExpression", "a.m()");
9065 expect(expression.target, isNotNull);
9066 expect(expression.operator.type, TokenType.PERIOD);
9067 expect(expression.methodName, isNotNull);
9068 expect(expression.typeArguments, isNull);
9069 expect(expression.argumentList, isNotNull);
9070 }
9071
9072 void test_parsePostfixExpression_none_methodInvocation_question_dot() {
9073 MethodInvocation expression = parse4('parsePostfixExpression', 'a?.m()');
9074 expect(expression.target, isNotNull);
9075 expect(expression.operator.type, TokenType.QUESTION_PERIOD);
9076 expect(expression.methodName, isNotNull);
9077 expect(expression.typeArguments, isNull);
9078 expect(expression.argumentList, isNotNull);
9079 }
9080
9081 void test_parsePostfixExpression_none_methodInvocation_question_dot_typeArgume nts() {
9082 enableGenericMethods = true;
9083 MethodInvocation expression = parse4('parsePostfixExpression', 'a?.m<E>()');
9084 expect(expression.target, isNotNull);
9085 expect(expression.operator.type, TokenType.QUESTION_PERIOD);
9086 expect(expression.methodName, isNotNull);
9087 expect(expression.typeArguments, isNotNull);
9088 expect(expression.argumentList, isNotNull);
9089 }
9090
9091 void test_parsePostfixExpression_none_methodInvocation_typeArguments() {
9092 enableGenericMethods = true;
9093 MethodInvocation expression = parse4("parsePostfixExpression", "a.m<E>()");
9094 expect(expression.target, isNotNull);
9095 expect(expression.operator.type, TokenType.PERIOD);
9096 expect(expression.methodName, isNotNull);
9097 expect(expression.typeArguments, isNotNull);
9098 expect(expression.argumentList, isNotNull);
9099 }
9100
9101 void test_parsePostfixExpression_none_propertyAccess() {
9102 PrefixedIdentifier expression = parse4("parsePostfixExpression", "a.b");
9103 expect(expression.prefix, isNotNull);
9104 expect(expression.identifier, isNotNull);
9105 }
9106
9107 void test_parsePrefixedIdentifier_noPrefix() {
9108 String lexeme = "bar";
9109 SimpleIdentifier identifier = parse4("parsePrefixedIdentifier", lexeme);
9110 expect(identifier.token, isNotNull);
9111 expect(identifier.name, lexeme);
9112 }
9113
9114 void test_parsePrefixedIdentifier_prefix() {
9115 String lexeme = "foo.bar";
9116 PrefixedIdentifier identifier = parse4("parsePrefixedIdentifier", lexeme);
9117 expect(identifier.prefix.name, "foo");
9118 expect(identifier.period, isNotNull);
9119 expect(identifier.identifier.name, "bar");
9120 }
9121
9122 void test_parsePrimaryExpression_const() {
9123 InstanceCreationExpression expression =
9124 parse4("parsePrimaryExpression", "const A()");
9125 expect(expression, isNotNull);
9126 }
9127
9128 void test_parsePrimaryExpression_double() {
9129 String doubleLiteral = "3.2e4";
9130 DoubleLiteral literal = parse4("parsePrimaryExpression", doubleLiteral);
9131 expect(literal.literal, isNotNull);
9132 expect(literal.value, double.parse(doubleLiteral));
9133 }
9134
9135 void test_parsePrimaryExpression_false() {
9136 BooleanLiteral literal = parse4("parsePrimaryExpression", "false");
9137 expect(literal.literal, isNotNull);
9138 expect(literal.value, isFalse);
9139 }
9140
9141 void test_parsePrimaryExpression_function_arguments() {
9142 FunctionExpression expression =
9143 parse4("parsePrimaryExpression", "(int i) => i + 1");
9144 expect(expression.parameters, isNotNull);
9145 expect(expression.body, isNotNull);
9146 }
9147
9148 void test_parsePrimaryExpression_function_noArguments() {
9149 FunctionExpression expression =
9150 parse4("parsePrimaryExpression", "() => 42");
9151 expect(expression.parameters, isNotNull);
9152 expect(expression.body, isNotNull);
9153 }
9154
9155 void test_parsePrimaryExpression_hex() {
9156 String hexLiteral = "3F";
9157 IntegerLiteral literal = parse4("parsePrimaryExpression", "0x$hexLiteral");
9158 expect(literal.literal, isNotNull);
9159 expect(literal.value, int.parse(hexLiteral, radix: 16));
9160 }
9161
9162 void test_parsePrimaryExpression_identifier() {
9163 SimpleIdentifier identifier = parse4("parsePrimaryExpression", "a");
9164 expect(identifier, isNotNull);
9165 }
9166
9167 void test_parsePrimaryExpression_int() {
9168 String intLiteral = "472";
9169 IntegerLiteral literal = parse4("parsePrimaryExpression", intLiteral);
9170 expect(literal.literal, isNotNull);
9171 expect(literal.value, int.parse(intLiteral));
9172 }
9173
9174 void test_parsePrimaryExpression_listLiteral() {
9175 ListLiteral literal = parse4("parsePrimaryExpression", "[ ]");
9176 expect(literal, isNotNull);
9177 }
9178
9179 void test_parsePrimaryExpression_listLiteral_index() {
9180 ListLiteral literal = parse4("parsePrimaryExpression", "[]");
9181 expect(literal, isNotNull);
9182 }
9183
9184 void test_parsePrimaryExpression_listLiteral_typed() {
9185 ListLiteral literal = parse4("parsePrimaryExpression", "<A>[ ]");
9186 expect(literal.typeArguments, isNotNull);
9187 expect(literal.typeArguments.arguments, hasLength(1));
9188 }
9189
9190 void test_parsePrimaryExpression_mapLiteral() {
9191 MapLiteral literal = parse4("parsePrimaryExpression", "{}");
9192 expect(literal, isNotNull);
9193 }
9194
9195 void test_parsePrimaryExpression_mapLiteral_typed() {
9196 MapLiteral literal = parse4("parsePrimaryExpression", "<A, B>{}");
9197 expect(literal.typeArguments, isNotNull);
9198 expect(literal.typeArguments.arguments, hasLength(2));
9199 }
9200
9201 void test_parsePrimaryExpression_new() {
9202 InstanceCreationExpression expression =
9203 parse4("parsePrimaryExpression", "new A()");
9204 expect(expression, isNotNull);
9205 }
9206
9207 void test_parsePrimaryExpression_null() {
9208 NullLiteral literal = parse4("parsePrimaryExpression", "null");
9209 expect(literal.literal, isNotNull);
9210 }
9211
9212 void test_parsePrimaryExpression_parenthesized() {
9213 ParenthesizedExpression expression =
9214 parse4("parsePrimaryExpression", "(x)");
9215 expect(expression, isNotNull);
9216 }
9217
9218 void test_parsePrimaryExpression_string() {
9219 SimpleStringLiteral literal =
9220 parse4("parsePrimaryExpression", "\"string\"");
9221 expect(literal.isMultiline, isFalse);
9222 expect(literal.isRaw, isFalse);
9223 expect(literal.value, "string");
9224 }
9225
9226 void test_parsePrimaryExpression_string_multiline() {
9227 SimpleStringLiteral literal =
9228 parse4("parsePrimaryExpression", "'''string'''");
9229 expect(literal.isMultiline, isTrue);
9230 expect(literal.isRaw, isFalse);
9231 expect(literal.value, "string");
9232 }
9233
9234 void test_parsePrimaryExpression_string_raw() {
9235 SimpleStringLiteral literal = parse4("parsePrimaryExpression", "r'string'");
9236 expect(literal.isMultiline, isFalse);
9237 expect(literal.isRaw, isTrue);
9238 expect(literal.value, "string");
9239 }
9240
9241 void test_parsePrimaryExpression_super() {
9242 PropertyAccess propertyAccess = parse4("parsePrimaryExpression", "super.x");
9243 expect(propertyAccess.target is SuperExpression, isTrue);
9244 expect(propertyAccess.operator, isNotNull);
9245 expect(propertyAccess.operator.type, TokenType.PERIOD);
9246 expect(propertyAccess.propertyName, isNotNull);
9247 }
9248
9249 void test_parsePrimaryExpression_this() {
9250 ThisExpression expression = parse4("parsePrimaryExpression", "this");
9251 expect(expression.thisKeyword, isNotNull);
9252 }
9253
9254 void test_parsePrimaryExpression_true() {
9255 BooleanLiteral literal = parse4("parsePrimaryExpression", "true");
9256 expect(literal.literal, isNotNull);
9257 expect(literal.value, isTrue);
9258 }
9259
9260 void test_Parser() {
9261 expect(new Parser(null, null), isNotNull);
9262 }
9263
9264 void test_parseRedirectingConstructorInvocation_named() {
9265 RedirectingConstructorInvocation invocation =
9266 parse4("parseRedirectingConstructorInvocation", "this.a()");
9267 expect(invocation.argumentList, isNotNull);
9268 expect(invocation.constructorName, isNotNull);
9269 expect(invocation.thisKeyword, isNotNull);
9270 expect(invocation.period, isNotNull);
9271 }
9272
9273 void test_parseRedirectingConstructorInvocation_unnamed() {
9274 RedirectingConstructorInvocation invocation =
9275 parse4("parseRedirectingConstructorInvocation", "this()");
9276 expect(invocation.argumentList, isNotNull);
9277 expect(invocation.constructorName, isNull);
9278 expect(invocation.thisKeyword, isNotNull);
9279 expect(invocation.period, isNull);
9280 }
9281
9282 void test_parseRelationalExpression_as() {
9283 AsExpression expression = parse4("parseRelationalExpression", "x as Y");
9284 expect(expression.expression, isNotNull);
9285 expect(expression.asOperator, isNotNull);
9286 expect(expression.type, isNotNull);
9287 }
9288
9289 void test_parseRelationalExpression_is() {
9290 IsExpression expression = parse4("parseRelationalExpression", "x is y");
9291 expect(expression.expression, isNotNull);
9292 expect(expression.isOperator, isNotNull);
9293 expect(expression.notOperator, isNull);
9294 expect(expression.type, isNotNull);
9295 }
9296
9297 void test_parseRelationalExpression_isNot() {
9298 IsExpression expression = parse4("parseRelationalExpression", "x is! y");
9299 expect(expression.expression, isNotNull);
9300 expect(expression.isOperator, isNotNull);
9301 expect(expression.notOperator, isNotNull);
9302 expect(expression.type, isNotNull);
9303 }
9304
9305 void test_parseRelationalExpression_normal() {
9306 BinaryExpression expression = parse4("parseRelationalExpression", "x < y");
9307 expect(expression.leftOperand, isNotNull);
9308 expect(expression.operator, isNotNull);
9309 expect(expression.operator.type, TokenType.LT);
9310 expect(expression.rightOperand, isNotNull);
9311 }
9312
9313 void test_parseRelationalExpression_super() {
9314 BinaryExpression expression =
9315 parse4("parseRelationalExpression", "super < y");
9316 expect(expression.leftOperand, isNotNull);
9317 expect(expression.operator, isNotNull);
9318 expect(expression.operator.type, TokenType.LT);
9319 expect(expression.rightOperand, isNotNull);
9320 }
9321
9322 void test_parseRethrowExpression() {
9323 RethrowExpression expression = parse4("parseRethrowExpression", "rethrow;");
9324 expect(expression.rethrowKeyword, isNotNull);
9325 }
9326
9327 void test_parseReturnStatement_noValue() {
9328 ReturnStatement statement = parse4("parseReturnStatement", "return;");
9329 expect(statement.returnKeyword, isNotNull);
9330 expect(statement.expression, isNull);
9331 expect(statement.semicolon, isNotNull);
9332 }
9333
9334 void test_parseReturnStatement_value() {
9335 ReturnStatement statement = parse4("parseReturnStatement", "return x;");
9336 expect(statement.returnKeyword, isNotNull);
9337 expect(statement.expression, isNotNull);
9338 expect(statement.semicolon, isNotNull);
9339 }
9340
9341 void test_parseReturnType_nonVoid() {
9342 TypeName typeName = parse4("parseReturnType", "A<B>");
9343 expect(typeName.name, isNotNull);
9344 expect(typeName.typeArguments, isNotNull);
9345 }
9346
9347 void test_parseReturnType_void() {
9348 TypeName typeName = parse4("parseReturnType", "void");
9349 expect(typeName.name, isNotNull);
9350 expect(typeName.typeArguments, isNull);
9351 }
9352
9353 void test_parseSetter_nonStatic() {
9354 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
9355 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
9356 MethodDeclaration method = parse(
9357 "parseSetter",
9358 <Object>[commentAndMetadata(comment), null, null, returnType],
9359 "set a(var x);");
9360 expect(method.body, isNotNull);
9361 expect(method.documentationComment, comment);
9362 expect(method.externalKeyword, isNull);
9363 expect(method.modifierKeyword, isNull);
9364 expect(method.name, isNotNull);
9365 expect(method.operatorKeyword, isNull);
9366 expect(method.typeParameters, isNull);
9367 expect(method.parameters, isNotNull);
9368 expect(method.propertyKeyword, isNotNull);
9369 expect(method.returnType, returnType);
9370 }
9371
9372 void test_parseSetter_static() {
9373 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
9374 Token staticKeyword = TokenFactory.tokenFromKeyword(Keyword.STATIC);
9375 TypeName returnType = new TypeName(new SimpleIdentifier(null), null);
9376 MethodDeclaration method = parse(
9377 "parseSetter",
9378 <Object>[commentAndMetadata(comment), null, staticKeyword, returnType],
9379 "set a(var x) {}");
9380 expect(method.body, isNotNull);
9381 expect(method.documentationComment, comment);
9382 expect(method.externalKeyword, isNull);
9383 expect(method.modifierKeyword, staticKeyword);
9384 expect(method.name, isNotNull);
9385 expect(method.operatorKeyword, isNull);
9386 expect(method.typeParameters, isNull);
9387 expect(method.parameters, isNotNull);
9388 expect(method.propertyKeyword, isNotNull);
9389 expect(method.returnType, returnType);
9390 }
9391
9392 void test_parseShiftExpression_normal() {
9393 BinaryExpression expression = parse4("parseShiftExpression", "x << y");
9394 expect(expression.leftOperand, isNotNull);
9395 expect(expression.operator, isNotNull);
9396 expect(expression.operator.type, TokenType.LT_LT);
9397 expect(expression.rightOperand, isNotNull);
9398 }
9399
9400 void test_parseShiftExpression_super() {
9401 BinaryExpression expression = parse4("parseShiftExpression", "super << y");
9402 expect(expression.leftOperand, isNotNull);
9403 expect(expression.operator, isNotNull);
9404 expect(expression.operator.type, TokenType.LT_LT);
9405 expect(expression.rightOperand, isNotNull);
9406 }
9407
9408 void test_parseSimpleIdentifier1_normalIdentifier() {
9409 // TODO(brianwilkerson) Implement tests for this method.
9410 }
9411
9412 void test_parseSimpleIdentifier_builtInIdentifier() {
9413 String lexeme = "as";
9414 SimpleIdentifier identifier = parse4("parseSimpleIdentifier", lexeme);
9415 expect(identifier.token, isNotNull);
9416 expect(identifier.name, lexeme);
9417 }
9418
9419 void test_parseSimpleIdentifier_normalIdentifier() {
9420 String lexeme = "foo";
9421 SimpleIdentifier identifier = parse4("parseSimpleIdentifier", lexeme);
9422 expect(identifier.token, isNotNull);
9423 expect(identifier.name, lexeme);
9424 }
9425
9426 void test_parseStatement_emptyTypeArgumentListt() {
9427 VariableDeclarationStatement statement = parse4(
9428 "parseStatement", "C<> c;", [ParserErrorCode.EXPECTED_TYPE_NAME]);
9429 VariableDeclarationList variables = statement.variables;
9430 TypeName type = variables.type;
9431 TypeArgumentList argumentList = type.typeArguments;
9432 expect(argumentList.leftBracket, isNotNull);
9433 expect(argumentList.arguments, hasLength(1));
9434 expect(argumentList.arguments[0].isSynthetic, isTrue);
9435 expect(argumentList.rightBracket, isNotNull);
9436 }
9437
9438 void test_parseStatement_functionDeclaration_noReturnType() {
9439 FunctionDeclarationStatement statement =
9440 parse4("parseStatement", "f(a, b) {};");
9441 expect(statement.functionDeclaration, isNotNull);
9442 }
9443
9444 void test_parseStatement_functionDeclaration_noReturnType_typeParameters() {
9445 enableGenericMethods = true;
9446 FunctionDeclarationStatement statement =
9447 parse4("parseStatement", "f(a, b) {};");
9448 expect(statement.functionDeclaration, isNotNull);
9449 }
9450
9451 void test_parseStatement_functionDeclaration_returnType() {
9452 // TODO(brianwilkerson) Implement more tests for this method.
9453 FunctionDeclarationStatement statement =
9454 parse4("parseStatement", "int f(a, b) {};");
9455 expect(statement.functionDeclaration, isNotNull);
9456 }
9457
9458 void test_parseStatement_functionDeclaration_returnType_typeParameters() {
9459 enableGenericMethods = true;
9460 FunctionDeclarationStatement statement =
9461 parse4("parseStatement", "int f<E>(a, b) {};");
9462 expect(statement.functionDeclaration, isNotNull);
9463 }
9464
9465 void test_parseStatement_mulipleLabels() {
9466 LabeledStatement statement = parse4("parseStatement", "l: m: return x;");
9467 expect(statement.labels, hasLength(2));
9468 expect(statement.statement, isNotNull);
9469 }
9470
9471 void test_parseStatement_noLabels() {
9472 parse4("parseStatement", "return x;");
9473 }
9474
9475 void test_parseStatement_singleLabel() {
9476 LabeledStatement statement = parse4("parseStatement", "l: return x;");
9477 expect(statement.labels, hasLength(1));
9478 expect(statement.statement, isNotNull);
9479 }
9480
9481 void test_parseStatements_multiple() {
9482 List<Statement> statements =
9483 ParserTestCase.parseStatements("return; return;", 2);
9484 expect(statements, hasLength(2));
9485 }
9486
9487 void test_parseStatements_single() {
9488 List<Statement> statements = ParserTestCase.parseStatements("return;", 1);
9489 expect(statements, hasLength(1));
9490 }
9491
9492 void test_parseStringLiteral_adjacent() {
9493 AdjacentStrings literal = parse4("parseStringLiteral", "'a' 'b'");
9494 NodeList<StringLiteral> strings = literal.strings;
9495 expect(strings, hasLength(2));
9496 StringLiteral firstString = strings[0];
9497 StringLiteral secondString = strings[1];
9498 expect((firstString as SimpleStringLiteral).value, "a");
9499 expect((secondString as SimpleStringLiteral).value, "b");
9500 }
9501
9502 void test_parseStringLiteral_endsWithInterpolation() {
9503 StringLiteral literal = parse4('parseStringLiteral', r"'x$y'");
9504 expect(literal, new isInstanceOf<StringInterpolation>());
9505 StringInterpolation interpolation = literal;
9506 expect(interpolation.elements, hasLength(3));
9507 expect(interpolation.elements[0], new isInstanceOf<InterpolationString>());
9508 InterpolationString element0 = interpolation.elements[0];
9509 expect(element0.value, 'x');
9510 expect(
9511 interpolation.elements[1], new isInstanceOf<InterpolationExpression>());
9512 InterpolationExpression element1 = interpolation.elements[1];
9513 expect(element1.expression, new isInstanceOf<SimpleIdentifier>());
9514 expect(interpolation.elements[2], new isInstanceOf<InterpolationString>());
9515 InterpolationString element2 = interpolation.elements[2];
9516 expect(element2.value, '');
9517 }
9518
9519 void test_parseStringLiteral_interpolated() {
9520 StringInterpolation literal =
9521 parse4("parseStringLiteral", "'a \${b} c \$this d'");
9522 NodeList<InterpolationElement> elements = literal.elements;
9523 expect(elements, hasLength(5));
9524 expect(elements[0] is InterpolationString, isTrue);
9525 expect(elements[1] is InterpolationExpression, isTrue);
9526 expect(elements[2] is InterpolationString, isTrue);
9527 expect(elements[3] is InterpolationExpression, isTrue);
9528 expect(elements[4] is InterpolationString, isTrue);
9529 }
9530
9531 void test_parseStringLiteral_multiline_encodedSpace() {
9532 SimpleStringLiteral literal =
9533 parse4("parseStringLiteral", "'''\\x20\na'''");
9534 expect(literal.literal, isNotNull);
9535 expect(literal.value, " \na");
9536 }
9537
9538 void test_parseStringLiteral_multiline_endsWithInterpolation() {
9539 StringLiteral literal = parse4('parseStringLiteral', r"'''x$y'''");
9540 expect(literal, new isInstanceOf<StringInterpolation>());
9541 StringInterpolation interpolation = literal;
9542 expect(interpolation.elements, hasLength(3));
9543 expect(interpolation.elements[0], new isInstanceOf<InterpolationString>());
9544 InterpolationString element0 = interpolation.elements[0];
9545 expect(element0.value, 'x');
9546 expect(
9547 interpolation.elements[1], new isInstanceOf<InterpolationExpression>());
9548 InterpolationExpression element1 = interpolation.elements[1];
9549 expect(element1.expression, new isInstanceOf<SimpleIdentifier>());
9550 expect(interpolation.elements[2], new isInstanceOf<InterpolationString>());
9551 InterpolationString element2 = interpolation.elements[2];
9552 expect(element2.value, '');
9553 }
9554
9555 void test_parseStringLiteral_multiline_escapedBackslash() {
9556 SimpleStringLiteral literal = parse4("parseStringLiteral", "'''\\\\\na'''");
9557 expect(literal.literal, isNotNull);
9558 expect(literal.value, "\\\na");
9559 }
9560
9561 void test_parseStringLiteral_multiline_escapedBackslash_raw() {
9562 SimpleStringLiteral literal =
9563 parse4("parseStringLiteral", "r'''\\\\\na'''");
9564 expect(literal.literal, isNotNull);
9565 expect(literal.value, "\\\\\na");
9566 }
9567
9568 void test_parseStringLiteral_multiline_escapedEolMarker() {
9569 SimpleStringLiteral literal = parse4("parseStringLiteral", "'''\\\na'''");
9570 expect(literal.literal, isNotNull);
9571 expect(literal.value, "a");
9572 }
9573
9574 void test_parseStringLiteral_multiline_escapedEolMarker_raw() {
9575 SimpleStringLiteral literal = parse4("parseStringLiteral", "r'''\\\na'''");
9576 expect(literal.literal, isNotNull);
9577 expect(literal.value, "a");
9578 }
9579
9580 void test_parseStringLiteral_multiline_escapedSpaceAndEolMarker() {
9581 SimpleStringLiteral literal =
9582 parse4("parseStringLiteral", "'''\\ \\\na'''");
9583 expect(literal.literal, isNotNull);
9584 expect(literal.value, "a");
9585 }
9586
9587 void test_parseStringLiteral_multiline_escapedSpaceAndEolMarker_raw() {
9588 SimpleStringLiteral literal =
9589 parse4("parseStringLiteral", "r'''\\ \\\na'''");
9590 expect(literal.literal, isNotNull);
9591 expect(literal.value, "a");
9592 }
9593
9594 void test_parseStringLiteral_multiline_escapedTab() {
9595 SimpleStringLiteral literal = parse4("parseStringLiteral", "'''\\t\na'''");
9596 expect(literal.literal, isNotNull);
9597 expect(literal.value, "\t\na");
9598 }
9599
9600 void test_parseStringLiteral_multiline_escapedTab_raw() {
9601 SimpleStringLiteral literal = parse4("parseStringLiteral", "r'''\\t\na'''");
9602 expect(literal.literal, isNotNull);
9603 expect(literal.value, "\\t\na");
9604 }
9605
9606 void test_parseStringLiteral_multiline_quoteAfterInterpolation() {
9607 StringLiteral literal = parse4('parseStringLiteral', r"""'''$x'y'''""");
9608 expect(literal, new isInstanceOf<StringInterpolation>());
9609 StringInterpolation interpolation = literal;
9610 expect(interpolation.elements, hasLength(3));
9611 expect(interpolation.elements[0], new isInstanceOf<InterpolationString>());
9612 InterpolationString element0 = interpolation.elements[0];
9613 expect(element0.value, '');
9614 expect(
9615 interpolation.elements[1], new isInstanceOf<InterpolationExpression>());
9616 InterpolationExpression element1 = interpolation.elements[1];
9617 expect(element1.expression, new isInstanceOf<SimpleIdentifier>());
9618 expect(interpolation.elements[2], new isInstanceOf<InterpolationString>());
9619 InterpolationString element2 = interpolation.elements[2];
9620 expect(element2.value, "'y");
9621 }
9622
9623 void test_parseStringLiteral_multiline_startsWithInterpolation() {
9624 StringLiteral literal = parse4('parseStringLiteral', r"'''${x}y'''");
9625 expect(literal, new isInstanceOf<StringInterpolation>());
9626 StringInterpolation interpolation = literal;
9627 expect(interpolation.elements, hasLength(3));
9628 expect(interpolation.elements[0], new isInstanceOf<InterpolationString>());
9629 InterpolationString element0 = interpolation.elements[0];
9630 expect(element0.value, '');
9631 expect(
9632 interpolation.elements[1], new isInstanceOf<InterpolationExpression>());
9633 InterpolationExpression element1 = interpolation.elements[1];
9634 expect(element1.expression, new isInstanceOf<SimpleIdentifier>());
9635 expect(interpolation.elements[2], new isInstanceOf<InterpolationString>());
9636 InterpolationString element2 = interpolation.elements[2];
9637 expect(element2.value, 'y');
9638 }
9639
9640 void test_parseStringLiteral_multiline_twoSpaces() {
9641 SimpleStringLiteral literal = parse4("parseStringLiteral", "''' \na'''");
9642 expect(literal.literal, isNotNull);
9643 expect(literal.value, "a");
9644 }
9645
9646 void test_parseStringLiteral_multiline_twoSpaces_raw() {
9647 SimpleStringLiteral literal = parse4("parseStringLiteral", "r''' \na'''");
9648 expect(literal.literal, isNotNull);
9649 expect(literal.value, "a");
9650 }
9651
9652 void test_parseStringLiteral_multiline_untrimmed() {
9653 SimpleStringLiteral literal = parse4("parseStringLiteral", "''' a\nb'''");
9654 expect(literal.literal, isNotNull);
9655 expect(literal.value, " a\nb");
9656 }
9657
9658 void test_parseStringLiteral_quoteAfterInterpolation() {
9659 StringLiteral literal = parse4('parseStringLiteral', r"""'$x"'""");
9660 expect(literal, new isInstanceOf<StringInterpolation>());
9661 StringInterpolation interpolation = literal;
9662 expect(interpolation.elements, hasLength(3));
9663 expect(interpolation.elements[0], new isInstanceOf<InterpolationString>());
9664 InterpolationString element0 = interpolation.elements[0];
9665 expect(element0.value, '');
9666 expect(
9667 interpolation.elements[1], new isInstanceOf<InterpolationExpression>());
9668 InterpolationExpression element1 = interpolation.elements[1];
9669 expect(element1.expression, new isInstanceOf<SimpleIdentifier>());
9670 expect(interpolation.elements[2], new isInstanceOf<InterpolationString>());
9671 InterpolationString element2 = interpolation.elements[2];
9672 expect(element2.value, '"');
9673 }
9674
9675 void test_parseStringLiteral_single() {
9676 SimpleStringLiteral literal = parse4("parseStringLiteral", "'a'");
9677 expect(literal.literal, isNotNull);
9678 expect(literal.value, "a");
9679 }
9680
9681 void test_parseStringLiteral_startsWithInterpolation() {
9682 StringLiteral literal = parse4('parseStringLiteral', r"'${x}y'");
9683 expect(literal, new isInstanceOf<StringInterpolation>());
9684 StringInterpolation interpolation = literal;
9685 expect(interpolation.elements, hasLength(3));
9686 expect(interpolation.elements[0], new isInstanceOf<InterpolationString>());
9687 InterpolationString element0 = interpolation.elements[0];
9688 expect(element0.value, '');
9689 expect(
9690 interpolation.elements[1], new isInstanceOf<InterpolationExpression>());
9691 InterpolationExpression element1 = interpolation.elements[1];
9692 expect(element1.expression, new isInstanceOf<SimpleIdentifier>());
9693 expect(interpolation.elements[2], new isInstanceOf<InterpolationString>());
9694 InterpolationString element2 = interpolation.elements[2];
9695 expect(element2.value, 'y');
9696 }
9697
9698 void test_parseSuperConstructorInvocation_named() {
9699 SuperConstructorInvocation invocation =
9700 parse4("parseSuperConstructorInvocation", "super.a()");
9701 expect(invocation.argumentList, isNotNull);
9702 expect(invocation.constructorName, isNotNull);
9703 expect(invocation.superKeyword, isNotNull);
9704 expect(invocation.period, isNotNull);
9705 }
9706
9707 void test_parseSuperConstructorInvocation_unnamed() {
9708 SuperConstructorInvocation invocation =
9709 parse4("parseSuperConstructorInvocation", "super()");
9710 expect(invocation.argumentList, isNotNull);
9711 expect(invocation.constructorName, isNull);
9712 expect(invocation.superKeyword, isNotNull);
9713 expect(invocation.period, isNull);
9714 }
9715
9716 void test_parseSwitchStatement_case() {
9717 SwitchStatement statement =
9718 parse4("parseSwitchStatement", "switch (a) {case 1: return 'I';}");
9719 expect(statement.switchKeyword, isNotNull);
9720 expect(statement.leftParenthesis, isNotNull);
9721 expect(statement.expression, isNotNull);
9722 expect(statement.rightParenthesis, isNotNull);
9723 expect(statement.leftBracket, isNotNull);
9724 expect(statement.members, hasLength(1));
9725 expect(statement.rightBracket, isNotNull);
9726 }
9727
9728 void test_parseSwitchStatement_empty() {
9729 SwitchStatement statement = parse4("parseSwitchStatement", "switch (a) {}");
9730 expect(statement.switchKeyword, isNotNull);
9731 expect(statement.leftParenthesis, isNotNull);
9732 expect(statement.expression, isNotNull);
9733 expect(statement.rightParenthesis, isNotNull);
9734 expect(statement.leftBracket, isNotNull);
9735 expect(statement.members, hasLength(0));
9736 expect(statement.rightBracket, isNotNull);
9737 }
9738
9739 void test_parseSwitchStatement_labeledCase() {
9740 SwitchStatement statement =
9741 parse4("parseSwitchStatement", "switch (a) {l1: l2: l3: case(1):}");
9742 expect(statement.switchKeyword, isNotNull);
9743 expect(statement.leftParenthesis, isNotNull);
9744 expect(statement.expression, isNotNull);
9745 expect(statement.rightParenthesis, isNotNull);
9746 expect(statement.leftBracket, isNotNull);
9747 expect(statement.members, hasLength(1));
9748 expect(statement.members[0].labels, hasLength(3));
9749 expect(statement.rightBracket, isNotNull);
9750 }
9751
9752 void test_parseSwitchStatement_labeledStatementInCase() {
9753 SwitchStatement statement = parse4(
9754 "parseSwitchStatement", "switch (a) {case 0: f(); l1: g(); break;}");
9755 expect(statement.switchKeyword, isNotNull);
9756 expect(statement.leftParenthesis, isNotNull);
9757 expect(statement.expression, isNotNull);
9758 expect(statement.rightParenthesis, isNotNull);
9759 expect(statement.leftBracket, isNotNull);
9760 expect(statement.members, hasLength(1));
9761 expect(statement.members[0].statements, hasLength(3));
9762 expect(statement.rightBracket, isNotNull);
9763 }
9764
9765 void test_parseSymbolLiteral_builtInIdentifier() {
9766 SymbolLiteral literal =
9767 parse4("parseSymbolLiteral", "#dynamic.static.abstract");
9768 expect(literal.poundSign, isNotNull);
9769 List<Token> components = literal.components;
9770 expect(components, hasLength(3));
9771 expect(components[0].lexeme, "dynamic");
9772 expect(components[1].lexeme, "static");
9773 expect(components[2].lexeme, "abstract");
9774 }
9775
9776 void test_parseSymbolLiteral_multiple() {
9777 SymbolLiteral literal = parse4("parseSymbolLiteral", "#a.b.c");
9778 expect(literal.poundSign, isNotNull);
9779 List<Token> components = literal.components;
9780 expect(components, hasLength(3));
9781 expect(components[0].lexeme, "a");
9782 expect(components[1].lexeme, "b");
9783 expect(components[2].lexeme, "c");
9784 }
9785
9786 void test_parseSymbolLiteral_operator() {
9787 SymbolLiteral literal = parse4("parseSymbolLiteral", "#==");
9788 expect(literal.poundSign, isNotNull);
9789 List<Token> components = literal.components;
9790 expect(components, hasLength(1));
9791 expect(components[0].lexeme, "==");
9792 }
9793
9794 void test_parseSymbolLiteral_single() {
9795 SymbolLiteral literal = parse4("parseSymbolLiteral", "#a");
9796 expect(literal.poundSign, isNotNull);
9797 List<Token> components = literal.components;
9798 expect(components, hasLength(1));
9799 expect(components[0].lexeme, "a");
9800 }
9801
9802 void test_parseSymbolLiteral_void() {
9803 SymbolLiteral literal = parse4("parseSymbolLiteral", "#void");
9804 expect(literal.poundSign, isNotNull);
9805 List<Token> components = literal.components;
9806 expect(components, hasLength(1));
9807 expect(components[0].lexeme, "void");
9808 }
9809
9810 void test_parseThrowExpression() {
9811 ThrowExpression expression = parse4("parseThrowExpression", "throw x;");
9812 expect(expression.throwKeyword, isNotNull);
9813 expect(expression.expression, isNotNull);
9814 }
9815
9816 void test_parseThrowExpressionWithoutCascade() {
9817 ThrowExpression expression =
9818 parse4("parseThrowExpressionWithoutCascade", "throw x;");
9819 expect(expression.throwKeyword, isNotNull);
9820 expect(expression.expression, isNotNull);
9821 }
9822
9823 void test_parseTryStatement_catch() {
9824 TryStatement statement = parse4("parseTryStatement", "try {} catch (e) {}");
9825 expect(statement.tryKeyword, isNotNull);
9826 expect(statement.body, isNotNull);
9827 NodeList<CatchClause> catchClauses = statement.catchClauses;
9828 expect(catchClauses, hasLength(1));
9829 CatchClause clause = catchClauses[0];
9830 expect(clause.onKeyword, isNull);
9831 expect(clause.exceptionType, isNull);
9832 expect(clause.catchKeyword, isNotNull);
9833 expect(clause.exceptionParameter, isNotNull);
9834 expect(clause.comma, isNull);
9835 expect(clause.stackTraceParameter, isNull);
9836 expect(clause.body, isNotNull);
9837 expect(statement.finallyKeyword, isNull);
9838 expect(statement.finallyBlock, isNull);
9839 }
9840
9841 void test_parseTryStatement_catch_finally() {
9842 TryStatement statement =
9843 parse4("parseTryStatement", "try {} catch (e, s) {} finally {}");
9844 expect(statement.tryKeyword, isNotNull);
9845 expect(statement.body, isNotNull);
9846 NodeList<CatchClause> catchClauses = statement.catchClauses;
9847 expect(catchClauses, hasLength(1));
9848 CatchClause clause = catchClauses[0];
9849 expect(clause.onKeyword, isNull);
9850 expect(clause.exceptionType, isNull);
9851 expect(clause.catchKeyword, isNotNull);
9852 expect(clause.exceptionParameter, isNotNull);
9853 expect(clause.comma, isNotNull);
9854 expect(clause.stackTraceParameter, isNotNull);
9855 expect(clause.body, isNotNull);
9856 expect(statement.finallyKeyword, isNotNull);
9857 expect(statement.finallyBlock, isNotNull);
9858 }
9859
9860 void test_parseTryStatement_finally() {
9861 TryStatement statement = parse4("parseTryStatement", "try {} finally {}");
9862 expect(statement.tryKeyword, isNotNull);
9863 expect(statement.body, isNotNull);
9864 expect(statement.catchClauses, hasLength(0));
9865 expect(statement.finallyKeyword, isNotNull);
9866 expect(statement.finallyBlock, isNotNull);
9867 }
9868
9869 void test_parseTryStatement_multiple() {
9870 TryStatement statement = parse4("parseTryStatement",
9871 "try {} on NPE catch (e) {} on Error {} catch (e) {}");
9872 expect(statement.tryKeyword, isNotNull);
9873 expect(statement.body, isNotNull);
9874 expect(statement.catchClauses, hasLength(3));
9875 expect(statement.finallyKeyword, isNull);
9876 expect(statement.finallyBlock, isNull);
9877 }
9878
9879 void test_parseTryStatement_on() {
9880 TryStatement statement = parse4("parseTryStatement", "try {} on Error {}");
9881 expect(statement.tryKeyword, isNotNull);
9882 expect(statement.body, isNotNull);
9883 NodeList<CatchClause> catchClauses = statement.catchClauses;
9884 expect(catchClauses, hasLength(1));
9885 CatchClause clause = catchClauses[0];
9886 expect(clause.onKeyword, isNotNull);
9887 expect(clause.exceptionType, isNotNull);
9888 expect(clause.catchKeyword, isNull);
9889 expect(clause.exceptionParameter, isNull);
9890 expect(clause.comma, isNull);
9891 expect(clause.stackTraceParameter, isNull);
9892 expect(clause.body, isNotNull);
9893 expect(statement.finallyKeyword, isNull);
9894 expect(statement.finallyBlock, isNull);
9895 }
9896
9897 void test_parseTryStatement_on_catch() {
9898 TryStatement statement =
9899 parse4("parseTryStatement", "try {} on Error catch (e, s) {}");
9900 expect(statement.tryKeyword, isNotNull);
9901 expect(statement.body, isNotNull);
9902 NodeList<CatchClause> catchClauses = statement.catchClauses;
9903 expect(catchClauses, hasLength(1));
9904 CatchClause clause = catchClauses[0];
9905 expect(clause.onKeyword, isNotNull);
9906 expect(clause.exceptionType, isNotNull);
9907 expect(clause.catchKeyword, isNotNull);
9908 expect(clause.exceptionParameter, isNotNull);
9909 expect(clause.comma, isNotNull);
9910 expect(clause.stackTraceParameter, isNotNull);
9911 expect(clause.body, isNotNull);
9912 expect(statement.finallyKeyword, isNull);
9913 expect(statement.finallyBlock, isNull);
9914 }
9915
9916 void test_parseTryStatement_on_catch_finally() {
9917 TryStatement statement = parse4(
9918 "parseTryStatement", "try {} on Error catch (e, s) {} finally {}");
9919 expect(statement.tryKeyword, isNotNull);
9920 expect(statement.body, isNotNull);
9921 NodeList<CatchClause> catchClauses = statement.catchClauses;
9922 expect(catchClauses, hasLength(1));
9923 CatchClause clause = catchClauses[0];
9924 expect(clause.onKeyword, isNotNull);
9925 expect(clause.exceptionType, isNotNull);
9926 expect(clause.catchKeyword, isNotNull);
9927 expect(clause.exceptionParameter, isNotNull);
9928 expect(clause.comma, isNotNull);
9929 expect(clause.stackTraceParameter, isNotNull);
9930 expect(clause.body, isNotNull);
9931 expect(statement.finallyKeyword, isNotNull);
9932 expect(statement.finallyBlock, isNotNull);
9933 }
9934
9935 void test_parseTypeAlias_function_noParameters() {
9936 FunctionTypeAlias typeAlias = parse("parseTypeAlias",
9937 <Object>[emptyCommentAndMetadata()], "typedef bool F();");
9938 expect(typeAlias.typedefKeyword, isNotNull);
9939 expect(typeAlias.name, isNotNull);
9940 expect(typeAlias.parameters, isNotNull);
9941 expect(typeAlias.returnType, isNotNull);
9942 expect(typeAlias.semicolon, isNotNull);
9943 expect(typeAlias.typeParameters, isNull);
9944 }
9945
9946 void test_parseTypeAlias_function_noReturnType() {
9947 FunctionTypeAlias typeAlias = parse(
9948 "parseTypeAlias", <Object>[emptyCommentAndMetadata()], "typedef F();");
9949 expect(typeAlias.typedefKeyword, isNotNull);
9950 expect(typeAlias.name, isNotNull);
9951 expect(typeAlias.parameters, isNotNull);
9952 expect(typeAlias.returnType, isNull);
9953 expect(typeAlias.semicolon, isNotNull);
9954 expect(typeAlias.typeParameters, isNull);
9955 }
9956
9957 void test_parseTypeAlias_function_parameterizedReturnType() {
9958 FunctionTypeAlias typeAlias = parse("parseTypeAlias",
9959 <Object>[emptyCommentAndMetadata()], "typedef A<B> F();");
9960 expect(typeAlias.typedefKeyword, isNotNull);
9961 expect(typeAlias.name, isNotNull);
9962 expect(typeAlias.parameters, isNotNull);
9963 expect(typeAlias.returnType, isNotNull);
9964 expect(typeAlias.semicolon, isNotNull);
9965 expect(typeAlias.typeParameters, isNull);
9966 }
9967
9968 void test_parseTypeAlias_function_parameters() {
9969 FunctionTypeAlias typeAlias = parse("parseTypeAlias",
9970 <Object>[emptyCommentAndMetadata()], "typedef bool F(Object value);");
9971 expect(typeAlias.typedefKeyword, isNotNull);
9972 expect(typeAlias.name, isNotNull);
9973 expect(typeAlias.parameters, isNotNull);
9974 expect(typeAlias.returnType, isNotNull);
9975 expect(typeAlias.semicolon, isNotNull);
9976 expect(typeAlias.typeParameters, isNull);
9977 }
9978
9979 void test_parseTypeAlias_function_typeParameters() {
9980 FunctionTypeAlias typeAlias = parse("parseTypeAlias",
9981 <Object>[emptyCommentAndMetadata()], "typedef bool F<E>();");
9982 expect(typeAlias.typedefKeyword, isNotNull);
9983 expect(typeAlias.name, isNotNull);
9984 expect(typeAlias.parameters, isNotNull);
9985 expect(typeAlias.returnType, isNotNull);
9986 expect(typeAlias.semicolon, isNotNull);
9987 expect(typeAlias.typeParameters, isNotNull);
9988 }
9989
9990 void test_parseTypeAlias_function_voidReturnType() {
9991 FunctionTypeAlias typeAlias = parse("parseTypeAlias",
9992 <Object>[emptyCommentAndMetadata()], "typedef void F();");
9993 expect(typeAlias.typedefKeyword, isNotNull);
9994 expect(typeAlias.name, isNotNull);
9995 expect(typeAlias.parameters, isNotNull);
9996 expect(typeAlias.returnType, isNotNull);
9997 expect(typeAlias.semicolon, isNotNull);
9998 expect(typeAlias.typeParameters, isNull);
9999 }
10000
10001 void test_parseTypeArgumentList_empty() {
10002 TypeArgumentList argumentList = parse4(
10003 "parseTypeArgumentList", "<>", [ParserErrorCode.EXPECTED_TYPE_NAME]);
10004 expect(argumentList.leftBracket, isNotNull);
10005 expect(argumentList.arguments, hasLength(1));
10006 expect(argumentList.rightBracket, isNotNull);
10007 }
10008
10009 void test_parseTypeArgumentList_multiple() {
10010 TypeArgumentList argumentList =
10011 parse4("parseTypeArgumentList", "<int, int, int>");
10012 expect(argumentList.leftBracket, isNotNull);
10013 expect(argumentList.arguments, hasLength(3));
10014 expect(argumentList.rightBracket, isNotNull);
10015 }
10016
10017 void test_parseTypeArgumentList_nested() {
10018 TypeArgumentList argumentList = parse4("parseTypeArgumentList", "<A<B>>");
10019 expect(argumentList.leftBracket, isNotNull);
10020 expect(argumentList.arguments, hasLength(1));
10021 TypeName argument = argumentList.arguments[0];
10022 expect(argument, isNotNull);
10023 TypeArgumentList innerList = argument.typeArguments;
10024 expect(innerList, isNotNull);
10025 expect(innerList.arguments, hasLength(1));
10026 expect(argumentList.rightBracket, isNotNull);
10027 }
10028
10029 void test_parseTypeArgumentList_nested_withComment_double() {
10030 TypeArgumentList argumentList =
10031 parse4("parseTypeArgumentList", "<A<B /* 0 */ >>");
10032 expect(argumentList.leftBracket, isNotNull);
10033 expect(argumentList.rightBracket, isNotNull);
10034 expect(argumentList.arguments, hasLength(1));
10035
10036 TypeName argument = argumentList.arguments[0];
10037 expect(argument, isNotNull);
10038
10039 TypeArgumentList innerList = argument.typeArguments;
10040 expect(innerList, isNotNull);
10041 expect(innerList.leftBracket, isNotNull);
10042 expect(innerList.arguments, hasLength(1));
10043 expect(innerList.rightBracket, isNotNull);
10044 expect(innerList.rightBracket.precedingComments, isNotNull);
10045 }
10046
10047 void test_parseTypeArgumentList_nested_withComment_tripple() {
10048 TypeArgumentList argumentList =
10049 parse4("parseTypeArgumentList", "<A<B<C /* 0 */ >>>");
10050 expect(argumentList.leftBracket, isNotNull);
10051 expect(argumentList.rightBracket, isNotNull);
10052 expect(argumentList.arguments, hasLength(1));
10053
10054 TypeName argument = argumentList.arguments[0];
10055 expect(argument, isNotNull);
10056
10057 TypeArgumentList innerList = argument.typeArguments;
10058 expect(innerList, isNotNull);
10059 expect(innerList.leftBracket, isNotNull);
10060 expect(innerList.arguments, hasLength(1));
10061 expect(innerList.rightBracket, isNotNull);
10062
10063 TypeName innerArgument = innerList.arguments[0];
10064 expect(innerArgument, isNotNull);
10065
10066 TypeArgumentList innerInnerList = innerArgument.typeArguments;
10067 expect(innerInnerList, isNotNull);
10068 expect(innerInnerList.leftBracket, isNotNull);
10069 expect(innerInnerList.arguments, hasLength(1));
10070 expect(innerInnerList.rightBracket, isNotNull);
10071 expect(innerInnerList.rightBracket.precedingComments, isNotNull);
10072 }
10073
10074 void test_parseTypeArgumentList_single() {
10075 TypeArgumentList argumentList = parse4("parseTypeArgumentList", "<int>");
10076 expect(argumentList.leftBracket, isNotNull);
10077 expect(argumentList.arguments, hasLength(1));
10078 expect(argumentList.rightBracket, isNotNull);
10079 }
10080
10081 void test_parseTypeName_parameterized() {
10082 TypeName typeName = parse4("parseTypeName", "List<int>");
10083 expect(typeName.name, isNotNull);
10084 expect(typeName.typeArguments, isNotNull);
10085 }
10086
10087 void test_parseTypeName_simple() {
10088 TypeName typeName = parse4("parseTypeName", "int");
10089 expect(typeName.name, isNotNull);
10090 expect(typeName.typeArguments, isNull);
10091 }
10092
10093 void test_parseTypeParameter_bounded() {
10094 TypeParameter parameter = parse4("parseTypeParameter", "A extends B");
10095 expect(parameter.bound, isNotNull);
10096 expect(parameter.extendsKeyword, isNotNull);
10097 expect(parameter.name, isNotNull);
10098 }
10099
10100 void test_parseTypeParameter_simple() {
10101 TypeParameter parameter = parse4("parseTypeParameter", "A");
10102 expect(parameter.bound, isNull);
10103 expect(parameter.extendsKeyword, isNull);
10104 expect(parameter.name, isNotNull);
10105 }
10106
10107 void test_parseTypeParameterList_multiple() {
10108 TypeParameterList parameterList =
10109 parse4("parseTypeParameterList", "<A, B extends C, D>");
10110 expect(parameterList.leftBracket, isNotNull);
10111 expect(parameterList.rightBracket, isNotNull);
10112 expect(parameterList.typeParameters, hasLength(3));
10113 }
10114
10115 void test_parseTypeParameterList_parameterizedWithTrailingEquals() {
10116 TypeParameterList parameterList =
10117 parse4("parseTypeParameterList", "<A extends B<E>>=");
10118 expect(parameterList.leftBracket, isNotNull);
10119 expect(parameterList.rightBracket, isNotNull);
10120 expect(parameterList.typeParameters, hasLength(1));
10121 }
10122
10123 void test_parseTypeParameterList_single() {
10124 TypeParameterList parameterList = parse4("parseTypeParameterList", "<A>");
10125 expect(parameterList.leftBracket, isNotNull);
10126 expect(parameterList.rightBracket, isNotNull);
10127 expect(parameterList.typeParameters, hasLength(1));
10128 }
10129
10130 void test_parseTypeParameterList_withTrailingEquals() {
10131 TypeParameterList parameterList = parse4("parseTypeParameterList", "<A>=");
10132 expect(parameterList.leftBracket, isNotNull);
10133 expect(parameterList.rightBracket, isNotNull);
10134 expect(parameterList.typeParameters, hasLength(1));
10135 }
10136
10137 void test_parseUnaryExpression_decrement_normal() {
10138 PrefixExpression expression = parse4("parseUnaryExpression", "--x");
10139 expect(expression.operator, isNotNull);
10140 expect(expression.operator.type, TokenType.MINUS_MINUS);
10141 expect(expression.operand, isNotNull);
10142 }
10143
10144 void test_parseUnaryExpression_decrement_super() {
10145 PrefixExpression expression = parse4("parseUnaryExpression", "--super");
10146 expect(expression.operator, isNotNull);
10147 expect(expression.operator.type, TokenType.MINUS);
10148 Expression innerExpression = expression.operand;
10149 expect(innerExpression, isNotNull);
10150 expect(innerExpression is PrefixExpression, isTrue);
10151 PrefixExpression operand = innerExpression as PrefixExpression;
10152 expect(operand.operator, isNotNull);
10153 expect(operand.operator.type, TokenType.MINUS);
10154 expect(operand.operand, isNotNull);
10155 }
10156
10157 void test_parseUnaryExpression_decrement_super_propertyAccess() {
10158 PrefixExpression expression = parse4("parseUnaryExpression", "--super.x");
10159 expect(expression.operator, isNotNull);
10160 expect(expression.operator.type, TokenType.MINUS_MINUS);
10161 expect(expression.operand, isNotNull);
10162 PropertyAccess operand = expression.operand as PropertyAccess;
10163 expect(operand.target is SuperExpression, isTrue);
10164 expect(operand.propertyName.name, "x");
10165 }
10166
10167 void test_parseUnaryExpression_decrement_super_withComment() {
10168 PrefixExpression expression =
10169 parse4("parseUnaryExpression", "/* 0 */ --super");
10170 expect(expression.operator, isNotNull);
10171 expect(expression.operator.type, TokenType.MINUS);
10172 expect(expression.operator.precedingComments, isNotNull);
10173 Expression innerExpression = expression.operand;
10174 expect(innerExpression, isNotNull);
10175 expect(innerExpression is PrefixExpression, isTrue);
10176 PrefixExpression operand = innerExpression as PrefixExpression;
10177 expect(operand.operator, isNotNull);
10178 expect(operand.operator.type, TokenType.MINUS);
10179 expect(operand.operand, isNotNull);
10180 }
10181
10182 void test_parseUnaryExpression_increment_normal() {
10183 PrefixExpression expression = parse4("parseUnaryExpression", "++x");
10184 expect(expression.operator, isNotNull);
10185 expect(expression.operator.type, TokenType.PLUS_PLUS);
10186 expect(expression.operand, isNotNull);
10187 }
10188
10189 void test_parseUnaryExpression_increment_super_index() {
10190 PrefixExpression expression = parse4("parseUnaryExpression", "++super[0]");
10191 expect(expression.operator, isNotNull);
10192 expect(expression.operator.type, TokenType.PLUS_PLUS);
10193 expect(expression.operand, isNotNull);
10194 IndexExpression operand = expression.operand as IndexExpression;
10195 expect(operand.realTarget is SuperExpression, isTrue);
10196 expect(operand.index is IntegerLiteral, isTrue);
10197 }
10198
10199 void test_parseUnaryExpression_increment_super_propertyAccess() {
10200 PrefixExpression expression = parse4("parseUnaryExpression", "++super.x");
10201 expect(expression.operator, isNotNull);
10202 expect(expression.operator.type, TokenType.PLUS_PLUS);
10203 expect(expression.operand, isNotNull);
10204 PropertyAccess operand = expression.operand as PropertyAccess;
10205 expect(operand.target is SuperExpression, isTrue);
10206 expect(operand.propertyName.name, "x");
10207 }
10208
10209 void test_parseUnaryExpression_minus_normal() {
10210 PrefixExpression expression = parse4("parseUnaryExpression", "-x");
10211 expect(expression.operator, isNotNull);
10212 expect(expression.operator.type, TokenType.MINUS);
10213 expect(expression.operand, isNotNull);
10214 }
10215
10216 void test_parseUnaryExpression_minus_super() {
10217 PrefixExpression expression = parse4("parseUnaryExpression", "-super");
10218 expect(expression.operator, isNotNull);
10219 expect(expression.operator.type, TokenType.MINUS);
10220 expect(expression.operand, isNotNull);
10221 }
10222
10223 void test_parseUnaryExpression_not_normal() {
10224 PrefixExpression expression = parse4("parseUnaryExpression", "!x");
10225 expect(expression.operator, isNotNull);
10226 expect(expression.operator.type, TokenType.BANG);
10227 expect(expression.operand, isNotNull);
10228 }
10229
10230 void test_parseUnaryExpression_not_super() {
10231 PrefixExpression expression = parse4("parseUnaryExpression", "!super");
10232 expect(expression.operator, isNotNull);
10233 expect(expression.operator.type, TokenType.BANG);
10234 expect(expression.operand, isNotNull);
10235 }
10236
10237 void test_parseUnaryExpression_tilda_normal() {
10238 PrefixExpression expression = parse4("parseUnaryExpression", "~x");
10239 expect(expression.operator, isNotNull);
10240 expect(expression.operator.type, TokenType.TILDE);
10241 expect(expression.operand, isNotNull);
10242 }
10243
10244 void test_parseUnaryExpression_tilda_super() {
10245 PrefixExpression expression = parse4("parseUnaryExpression", "~super");
10246 expect(expression.operator, isNotNull);
10247 expect(expression.operator.type, TokenType.TILDE);
10248 expect(expression.operand, isNotNull);
10249 }
10250
10251 void test_parseVariableDeclaration_equals() {
10252 VariableDeclaration declaration =
10253 parse4("parseVariableDeclaration", "a = b");
10254 expect(declaration.name, isNotNull);
10255 expect(declaration.equals, isNotNull);
10256 expect(declaration.initializer, isNotNull);
10257 }
10258
10259 void test_parseVariableDeclaration_noEquals() {
10260 VariableDeclaration declaration = parse4("parseVariableDeclaration", "a");
10261 expect(declaration.name, isNotNull);
10262 expect(declaration.equals, isNull);
10263 expect(declaration.initializer, isNull);
10264 }
10265
10266 void test_parseVariableDeclarationListAfterMetadata_const_noType() {
10267 VariableDeclarationList declarationList = parse(
10268 "parseVariableDeclarationListAfterMetadata",
10269 <Object>[emptyCommentAndMetadata()],
10270 "const a");
10271 expect(declarationList.keyword, isNotNull);
10272 expect(declarationList.type, isNull);
10273 expect(declarationList.variables, hasLength(1));
10274 }
10275
10276 void test_parseVariableDeclarationListAfterMetadata_const_type() {
10277 VariableDeclarationList declarationList = parse(
10278 "parseVariableDeclarationListAfterMetadata",
10279 <Object>[emptyCommentAndMetadata()],
10280 "const A a");
10281 expect(declarationList.keyword, isNotNull);
10282 expect(declarationList.type, isNotNull);
10283 expect(declarationList.variables, hasLength(1));
10284 }
10285
10286 void test_parseVariableDeclarationListAfterMetadata_final_noType() {
10287 VariableDeclarationList declarationList = parse(
10288 "parseVariableDeclarationListAfterMetadata",
10289 <Object>[emptyCommentAndMetadata()],
10290 "final a");
10291 expect(declarationList.keyword, isNotNull);
10292 expect(declarationList.type, isNull);
10293 expect(declarationList.variables, hasLength(1));
10294 }
10295
10296 void test_parseVariableDeclarationListAfterMetadata_final_type() {
10297 VariableDeclarationList declarationList = parse(
10298 "parseVariableDeclarationListAfterMetadata",
10299 <Object>[emptyCommentAndMetadata()],
10300 "final A a");
10301 expect(declarationList.keyword, isNotNull);
10302 expect(declarationList.type, isNotNull);
10303 expect(declarationList.variables, hasLength(1));
10304 }
10305
10306 void test_parseVariableDeclarationListAfterMetadata_type_multiple() {
10307 VariableDeclarationList declarationList = parse(
10308 "parseVariableDeclarationListAfterMetadata",
10309 <Object>[emptyCommentAndMetadata()],
10310 "A a, b, c");
10311 expect(declarationList.keyword, isNull);
10312 expect(declarationList.type, isNotNull);
10313 expect(declarationList.variables, hasLength(3));
10314 }
10315
10316 void test_parseVariableDeclarationListAfterMetadata_type_single() {
10317 VariableDeclarationList declarationList = parse(
10318 "parseVariableDeclarationListAfterMetadata",
10319 <Object>[emptyCommentAndMetadata()],
10320 "A a");
10321 expect(declarationList.keyword, isNull);
10322 expect(declarationList.type, isNotNull);
10323 expect(declarationList.variables, hasLength(1));
10324 }
10325
10326 void test_parseVariableDeclarationListAfterMetadata_var_multiple() {
10327 VariableDeclarationList declarationList = parse(
10328 "parseVariableDeclarationListAfterMetadata",
10329 <Object>[emptyCommentAndMetadata()],
10330 "var a, b, c");
10331 expect(declarationList.keyword, isNotNull);
10332 expect(declarationList.type, isNull);
10333 expect(declarationList.variables, hasLength(3));
10334 }
10335
10336 void test_parseVariableDeclarationListAfterMetadata_var_single() {
10337 VariableDeclarationList declarationList = parse(
10338 "parseVariableDeclarationListAfterMetadata",
10339 <Object>[emptyCommentAndMetadata()],
10340 "var a");
10341 expect(declarationList.keyword, isNotNull);
10342 expect(declarationList.type, isNull);
10343 expect(declarationList.variables, hasLength(1));
10344 }
10345
10346 void test_parseVariableDeclarationListAfterType_type() {
10347 TypeName type = new TypeName(new SimpleIdentifier(null), null);
10348 VariableDeclarationList declarationList = parse(
10349 "parseVariableDeclarationListAfterType",
10350 <Object>[emptyCommentAndMetadata(), null, type],
10351 "a");
10352 expect(declarationList.keyword, isNull);
10353 expect(declarationList.type, type);
10354 expect(declarationList.variables, hasLength(1));
10355 }
10356
10357 void test_parseVariableDeclarationListAfterType_var() {
10358 Token keyword = TokenFactory.tokenFromKeyword(Keyword.VAR);
10359 VariableDeclarationList declarationList = parse(
10360 "parseVariableDeclarationListAfterType",
10361 <Object>[emptyCommentAndMetadata(), keyword, null],
10362 "a, b, c");
10363 expect(declarationList.keyword, keyword);
10364 expect(declarationList.type, isNull);
10365 expect(declarationList.variables, hasLength(3));
10366 }
10367
10368 void test_parseVariableDeclarationStatementAfterMetadata_multiple() {
10369 VariableDeclarationStatement statement = parse(
10370 "parseVariableDeclarationStatementAfterMetadata",
10371 <Object>[emptyCommentAndMetadata()],
10372 "var x, y, z;");
10373 expect(statement.semicolon, isNotNull);
10374 VariableDeclarationList variableList = statement.variables;
10375 expect(variableList, isNotNull);
10376 expect(variableList.variables, hasLength(3));
10377 }
10378
10379 void test_parseVariableDeclarationStatementAfterMetadata_single() {
10380 VariableDeclarationStatement statement = parse(
10381 "parseVariableDeclarationStatementAfterMetadata",
10382 <Object>[emptyCommentAndMetadata()],
10383 "var x;");
10384 expect(statement.semicolon, isNotNull);
10385 VariableDeclarationList variableList = statement.variables;
10386 expect(variableList, isNotNull);
10387 expect(variableList.variables, hasLength(1));
10388 }
10389
10390 void test_parseWhileStatement() {
10391 WhileStatement statement = parse4("parseWhileStatement", "while (x) {}");
10392 expect(statement.whileKeyword, isNotNull);
10393 expect(statement.leftParenthesis, isNotNull);
10394 expect(statement.condition, isNotNull);
10395 expect(statement.rightParenthesis, isNotNull);
10396 expect(statement.body, isNotNull);
10397 }
10398
10399 void test_parseWithClause_multiple() {
10400 WithClause clause = parse4("parseWithClause", "with A, B, C");
10401 expect(clause.withKeyword, isNotNull);
10402 expect(clause.mixinTypes, hasLength(3));
10403 }
10404
10405 void test_parseWithClause_single() {
10406 WithClause clause = parse4("parseWithClause", "with M");
10407 expect(clause.withKeyword, isNotNull);
10408 expect(clause.mixinTypes, hasLength(1));
10409 }
10410
10411 void test_parseYieldStatement_each() {
10412 YieldStatement statement = parse4("parseYieldStatement", "yield* x;");
10413 expect(statement.yieldKeyword, isNotNull);
10414 expect(statement.star, isNotNull);
10415 expect(statement.expression, isNotNull);
10416 expect(statement.semicolon, isNotNull);
10417 }
10418
10419 void test_parseYieldStatement_normal() {
10420 YieldStatement statement = parse4("parseYieldStatement", "yield x;");
10421 expect(statement.yieldKeyword, isNotNull);
10422 expect(statement.star, isNull);
10423 expect(statement.expression, isNotNull);
10424 expect(statement.semicolon, isNotNull);
10425 }
10426
10427 void test_skipPrefixedIdentifier_invalid() {
10428 Token following = _skip("skipPrefixedIdentifier", "+");
10429 expect(following, isNull);
10430 }
10431
10432 void test_skipPrefixedIdentifier_notPrefixed() {
10433 Token following = _skip("skipPrefixedIdentifier", "a +");
10434 expect(following, isNotNull);
10435 expect(following.type, TokenType.PLUS);
10436 }
10437
10438 void test_skipPrefixedIdentifier_prefixed() {
10439 Token following = _skip("skipPrefixedIdentifier", "a.b +");
10440 expect(following, isNotNull);
10441 expect(following.type, TokenType.PLUS);
10442 }
10443
10444 void test_skipReturnType_invalid() {
10445 Token following = _skip("skipReturnType", "+");
10446 expect(following, isNull);
10447 }
10448
10449 void test_skipReturnType_type() {
10450 Token following = _skip("skipReturnType", "C +");
10451 expect(following, isNotNull);
10452 expect(following.type, TokenType.PLUS);
10453 }
10454
10455 void test_skipReturnType_void() {
10456 Token following = _skip("skipReturnType", "void +");
10457 expect(following, isNotNull);
10458 expect(following.type, TokenType.PLUS);
10459 }
10460
10461 void test_skipSimpleIdentifier_identifier() {
10462 Token following = _skip("skipSimpleIdentifier", "i +");
10463 expect(following, isNotNull);
10464 expect(following.type, TokenType.PLUS);
10465 }
10466
10467 void test_skipSimpleIdentifier_invalid() {
10468 Token following = _skip("skipSimpleIdentifier", "9 +");
10469 expect(following, isNull);
10470 }
10471
10472 void test_skipSimpleIdentifier_pseudoKeyword() {
10473 Token following = _skip("skipSimpleIdentifier", "as +");
10474 expect(following, isNotNull);
10475 expect(following.type, TokenType.PLUS);
10476 }
10477
10478 void test_skipStringLiteral_adjacent() {
10479 Token following = _skip("skipStringLiteral", "'a' 'b' +");
10480 expect(following, isNotNull);
10481 expect(following.type, TokenType.PLUS);
10482 }
10483
10484 void test_skipStringLiteral_interpolated() {
10485 Token following = _skip("skipStringLiteral", "'a\${b}c' +");
10486 expect(following, isNotNull);
10487 expect(following.type, TokenType.PLUS);
10488 }
10489
10490 void test_skipStringLiteral_invalid() {
10491 Token following = _skip("skipStringLiteral", "a");
10492 expect(following, isNull);
10493 }
10494
10495 void test_skipStringLiteral_single() {
10496 Token following = _skip("skipStringLiteral", "'a' +");
10497 expect(following, isNotNull);
10498 expect(following.type, TokenType.PLUS);
10499 }
10500
10501 void test_skipTypeArgumentList_invalid() {
10502 Token following = _skip("skipTypeArgumentList", "+");
10503 expect(following, isNull);
10504 }
10505
10506 void test_skipTypeArgumentList_multiple() {
10507 Token following = _skip("skipTypeArgumentList", "<E, F, G> +");
10508 expect(following, isNotNull);
10509 expect(following.type, TokenType.PLUS);
10510 }
10511
10512 void test_skipTypeArgumentList_single() {
10513 Token following = _skip("skipTypeArgumentList", "<E> +");
10514 expect(following, isNotNull);
10515 expect(following.type, TokenType.PLUS);
10516 }
10517
10518 void test_skipTypeName_invalid() {
10519 Token following = _skip("skipTypeName", "+");
10520 expect(following, isNull);
10521 }
10522
10523 void test_skipTypeName_parameterized() {
10524 Token following = _skip("skipTypeName", "C<E<F<G>>> +");
10525 expect(following, isNotNull);
10526 expect(following.type, TokenType.PLUS);
10527 }
10528
10529 void test_skipTypeName_simple() {
10530 Token following = _skip("skipTypeName", "C +");
10531 expect(following, isNotNull);
10532 expect(following.type, TokenType.PLUS);
10533 }
10534
10535 /**
10536 * Invoke the method [Parser.computeStringValue] with the given argument.
10537 *
10538 * @param lexeme the argument to the method
10539 * @param first `true` if this is the first token in a string literal
10540 * @param last `true` if this is the last token in a string literal
10541 * @return the result of invoking the method
10542 * @throws Exception if the method could not be invoked or throws an exception
10543 */
10544 String _computeStringValue(String lexeme, bool first, bool last) {
10545 AnalysisErrorListener listener =
10546 new AnalysisErrorListener_SimpleParserTest_computeStringValue();
10547 Parser parser = new Parser(null, listener);
10548 return invokeParserMethodImpl(
10549 parser, "computeStringValue", <Object>[lexeme, first, last], null)
10550 as String;
10551 }
10552
10553 /**
10554 * Invoke the method [Parser.createSyntheticIdentifier] with the parser set to the token
10555 * stream produced by scanning the given source.
10556 *
10557 * @param source the source to be scanned to produce the token stream being te sted
10558 * @return the result of invoking the method
10559 * @throws Exception if the method could not be invoked or throws an exception
10560 */
10561 SimpleIdentifier _createSyntheticIdentifier() {
10562 GatheringErrorListener listener = new GatheringErrorListener();
10563 return invokeParserMethod2("createSyntheticIdentifier", "", listener);
10564 }
10565
10566 /**
10567 * Invoke the method [Parser.createSyntheticIdentifier] with the parser set to the token
10568 * stream produced by scanning the given source.
10569 *
10570 * @param source the source to be scanned to produce the token stream being te sted
10571 * @return the result of invoking the method
10572 * @throws Exception if the method could not be invoked or throws an exception
10573 */
10574 SimpleStringLiteral _createSyntheticStringLiteral() {
10575 GatheringErrorListener listener = new GatheringErrorListener();
10576 return invokeParserMethod2("createSyntheticStringLiteral", "", listener);
10577 }
10578
10579 /**
10580 * Invoke the method [Parser.isFunctionDeclaration] with the parser set to the token
10581 * stream produced by scanning the given source.
10582 *
10583 * @param source the source to be scanned to produce the token stream being te sted
10584 * @return the result of invoking the method
10585 * @throws Exception if the method could not be invoked or throws an exception
10586 */
10587 bool _isFunctionDeclaration(String source) {
10588 GatheringErrorListener listener = new GatheringErrorListener();
10589 return invokeParserMethod2("isFunctionDeclaration", source, listener)
10590 as bool;
10591 }
10592
10593 /**
10594 * Invoke the method [Parser.isFunctionExpression] with the parser set to the token stream
10595 * produced by scanning the given source.
10596 *
10597 * @param source the source to be scanned to produce the token stream being te sted
10598 * @return the result of invoking the method
10599 * @throws Exception if the method could not be invoked or throws an exception
10600 */
10601 bool _isFunctionExpression(String source) {
10602 GatheringErrorListener listener = new GatheringErrorListener();
10603 //
10604 // Scan the source.
10605 //
10606 Scanner scanner =
10607 new Scanner(null, new CharSequenceReader(source), listener);
10608 Token tokenStream = scanner.tokenize();
10609 //
10610 // Parse the source.
10611 //
10612 Parser parser = new Parser(null, listener);
10613 return invokeParserMethodImpl(
10614 parser, "isFunctionExpression", <Object>[tokenStream], tokenStream)
10615 as bool;
10616 }
10617
10618 /**
10619 * Invoke the method [Parser.isInitializedVariableDeclaration] with the parser set to the
10620 * token stream produced by scanning the given source.
10621 *
10622 * @param source the source to be scanned to produce the token stream being te sted
10623 * @return the result of invoking the method
10624 * @throws Exception if the method could not be invoked or throws an exception
10625 */
10626 bool _isInitializedVariableDeclaration(String source) {
10627 GatheringErrorListener listener = new GatheringErrorListener();
10628 return invokeParserMethod2(
10629 "isInitializedVariableDeclaration", source, listener) as bool;
10630 }
10631
10632 /**
10633 * Invoke the method [Parser.isSwitchMember] with the parser set to the token stream
10634 * produced by scanning the given source.
10635 *
10636 * @param source the source to be scanned to produce the token stream being te sted
10637 * @return the result of invoking the method
10638 * @throws Exception if the method could not be invoked or throws an exception
10639 */
10640 bool _isSwitchMember(String source) {
10641 GatheringErrorListener listener = new GatheringErrorListener();
10642 return invokeParserMethod2("isSwitchMember", source, listener) as bool;
10643 }
10644
10645 /**
10646 * Parse the given source as a compilation unit.
10647 *
10648 * @param source the source to be parsed
10649 * @param errorCodes the error codes of the errors that are expected to be fou nd
10650 * @return the compilation unit that was parsed
10651 * @throws Exception if the source could not be parsed, if the compilation err ors in the source do
10652 * not match those that are expected, or if the result would have be en `null`
10653 */
10654 CompilationUnit _parseDirectives(String source,
10655 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) {
10656 GatheringErrorListener listener = new GatheringErrorListener();
10657 Scanner scanner =
10658 new Scanner(null, new CharSequenceReader(source), listener);
10659 listener.setLineInfo(new TestSource(), scanner.lineStarts);
10660 Token token = scanner.tokenize();
10661 Parser parser = new Parser(null, listener);
10662 CompilationUnit unit = parser.parseDirectives(token);
10663 expect(unit, isNotNull);
10664 expect(unit.declarations, hasLength(0));
10665 listener.assertErrorsWithCodes(errorCodes);
10666 return unit;
10667 }
10668
10669 /**
10670 * Invoke a "skip" method in [Parser]. The method is assumed to take a token a s it's
10671 * parameter and is given the first token in the scanned source.
10672 *
10673 * @param methodName the name of the method that should be invoked
10674 * @param source the source to be processed by the method
10675 * @return the result of invoking the method
10676 * @throws Exception if the method could not be invoked or throws an exception
10677 * @throws AssertionFailedError if the result is `null`
10678 */
10679 Token _skip(String methodName, String source) {
10680 GatheringErrorListener listener = new GatheringErrorListener();
10681 //
10682 // Scan the source.
10683 //
10684 Scanner scanner =
10685 new Scanner(null, new CharSequenceReader(source), listener);
10686 Token tokenStream = scanner.tokenize();
10687 //
10688 // Parse the source.
10689 //
10690 Parser parser = new Parser(null, listener);
10691 return invokeParserMethodImpl(
10692 parser, methodName, <Object>[tokenStream], tokenStream) as Token;
10693 }
10694 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/generated/non_error_resolver_test.dart ('k') | packages/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698