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

Side by Side Diff: packages/analyzer/test/generated/ast_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.ast_test;
6
7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/java_core.dart';
9 import 'package:analyzer/src/generated/java_engine.dart' show Predicate;
10 import 'package:analyzer/src/generated/java_engine.dart';
11 import 'package:analyzer/src/generated/scanner.dart';
12 import 'package:analyzer/src/generated/testing/ast_factory.dart';
13 import 'package:analyzer/src/generated/testing/token_factory.dart';
14 import 'package:unittest/unittest.dart';
15
16 import '../reflective_tests.dart';
17 import '../utils.dart';
18 import 'parser_test.dart' show ParserTestCase;
19 import 'test_support.dart';
20
21 main() {
22 initializeTestEnvironment();
23 runReflectiveTests(BreadthFirstVisitorTest);
24 runReflectiveTests(ClassDeclarationTest);
25 runReflectiveTests(ClassTypeAliasTest);
26 runReflectiveTests(ConstantEvaluatorTest);
27 runReflectiveTests(ConstructorDeclarationTest);
28 runReflectiveTests(FieldFormalParameterTest);
29 runReflectiveTests(IndexExpressionTest);
30 runReflectiveTests(NodeListTest);
31 runReflectiveTests(NodeLocatorTest);
32 runReflectiveTests(SimpleIdentifierTest);
33 runReflectiveTests(SimpleStringLiteralTest);
34 runReflectiveTests(StringInterpolationTest);
35 runReflectiveTests(ToSourceVisitorTest);
36 runReflectiveTests(VariableDeclarationTest);
37 }
38
39 class AssignmentKind extends Enum<AssignmentKind> {
40 static const AssignmentKind BINARY = const AssignmentKind('BINARY', 0);
41
42 static const AssignmentKind COMPOUND_LEFT =
43 const AssignmentKind('COMPOUND_LEFT', 1);
44
45 static const AssignmentKind COMPOUND_RIGHT =
46 const AssignmentKind('COMPOUND_RIGHT', 2);
47
48 static const AssignmentKind POSTFIX_INC =
49 const AssignmentKind('POSTFIX_INC', 3);
50
51 static const AssignmentKind PREFIX_DEC =
52 const AssignmentKind('PREFIX_DEC', 4);
53
54 static const AssignmentKind PREFIX_INC =
55 const AssignmentKind('PREFIX_INC', 5);
56
57 static const AssignmentKind PREFIX_NOT =
58 const AssignmentKind('PREFIX_NOT', 6);
59
60 static const AssignmentKind SIMPLE_LEFT =
61 const AssignmentKind('SIMPLE_LEFT', 7);
62
63 static const AssignmentKind SIMPLE_RIGHT =
64 const AssignmentKind('SIMPLE_RIGHT', 8);
65
66 static const AssignmentKind NONE = const AssignmentKind('NONE', 9);
67
68 static const List<AssignmentKind> values = const [
69 BINARY,
70 COMPOUND_LEFT,
71 COMPOUND_RIGHT,
72 POSTFIX_INC,
73 PREFIX_DEC,
74 PREFIX_INC,
75 PREFIX_NOT,
76 SIMPLE_LEFT,
77 SIMPLE_RIGHT,
78 NONE
79 ];
80
81 const AssignmentKind(String name, int ordinal) : super(name, ordinal);
82 }
83
84 class BreadthFirstVisitor_BreadthFirstVisitorTest_testIt
85 extends BreadthFirstVisitor<Object> {
86 List<AstNode> nodes;
87
88 BreadthFirstVisitor_BreadthFirstVisitorTest_testIt(this.nodes) : super();
89
90 @override
91 Object visitNode(AstNode node) {
92 nodes.add(node);
93 return super.visitNode(node);
94 }
95 }
96
97 @reflectiveTest
98 class BreadthFirstVisitorTest extends ParserTestCase {
99 void test_it() {
100 String source = r'''
101 class A {
102 bool get g => true;
103 }
104 class B {
105 int f() {
106 num q() {
107 return 3;
108 }
109 return q() + 4;
110 }
111 }
112 A f(var p) {
113 if ((p as A).g) {
114 return p;
115 } else {
116 return null;
117 }
118 }''';
119 CompilationUnit unit = ParserTestCase.parseCompilationUnit(source);
120 List<AstNode> nodes = new List<AstNode>();
121 BreadthFirstVisitor<Object> visitor =
122 new BreadthFirstVisitor_BreadthFirstVisitorTest_testIt(nodes);
123 visitor.visitAllNodes(unit);
124 expect(nodes, hasLength(59));
125 EngineTestCase.assertInstanceOf(
126 (obj) => obj is CompilationUnit, CompilationUnit, nodes[0]);
127 EngineTestCase.assertInstanceOf(
128 (obj) => obj is ClassDeclaration, ClassDeclaration, nodes[2]);
129 EngineTestCase.assertInstanceOf(
130 (obj) => obj is FunctionDeclaration, FunctionDeclaration, nodes[3]);
131 EngineTestCase.assertInstanceOf(
132 (obj) => obj is FunctionDeclarationStatement,
133 FunctionDeclarationStatement,
134 nodes[27]);
135 EngineTestCase.assertInstanceOf(
136 (obj) => obj is IntegerLiteral, IntegerLiteral, nodes[58]);
137 //3
138 }
139 }
140
141 @reflectiveTest
142 class ClassDeclarationTest extends ParserTestCase {
143 void test_getConstructor() {
144 List<ConstructorInitializer> initializers =
145 new List<ConstructorInitializer>();
146 ConstructorDeclaration defaultConstructor = AstFactory
147 .constructorDeclaration(AstFactory.identifier3("Test"), null,
148 AstFactory.formalParameterList(), initializers);
149 ConstructorDeclaration aConstructor = AstFactory.constructorDeclaration(
150 AstFactory.identifier3("Test"),
151 "a",
152 AstFactory.formalParameterList(),
153 initializers);
154 ConstructorDeclaration bConstructor = AstFactory.constructorDeclaration(
155 AstFactory.identifier3("Test"),
156 "b",
157 AstFactory.formalParameterList(),
158 initializers);
159 ClassDeclaration clazz = AstFactory.classDeclaration(null, "Test", null,
160 null, null, null, [defaultConstructor, aConstructor, bConstructor]);
161 expect(clazz.getConstructor(null), same(defaultConstructor));
162 expect(clazz.getConstructor("a"), same(aConstructor));
163 expect(clazz.getConstructor("b"), same(bConstructor));
164 expect(clazz.getConstructor("noSuchConstructor"), same(null));
165 }
166
167 void test_getField() {
168 VariableDeclaration aVar = AstFactory.variableDeclaration("a");
169 VariableDeclaration bVar = AstFactory.variableDeclaration("b");
170 VariableDeclaration cVar = AstFactory.variableDeclaration("c");
171 ClassDeclaration clazz =
172 AstFactory.classDeclaration(null, "Test", null, null, null, null, [
173 AstFactory.fieldDeclaration2(false, null, [aVar]),
174 AstFactory.fieldDeclaration2(false, null, [bVar, cVar])
175 ]);
176 expect(clazz.getField("a"), same(aVar));
177 expect(clazz.getField("b"), same(bVar));
178 expect(clazz.getField("c"), same(cVar));
179 expect(clazz.getField("noSuchField"), same(null));
180 }
181
182 void test_getMethod() {
183 MethodDeclaration aMethod = AstFactory.methodDeclaration(null, null, null,
184 null, AstFactory.identifier3("a"), AstFactory.formalParameterList());
185 MethodDeclaration bMethod = AstFactory.methodDeclaration(null, null, null,
186 null, AstFactory.identifier3("b"), AstFactory.formalParameterList());
187 ClassDeclaration clazz = AstFactory.classDeclaration(
188 null, "Test", null, null, null, null, [aMethod, bMethod]);
189 expect(clazz.getMethod("a"), same(aMethod));
190 expect(clazz.getMethod("b"), same(bMethod));
191 expect(clazz.getMethod("noSuchMethod"), same(null));
192 }
193
194 void test_isAbstract() {
195 expect(
196 AstFactory
197 .classDeclaration(null, "A", null, null, null, null)
198 .isAbstract,
199 isFalse);
200 expect(
201 AstFactory
202 .classDeclaration(Keyword.ABSTRACT, "B", null, null, null, null)
203 .isAbstract,
204 isTrue);
205 }
206 }
207
208 @reflectiveTest
209 class ClassTypeAliasTest extends ParserTestCase {
210 void test_isAbstract() {
211 expect(
212 AstFactory.classTypeAlias("A", null, null, null, null, null).isAbstract,
213 isFalse);
214 expect(
215 AstFactory
216 .classTypeAlias("B", null, Keyword.ABSTRACT, null, null, null)
217 .isAbstract,
218 isTrue);
219 }
220 }
221
222 @reflectiveTest
223 class ConstantEvaluatorTest extends ParserTestCase {
224 void fail_constructor() {
225 Object value = _getConstantValue("?");
226 expect(value, null);
227 }
228
229 void fail_identifier_class() {
230 Object value = _getConstantValue("?");
231 expect(value, null);
232 }
233
234 void fail_identifier_function() {
235 Object value = _getConstantValue("?");
236 expect(value, null);
237 }
238
239 void fail_identifier_static() {
240 Object value = _getConstantValue("?");
241 expect(value, null);
242 }
243
244 void fail_identifier_staticMethod() {
245 Object value = _getConstantValue("?");
246 expect(value, null);
247 }
248
249 void fail_identifier_topLevel() {
250 Object value = _getConstantValue("?");
251 expect(value, null);
252 }
253
254 void fail_identifier_typeParameter() {
255 Object value = _getConstantValue("?");
256 expect(value, null);
257 }
258
259 void test_binary_bitAnd() {
260 Object value = _getConstantValue("74 & 42");
261 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
262 expect(value as int, 74 & 42);
263 }
264
265 void test_binary_bitOr() {
266 Object value = _getConstantValue("74 | 42");
267 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
268 expect(value as int, 74 | 42);
269 }
270
271 void test_binary_bitXor() {
272 Object value = _getConstantValue("74 ^ 42");
273 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
274 expect(value as int, 74 ^ 42);
275 }
276
277 void test_binary_divide_double() {
278 Object value = _getConstantValue("3.2 / 2.3");
279 expect(value, 3.2 / 2.3);
280 }
281
282 void test_binary_divide_integer() {
283 Object value = _getConstantValue("3 / 2");
284 expect(value, 1.5);
285 }
286
287 void test_binary_equal_boolean() {
288 Object value = _getConstantValue("true == false");
289 expect(value, false);
290 }
291
292 void test_binary_equal_integer() {
293 Object value = _getConstantValue("2 == 3");
294 expect(value, false);
295 }
296
297 void test_binary_equal_invalidLeft() {
298 Object value = _getConstantValue("a == 3");
299 expect(value, ConstantEvaluator.NOT_A_CONSTANT);
300 }
301
302 void test_binary_equal_invalidRight() {
303 Object value = _getConstantValue("2 == a");
304 expect(value, ConstantEvaluator.NOT_A_CONSTANT);
305 }
306
307 void test_binary_equal_string() {
308 Object value = _getConstantValue("'a' == 'b'");
309 expect(value, false);
310 }
311
312 void test_binary_greaterThan() {
313 Object value = _getConstantValue("2 > 3");
314 expect(value, false);
315 }
316
317 void test_binary_greaterThanOrEqual() {
318 Object value = _getConstantValue("2 >= 3");
319 expect(value, false);
320 }
321
322 void test_binary_leftShift() {
323 Object value = _getConstantValue("16 << 2");
324 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
325 expect(value as int, 64);
326 }
327
328 void test_binary_lessThan() {
329 Object value = _getConstantValue("2 < 3");
330 expect(value, true);
331 }
332
333 void test_binary_lessThanOrEqual() {
334 Object value = _getConstantValue("2 <= 3");
335 expect(value, true);
336 }
337
338 void test_binary_logicalAnd() {
339 Object value = _getConstantValue("true && false");
340 expect(value, false);
341 }
342
343 void test_binary_logicalOr() {
344 Object value = _getConstantValue("true || false");
345 expect(value, true);
346 }
347
348 void test_binary_minus_double() {
349 Object value = _getConstantValue("3.2 - 2.3");
350 expect(value, 3.2 - 2.3);
351 }
352
353 void test_binary_minus_integer() {
354 Object value = _getConstantValue("3 - 2");
355 expect(value, 1);
356 }
357
358 void test_binary_notEqual_boolean() {
359 Object value = _getConstantValue("true != false");
360 expect(value, true);
361 }
362
363 void test_binary_notEqual_integer() {
364 Object value = _getConstantValue("2 != 3");
365 expect(value, true);
366 }
367
368 void test_binary_notEqual_invalidLeft() {
369 Object value = _getConstantValue("a != 3");
370 expect(value, ConstantEvaluator.NOT_A_CONSTANT);
371 }
372
373 void test_binary_notEqual_invalidRight() {
374 Object value = _getConstantValue("2 != a");
375 expect(value, ConstantEvaluator.NOT_A_CONSTANT);
376 }
377
378 void test_binary_notEqual_string() {
379 Object value = _getConstantValue("'a' != 'b'");
380 expect(value, true);
381 }
382
383 void test_binary_plus_double() {
384 Object value = _getConstantValue("2.3 + 3.2");
385 expect(value, 2.3 + 3.2);
386 }
387
388 void test_binary_plus_integer() {
389 Object value = _getConstantValue("2 + 3");
390 expect(value, 5);
391 }
392
393 void test_binary_remainder_double() {
394 Object value = _getConstantValue("3.2 % 2.3");
395 expect(value, 3.2 % 2.3);
396 }
397
398 void test_binary_remainder_integer() {
399 Object value = _getConstantValue("8 % 3");
400 expect(value, 2);
401 }
402
403 void test_binary_rightShift() {
404 Object value = _getConstantValue("64 >> 2");
405 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
406 expect(value as int, 16);
407 }
408
409 void test_binary_times_double() {
410 Object value = _getConstantValue("2.3 * 3.2");
411 expect(value, 2.3 * 3.2);
412 }
413
414 void test_binary_times_integer() {
415 Object value = _getConstantValue("2 * 3");
416 expect(value, 6);
417 }
418
419 void test_binary_truncatingDivide_double() {
420 Object value = _getConstantValue("3.2 ~/ 2.3");
421 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
422 expect(value as int, 1);
423 }
424
425 void test_binary_truncatingDivide_integer() {
426 Object value = _getConstantValue("10 ~/ 3");
427 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
428 expect(value as int, 3);
429 }
430
431 void test_literal_boolean_false() {
432 Object value = _getConstantValue("false");
433 expect(value, false);
434 }
435
436 void test_literal_boolean_true() {
437 Object value = _getConstantValue("true");
438 expect(value, true);
439 }
440
441 void test_literal_list() {
442 Object value = _getConstantValue("['a', 'b', 'c']");
443 EngineTestCase.assertInstanceOf((obj) => obj is List, List, value);
444 List list = value as List;
445 expect(list.length, 3);
446 expect(list[0], "a");
447 expect(list[1], "b");
448 expect(list[2], "c");
449 }
450
451 void test_literal_map() {
452 Object value = _getConstantValue("{'a' : 'm', 'b' : 'n', 'c' : 'o'}");
453 EngineTestCase.assertInstanceOf((obj) => obj is Map, Map, value);
454 Map map = value as Map;
455 expect(map.length, 3);
456 expect(map["a"], "m");
457 expect(map["b"], "n");
458 expect(map["c"], "o");
459 }
460
461 void test_literal_null() {
462 Object value = _getConstantValue("null");
463 expect(value, null);
464 }
465
466 void test_literal_number_double() {
467 Object value = _getConstantValue("3.45");
468 expect(value, 3.45);
469 }
470
471 void test_literal_number_integer() {
472 Object value = _getConstantValue("42");
473 expect(value, 42);
474 }
475
476 void test_literal_string_adjacent() {
477 Object value = _getConstantValue("'abc' 'def'");
478 expect(value, "abcdef");
479 }
480
481 void test_literal_string_interpolation_invalid() {
482 Object value = _getConstantValue("'a\${f()}c'");
483 expect(value, ConstantEvaluator.NOT_A_CONSTANT);
484 }
485
486 void test_literal_string_interpolation_valid() {
487 Object value = _getConstantValue("'a\${3}c'");
488 expect(value, "a3c");
489 }
490
491 void test_literal_string_simple() {
492 Object value = _getConstantValue("'abc'");
493 expect(value, "abc");
494 }
495
496 void test_parenthesizedExpression() {
497 Object value = _getConstantValue("('a')");
498 expect(value, "a");
499 }
500
501 void test_unary_bitNot() {
502 Object value = _getConstantValue("~42");
503 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value);
504 expect(value as int, ~42);
505 }
506
507 void test_unary_logicalNot() {
508 Object value = _getConstantValue("!true");
509 expect(value, false);
510 }
511
512 void test_unary_negated_double() {
513 Object value = _getConstantValue("-42.3");
514 expect(value, -42.3);
515 }
516
517 void test_unary_negated_integer() {
518 Object value = _getConstantValue("-42");
519 expect(value, -42);
520 }
521
522 Object _getConstantValue(String source) =>
523 parseExpression(source).accept(new ConstantEvaluator());
524 }
525
526 @reflectiveTest
527 class ConstructorDeclarationTest extends EngineTestCase {
528 void test_firstTokenAfterCommentAndMetadata_all_inverted() {
529 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
530 externalKeyword.offset = 14;
531 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
532 Keyword.CONST,
533 Keyword.FACTORY,
534 AstFactory.identifier3('int'),
535 null,
536 null,
537 null,
538 null);
539 declaration.externalKeyword = externalKeyword;
540 declaration.constKeyword.offset = 8;
541 Token factoryKeyword = declaration.factoryKeyword;
542 factoryKeyword.offset = 0;
543 expect(declaration.firstTokenAfterCommentAndMetadata, factoryKeyword);
544 }
545
546 void test_firstTokenAfterCommentAndMetadata_all_normal() {
547 Token token = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
548 token.offset = 0;
549 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
550 Keyword.CONST,
551 Keyword.FACTORY,
552 AstFactory.identifier3('int'),
553 null,
554 null,
555 null,
556 null);
557 declaration.externalKeyword = token;
558 declaration.constKeyword.offset = 9;
559 declaration.factoryKeyword.offset = 15;
560 expect(declaration.firstTokenAfterCommentAndMetadata, token);
561 }
562
563 void test_firstTokenAfterCommentAndMetadata_constOnly() {
564 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
565 Keyword.CONST,
566 null,
567 AstFactory.identifier3('int'),
568 null,
569 null,
570 null,
571 null);
572 expect(declaration.firstTokenAfterCommentAndMetadata,
573 declaration.constKeyword);
574 }
575
576 void test_firstTokenAfterCommentAndMetadata_externalOnly() {
577 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
578 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
579 null, null, AstFactory.identifier3('int'), null, null, null, null);
580 declaration.externalKeyword = externalKeyword;
581 expect(declaration.firstTokenAfterCommentAndMetadata, externalKeyword);
582 }
583
584 void test_firstTokenAfterCommentAndMetadata_factoryOnly() {
585 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
586 null,
587 Keyword.FACTORY,
588 AstFactory.identifier3('int'),
589 null,
590 null,
591 null,
592 null);
593 expect(declaration.firstTokenAfterCommentAndMetadata,
594 declaration.factoryKeyword);
595 }
596 }
597
598 @reflectiveTest
599 class FieldFormalParameterTest extends EngineTestCase {
600 void test_endToken_noParameters() {
601 FieldFormalParameter parameter = AstFactory.fieldFormalParameter2('field');
602 expect(parameter.endToken, parameter.identifier.endToken);
603 }
604
605 void test_endToken_parameters() {
606 FieldFormalParameter parameter = AstFactory.fieldFormalParameter(
607 null, null, 'field', AstFactory.formalParameterList([]));
608 expect(parameter.endToken, parameter.parameters.endToken);
609 }
610 }
611
612 @reflectiveTest
613 class IndexExpressionTest extends EngineTestCase {
614 void test_inGetterContext_assignment_compound_left() {
615 IndexExpression expression = AstFactory.indexExpression(
616 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
617 // a[b] += c
618 AstFactory.assignmentExpression(
619 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c"));
620 expect(expression.inGetterContext(), isTrue);
621 }
622
623 void test_inGetterContext_assignment_simple_left() {
624 IndexExpression expression = AstFactory.indexExpression(
625 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
626 // a[b] = c
627 AstFactory.assignmentExpression(
628 expression, TokenType.EQ, AstFactory.identifier3("c"));
629 expect(expression.inGetterContext(), isFalse);
630 }
631
632 void test_inGetterContext_nonAssignment() {
633 IndexExpression expression = AstFactory.indexExpression(
634 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
635 // a[b] + c
636 AstFactory.binaryExpression(
637 expression, TokenType.PLUS, AstFactory.identifier3("c"));
638 expect(expression.inGetterContext(), isTrue);
639 }
640
641 void test_inSetterContext_assignment_compound_left() {
642 IndexExpression expression = AstFactory.indexExpression(
643 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
644 // a[b] += c
645 AstFactory.assignmentExpression(
646 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c"));
647 expect(expression.inSetterContext(), isTrue);
648 }
649
650 void test_inSetterContext_assignment_compound_right() {
651 IndexExpression expression = AstFactory.indexExpression(
652 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
653 // c += a[b]
654 AstFactory.assignmentExpression(
655 AstFactory.identifier3("c"), TokenType.PLUS_EQ, expression);
656 expect(expression.inSetterContext(), isFalse);
657 }
658
659 void test_inSetterContext_assignment_simple_left() {
660 IndexExpression expression = AstFactory.indexExpression(
661 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
662 // a[b] = c
663 AstFactory.assignmentExpression(
664 expression, TokenType.EQ, AstFactory.identifier3("c"));
665 expect(expression.inSetterContext(), isTrue);
666 }
667
668 void test_inSetterContext_assignment_simple_right() {
669 IndexExpression expression = AstFactory.indexExpression(
670 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
671 // c = a[b]
672 AstFactory.assignmentExpression(
673 AstFactory.identifier3("c"), TokenType.EQ, expression);
674 expect(expression.inSetterContext(), isFalse);
675 }
676
677 void test_inSetterContext_nonAssignment() {
678 IndexExpression expression = AstFactory.indexExpression(
679 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
680 AstFactory.binaryExpression(
681 expression, TokenType.PLUS, AstFactory.identifier3("c"));
682 // a[b] + cc
683 expect(expression.inSetterContext(), isFalse);
684 }
685
686 void test_inSetterContext_postfix() {
687 IndexExpression expression = AstFactory.indexExpression(
688 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
689 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS);
690 // a[b]++
691 expect(expression.inSetterContext(), isTrue);
692 }
693
694 void test_inSetterContext_prefix_bang() {
695 IndexExpression expression = AstFactory.indexExpression(
696 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
697 // !a[b]
698 AstFactory.prefixExpression(TokenType.BANG, expression);
699 expect(expression.inSetterContext(), isFalse);
700 }
701
702 void test_inSetterContext_prefix_minusMinus() {
703 IndexExpression expression = AstFactory.indexExpression(
704 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
705 // --a[b]
706 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression);
707 expect(expression.inSetterContext(), isTrue);
708 }
709
710 void test_inSetterContext_prefix_plusPlus() {
711 IndexExpression expression = AstFactory.indexExpression(
712 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
713 // ++a[b]
714 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression);
715 expect(expression.inSetterContext(), isTrue);
716 }
717 }
718
719 @reflectiveTest
720 class NodeListTest extends EngineTestCase {
721 void test_add() {
722 AstNode parent = AstFactory.argumentList();
723 AstNode firstNode = AstFactory.booleanLiteral(true);
724 AstNode secondNode = AstFactory.booleanLiteral(false);
725 NodeList<AstNode> list = new NodeList<AstNode>(parent);
726 list.insert(0, secondNode);
727 list.insert(0, firstNode);
728 expect(list, hasLength(2));
729 expect(list[0], same(firstNode));
730 expect(list[1], same(secondNode));
731 expect(firstNode.parent, same(parent));
732 expect(secondNode.parent, same(parent));
733 AstNode thirdNode = AstFactory.booleanLiteral(false);
734 list.insert(1, thirdNode);
735 expect(list, hasLength(3));
736 expect(list[0], same(firstNode));
737 expect(list[1], same(thirdNode));
738 expect(list[2], same(secondNode));
739 expect(firstNode.parent, same(parent));
740 expect(secondNode.parent, same(parent));
741 expect(thirdNode.parent, same(parent));
742 }
743
744 void test_add_negative() {
745 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
746 try {
747 list.insert(-1, AstFactory.booleanLiteral(true));
748 fail("Expected IndexOutOfBoundsException");
749 } on RangeError {
750 // Expected
751 }
752 }
753
754 void test_add_tooBig() {
755 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
756 try {
757 list.insert(1, AstFactory.booleanLiteral(true));
758 fail("Expected IndexOutOfBoundsException");
759 } on RangeError {
760 // Expected
761 }
762 }
763
764 void test_addAll() {
765 AstNode parent = AstFactory.argumentList();
766 List<AstNode> firstNodes = new List<AstNode>();
767 AstNode firstNode = AstFactory.booleanLiteral(true);
768 AstNode secondNode = AstFactory.booleanLiteral(false);
769 firstNodes.add(firstNode);
770 firstNodes.add(secondNode);
771 NodeList<AstNode> list = new NodeList<AstNode>(parent);
772 list.addAll(firstNodes);
773 expect(list, hasLength(2));
774 expect(list[0], same(firstNode));
775 expect(list[1], same(secondNode));
776 expect(firstNode.parent, same(parent));
777 expect(secondNode.parent, same(parent));
778 List<AstNode> secondNodes = new List<AstNode>();
779 AstNode thirdNode = AstFactory.booleanLiteral(true);
780 AstNode fourthNode = AstFactory.booleanLiteral(false);
781 secondNodes.add(thirdNode);
782 secondNodes.add(fourthNode);
783 list.addAll(secondNodes);
784 expect(list, hasLength(4));
785 expect(list[0], same(firstNode));
786 expect(list[1], same(secondNode));
787 expect(list[2], same(thirdNode));
788 expect(list[3], same(fourthNode));
789 expect(firstNode.parent, same(parent));
790 expect(secondNode.parent, same(parent));
791 expect(thirdNode.parent, same(parent));
792 expect(fourthNode.parent, same(parent));
793 }
794
795 void test_creation() {
796 AstNode owner = AstFactory.argumentList();
797 NodeList<AstNode> list = new NodeList<AstNode>(owner);
798 expect(list, isNotNull);
799 expect(list, hasLength(0));
800 expect(list.owner, same(owner));
801 }
802
803 void test_get_negative() {
804 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
805 try {
806 list[-1];
807 fail("Expected IndexOutOfBoundsException");
808 } on RangeError {
809 // Expected
810 }
811 }
812
813 void test_get_tooBig() {
814 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
815 try {
816 list[1];
817 fail("Expected IndexOutOfBoundsException");
818 } on RangeError {
819 // Expected
820 }
821 }
822
823 void test_getBeginToken_empty() {
824 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
825 expect(list.beginToken, isNull);
826 }
827
828 void test_getBeginToken_nonEmpty() {
829 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
830 AstNode node =
831 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true));
832 list.add(node);
833 expect(list.beginToken, same(node.beginToken));
834 }
835
836 void test_getEndToken_empty() {
837 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
838 expect(list.endToken, isNull);
839 }
840
841 void test_getEndToken_nonEmpty() {
842 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
843 AstNode node =
844 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true));
845 list.add(node);
846 expect(list.endToken, same(node.endToken));
847 }
848
849 void test_indexOf() {
850 List<AstNode> nodes = new List<AstNode>();
851 AstNode firstNode = AstFactory.booleanLiteral(true);
852 AstNode secondNode = AstFactory.booleanLiteral(false);
853 AstNode thirdNode = AstFactory.booleanLiteral(true);
854 AstNode fourthNode = AstFactory.booleanLiteral(false);
855 nodes.add(firstNode);
856 nodes.add(secondNode);
857 nodes.add(thirdNode);
858 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
859 list.addAll(nodes);
860 expect(list, hasLength(3));
861 expect(list.indexOf(firstNode), 0);
862 expect(list.indexOf(secondNode), 1);
863 expect(list.indexOf(thirdNode), 2);
864 expect(list.indexOf(fourthNode), -1);
865 expect(list.indexOf(null), -1);
866 }
867
868 void test_remove() {
869 List<AstNode> nodes = new List<AstNode>();
870 AstNode firstNode = AstFactory.booleanLiteral(true);
871 AstNode secondNode = AstFactory.booleanLiteral(false);
872 AstNode thirdNode = AstFactory.booleanLiteral(true);
873 nodes.add(firstNode);
874 nodes.add(secondNode);
875 nodes.add(thirdNode);
876 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
877 list.addAll(nodes);
878 expect(list, hasLength(3));
879 expect(list.removeAt(1), same(secondNode));
880 expect(list, hasLength(2));
881 expect(list[0], same(firstNode));
882 expect(list[1], same(thirdNode));
883 }
884
885 void test_remove_negative() {
886 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
887 try {
888 list.removeAt(-1);
889 fail("Expected IndexOutOfBoundsException");
890 } on RangeError {
891 // Expected
892 }
893 }
894
895 void test_remove_tooBig() {
896 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
897 try {
898 list.removeAt(1);
899 fail("Expected IndexOutOfBoundsException");
900 } on RangeError {
901 // Expected
902 }
903 }
904
905 void test_set() {
906 List<AstNode> nodes = new List<AstNode>();
907 AstNode firstNode = AstFactory.booleanLiteral(true);
908 AstNode secondNode = AstFactory.booleanLiteral(false);
909 AstNode thirdNode = AstFactory.booleanLiteral(true);
910 nodes.add(firstNode);
911 nodes.add(secondNode);
912 nodes.add(thirdNode);
913 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
914 list.addAll(nodes);
915 expect(list, hasLength(3));
916 AstNode fourthNode = AstFactory.integer(0);
917 expect(javaListSet(list, 1, fourthNode), same(secondNode));
918 expect(list, hasLength(3));
919 expect(list[0], same(firstNode));
920 expect(list[1], same(fourthNode));
921 expect(list[2], same(thirdNode));
922 }
923
924 void test_set_negative() {
925 AstNode node = AstFactory.booleanLiteral(true);
926 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
927 try {
928 javaListSet(list, -1, node);
929 fail("Expected IndexOutOfBoundsException");
930 } on RangeError {
931 // Expected
932 }
933 }
934
935 void test_set_tooBig() {
936 AstNode node = AstFactory.booleanLiteral(true);
937 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
938 try {
939 javaListSet(list, 1, node);
940 fail("Expected IndexOutOfBoundsException");
941 } on RangeError {
942 // Expected
943 }
944 }
945 }
946
947 @reflectiveTest
948 class NodeLocatorTest extends ParserTestCase {
949 void test_range() {
950 CompilationUnit unit =
951 ParserTestCase.parseCompilationUnit("library myLib;");
952 _assertLocate(
953 unit, 4, 10, (node) => node is LibraryDirective, LibraryDirective);
954 }
955
956 void test_searchWithin_null() {
957 NodeLocator locator = new NodeLocator(0, 0);
958 expect(locator.searchWithin(null), isNull);
959 }
960
961 void test_searchWithin_offset() {
962 CompilationUnit unit =
963 ParserTestCase.parseCompilationUnit("library myLib;");
964 _assertLocate(
965 unit, 10, 10, (node) => node is SimpleIdentifier, SimpleIdentifier);
966 }
967
968 void test_searchWithin_offsetAfterNode() {
969 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r'''
970 class A {}
971 class B {}''');
972 NodeLocator locator = new NodeLocator(1024, 1024);
973 AstNode node = locator.searchWithin(unit.declarations[0]);
974 expect(node, isNull);
975 }
976
977 void test_searchWithin_offsetBeforeNode() {
978 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r'''
979 class A {}
980 class B {}''');
981 NodeLocator locator = new NodeLocator(0, 0);
982 AstNode node = locator.searchWithin(unit.declarations[1]);
983 expect(node, isNull);
984 }
985
986 void _assertLocate(CompilationUnit unit, int start, int end,
987 Predicate<AstNode> predicate, Type expectedClass) {
988 NodeLocator locator = new NodeLocator(start, end);
989 AstNode node = locator.searchWithin(unit);
990 expect(node, isNotNull);
991 expect(locator.foundNode, same(node));
992 expect(node.offset <= start, isTrue, reason: "Node starts after range");
993 expect(node.offset + node.length > end, isTrue,
994 reason: "Node ends before range");
995 EngineTestCase.assertInstanceOf(predicate, expectedClass, node);
996 }
997 }
998
999 @reflectiveTest
1000 class SimpleIdentifierTest extends ParserTestCase {
1001 void test_inDeclarationContext_catch_exception() {
1002 SimpleIdentifier identifier =
1003 AstFactory.catchClause("e").exceptionParameter;
1004 expect(identifier.inDeclarationContext(), isTrue);
1005 }
1006
1007 void test_inDeclarationContext_catch_stack() {
1008 SimpleIdentifier identifier =
1009 AstFactory.catchClause2("e", "s").stackTraceParameter;
1010 expect(identifier.inDeclarationContext(), isTrue);
1011 }
1012
1013 void test_inDeclarationContext_classDeclaration() {
1014 SimpleIdentifier identifier =
1015 AstFactory.classDeclaration(null, "C", null, null, null, null).name;
1016 expect(identifier.inDeclarationContext(), isTrue);
1017 }
1018
1019 void test_inDeclarationContext_classTypeAlias() {
1020 SimpleIdentifier identifier =
1021 AstFactory.classTypeAlias("C", null, null, null, null, null).name;
1022 expect(identifier.inDeclarationContext(), isTrue);
1023 }
1024
1025 void test_inDeclarationContext_constructorDeclaration() {
1026 SimpleIdentifier identifier = AstFactory
1027 .constructorDeclaration(AstFactory.identifier3("C"), "c", null, null)
1028 .name;
1029 expect(identifier.inDeclarationContext(), isTrue);
1030 }
1031
1032 void test_inDeclarationContext_declaredIdentifier() {
1033 DeclaredIdentifier declaredIdentifier = AstFactory.declaredIdentifier3("v");
1034 SimpleIdentifier identifier = declaredIdentifier.identifier;
1035 expect(identifier.inDeclarationContext(), isTrue);
1036 }
1037
1038 void test_inDeclarationContext_enumConstantDeclaration() {
1039 EnumDeclaration enumDeclaration =
1040 AstFactory.enumDeclaration2('MyEnum', ['CONST']);
1041 SimpleIdentifier identifier = enumDeclaration.constants[0].name;
1042 expect(identifier.inDeclarationContext(), isTrue);
1043 }
1044
1045 void test_inDeclarationContext_enumDeclaration() {
1046 EnumDeclaration enumDeclaration =
1047 AstFactory.enumDeclaration2('MyEnum', ['A', 'B', 'C']);
1048 SimpleIdentifier identifier = enumDeclaration.name;
1049 expect(identifier.inDeclarationContext(), isTrue);
1050 }
1051
1052 void test_inDeclarationContext_fieldFormalParameter() {
1053 SimpleIdentifier identifier =
1054 AstFactory.fieldFormalParameter2("p").identifier;
1055 expect(identifier.inDeclarationContext(), isFalse);
1056 }
1057
1058 void test_inDeclarationContext_functionDeclaration() {
1059 SimpleIdentifier identifier =
1060 AstFactory.functionDeclaration(null, null, "f", null).name;
1061 expect(identifier.inDeclarationContext(), isTrue);
1062 }
1063
1064 void test_inDeclarationContext_functionTypeAlias() {
1065 SimpleIdentifier identifier =
1066 AstFactory.typeAlias(null, "F", null, null).name;
1067 expect(identifier.inDeclarationContext(), isTrue);
1068 }
1069
1070 void test_inDeclarationContext_label_false() {
1071 SimpleIdentifier identifier =
1072 AstFactory.namedExpression2("l", AstFactory.integer(0)).name.label;
1073 expect(identifier.inDeclarationContext(), isFalse);
1074 }
1075
1076 void test_inDeclarationContext_label_true() {
1077 Label label = AstFactory.label2("l");
1078 SimpleIdentifier identifier = label.label;
1079 AstFactory.labeledStatement([label], AstFactory.emptyStatement());
1080 expect(identifier.inDeclarationContext(), isTrue);
1081 }
1082
1083 void test_inDeclarationContext_methodDeclaration() {
1084 SimpleIdentifier identifier = AstFactory.identifier3("m");
1085 AstFactory.methodDeclaration2(
1086 null, null, null, null, identifier, null, null);
1087 expect(identifier.inDeclarationContext(), isTrue);
1088 }
1089
1090 void test_inDeclarationContext_prefix() {
1091 SimpleIdentifier identifier =
1092 AstFactory.importDirective3("uri", "pref").prefix;
1093 expect(identifier.inDeclarationContext(), isTrue);
1094 }
1095
1096 void test_inDeclarationContext_simpleFormalParameter() {
1097 SimpleIdentifier identifier =
1098 AstFactory.simpleFormalParameter3("p").identifier;
1099 expect(identifier.inDeclarationContext(), isTrue);
1100 }
1101
1102 void test_inDeclarationContext_typeParameter_bound() {
1103 TypeName bound = AstFactory.typeName4("A");
1104 SimpleIdentifier identifier = bound.name as SimpleIdentifier;
1105 AstFactory.typeParameter2("E", bound);
1106 expect(identifier.inDeclarationContext(), isFalse);
1107 }
1108
1109 void test_inDeclarationContext_typeParameter_name() {
1110 SimpleIdentifier identifier = AstFactory.typeParameter("E").name;
1111 expect(identifier.inDeclarationContext(), isTrue);
1112 }
1113
1114 void test_inDeclarationContext_variableDeclaration() {
1115 SimpleIdentifier identifier = AstFactory.variableDeclaration("v").name;
1116 expect(identifier.inDeclarationContext(), isTrue);
1117 }
1118
1119 void test_inGetterContext() {
1120 for (WrapperKind wrapper in WrapperKind.values) {
1121 for (AssignmentKind assignment in AssignmentKind.values) {
1122 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment);
1123 if (assignment == AssignmentKind.SIMPLE_LEFT &&
1124 wrapper != WrapperKind.PREFIXED_LEFT &&
1125 wrapper != WrapperKind.PROPERTY_LEFT) {
1126 if (identifier.inGetterContext()) {
1127 fail("Expected ${_topMostNode(identifier).toSource()} to be false");
1128 }
1129 } else {
1130 if (!identifier.inGetterContext()) {
1131 fail("Expected ${_topMostNode(identifier).toSource()} to be true");
1132 }
1133 }
1134 }
1135 }
1136 }
1137
1138 void test_inGetterContext_forEachLoop() {
1139 SimpleIdentifier identifier = AstFactory.identifier3("a");
1140 Expression iterator = AstFactory.listLiteral();
1141 Statement body = AstFactory.block();
1142 AstFactory.forEachStatement2(identifier, iterator, body);
1143 expect(identifier.inGetterContext(), isFalse);
1144 }
1145
1146 void test_inReferenceContext() {
1147 SimpleIdentifier identifier = AstFactory.identifier3("id");
1148 AstFactory.namedExpression(
1149 AstFactory.label(identifier), AstFactory.identifier3("_"));
1150 expect(identifier.inGetterContext(), isFalse);
1151 expect(identifier.inSetterContext(), isFalse);
1152 }
1153
1154 void test_inSetterContext() {
1155 for (WrapperKind wrapper in WrapperKind.values) {
1156 for (AssignmentKind assignment in AssignmentKind.values) {
1157 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment);
1158 if (wrapper == WrapperKind.PREFIXED_LEFT ||
1159 wrapper == WrapperKind.PROPERTY_LEFT ||
1160 assignment == AssignmentKind.BINARY ||
1161 assignment == AssignmentKind.COMPOUND_RIGHT ||
1162 assignment == AssignmentKind.PREFIX_NOT ||
1163 assignment == AssignmentKind.SIMPLE_RIGHT ||
1164 assignment == AssignmentKind.NONE) {
1165 if (identifier.inSetterContext()) {
1166 fail("Expected ${_topMostNode(identifier).toSource()} to be false");
1167 }
1168 } else {
1169 if (!identifier.inSetterContext()) {
1170 fail("Expected ${_topMostNode(identifier).toSource()} to be true");
1171 }
1172 }
1173 }
1174 }
1175 }
1176
1177 void test_inSetterContext_forEachLoop() {
1178 SimpleIdentifier identifier = AstFactory.identifier3("a");
1179 Expression iterator = AstFactory.listLiteral();
1180 Statement body = AstFactory.block();
1181 AstFactory.forEachStatement2(identifier, iterator, body);
1182 expect(identifier.inSetterContext(), isTrue);
1183 }
1184
1185 void test_isQualified_inMethodInvocation_noTarget() {
1186 MethodInvocation invocation =
1187 AstFactory.methodInvocation2("test", [AstFactory.identifier3("arg0")]);
1188 SimpleIdentifier identifier = invocation.methodName;
1189 expect(identifier.isQualified, isFalse);
1190 }
1191
1192 void test_isQualified_inMethodInvocation_withTarget() {
1193 MethodInvocation invocation = AstFactory.methodInvocation(
1194 AstFactory.identifier3("target"),
1195 "test",
1196 [AstFactory.identifier3("arg0")]);
1197 SimpleIdentifier identifier = invocation.methodName;
1198 expect(identifier.isQualified, isTrue);
1199 }
1200
1201 void test_isQualified_inPrefixedIdentifier_name() {
1202 SimpleIdentifier identifier = AstFactory.identifier3("test");
1203 AstFactory.identifier4("prefix", identifier);
1204 expect(identifier.isQualified, isTrue);
1205 }
1206
1207 void test_isQualified_inPrefixedIdentifier_prefix() {
1208 SimpleIdentifier identifier = AstFactory.identifier3("test");
1209 AstFactory.identifier(identifier, AstFactory.identifier3("name"));
1210 expect(identifier.isQualified, isFalse);
1211 }
1212
1213 void test_isQualified_inPropertyAccess_name() {
1214 SimpleIdentifier identifier = AstFactory.identifier3("test");
1215 AstFactory.propertyAccess(AstFactory.identifier3("target"), identifier);
1216 expect(identifier.isQualified, isTrue);
1217 }
1218
1219 void test_isQualified_inPropertyAccess_target() {
1220 SimpleIdentifier identifier = AstFactory.identifier3("test");
1221 AstFactory.propertyAccess(identifier, AstFactory.identifier3("name"));
1222 expect(identifier.isQualified, isFalse);
1223 }
1224
1225 void test_isQualified_inReturnStatement() {
1226 SimpleIdentifier identifier = AstFactory.identifier3("test");
1227 AstFactory.returnStatement2(identifier);
1228 expect(identifier.isQualified, isFalse);
1229 }
1230
1231 SimpleIdentifier _createIdentifier(
1232 WrapperKind wrapper, AssignmentKind assignment) {
1233 SimpleIdentifier identifier = AstFactory.identifier3("a");
1234 Expression expression = identifier;
1235 while (true) {
1236 if (wrapper == WrapperKind.PREFIXED_LEFT) {
1237 expression =
1238 AstFactory.identifier(identifier, AstFactory.identifier3("_"));
1239 } else if (wrapper == WrapperKind.PREFIXED_RIGHT) {
1240 expression =
1241 AstFactory.identifier(AstFactory.identifier3("_"), identifier);
1242 } else if (wrapper == WrapperKind.PROPERTY_LEFT) {
1243 expression = AstFactory.propertyAccess2(expression, "_");
1244 } else if (wrapper == WrapperKind.PROPERTY_RIGHT) {
1245 expression =
1246 AstFactory.propertyAccess(AstFactory.identifier3("_"), identifier);
1247 } else if (wrapper == WrapperKind.NONE) {}
1248 break;
1249 }
1250 while (true) {
1251 if (assignment == AssignmentKind.BINARY) {
1252 AstFactory.binaryExpression(
1253 expression, TokenType.PLUS, AstFactory.identifier3("_"));
1254 } else if (assignment == AssignmentKind.COMPOUND_LEFT) {
1255 AstFactory.assignmentExpression(
1256 expression, TokenType.PLUS_EQ, AstFactory.identifier3("_"));
1257 } else if (assignment == AssignmentKind.COMPOUND_RIGHT) {
1258 AstFactory.assignmentExpression(
1259 AstFactory.identifier3("_"), TokenType.PLUS_EQ, expression);
1260 } else if (assignment == AssignmentKind.POSTFIX_INC) {
1261 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS);
1262 } else if (assignment == AssignmentKind.PREFIX_DEC) {
1263 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression);
1264 } else if (assignment == AssignmentKind.PREFIX_INC) {
1265 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression);
1266 } else if (assignment == AssignmentKind.PREFIX_NOT) {
1267 AstFactory.prefixExpression(TokenType.BANG, expression);
1268 } else if (assignment == AssignmentKind.SIMPLE_LEFT) {
1269 AstFactory.assignmentExpression(
1270 expression, TokenType.EQ, AstFactory.identifier3("_"));
1271 } else if (assignment == AssignmentKind.SIMPLE_RIGHT) {
1272 AstFactory.assignmentExpression(
1273 AstFactory.identifier3("_"), TokenType.EQ, expression);
1274 } else if (assignment == AssignmentKind.NONE) {}
1275 break;
1276 }
1277 return identifier;
1278 }
1279
1280 /**
1281 * Return the top-most node in the AST structure containing the given identifi er.
1282 *
1283 * @param identifier the identifier in the AST structure being traversed
1284 * @return the root of the AST structure containing the identifier
1285 */
1286 AstNode _topMostNode(SimpleIdentifier identifier) {
1287 AstNode child = identifier;
1288 AstNode parent = identifier.parent;
1289 while (parent != null) {
1290 child = parent;
1291 parent = parent.parent;
1292 }
1293 return child;
1294 }
1295 }
1296
1297 @reflectiveTest
1298 class SimpleStringLiteralTest extends ParserTestCase {
1299 void test_contentsEnd() {
1300 expect(
1301 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X")
1302 .contentsEnd,
1303 2);
1304 expect(
1305 new SimpleStringLiteral(TokenFactory.tokenFromString('"X"'), "X")
1306 .contentsEnd,
1307 2);
1308
1309 expect(
1310 new SimpleStringLiteral(TokenFactory.tokenFromString('"""X"""'), "X")
1311 .contentsEnd,
1312 4);
1313 expect(
1314 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1315 .contentsEnd,
1316 4);
1317 expect(
1318 new SimpleStringLiteral(
1319 TokenFactory.tokenFromString("''' \nX'''"), "X").contentsEnd,
1320 7);
1321
1322 expect(
1323 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1324 .contentsEnd,
1325 3);
1326 expect(
1327 new SimpleStringLiteral(TokenFactory.tokenFromString('r"X"'), "X")
1328 .contentsEnd,
1329 3);
1330
1331 expect(
1332 new SimpleStringLiteral(TokenFactory.tokenFromString('r"""X"""'), "X")
1333 .contentsEnd,
1334 5);
1335 expect(
1336 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1337 .contentsEnd,
1338 5);
1339 expect(
1340 new SimpleStringLiteral(
1341 TokenFactory.tokenFromString("r''' \nX'''"), "X").contentsEnd,
1342 8);
1343 }
1344
1345 void test_contentsOffset() {
1346 expect(
1347 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X")
1348 .contentsOffset,
1349 1);
1350 expect(
1351 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X")
1352 .contentsOffset,
1353 1);
1354 expect(
1355 new SimpleStringLiteral(
1356 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X").contentsOffset,
1357 3);
1358 expect(
1359 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1360 .contentsOffset,
1361 3);
1362 expect(
1363 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1364 .contentsOffset,
1365 2);
1366 expect(
1367 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X")
1368 .contentsOffset,
1369 2);
1370 expect(
1371 new SimpleStringLiteral(
1372 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X").contentsOffset,
1373 4);
1374 expect(
1375 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1376 .contentsOffset,
1377 4);
1378 // leading whitespace
1379 expect(
1380 new SimpleStringLiteral(
1381 TokenFactory.tokenFromString("''' \ \nX''"), "X").contentsOffset,
1382 6);
1383 expect(
1384 new SimpleStringLiteral(
1385 TokenFactory.tokenFromString('r""" \ \nX"""'), "X").contentsOffset,
1386 7);
1387 }
1388
1389 void test_isMultiline() {
1390 expect(
1391 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X")
1392 .isMultiline,
1393 isFalse);
1394 expect(
1395 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1396 .isMultiline,
1397 isFalse);
1398 expect(
1399 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X")
1400 .isMultiline,
1401 isFalse);
1402 expect(
1403 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X")
1404 .isMultiline,
1405 isFalse);
1406 expect(
1407 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1408 .isMultiline,
1409 isTrue);
1410 expect(
1411 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1412 .isMultiline,
1413 isTrue);
1414 expect(
1415 new SimpleStringLiteral(
1416 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X").isMultiline,
1417 isTrue);
1418 expect(
1419 new SimpleStringLiteral(
1420 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X").isMultiline,
1421 isTrue);
1422 }
1423
1424 void test_isRaw() {
1425 expect(
1426 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X").isRaw,
1427 isFalse);
1428 expect(
1429 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X")
1430 .isRaw,
1431 isFalse);
1432 expect(
1433 new SimpleStringLiteral(
1434 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X").isRaw,
1435 isFalse);
1436 expect(
1437 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1438 .isRaw,
1439 isFalse);
1440 expect(
1441 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1442 .isRaw,
1443 isTrue);
1444 expect(
1445 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X")
1446 .isRaw,
1447 isTrue);
1448 expect(
1449 new SimpleStringLiteral(
1450 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X").isRaw,
1451 isTrue);
1452 expect(
1453 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1454 .isRaw,
1455 isTrue);
1456 }
1457
1458 void test_isSingleQuoted() {
1459 // '
1460 {
1461 var token = TokenFactory.tokenFromString("'X'");
1462 var node = new SimpleStringLiteral(token, null);
1463 expect(node.isSingleQuoted, isTrue);
1464 }
1465 // '''
1466 {
1467 var token = TokenFactory.tokenFromString("'''X'''");
1468 var node = new SimpleStringLiteral(token, null);
1469 expect(node.isSingleQuoted, isTrue);
1470 }
1471 // "
1472 {
1473 var token = TokenFactory.tokenFromString('"X"');
1474 var node = new SimpleStringLiteral(token, null);
1475 expect(node.isSingleQuoted, isFalse);
1476 }
1477 // """
1478 {
1479 var token = TokenFactory.tokenFromString('"""X"""');
1480 var node = new SimpleStringLiteral(token, null);
1481 expect(node.isSingleQuoted, isFalse);
1482 }
1483 }
1484
1485 void test_isSingleQuoted_raw() {
1486 // r'
1487 {
1488 var token = TokenFactory.tokenFromString("r'X'");
1489 var node = new SimpleStringLiteral(token, null);
1490 expect(node.isSingleQuoted, isTrue);
1491 }
1492 // r'''
1493 {
1494 var token = TokenFactory.tokenFromString("r'''X'''");
1495 var node = new SimpleStringLiteral(token, null);
1496 expect(node.isSingleQuoted, isTrue);
1497 }
1498 // r"
1499 {
1500 var token = TokenFactory.tokenFromString('r"X"');
1501 var node = new SimpleStringLiteral(token, null);
1502 expect(node.isSingleQuoted, isFalse);
1503 }
1504 // r"""
1505 {
1506 var token = TokenFactory.tokenFromString('r"""X"""');
1507 var node = new SimpleStringLiteral(token, null);
1508 expect(node.isSingleQuoted, isFalse);
1509 }
1510 }
1511
1512 void test_simple() {
1513 Token token = TokenFactory.tokenFromString("'value'");
1514 SimpleStringLiteral stringLiteral = new SimpleStringLiteral(token, "value");
1515 expect(stringLiteral.literal, same(token));
1516 expect(stringLiteral.beginToken, same(token));
1517 expect(stringLiteral.endToken, same(token));
1518 expect(stringLiteral.value, "value");
1519 }
1520 }
1521
1522 @reflectiveTest
1523 class StringInterpolationTest extends ParserTestCase {
1524 void test_contentsOffsetEnd() {
1525 AstFactory.interpolationExpression(AstFactory.identifier3('bb'));
1526 // 'a${bb}ccc'
1527 {
1528 var ae = AstFactory.interpolationString("'a", "a");
1529 var cToken = new StringToken(TokenType.STRING, "ccc'", 10);
1530 var cElement = new InterpolationString(cToken, 'ccc');
1531 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1532 expect(node.contentsOffset, 1);
1533 expect(node.contentsEnd, 10 + 4 - 1);
1534 }
1535 // '''a${bb}ccc'''
1536 {
1537 var ae = AstFactory.interpolationString("'''a", "a");
1538 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10);
1539 var cElement = new InterpolationString(cToken, 'ccc');
1540 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1541 expect(node.contentsOffset, 3);
1542 expect(node.contentsEnd, 10 + 4 - 1);
1543 }
1544 // """a${bb}ccc"""
1545 {
1546 var ae = AstFactory.interpolationString('"""a', "a");
1547 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10);
1548 var cElement = new InterpolationString(cToken, 'ccc');
1549 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1550 expect(node.contentsOffset, 3);
1551 expect(node.contentsEnd, 10 + 4 - 1);
1552 }
1553 // r'a${bb}ccc'
1554 {
1555 var ae = AstFactory.interpolationString("r'a", "a");
1556 var cToken = new StringToken(TokenType.STRING, "ccc'", 10);
1557 var cElement = new InterpolationString(cToken, 'ccc');
1558 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1559 expect(node.contentsOffset, 2);
1560 expect(node.contentsEnd, 10 + 4 - 1);
1561 }
1562 // r'''a${bb}ccc'''
1563 {
1564 var ae = AstFactory.interpolationString("r'''a", "a");
1565 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10);
1566 var cElement = new InterpolationString(cToken, 'ccc');
1567 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1568 expect(node.contentsOffset, 4);
1569 expect(node.contentsEnd, 10 + 4 - 1);
1570 }
1571 // r"""a${bb}ccc"""
1572 {
1573 var ae = AstFactory.interpolationString('r"""a', "a");
1574 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10);
1575 var cElement = new InterpolationString(cToken, 'ccc');
1576 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1577 expect(node.contentsOffset, 4);
1578 expect(node.contentsEnd, 10 + 4 - 1);
1579 }
1580 }
1581
1582 void test_isMultiline() {
1583 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb'));
1584 // '
1585 {
1586 var a = AstFactory.interpolationString("'a", "a");
1587 var c = AstFactory.interpolationString("ccc'", "ccc");
1588 StringInterpolation node = AstFactory.string([a, b, c]);
1589 expect(node.isMultiline, isFalse);
1590 }
1591 // '''
1592 {
1593 var a = AstFactory.interpolationString("'''a", "a");
1594 var c = AstFactory.interpolationString("ccc'''", "ccc");
1595 StringInterpolation node = AstFactory.string([a, b, c]);
1596 expect(node.isMultiline, isTrue);
1597 }
1598 // "
1599 {
1600 var a = AstFactory.interpolationString('"a', "a");
1601 var c = AstFactory.interpolationString('ccc"', "ccc");
1602 StringInterpolation node = AstFactory.string([a, b, c]);
1603 expect(node.isMultiline, isFalse);
1604 }
1605 // """
1606 {
1607 var a = AstFactory.interpolationString('"""a', "a");
1608 var c = AstFactory.interpolationString('ccc"""', "ccc");
1609 StringInterpolation node = AstFactory.string([a, b, c]);
1610 expect(node.isMultiline, isTrue);
1611 }
1612 }
1613
1614 void test_isRaw() {
1615 StringInterpolation node = AstFactory.string();
1616 expect(node.isRaw, isFalse);
1617 }
1618
1619 void test_isSingleQuoted() {
1620 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb'));
1621 // "
1622 {
1623 var a = AstFactory.interpolationString('"a', "a");
1624 var c = AstFactory.interpolationString('ccc"', "ccc");
1625 StringInterpolation node = AstFactory.string([a, b, c]);
1626 expect(node.isSingleQuoted, isFalse);
1627 }
1628 // """
1629 {
1630 var a = AstFactory.interpolationString('"""a', "a");
1631 var c = AstFactory.interpolationString('ccc"""', "ccc");
1632 StringInterpolation node = AstFactory.string([a, b, c]);
1633 expect(node.isSingleQuoted, isFalse);
1634 }
1635 // '
1636 {
1637 var a = AstFactory.interpolationString("'a", "a");
1638 var c = AstFactory.interpolationString("ccc'", "ccc");
1639 StringInterpolation node = AstFactory.string([a, b, c]);
1640 expect(node.isSingleQuoted, isTrue);
1641 }
1642 // '''
1643 {
1644 var a = AstFactory.interpolationString("'''a", "a");
1645 var c = AstFactory.interpolationString("ccc'''", "ccc");
1646 StringInterpolation node = AstFactory.string([a, b, c]);
1647 expect(node.isSingleQuoted, isTrue);
1648 }
1649 }
1650 }
1651
1652 @reflectiveTest
1653 class ToSourceVisitorTest extends EngineTestCase {
1654 void test_visitAdjacentStrings() {
1655 _assertSource(
1656 "'a' 'b'",
1657 AstFactory.adjacentStrings(
1658 [AstFactory.string2("a"), AstFactory.string2("b")]));
1659 }
1660
1661 void test_visitAnnotation_constant() {
1662 _assertSource("@A", AstFactory.annotation(AstFactory.identifier3("A")));
1663 }
1664
1665 void test_visitAnnotation_constructor() {
1666 _assertSource(
1667 "@A.c()",
1668 AstFactory.annotation2(AstFactory.identifier3("A"),
1669 AstFactory.identifier3("c"), AstFactory.argumentList()));
1670 }
1671
1672 void test_visitArgumentList() {
1673 _assertSource(
1674 "(a, b)",
1675 AstFactory.argumentList(
1676 [AstFactory.identifier3("a"), AstFactory.identifier3("b")]));
1677 }
1678
1679 void test_visitAsExpression() {
1680 _assertSource(
1681 "e as T",
1682 AstFactory.asExpression(
1683 AstFactory.identifier3("e"), AstFactory.typeName4("T")));
1684 }
1685
1686 void test_visitAssertStatement() {
1687 _assertSource(
1688 "assert (a);", AstFactory.assertStatement(AstFactory.identifier3("a")));
1689 }
1690
1691 void test_visitAssignmentExpression() {
1692 _assertSource(
1693 "a = b",
1694 AstFactory.assignmentExpression(AstFactory.identifier3("a"),
1695 TokenType.EQ, AstFactory.identifier3("b")));
1696 }
1697
1698 void test_visitAwaitExpression() {
1699 _assertSource(
1700 "await e", AstFactory.awaitExpression(AstFactory.identifier3("e")));
1701 }
1702
1703 void test_visitBinaryExpression() {
1704 _assertSource(
1705 "a + b",
1706 AstFactory.binaryExpression(AstFactory.identifier3("a"), TokenType.PLUS,
1707 AstFactory.identifier3("b")));
1708 }
1709
1710 void test_visitBlock_empty() {
1711 _assertSource("{}", AstFactory.block());
1712 }
1713
1714 void test_visitBlock_nonEmpty() {
1715 _assertSource(
1716 "{break; break;}",
1717 AstFactory
1718 .block([AstFactory.breakStatement(), AstFactory.breakStatement()]));
1719 }
1720
1721 void test_visitBlockFunctionBody_async() {
1722 _assertSource("async {}", AstFactory.asyncBlockFunctionBody());
1723 }
1724
1725 void test_visitBlockFunctionBody_async_star() {
1726 _assertSource("async* {}", AstFactory.asyncGeneratorBlockFunctionBody());
1727 }
1728
1729 void test_visitBlockFunctionBody_simple() {
1730 _assertSource("{}", AstFactory.blockFunctionBody2());
1731 }
1732
1733 void test_visitBlockFunctionBody_sync() {
1734 _assertSource("sync {}", AstFactory.syncBlockFunctionBody());
1735 }
1736
1737 void test_visitBlockFunctionBody_sync_star() {
1738 _assertSource("sync* {}", AstFactory.syncGeneratorBlockFunctionBody());
1739 }
1740
1741 void test_visitBooleanLiteral_false() {
1742 _assertSource("false", AstFactory.booleanLiteral(false));
1743 }
1744
1745 void test_visitBooleanLiteral_true() {
1746 _assertSource("true", AstFactory.booleanLiteral(true));
1747 }
1748
1749 void test_visitBreakStatement_label() {
1750 _assertSource("break l;", AstFactory.breakStatement2("l"));
1751 }
1752
1753 void test_visitBreakStatement_noLabel() {
1754 _assertSource("break;", AstFactory.breakStatement());
1755 }
1756
1757 void test_visitCascadeExpression_field() {
1758 _assertSource(
1759 "a..b..c",
1760 AstFactory.cascadeExpression(AstFactory.identifier3("a"), [
1761 AstFactory.cascadedPropertyAccess("b"),
1762 AstFactory.cascadedPropertyAccess("c")
1763 ]));
1764 }
1765
1766 void test_visitCascadeExpression_index() {
1767 _assertSource(
1768 "a..[0]..[1]",
1769 AstFactory.cascadeExpression(AstFactory.identifier3("a"), [
1770 AstFactory.cascadedIndexExpression(AstFactory.integer(0)),
1771 AstFactory.cascadedIndexExpression(AstFactory.integer(1))
1772 ]));
1773 }
1774
1775 void test_visitCascadeExpression_method() {
1776 _assertSource(
1777 "a..b()..c()",
1778 AstFactory.cascadeExpression(AstFactory.identifier3("a"), [
1779 AstFactory.cascadedMethodInvocation("b"),
1780 AstFactory.cascadedMethodInvocation("c")
1781 ]));
1782 }
1783
1784 void test_visitCatchClause_catch_noStack() {
1785 _assertSource("catch (e) {}", AstFactory.catchClause("e"));
1786 }
1787
1788 void test_visitCatchClause_catch_stack() {
1789 _assertSource("catch (e, s) {}", AstFactory.catchClause2("e", "s"));
1790 }
1791
1792 void test_visitCatchClause_on() {
1793 _assertSource(
1794 "on E {}", AstFactory.catchClause3(AstFactory.typeName4("E")));
1795 }
1796
1797 void test_visitCatchClause_on_catch() {
1798 _assertSource("on E catch (e) {}",
1799 AstFactory.catchClause4(AstFactory.typeName4("E"), "e"));
1800 }
1801
1802 void test_visitClassDeclaration_abstract() {
1803 _assertSource(
1804 "abstract class C {}",
1805 AstFactory.classDeclaration(
1806 Keyword.ABSTRACT, "C", null, null, null, null));
1807 }
1808
1809 void test_visitClassDeclaration_empty() {
1810 _assertSource("class C {}",
1811 AstFactory.classDeclaration(null, "C", null, null, null, null));
1812 }
1813
1814 void test_visitClassDeclaration_extends() {
1815 _assertSource(
1816 "class C extends A {}",
1817 AstFactory.classDeclaration(null, "C", null,
1818 AstFactory.extendsClause(AstFactory.typeName4("A")), null, null));
1819 }
1820
1821 void test_visitClassDeclaration_extends_implements() {
1822 _assertSource(
1823 "class C extends A implements B {}",
1824 AstFactory.classDeclaration(
1825 null,
1826 "C",
1827 null,
1828 AstFactory.extendsClause(AstFactory.typeName4("A")),
1829 null,
1830 AstFactory.implementsClause([AstFactory.typeName4("B")])));
1831 }
1832
1833 void test_visitClassDeclaration_extends_with() {
1834 _assertSource(
1835 "class C extends A with M {}",
1836 AstFactory.classDeclaration(
1837 null,
1838 "C",
1839 null,
1840 AstFactory.extendsClause(AstFactory.typeName4("A")),
1841 AstFactory.withClause([AstFactory.typeName4("M")]),
1842 null));
1843 }
1844
1845 void test_visitClassDeclaration_extends_with_implements() {
1846 _assertSource(
1847 "class C extends A with M implements B {}",
1848 AstFactory.classDeclaration(
1849 null,
1850 "C",
1851 null,
1852 AstFactory.extendsClause(AstFactory.typeName4("A")),
1853 AstFactory.withClause([AstFactory.typeName4("M")]),
1854 AstFactory.implementsClause([AstFactory.typeName4("B")])));
1855 }
1856
1857 void test_visitClassDeclaration_implements() {
1858 _assertSource(
1859 "class C implements B {}",
1860 AstFactory.classDeclaration(null, "C", null, null, null,
1861 AstFactory.implementsClause([AstFactory.typeName4("B")])));
1862 }
1863
1864 void test_visitClassDeclaration_multipleMember() {
1865 _assertSource(
1866 "class C {var a; var b;}",
1867 AstFactory.classDeclaration(null, "C", null, null, null, null, [
1868 AstFactory.fieldDeclaration2(
1869 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]),
1870 AstFactory.fieldDeclaration2(
1871 false, Keyword.VAR, [AstFactory.variableDeclaration("b")])
1872 ]));
1873 }
1874
1875 void test_visitClassDeclaration_parameters() {
1876 _assertSource(
1877 "class C<E> {}",
1878 AstFactory.classDeclaration(
1879 null, "C", AstFactory.typeParameterList(["E"]), null, null, null));
1880 }
1881
1882 void test_visitClassDeclaration_parameters_extends() {
1883 _assertSource(
1884 "class C<E> extends A {}",
1885 AstFactory.classDeclaration(
1886 null,
1887 "C",
1888 AstFactory.typeParameterList(["E"]),
1889 AstFactory.extendsClause(AstFactory.typeName4("A")),
1890 null,
1891 null));
1892 }
1893
1894 void test_visitClassDeclaration_parameters_extends_implements() {
1895 _assertSource(
1896 "class C<E> extends A implements B {}",
1897 AstFactory.classDeclaration(
1898 null,
1899 "C",
1900 AstFactory.typeParameterList(["E"]),
1901 AstFactory.extendsClause(AstFactory.typeName4("A")),
1902 null,
1903 AstFactory.implementsClause([AstFactory.typeName4("B")])));
1904 }
1905
1906 void test_visitClassDeclaration_parameters_extends_with() {
1907 _assertSource(
1908 "class C<E> extends A with M {}",
1909 AstFactory.classDeclaration(
1910 null,
1911 "C",
1912 AstFactory.typeParameterList(["E"]),
1913 AstFactory.extendsClause(AstFactory.typeName4("A")),
1914 AstFactory.withClause([AstFactory.typeName4("M")]),
1915 null));
1916 }
1917
1918 void test_visitClassDeclaration_parameters_extends_with_implements() {
1919 _assertSource(
1920 "class C<E> extends A with M implements B {}",
1921 AstFactory.classDeclaration(
1922 null,
1923 "C",
1924 AstFactory.typeParameterList(["E"]),
1925 AstFactory.extendsClause(AstFactory.typeName4("A")),
1926 AstFactory.withClause([AstFactory.typeName4("M")]),
1927 AstFactory.implementsClause([AstFactory.typeName4("B")])));
1928 }
1929
1930 void test_visitClassDeclaration_parameters_implements() {
1931 _assertSource(
1932 "class C<E> implements B {}",
1933 AstFactory.classDeclaration(
1934 null,
1935 "C",
1936 AstFactory.typeParameterList(["E"]),
1937 null,
1938 null,
1939 AstFactory.implementsClause([AstFactory.typeName4("B")])));
1940 }
1941
1942 void test_visitClassDeclaration_singleMember() {
1943 _assertSource(
1944 "class C {var a;}",
1945 AstFactory.classDeclaration(null, "C", null, null, null, null, [
1946 AstFactory.fieldDeclaration2(
1947 false, Keyword.VAR, [AstFactory.variableDeclaration("a")])
1948 ]));
1949 }
1950
1951 void test_visitClassDeclaration_withMetadata() {
1952 ClassDeclaration declaration =
1953 AstFactory.classDeclaration(null, "C", null, null, null, null);
1954 declaration.metadata
1955 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
1956 _assertSource("@deprecated class C {}", declaration);
1957 }
1958
1959 void test_visitClassTypeAlias_abstract() {
1960 _assertSource(
1961 "abstract class C = S with M1;",
1962 AstFactory.classTypeAlias(
1963 "C",
1964 null,
1965 Keyword.ABSTRACT,
1966 AstFactory.typeName4("S"),
1967 AstFactory.withClause([AstFactory.typeName4("M1")]),
1968 null));
1969 }
1970
1971 void test_visitClassTypeAlias_abstract_implements() {
1972 _assertSource(
1973 "abstract class C = S with M1 implements I;",
1974 AstFactory.classTypeAlias(
1975 "C",
1976 null,
1977 Keyword.ABSTRACT,
1978 AstFactory.typeName4("S"),
1979 AstFactory.withClause([AstFactory.typeName4("M1")]),
1980 AstFactory.implementsClause([AstFactory.typeName4("I")])));
1981 }
1982
1983 void test_visitClassTypeAlias_generic() {
1984 _assertSource(
1985 "class C<E> = S<E> with M1<E>;",
1986 AstFactory.classTypeAlias(
1987 "C",
1988 AstFactory.typeParameterList(["E"]),
1989 null,
1990 AstFactory.typeName4("S", [AstFactory.typeName4("E")]),
1991 AstFactory.withClause([
1992 AstFactory.typeName4("M1", [AstFactory.typeName4("E")])
1993 ]),
1994 null));
1995 }
1996
1997 void test_visitClassTypeAlias_implements() {
1998 _assertSource(
1999 "class C = S with M1 implements I;",
2000 AstFactory.classTypeAlias(
2001 "C",
2002 null,
2003 null,
2004 AstFactory.typeName4("S"),
2005 AstFactory.withClause([AstFactory.typeName4("M1")]),
2006 AstFactory.implementsClause([AstFactory.typeName4("I")])));
2007 }
2008
2009 void test_visitClassTypeAlias_minimal() {
2010 _assertSource(
2011 "class C = S with M1;",
2012 AstFactory.classTypeAlias("C", null, null, AstFactory.typeName4("S"),
2013 AstFactory.withClause([AstFactory.typeName4("M1")]), null));
2014 }
2015
2016 void test_visitClassTypeAlias_parameters_abstract() {
2017 _assertSource(
2018 "abstract class C<E> = S with M1;",
2019 AstFactory.classTypeAlias(
2020 "C",
2021 AstFactory.typeParameterList(["E"]),
2022 Keyword.ABSTRACT,
2023 AstFactory.typeName4("S"),
2024 AstFactory.withClause([AstFactory.typeName4("M1")]),
2025 null));
2026 }
2027
2028 void test_visitClassTypeAlias_parameters_abstract_implements() {
2029 _assertSource(
2030 "abstract class C<E> = S with M1 implements I;",
2031 AstFactory.classTypeAlias(
2032 "C",
2033 AstFactory.typeParameterList(["E"]),
2034 Keyword.ABSTRACT,
2035 AstFactory.typeName4("S"),
2036 AstFactory.withClause([AstFactory.typeName4("M1")]),
2037 AstFactory.implementsClause([AstFactory.typeName4("I")])));
2038 }
2039
2040 void test_visitClassTypeAlias_parameters_implements() {
2041 _assertSource(
2042 "class C<E> = S with M1 implements I;",
2043 AstFactory.classTypeAlias(
2044 "C",
2045 AstFactory.typeParameterList(["E"]),
2046 null,
2047 AstFactory.typeName4("S"),
2048 AstFactory.withClause([AstFactory.typeName4("M1")]),
2049 AstFactory.implementsClause([AstFactory.typeName4("I")])));
2050 }
2051
2052 void test_visitClassTypeAlias_withMetadata() {
2053 ClassTypeAlias declaration = AstFactory.classTypeAlias(
2054 "C",
2055 null,
2056 null,
2057 AstFactory.typeName4("S"),
2058 AstFactory.withClause([AstFactory.typeName4("M1")]),
2059 null);
2060 declaration.metadata
2061 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2062 _assertSource("@deprecated class C = S with M1;", declaration);
2063 }
2064
2065 void test_visitComment() {
2066 _assertSource(
2067 "",
2068 Comment.createBlockComment(
2069 <Token>[TokenFactory.tokenFromString("/* comment */")]));
2070 }
2071
2072 void test_visitCommentReference() {
2073 _assertSource("", new CommentReference(null, AstFactory.identifier3("a")));
2074 }
2075
2076 void test_visitCompilationUnit_declaration() {
2077 _assertSource(
2078 "var a;",
2079 AstFactory.compilationUnit2([
2080 AstFactory.topLevelVariableDeclaration2(
2081 Keyword.VAR, [AstFactory.variableDeclaration("a")])
2082 ]));
2083 }
2084
2085 void test_visitCompilationUnit_directive() {
2086 _assertSource("library l;",
2087 AstFactory.compilationUnit3([AstFactory.libraryDirective2("l")]));
2088 }
2089
2090 void test_visitCompilationUnit_directive_declaration() {
2091 _assertSource(
2092 "library l; var a;",
2093 AstFactory.compilationUnit4([
2094 AstFactory.libraryDirective2("l")
2095 ], [
2096 AstFactory.topLevelVariableDeclaration2(
2097 Keyword.VAR, [AstFactory.variableDeclaration("a")])
2098 ]));
2099 }
2100
2101 void test_visitCompilationUnit_empty() {
2102 _assertSource("", AstFactory.compilationUnit());
2103 }
2104
2105 void test_visitCompilationUnit_script() {
2106 _assertSource(
2107 "!#/bin/dartvm", AstFactory.compilationUnit5("!#/bin/dartvm"));
2108 }
2109
2110 void test_visitCompilationUnit_script_declaration() {
2111 _assertSource(
2112 "!#/bin/dartvm var a;",
2113 AstFactory.compilationUnit6("!#/bin/dartvm", [
2114 AstFactory.topLevelVariableDeclaration2(
2115 Keyword.VAR, [AstFactory.variableDeclaration("a")])
2116 ]));
2117 }
2118
2119 void test_visitCompilationUnit_script_directive() {
2120 _assertSource(
2121 "!#/bin/dartvm library l;",
2122 AstFactory.compilationUnit7(
2123 "!#/bin/dartvm", [AstFactory.libraryDirective2("l")]));
2124 }
2125
2126 void test_visitCompilationUnit_script_directives_declarations() {
2127 _assertSource(
2128 "!#/bin/dartvm library l; var a;",
2129 AstFactory.compilationUnit8("!#/bin/dartvm", [
2130 AstFactory.libraryDirective2("l")
2131 ], [
2132 AstFactory.topLevelVariableDeclaration2(
2133 Keyword.VAR, [AstFactory.variableDeclaration("a")])
2134 ]));
2135 }
2136
2137 void test_visitConditionalExpression() {
2138 _assertSource(
2139 "a ? b : c",
2140 AstFactory.conditionalExpression(AstFactory.identifier3("a"),
2141 AstFactory.identifier3("b"), AstFactory.identifier3("c")));
2142 }
2143
2144 void test_visitConstructorDeclaration_const() {
2145 _assertSource(
2146 "const C() {}",
2147 AstFactory.constructorDeclaration2(
2148 Keyword.CONST,
2149 null,
2150 AstFactory.identifier3("C"),
2151 null,
2152 AstFactory.formalParameterList(),
2153 null,
2154 AstFactory.blockFunctionBody2()));
2155 }
2156
2157 void test_visitConstructorDeclaration_external() {
2158 _assertSource(
2159 "external C();",
2160 AstFactory.constructorDeclaration(AstFactory.identifier3("C"), null,
2161 AstFactory.formalParameterList(), null));
2162 }
2163
2164 void test_visitConstructorDeclaration_minimal() {
2165 _assertSource(
2166 "C() {}",
2167 AstFactory.constructorDeclaration2(
2168 null,
2169 null,
2170 AstFactory.identifier3("C"),
2171 null,
2172 AstFactory.formalParameterList(),
2173 null,
2174 AstFactory.blockFunctionBody2()));
2175 }
2176
2177 void test_visitConstructorDeclaration_multipleInitializers() {
2178 _assertSource(
2179 "C() : a = b, c = d {}",
2180 AstFactory.constructorDeclaration2(
2181 null,
2182 null,
2183 AstFactory.identifier3("C"),
2184 null,
2185 AstFactory.formalParameterList(),
2186 [
2187 AstFactory.constructorFieldInitializer(
2188 false, "a", AstFactory.identifier3("b")),
2189 AstFactory.constructorFieldInitializer(
2190 false, "c", AstFactory.identifier3("d"))
2191 ],
2192 AstFactory.blockFunctionBody2()));
2193 }
2194
2195 void test_visitConstructorDeclaration_multipleParameters() {
2196 _assertSource(
2197 "C(var a, var b) {}",
2198 AstFactory.constructorDeclaration2(
2199 null,
2200 null,
2201 AstFactory.identifier3("C"),
2202 null,
2203 AstFactory.formalParameterList([
2204 AstFactory.simpleFormalParameter(Keyword.VAR, "a"),
2205 AstFactory.simpleFormalParameter(Keyword.VAR, "b")
2206 ]),
2207 null,
2208 AstFactory.blockFunctionBody2()));
2209 }
2210
2211 void test_visitConstructorDeclaration_named() {
2212 _assertSource(
2213 "C.m() {}",
2214 AstFactory.constructorDeclaration2(
2215 null,
2216 null,
2217 AstFactory.identifier3("C"),
2218 "m",
2219 AstFactory.formalParameterList(),
2220 null,
2221 AstFactory.blockFunctionBody2()));
2222 }
2223
2224 void test_visitConstructorDeclaration_singleInitializer() {
2225 _assertSource(
2226 "C() : a = b {}",
2227 AstFactory.constructorDeclaration2(
2228 null,
2229 null,
2230 AstFactory.identifier3("C"),
2231 null,
2232 AstFactory.formalParameterList(),
2233 [
2234 AstFactory.constructorFieldInitializer(
2235 false, "a", AstFactory.identifier3("b"))
2236 ],
2237 AstFactory.blockFunctionBody2()));
2238 }
2239
2240 void test_visitConstructorDeclaration_withMetadata() {
2241 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
2242 null,
2243 null,
2244 AstFactory.identifier3("C"),
2245 null,
2246 AstFactory.formalParameterList(),
2247 null,
2248 AstFactory.blockFunctionBody2());
2249 declaration.metadata
2250 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2251 _assertSource("@deprecated C() {}", declaration);
2252 }
2253
2254 void test_visitConstructorFieldInitializer_withoutThis() {
2255 _assertSource(
2256 "a = b",
2257 AstFactory.constructorFieldInitializer(
2258 false, "a", AstFactory.identifier3("b")));
2259 }
2260
2261 void test_visitConstructorFieldInitializer_withThis() {
2262 _assertSource(
2263 "this.a = b",
2264 AstFactory.constructorFieldInitializer(
2265 true, "a", AstFactory.identifier3("b")));
2266 }
2267
2268 void test_visitConstructorName_named_prefix() {
2269 _assertSource("p.C.n",
2270 AstFactory.constructorName(AstFactory.typeName4("p.C.n"), null));
2271 }
2272
2273 void test_visitConstructorName_unnamed_noPrefix() {
2274 _assertSource(
2275 "C", AstFactory.constructorName(AstFactory.typeName4("C"), null));
2276 }
2277
2278 void test_visitConstructorName_unnamed_prefix() {
2279 _assertSource(
2280 "p.C",
2281 AstFactory.constructorName(
2282 AstFactory.typeName3(AstFactory.identifier5("p", "C")), null));
2283 }
2284
2285 void test_visitContinueStatement_label() {
2286 _assertSource("continue l;", AstFactory.continueStatement("l"));
2287 }
2288
2289 void test_visitContinueStatement_noLabel() {
2290 _assertSource("continue;", AstFactory.continueStatement());
2291 }
2292
2293 void test_visitDefaultFormalParameter_named_noValue() {
2294 _assertSource(
2295 "p",
2296 AstFactory.namedFormalParameter(
2297 AstFactory.simpleFormalParameter3("p"), null));
2298 }
2299
2300 void test_visitDefaultFormalParameter_named_value() {
2301 _assertSource(
2302 "p : 0",
2303 AstFactory.namedFormalParameter(
2304 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0)));
2305 }
2306
2307 void test_visitDefaultFormalParameter_positional_noValue() {
2308 _assertSource(
2309 "p",
2310 AstFactory.positionalFormalParameter(
2311 AstFactory.simpleFormalParameter3("p"), null));
2312 }
2313
2314 void test_visitDefaultFormalParameter_positional_value() {
2315 _assertSource(
2316 "p = 0",
2317 AstFactory.positionalFormalParameter(
2318 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0)));
2319 }
2320
2321 void test_visitDoStatement() {
2322 _assertSource(
2323 "do {} while (c);",
2324 AstFactory.doStatement(
2325 AstFactory.block(), AstFactory.identifier3("c")));
2326 }
2327
2328 void test_visitDoubleLiteral() {
2329 _assertSource("4.2", AstFactory.doubleLiteral(4.2));
2330 }
2331
2332 void test_visitEmptyFunctionBody() {
2333 _assertSource(";", AstFactory.emptyFunctionBody());
2334 }
2335
2336 void test_visitEmptyStatement() {
2337 _assertSource(";", AstFactory.emptyStatement());
2338 }
2339
2340 void test_visitEnumDeclaration_multiple() {
2341 _assertSource(
2342 "enum E {ONE, TWO}", AstFactory.enumDeclaration2("E", ["ONE", "TWO"]));
2343 }
2344
2345 void test_visitEnumDeclaration_single() {
2346 _assertSource("enum E {ONE}", AstFactory.enumDeclaration2("E", ["ONE"]));
2347 }
2348
2349 void test_visitExportDirective_combinator() {
2350 _assertSource(
2351 "export 'a.dart' show A;",
2352 AstFactory.exportDirective2("a.dart", [
2353 AstFactory.showCombinator([AstFactory.identifier3("A")])
2354 ]));
2355 }
2356
2357 void test_visitExportDirective_combinators() {
2358 _assertSource(
2359 "export 'a.dart' show A hide B;",
2360 AstFactory.exportDirective2("a.dart", [
2361 AstFactory.showCombinator([AstFactory.identifier3("A")]),
2362 AstFactory.hideCombinator([AstFactory.identifier3("B")])
2363 ]));
2364 }
2365
2366 void test_visitExportDirective_minimal() {
2367 _assertSource("export 'a.dart';", AstFactory.exportDirective2("a.dart"));
2368 }
2369
2370 void test_visitExportDirective_withMetadata() {
2371 ExportDirective directive = AstFactory.exportDirective2("a.dart");
2372 directive.metadata
2373 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2374 _assertSource("@deprecated export 'a.dart';", directive);
2375 }
2376
2377 void test_visitExpressionFunctionBody_async() {
2378 _assertSource("async => a;",
2379 AstFactory.asyncExpressionFunctionBody(AstFactory.identifier3("a")));
2380 }
2381
2382 void test_visitExpressionFunctionBody_simple() {
2383 _assertSource("=> a;",
2384 AstFactory.expressionFunctionBody(AstFactory.identifier3("a")));
2385 }
2386
2387 void test_visitExpressionStatement() {
2388 _assertSource(
2389 "a;", AstFactory.expressionStatement(AstFactory.identifier3("a")));
2390 }
2391
2392 void test_visitExtendsClause() {
2393 _assertSource(
2394 "extends C", AstFactory.extendsClause(AstFactory.typeName4("C")));
2395 }
2396
2397 void test_visitFieldDeclaration_instance() {
2398 _assertSource(
2399 "var a;",
2400 AstFactory.fieldDeclaration2(
2401 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]));
2402 }
2403
2404 void test_visitFieldDeclaration_static() {
2405 _assertSource(
2406 "static var a;",
2407 AstFactory.fieldDeclaration2(
2408 true, Keyword.VAR, [AstFactory.variableDeclaration("a")]));
2409 }
2410
2411 void test_visitFieldDeclaration_withMetadata() {
2412 FieldDeclaration declaration = AstFactory.fieldDeclaration2(
2413 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]);
2414 declaration.metadata
2415 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2416 _assertSource("@deprecated var a;", declaration);
2417 }
2418
2419 void test_visitFieldFormalParameter_functionTyped() {
2420 _assertSource(
2421 "A this.a(b)",
2422 AstFactory.fieldFormalParameter(
2423 null,
2424 AstFactory.typeName4("A"),
2425 "a",
2426 AstFactory.formalParameterList(
2427 [AstFactory.simpleFormalParameter3("b")])));
2428 }
2429
2430 void test_visitFieldFormalParameter_functionTyped_typeParameters() {
2431 _assertSource(
2432 "A this.a<E, F>(b)",
2433 new FieldFormalParameter(
2434 null,
2435 null,
2436 null,
2437 AstFactory.typeName4('A'),
2438 TokenFactory.tokenFromKeyword(Keyword.THIS),
2439 TokenFactory.tokenFromType(TokenType.PERIOD),
2440 AstFactory.identifier3('a'),
2441 AstFactory.typeParameterList(['E', 'F']),
2442 AstFactory.formalParameterList(
2443 [AstFactory.simpleFormalParameter3("b")])));
2444 }
2445
2446 void test_visitFieldFormalParameter_keyword() {
2447 _assertSource(
2448 "var this.a", AstFactory.fieldFormalParameter(Keyword.VAR, null, "a"));
2449 }
2450
2451 void test_visitFieldFormalParameter_keywordAndType() {
2452 _assertSource(
2453 "final A this.a",
2454 AstFactory.fieldFormalParameter(
2455 Keyword.FINAL, AstFactory.typeName4("A"), "a"));
2456 }
2457
2458 void test_visitFieldFormalParameter_type() {
2459 _assertSource("A this.a",
2460 AstFactory.fieldFormalParameter(null, AstFactory.typeName4("A"), "a"));
2461 }
2462
2463 void test_visitForEachStatement_declared() {
2464 _assertSource(
2465 "for (var a in b) {}",
2466 AstFactory.forEachStatement(AstFactory.declaredIdentifier3("a"),
2467 AstFactory.identifier3("b"), AstFactory.block()));
2468 }
2469
2470 void test_visitForEachStatement_variable() {
2471 _assertSource(
2472 "for (a in b) {}",
2473 new ForEachStatement.withReference(
2474 null,
2475 TokenFactory.tokenFromKeyword(Keyword.FOR),
2476 TokenFactory.tokenFromType(TokenType.OPEN_PAREN),
2477 AstFactory.identifier3("a"),
2478 TokenFactory.tokenFromKeyword(Keyword.IN),
2479 AstFactory.identifier3("b"),
2480 TokenFactory.tokenFromType(TokenType.CLOSE_PAREN),
2481 AstFactory.block()));
2482 }
2483
2484 void test_visitForEachStatement_variable_await() {
2485 _assertSource(
2486 "await for (a in b) {}",
2487 new ForEachStatement.withReference(
2488 TokenFactory.tokenFromString("await"),
2489 TokenFactory.tokenFromKeyword(Keyword.FOR),
2490 TokenFactory.tokenFromType(TokenType.OPEN_PAREN),
2491 AstFactory.identifier3("a"),
2492 TokenFactory.tokenFromKeyword(Keyword.IN),
2493 AstFactory.identifier3("b"),
2494 TokenFactory.tokenFromType(TokenType.CLOSE_PAREN),
2495 AstFactory.block()));
2496 }
2497
2498 void test_visitFormalParameterList_empty() {
2499 _assertSource("()", AstFactory.formalParameterList());
2500 }
2501
2502 void test_visitFormalParameterList_n() {
2503 _assertSource(
2504 "({a : 0})",
2505 AstFactory.formalParameterList([
2506 AstFactory.namedFormalParameter(
2507 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0))
2508 ]));
2509 }
2510
2511 void test_visitFormalParameterList_nn() {
2512 _assertSource(
2513 "({a : 0, b : 1})",
2514 AstFactory.formalParameterList([
2515 AstFactory.namedFormalParameter(
2516 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0)),
2517 AstFactory.namedFormalParameter(
2518 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1))
2519 ]));
2520 }
2521
2522 void test_visitFormalParameterList_p() {
2523 _assertSource(
2524 "([a = 0])",
2525 AstFactory.formalParameterList([
2526 AstFactory.positionalFormalParameter(
2527 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0))
2528 ]));
2529 }
2530
2531 void test_visitFormalParameterList_pp() {
2532 _assertSource(
2533 "([a = 0, b = 1])",
2534 AstFactory.formalParameterList([
2535 AstFactory.positionalFormalParameter(
2536 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0)),
2537 AstFactory.positionalFormalParameter(
2538 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1))
2539 ]));
2540 }
2541
2542 void test_visitFormalParameterList_r() {
2543 _assertSource(
2544 "(a)",
2545 AstFactory
2546 .formalParameterList([AstFactory.simpleFormalParameter3("a")]));
2547 }
2548
2549 void test_visitFormalParameterList_rn() {
2550 _assertSource(
2551 "(a, {b : 1})",
2552 AstFactory.formalParameterList([
2553 AstFactory.simpleFormalParameter3("a"),
2554 AstFactory.namedFormalParameter(
2555 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1))
2556 ]));
2557 }
2558
2559 void test_visitFormalParameterList_rnn() {
2560 _assertSource(
2561 "(a, {b : 1, c : 2})",
2562 AstFactory.formalParameterList([
2563 AstFactory.simpleFormalParameter3("a"),
2564 AstFactory.namedFormalParameter(
2565 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)),
2566 AstFactory.namedFormalParameter(
2567 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(2))
2568 ]));
2569 }
2570
2571 void test_visitFormalParameterList_rp() {
2572 _assertSource(
2573 "(a, [b = 1])",
2574 AstFactory.formalParameterList([
2575 AstFactory.simpleFormalParameter3("a"),
2576 AstFactory.positionalFormalParameter(
2577 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1))
2578 ]));
2579 }
2580
2581 void test_visitFormalParameterList_rpp() {
2582 _assertSource(
2583 "(a, [b = 1, c = 2])",
2584 AstFactory.formalParameterList([
2585 AstFactory.simpleFormalParameter3("a"),
2586 AstFactory.positionalFormalParameter(
2587 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)),
2588 AstFactory.positionalFormalParameter(
2589 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(2))
2590 ]));
2591 }
2592
2593 void test_visitFormalParameterList_rr() {
2594 _assertSource(
2595 "(a, b)",
2596 AstFactory.formalParameterList([
2597 AstFactory.simpleFormalParameter3("a"),
2598 AstFactory.simpleFormalParameter3("b")
2599 ]));
2600 }
2601
2602 void test_visitFormalParameterList_rrn() {
2603 _assertSource(
2604 "(a, b, {c : 3})",
2605 AstFactory.formalParameterList([
2606 AstFactory.simpleFormalParameter3("a"),
2607 AstFactory.simpleFormalParameter3("b"),
2608 AstFactory.namedFormalParameter(
2609 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3))
2610 ]));
2611 }
2612
2613 void test_visitFormalParameterList_rrnn() {
2614 _assertSource(
2615 "(a, b, {c : 3, d : 4})",
2616 AstFactory.formalParameterList([
2617 AstFactory.simpleFormalParameter3("a"),
2618 AstFactory.simpleFormalParameter3("b"),
2619 AstFactory.namedFormalParameter(
2620 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3)),
2621 AstFactory.namedFormalParameter(
2622 AstFactory.simpleFormalParameter3("d"), AstFactory.integer(4))
2623 ]));
2624 }
2625
2626 void test_visitFormalParameterList_rrp() {
2627 _assertSource(
2628 "(a, b, [c = 3])",
2629 AstFactory.formalParameterList([
2630 AstFactory.simpleFormalParameter3("a"),
2631 AstFactory.simpleFormalParameter3("b"),
2632 AstFactory.positionalFormalParameter(
2633 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3))
2634 ]));
2635 }
2636
2637 void test_visitFormalParameterList_rrpp() {
2638 _assertSource(
2639 "(a, b, [c = 3, d = 4])",
2640 AstFactory.formalParameterList([
2641 AstFactory.simpleFormalParameter3("a"),
2642 AstFactory.simpleFormalParameter3("b"),
2643 AstFactory.positionalFormalParameter(
2644 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3)),
2645 AstFactory.positionalFormalParameter(
2646 AstFactory.simpleFormalParameter3("d"), AstFactory.integer(4))
2647 ]));
2648 }
2649
2650 void test_visitForStatement_c() {
2651 _assertSource(
2652 "for (; c;) {}",
2653 AstFactory.forStatement(
2654 null, AstFactory.identifier3("c"), null, AstFactory.block()));
2655 }
2656
2657 void test_visitForStatement_cu() {
2658 _assertSource(
2659 "for (; c; u) {}",
2660 AstFactory.forStatement(null, AstFactory.identifier3("c"),
2661 [AstFactory.identifier3("u")], AstFactory.block()));
2662 }
2663
2664 void test_visitForStatement_e() {
2665 _assertSource(
2666 "for (e;;) {}",
2667 AstFactory.forStatement(
2668 AstFactory.identifier3("e"), null, null, AstFactory.block()));
2669 }
2670
2671 void test_visitForStatement_ec() {
2672 _assertSource(
2673 "for (e; c;) {}",
2674 AstFactory.forStatement(AstFactory.identifier3("e"),
2675 AstFactory.identifier3("c"), null, AstFactory.block()));
2676 }
2677
2678 void test_visitForStatement_ecu() {
2679 _assertSource(
2680 "for (e; c; u) {}",
2681 AstFactory.forStatement(
2682 AstFactory.identifier3("e"),
2683 AstFactory.identifier3("c"),
2684 [AstFactory.identifier3("u")],
2685 AstFactory.block()));
2686 }
2687
2688 void test_visitForStatement_eu() {
2689 _assertSource(
2690 "for (e;; u) {}",
2691 AstFactory.forStatement(AstFactory.identifier3("e"), null,
2692 [AstFactory.identifier3("u")], AstFactory.block()));
2693 }
2694
2695 void test_visitForStatement_i() {
2696 _assertSource(
2697 "for (var i;;) {}",
2698 AstFactory.forStatement2(
2699 AstFactory.variableDeclarationList2(
2700 Keyword.VAR, [AstFactory.variableDeclaration("i")]),
2701 null,
2702 null,
2703 AstFactory.block()));
2704 }
2705
2706 void test_visitForStatement_ic() {
2707 _assertSource(
2708 "for (var i; c;) {}",
2709 AstFactory.forStatement2(
2710 AstFactory.variableDeclarationList2(
2711 Keyword.VAR, [AstFactory.variableDeclaration("i")]),
2712 AstFactory.identifier3("c"),
2713 null,
2714 AstFactory.block()));
2715 }
2716
2717 void test_visitForStatement_icu() {
2718 _assertSource(
2719 "for (var i; c; u) {}",
2720 AstFactory.forStatement2(
2721 AstFactory.variableDeclarationList2(
2722 Keyword.VAR, [AstFactory.variableDeclaration("i")]),
2723 AstFactory.identifier3("c"),
2724 [AstFactory.identifier3("u")],
2725 AstFactory.block()));
2726 }
2727
2728 void test_visitForStatement_iu() {
2729 _assertSource(
2730 "for (var i;; u) {}",
2731 AstFactory.forStatement2(
2732 AstFactory.variableDeclarationList2(
2733 Keyword.VAR, [AstFactory.variableDeclaration("i")]),
2734 null,
2735 [AstFactory.identifier3("u")],
2736 AstFactory.block()));
2737 }
2738
2739 void test_visitForStatement_u() {
2740 _assertSource(
2741 "for (;; u) {}",
2742 AstFactory.forStatement(
2743 null, null, [AstFactory.identifier3("u")], AstFactory.block()));
2744 }
2745
2746 void test_visitFunctionDeclaration_external() {
2747 FunctionDeclaration functionDeclaration = AstFactory.functionDeclaration(
2748 null,
2749 null,
2750 "f",
2751 AstFactory.functionExpression2(
2752 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody()));
2753 functionDeclaration.externalKeyword =
2754 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
2755 _assertSource("external f();", functionDeclaration);
2756 }
2757
2758 void test_visitFunctionDeclaration_getter() {
2759 _assertSource(
2760 "get f() {}",
2761 AstFactory.functionDeclaration(
2762 null, Keyword.GET, "f", AstFactory.functionExpression()));
2763 }
2764
2765 void test_visitFunctionDeclaration_local_blockBody() {
2766 FunctionDeclaration f = AstFactory.functionDeclaration(
2767 null, null, "f", AstFactory.functionExpression());
2768 FunctionDeclarationStatement fStatement =
2769 new FunctionDeclarationStatement(f);
2770 _assertSource(
2771 "main() {f() {} 42;}",
2772 AstFactory.functionDeclaration(
2773 null,
2774 null,
2775 "main",
2776 AstFactory.functionExpression2(
2777 AstFactory.formalParameterList(),
2778 AstFactory.blockFunctionBody2([
2779 fStatement,
2780 AstFactory.expressionStatement(AstFactory.integer(42))
2781 ]))));
2782 }
2783
2784 void test_visitFunctionDeclaration_local_expressionBody() {
2785 FunctionDeclaration f = AstFactory.functionDeclaration(
2786 null,
2787 null,
2788 "f",
2789 AstFactory.functionExpression2(AstFactory.formalParameterList(),
2790 AstFactory.expressionFunctionBody(AstFactory.integer(1))));
2791 FunctionDeclarationStatement fStatement =
2792 new FunctionDeclarationStatement(f);
2793 _assertSource(
2794 "main() {f() => 1; 2;}",
2795 AstFactory.functionDeclaration(
2796 null,
2797 null,
2798 "main",
2799 AstFactory.functionExpression2(
2800 AstFactory.formalParameterList(),
2801 AstFactory.blockFunctionBody2([
2802 fStatement,
2803 AstFactory.expressionStatement(AstFactory.integer(2))
2804 ]))));
2805 }
2806
2807 void test_visitFunctionDeclaration_normal() {
2808 _assertSource(
2809 "f() {}",
2810 AstFactory.functionDeclaration(
2811 null, null, "f", AstFactory.functionExpression()));
2812 }
2813
2814 void test_visitFunctionDeclaration_setter() {
2815 _assertSource(
2816 "set f() {}",
2817 AstFactory.functionDeclaration(
2818 null, Keyword.SET, "f", AstFactory.functionExpression()));
2819 }
2820
2821 void test_visitFunctionDeclaration_typeParameters() {
2822 _assertSource(
2823 "f<E>() {}",
2824 AstFactory.functionDeclaration(
2825 null,
2826 null,
2827 "f",
2828 AstFactory.functionExpression3(
2829 AstFactory.typeParameterList(['E']),
2830 AstFactory.formalParameterList(),
2831 AstFactory.blockFunctionBody2())));
2832 }
2833
2834 void test_visitFunctionDeclaration_withMetadata() {
2835 FunctionDeclaration declaration = AstFactory.functionDeclaration(
2836 null, null, "f", AstFactory.functionExpression());
2837 declaration.metadata
2838 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2839 _assertSource("@deprecated f() {}", declaration);
2840 }
2841
2842 void test_visitFunctionDeclarationStatement() {
2843 _assertSource(
2844 "f() {}",
2845 AstFactory.functionDeclarationStatement(
2846 null, null, "f", AstFactory.functionExpression()));
2847 }
2848
2849 void test_visitFunctionExpression() {
2850 _assertSource("() {}", AstFactory.functionExpression());
2851 }
2852
2853 void test_visitFunctionExpression_typeParameters() {
2854 _assertSource(
2855 "<E>() {}",
2856 AstFactory.functionExpression3(AstFactory.typeParameterList(['E']),
2857 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()));
2858 }
2859
2860 void test_visitFunctionExpressionInvocation_minimal() {
2861 _assertSource("f()",
2862 AstFactory.functionExpressionInvocation(AstFactory.identifier3("f")));
2863 }
2864
2865 void test_visitFunctionExpressionInvocation_typeArguments() {
2866 _assertSource(
2867 "f<A>()",
2868 AstFactory.functionExpressionInvocation2(AstFactory.identifier3("f"),
2869 AstFactory.typeArgumentList([AstFactory.typeName4('A')])));
2870 }
2871
2872 void test_visitFunctionTypeAlias_generic() {
2873 _assertSource(
2874 "typedef A F<B>();",
2875 AstFactory.typeAlias(
2876 AstFactory.typeName4("A"),
2877 "F",
2878 AstFactory.typeParameterList(["B"]),
2879 AstFactory.formalParameterList()));
2880 }
2881
2882 void test_visitFunctionTypeAlias_nonGeneric() {
2883 _assertSource(
2884 "typedef A F();",
2885 AstFactory.typeAlias(AstFactory.typeName4("A"), "F", null,
2886 AstFactory.formalParameterList()));
2887 }
2888
2889 void test_visitFunctionTypeAlias_withMetadata() {
2890 FunctionTypeAlias declaration = AstFactory.typeAlias(
2891 AstFactory.typeName4("A"), "F", null, AstFactory.formalParameterList());
2892 declaration.metadata
2893 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2894 _assertSource("@deprecated typedef A F();", declaration);
2895 }
2896
2897 void test_visitFunctionTypedFormalParameter_noType() {
2898 _assertSource("f()", AstFactory.functionTypedFormalParameter(null, "f"));
2899 }
2900
2901 void test_visitFunctionTypedFormalParameter_type() {
2902 _assertSource(
2903 "T f()",
2904 AstFactory.functionTypedFormalParameter(
2905 AstFactory.typeName4("T"), "f"));
2906 }
2907
2908 void test_visitFunctionTypedFormalParameter_typeParameters() {
2909 _assertSource(
2910 "T f<E>()",
2911 new FunctionTypedFormalParameter(
2912 null,
2913 null,
2914 AstFactory.typeName4("T"),
2915 AstFactory.identifier3('f'),
2916 AstFactory.typeParameterList(['E']),
2917 AstFactory.formalParameterList([])));
2918 }
2919
2920 void test_visitIfStatement_withElse() {
2921 _assertSource(
2922 "if (c) {} else {}",
2923 AstFactory.ifStatement2(AstFactory.identifier3("c"), AstFactory.block(),
2924 AstFactory.block()));
2925 }
2926
2927 void test_visitIfStatement_withoutElse() {
2928 _assertSource(
2929 "if (c) {}",
2930 AstFactory.ifStatement(
2931 AstFactory.identifier3("c"), AstFactory.block()));
2932 }
2933
2934 void test_visitImplementsClause_multiple() {
2935 _assertSource(
2936 "implements A, B",
2937 AstFactory.implementsClause(
2938 [AstFactory.typeName4("A"), AstFactory.typeName4("B")]));
2939 }
2940
2941 void test_visitImplementsClause_single() {
2942 _assertSource("implements A",
2943 AstFactory.implementsClause([AstFactory.typeName4("A")]));
2944 }
2945
2946 void test_visitImportDirective_combinator() {
2947 _assertSource(
2948 "import 'a.dart' show A;",
2949 AstFactory.importDirective3("a.dart", null, [
2950 AstFactory.showCombinator([AstFactory.identifier3("A")])
2951 ]));
2952 }
2953
2954 void test_visitImportDirective_combinators() {
2955 _assertSource(
2956 "import 'a.dart' show A hide B;",
2957 AstFactory.importDirective3("a.dart", null, [
2958 AstFactory.showCombinator([AstFactory.identifier3("A")]),
2959 AstFactory.hideCombinator([AstFactory.identifier3("B")])
2960 ]));
2961 }
2962
2963 void test_visitImportDirective_deferred() {
2964 _assertSource("import 'a.dart' deferred as p;",
2965 AstFactory.importDirective2("a.dart", true, "p"));
2966 }
2967
2968 void test_visitImportDirective_minimal() {
2969 _assertSource(
2970 "import 'a.dart';", AstFactory.importDirective3("a.dart", null));
2971 }
2972
2973 void test_visitImportDirective_prefix() {
2974 _assertSource(
2975 "import 'a.dart' as p;", AstFactory.importDirective3("a.dart", "p"));
2976 }
2977
2978 void test_visitImportDirective_prefix_combinator() {
2979 _assertSource(
2980 "import 'a.dart' as p show A;",
2981 AstFactory.importDirective3("a.dart", "p", [
2982 AstFactory.showCombinator([AstFactory.identifier3("A")])
2983 ]));
2984 }
2985
2986 void test_visitImportDirective_prefix_combinators() {
2987 _assertSource(
2988 "import 'a.dart' as p show A hide B;",
2989 AstFactory.importDirective3("a.dart", "p", [
2990 AstFactory.showCombinator([AstFactory.identifier3("A")]),
2991 AstFactory.hideCombinator([AstFactory.identifier3("B")])
2992 ]));
2993 }
2994
2995 void test_visitImportDirective_withMetadata() {
2996 ImportDirective directive = AstFactory.importDirective3("a.dart", null);
2997 directive.metadata
2998 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2999 _assertSource("@deprecated import 'a.dart';", directive);
3000 }
3001
3002 void test_visitImportHideCombinator_multiple() {
3003 _assertSource(
3004 "hide a, b",
3005 AstFactory.hideCombinator(
3006 [AstFactory.identifier3("a"), AstFactory.identifier3("b")]));
3007 }
3008
3009 void test_visitImportHideCombinator_single() {
3010 _assertSource(
3011 "hide a", AstFactory.hideCombinator([AstFactory.identifier3("a")]));
3012 }
3013
3014 void test_visitImportShowCombinator_multiple() {
3015 _assertSource(
3016 "show a, b",
3017 AstFactory.showCombinator(
3018 [AstFactory.identifier3("a"), AstFactory.identifier3("b")]));
3019 }
3020
3021 void test_visitImportShowCombinator_single() {
3022 _assertSource(
3023 "show a", AstFactory.showCombinator([AstFactory.identifier3("a")]));
3024 }
3025
3026 void test_visitIndexExpression() {
3027 _assertSource(
3028 "a[i]",
3029 AstFactory.indexExpression(
3030 AstFactory.identifier3("a"), AstFactory.identifier3("i")));
3031 }
3032
3033 void test_visitInstanceCreationExpression_const() {
3034 _assertSource(
3035 "const C()",
3036 AstFactory.instanceCreationExpression2(
3037 Keyword.CONST, AstFactory.typeName4("C")));
3038 }
3039
3040 void test_visitInstanceCreationExpression_named() {
3041 _assertSource(
3042 "new C.c()",
3043 AstFactory.instanceCreationExpression3(
3044 Keyword.NEW, AstFactory.typeName4("C"), "c"));
3045 }
3046
3047 void test_visitInstanceCreationExpression_unnamed() {
3048 _assertSource(
3049 "new C()",
3050 AstFactory.instanceCreationExpression2(
3051 Keyword.NEW, AstFactory.typeName4("C")));
3052 }
3053
3054 void test_visitIntegerLiteral() {
3055 _assertSource("42", AstFactory.integer(42));
3056 }
3057
3058 void test_visitInterpolationExpression_expression() {
3059 _assertSource("\${a}",
3060 AstFactory.interpolationExpression(AstFactory.identifier3("a")));
3061 }
3062
3063 void test_visitInterpolationExpression_identifier() {
3064 _assertSource("\$a", AstFactory.interpolationExpression2("a"));
3065 }
3066
3067 void test_visitInterpolationString() {
3068 _assertSource("'x", AstFactory.interpolationString("'x", "x"));
3069 }
3070
3071 void test_visitIsExpression_negated() {
3072 _assertSource(
3073 "a is! C",
3074 AstFactory.isExpression(
3075 AstFactory.identifier3("a"), true, AstFactory.typeName4("C")));
3076 }
3077
3078 void test_visitIsExpression_normal() {
3079 _assertSource(
3080 "a is C",
3081 AstFactory.isExpression(
3082 AstFactory.identifier3("a"), false, AstFactory.typeName4("C")));
3083 }
3084
3085 void test_visitLabel() {
3086 _assertSource("a:", AstFactory.label2("a"));
3087 }
3088
3089 void test_visitLabeledStatement_multiple() {
3090 _assertSource(
3091 "a: b: return;",
3092 AstFactory.labeledStatement(
3093 [AstFactory.label2("a"), AstFactory.label2("b")],
3094 AstFactory.returnStatement()));
3095 }
3096
3097 void test_visitLabeledStatement_single() {
3098 _assertSource(
3099 "a: return;",
3100 AstFactory.labeledStatement(
3101 [AstFactory.label2("a")], AstFactory.returnStatement()));
3102 }
3103
3104 void test_visitLibraryDirective() {
3105 _assertSource("library l;", AstFactory.libraryDirective2("l"));
3106 }
3107
3108 void test_visitLibraryDirective_withMetadata() {
3109 LibraryDirective directive = AstFactory.libraryDirective2("l");
3110 directive.metadata
3111 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
3112 _assertSource("@deprecated library l;", directive);
3113 }
3114
3115 void test_visitLibraryIdentifier_multiple() {
3116 _assertSource(
3117 "a.b.c",
3118 AstFactory.libraryIdentifier([
3119 AstFactory.identifier3("a"),
3120 AstFactory.identifier3("b"),
3121 AstFactory.identifier3("c")
3122 ]));
3123 }
3124
3125 void test_visitLibraryIdentifier_single() {
3126 _assertSource(
3127 "a", AstFactory.libraryIdentifier([AstFactory.identifier3("a")]));
3128 }
3129
3130 void test_visitListLiteral_const() {
3131 _assertSource("const []", AstFactory.listLiteral2(Keyword.CONST, null));
3132 }
3133
3134 void test_visitListLiteral_empty() {
3135 _assertSource("[]", AstFactory.listLiteral());
3136 }
3137
3138 void test_visitListLiteral_nonEmpty() {
3139 _assertSource(
3140 "[a, b, c]",
3141 AstFactory.listLiteral([
3142 AstFactory.identifier3("a"),
3143 AstFactory.identifier3("b"),
3144 AstFactory.identifier3("c")
3145 ]));
3146 }
3147
3148 void test_visitMapLiteral_const() {
3149 _assertSource("const {}", AstFactory.mapLiteral(Keyword.CONST, null));
3150 }
3151
3152 void test_visitMapLiteral_empty() {
3153 _assertSource("{}", AstFactory.mapLiteral2());
3154 }
3155
3156 void test_visitMapLiteral_nonEmpty() {
3157 _assertSource(
3158 "{'a' : a, 'b' : b, 'c' : c}",
3159 AstFactory.mapLiteral2([
3160 AstFactory.mapLiteralEntry("a", AstFactory.identifier3("a")),
3161 AstFactory.mapLiteralEntry("b", AstFactory.identifier3("b")),
3162 AstFactory.mapLiteralEntry("c", AstFactory.identifier3("c"))
3163 ]));
3164 }
3165
3166 void test_visitMapLiteralEntry() {
3167 _assertSource("'a' : b",
3168 AstFactory.mapLiteralEntry("a", AstFactory.identifier3("b")));
3169 }
3170
3171 void test_visitMethodDeclaration_external() {
3172 _assertSource(
3173 "external m();",
3174 AstFactory.methodDeclaration(null, null, null, null,
3175 AstFactory.identifier3("m"), AstFactory.formalParameterList()));
3176 }
3177
3178 void test_visitMethodDeclaration_external_returnType() {
3179 _assertSource(
3180 "external T m();",
3181 AstFactory.methodDeclaration(
3182 null,
3183 AstFactory.typeName4("T"),
3184 null,
3185 null,
3186 AstFactory.identifier3("m"),
3187 AstFactory.formalParameterList()));
3188 }
3189
3190 void test_visitMethodDeclaration_getter() {
3191 _assertSource(
3192 "get m {}",
3193 AstFactory.methodDeclaration2(
3194 null,
3195 null,
3196 Keyword.GET,
3197 null,
3198 AstFactory.identifier3("m"),
3199 null,
3200 AstFactory.blockFunctionBody2()));
3201 }
3202
3203 void test_visitMethodDeclaration_getter_returnType() {
3204 _assertSource(
3205 "T get m {}",
3206 AstFactory.methodDeclaration2(
3207 null,
3208 AstFactory.typeName4("T"),
3209 Keyword.GET,
3210 null,
3211 AstFactory.identifier3("m"),
3212 null,
3213 AstFactory.blockFunctionBody2()));
3214 }
3215
3216 void test_visitMethodDeclaration_getter_seturnType() {
3217 _assertSource(
3218 "T set m(var v) {}",
3219 AstFactory.methodDeclaration2(
3220 null,
3221 AstFactory.typeName4("T"),
3222 Keyword.SET,
3223 null,
3224 AstFactory.identifier3("m"),
3225 AstFactory.formalParameterList(
3226 [AstFactory.simpleFormalParameter(Keyword.VAR, "v")]),
3227 AstFactory.blockFunctionBody2()));
3228 }
3229
3230 void test_visitMethodDeclaration_minimal() {
3231 _assertSource(
3232 "m() {}",
3233 AstFactory.methodDeclaration2(
3234 null,
3235 null,
3236 null,
3237 null,
3238 AstFactory.identifier3("m"),
3239 AstFactory.formalParameterList(),
3240 AstFactory.blockFunctionBody2()));
3241 }
3242
3243 void test_visitMethodDeclaration_multipleParameters() {
3244 _assertSource(
3245 "m(var a, var b) {}",
3246 AstFactory.methodDeclaration2(
3247 null,
3248 null,
3249 null,
3250 null,
3251 AstFactory.identifier3("m"),
3252 AstFactory.formalParameterList([
3253 AstFactory.simpleFormalParameter(Keyword.VAR, "a"),
3254 AstFactory.simpleFormalParameter(Keyword.VAR, "b")
3255 ]),
3256 AstFactory.blockFunctionBody2()));
3257 }
3258
3259 void test_visitMethodDeclaration_operator() {
3260 _assertSource(
3261 "operator +() {}",
3262 AstFactory.methodDeclaration2(
3263 null,
3264 null,
3265 null,
3266 Keyword.OPERATOR,
3267 AstFactory.identifier3("+"),
3268 AstFactory.formalParameterList(),
3269 AstFactory.blockFunctionBody2()));
3270 }
3271
3272 void test_visitMethodDeclaration_operator_returnType() {
3273 _assertSource(
3274 "T operator +() {}",
3275 AstFactory.methodDeclaration2(
3276 null,
3277 AstFactory.typeName4("T"),
3278 null,
3279 Keyword.OPERATOR,
3280 AstFactory.identifier3("+"),
3281 AstFactory.formalParameterList(),
3282 AstFactory.blockFunctionBody2()));
3283 }
3284
3285 void test_visitMethodDeclaration_returnType() {
3286 _assertSource(
3287 "T m() {}",
3288 AstFactory.methodDeclaration2(
3289 null,
3290 AstFactory.typeName4("T"),
3291 null,
3292 null,
3293 AstFactory.identifier3("m"),
3294 AstFactory.formalParameterList(),
3295 AstFactory.blockFunctionBody2()));
3296 }
3297
3298 void test_visitMethodDeclaration_setter() {
3299 _assertSource(
3300 "set m(var v) {}",
3301 AstFactory.methodDeclaration2(
3302 null,
3303 null,
3304 Keyword.SET,
3305 null,
3306 AstFactory.identifier3("m"),
3307 AstFactory.formalParameterList(
3308 [AstFactory.simpleFormalParameter(Keyword.VAR, "v")]),
3309 AstFactory.blockFunctionBody2()));
3310 }
3311
3312 void test_visitMethodDeclaration_static() {
3313 _assertSource(
3314 "static m() {}",
3315 AstFactory.methodDeclaration2(
3316 Keyword.STATIC,
3317 null,
3318 null,
3319 null,
3320 AstFactory.identifier3("m"),
3321 AstFactory.formalParameterList(),
3322 AstFactory.blockFunctionBody2()));
3323 }
3324
3325 void test_visitMethodDeclaration_static_returnType() {
3326 _assertSource(
3327 "static T m() {}",
3328 AstFactory.methodDeclaration2(
3329 Keyword.STATIC,
3330 AstFactory.typeName4("T"),
3331 null,
3332 null,
3333 AstFactory.identifier3("m"),
3334 AstFactory.formalParameterList(),
3335 AstFactory.blockFunctionBody2()));
3336 }
3337
3338 void test_visitMethodDeclaration_typeParameters() {
3339 _assertSource(
3340 "m<E>() {}",
3341 AstFactory.methodDeclaration3(
3342 null,
3343 null,
3344 null,
3345 null,
3346 AstFactory.identifier3("m"),
3347 AstFactory.typeParameterList(['E']),
3348 AstFactory.formalParameterList(),
3349 AstFactory.blockFunctionBody2()));
3350 }
3351
3352 void test_visitMethodDeclaration_withMetadata() {
3353 MethodDeclaration declaration = AstFactory.methodDeclaration2(
3354 null,
3355 null,
3356 null,
3357 null,
3358 AstFactory.identifier3("m"),
3359 AstFactory.formalParameterList(),
3360 AstFactory.blockFunctionBody2());
3361 declaration.metadata
3362 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
3363 _assertSource("@deprecated m() {}", declaration);
3364 }
3365
3366 void test_visitMethodInvocation_conditional() {
3367 _assertSource(
3368 "t?.m()",
3369 AstFactory.methodInvocation(
3370 AstFactory.identifier3("t"), "m", null, TokenType.QUESTION_PERIOD));
3371 }
3372
3373 void test_visitMethodInvocation_noTarget() {
3374 _assertSource("m()", AstFactory.methodInvocation2("m"));
3375 }
3376
3377 void test_visitMethodInvocation_target() {
3378 _assertSource(
3379 "t.m()", AstFactory.methodInvocation(AstFactory.identifier3("t"), "m"));
3380 }
3381
3382 void test_visitMethodInvocation_typeArguments() {
3383 _assertSource(
3384 "m<A>()",
3385 AstFactory.methodInvocation3(null, "m",
3386 AstFactory.typeArgumentList([AstFactory.typeName4('A')])));
3387 }
3388
3389 void test_visitNamedExpression() {
3390 _assertSource(
3391 "a: b", AstFactory.namedExpression2("a", AstFactory.identifier3("b")));
3392 }
3393
3394 void test_visitNamedFormalParameter() {
3395 _assertSource(
3396 "var a : 0",
3397 AstFactory.namedFormalParameter(
3398 AstFactory.simpleFormalParameter(Keyword.VAR, "a"),
3399 AstFactory.integer(0)));
3400 }
3401
3402 void test_visitNativeClause() {
3403 _assertSource("native 'code'", AstFactory.nativeClause("code"));
3404 }
3405
3406 void test_visitNativeFunctionBody() {
3407 _assertSource("native 'str';", AstFactory.nativeFunctionBody("str"));
3408 }
3409
3410 void test_visitNullLiteral() {
3411 _assertSource("null", AstFactory.nullLiteral());
3412 }
3413
3414 void test_visitParenthesizedExpression() {
3415 _assertSource(
3416 "(a)", AstFactory.parenthesizedExpression(AstFactory.identifier3("a")));
3417 }
3418
3419 void test_visitPartDirective() {
3420 _assertSource("part 'a.dart';", AstFactory.partDirective2("a.dart"));
3421 }
3422
3423 void test_visitPartDirective_withMetadata() {
3424 PartDirective directive = AstFactory.partDirective2("a.dart");
3425 directive.metadata
3426 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
3427 _assertSource("@deprecated part 'a.dart';", directive);
3428 }
3429
3430 void test_visitPartOfDirective() {
3431 _assertSource("part of l;",
3432 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["l"])));
3433 }
3434
3435 void test_visitPartOfDirective_withMetadata() {
3436 PartOfDirective directive =
3437 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["l"]));
3438 directive.metadata
3439 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
3440 _assertSource("@deprecated part of l;", directive);
3441 }
3442
3443 void test_visitPositionalFormalParameter() {
3444 _assertSource(
3445 "var a = 0",
3446 AstFactory.positionalFormalParameter(
3447 AstFactory.simpleFormalParameter(Keyword.VAR, "a"),
3448 AstFactory.integer(0)));
3449 }
3450
3451 void test_visitPostfixExpression() {
3452 _assertSource(
3453 "a++",
3454 AstFactory.postfixExpression(
3455 AstFactory.identifier3("a"), TokenType.PLUS_PLUS));
3456 }
3457
3458 void test_visitPrefixedIdentifier() {
3459 _assertSource("a.b", AstFactory.identifier5("a", "b"));
3460 }
3461
3462 void test_visitPrefixExpression() {
3463 _assertSource(
3464 "-a",
3465 AstFactory.prefixExpression(
3466 TokenType.MINUS, AstFactory.identifier3("a")));
3467 }
3468
3469 void test_visitPropertyAccess() {
3470 _assertSource(
3471 "a.b", AstFactory.propertyAccess2(AstFactory.identifier3("a"), "b"));
3472 }
3473
3474 void test_visitPropertyAccess_conditional() {
3475 _assertSource(
3476 "a?.b",
3477 AstFactory.propertyAccess2(
3478 AstFactory.identifier3("a"), "b", TokenType.QUESTION_PERIOD));
3479 }
3480
3481 void test_visitRedirectingConstructorInvocation_named() {
3482 _assertSource(
3483 "this.c()", AstFactory.redirectingConstructorInvocation2("c"));
3484 }
3485
3486 void test_visitRedirectingConstructorInvocation_unnamed() {
3487 _assertSource("this()", AstFactory.redirectingConstructorInvocation());
3488 }
3489
3490 void test_visitRethrowExpression() {
3491 _assertSource("rethrow", AstFactory.rethrowExpression());
3492 }
3493
3494 void test_visitReturnStatement_expression() {
3495 _assertSource(
3496 "return a;", AstFactory.returnStatement2(AstFactory.identifier3("a")));
3497 }
3498
3499 void test_visitReturnStatement_noExpression() {
3500 _assertSource("return;", AstFactory.returnStatement());
3501 }
3502
3503 void test_visitScriptTag() {
3504 String scriptTag = "!#/bin/dart.exe";
3505 _assertSource(scriptTag, AstFactory.scriptTag(scriptTag));
3506 }
3507
3508 void test_visitSimpleFormalParameter_keyword() {
3509 _assertSource("var a", AstFactory.simpleFormalParameter(Keyword.VAR, "a"));
3510 }
3511
3512 void test_visitSimpleFormalParameter_keyword_type() {
3513 _assertSource(
3514 "final A a",
3515 AstFactory.simpleFormalParameter2(
3516 Keyword.FINAL, AstFactory.typeName4("A"), "a"));
3517 }
3518
3519 void test_visitSimpleFormalParameter_type() {
3520 _assertSource("A a",
3521 AstFactory.simpleFormalParameter4(AstFactory.typeName4("A"), "a"));
3522 }
3523
3524 void test_visitSimpleIdentifier() {
3525 _assertSource("a", AstFactory.identifier3("a"));
3526 }
3527
3528 void test_visitSimpleStringLiteral() {
3529 _assertSource("'a'", AstFactory.string2("a"));
3530 }
3531
3532 void test_visitStringInterpolation() {
3533 _assertSource(
3534 "'a\${e}b'",
3535 AstFactory.string([
3536 AstFactory.interpolationString("'a", "a"),
3537 AstFactory.interpolationExpression(AstFactory.identifier3("e")),
3538 AstFactory.interpolationString("b'", "b")
3539 ]));
3540 }
3541
3542 void test_visitSuperConstructorInvocation() {
3543 _assertSource("super()", AstFactory.superConstructorInvocation());
3544 }
3545
3546 void test_visitSuperConstructorInvocation_named() {
3547 _assertSource("super.c()", AstFactory.superConstructorInvocation2("c"));
3548 }
3549
3550 void test_visitSuperExpression() {
3551 _assertSource("super", AstFactory.superExpression());
3552 }
3553
3554 void test_visitSwitchCase_multipleLabels() {
3555 _assertSource(
3556 "l1: l2: case a: {}",
3557 AstFactory.switchCase2(
3558 [AstFactory.label2("l1"), AstFactory.label2("l2")],
3559 AstFactory.identifier3("a"),
3560 [AstFactory.block()]));
3561 }
3562
3563 void test_visitSwitchCase_multipleStatements() {
3564 _assertSource(
3565 "case a: {} {}",
3566 AstFactory.switchCase(AstFactory.identifier3("a"),
3567 [AstFactory.block(), AstFactory.block()]));
3568 }
3569
3570 void test_visitSwitchCase_noLabels() {
3571 _assertSource(
3572 "case a: {}",
3573 AstFactory.switchCase(
3574 AstFactory.identifier3("a"), [AstFactory.block()]));
3575 }
3576
3577 void test_visitSwitchCase_singleLabel() {
3578 _assertSource(
3579 "l1: case a: {}",
3580 AstFactory.switchCase2([AstFactory.label2("l1")],
3581 AstFactory.identifier3("a"), [AstFactory.block()]));
3582 }
3583
3584 void test_visitSwitchDefault_multipleLabels() {
3585 _assertSource(
3586 "l1: l2: default: {}",
3587 AstFactory.switchDefault(
3588 [AstFactory.label2("l1"), AstFactory.label2("l2")],
3589 [AstFactory.block()]));
3590 }
3591
3592 void test_visitSwitchDefault_multipleStatements() {
3593 _assertSource("default: {} {}",
3594 AstFactory.switchDefault2([AstFactory.block(), AstFactory.block()]));
3595 }
3596
3597 void test_visitSwitchDefault_noLabels() {
3598 _assertSource(
3599 "default: {}", AstFactory.switchDefault2([AstFactory.block()]));
3600 }
3601
3602 void test_visitSwitchDefault_singleLabel() {
3603 _assertSource(
3604 "l1: default: {}",
3605 AstFactory.switchDefault(
3606 [AstFactory.label2("l1")], [AstFactory.block()]));
3607 }
3608
3609 void test_visitSwitchStatement() {
3610 _assertSource(
3611 "switch (a) {case 'b': {} default: {}}",
3612 AstFactory.switchStatement(AstFactory.identifier3("a"), [
3613 AstFactory.switchCase(AstFactory.string2("b"), [AstFactory.block()]),
3614 AstFactory.switchDefault2([AstFactory.block()])
3615 ]));
3616 }
3617
3618 void test_visitSymbolLiteral_multiple() {
3619 _assertSource("#a.b.c", AstFactory.symbolLiteral(["a", "b", "c"]));
3620 }
3621
3622 void test_visitSymbolLiteral_single() {
3623 _assertSource("#a", AstFactory.symbolLiteral(["a"]));
3624 }
3625
3626 void test_visitThisExpression() {
3627 _assertSource("this", AstFactory.thisExpression());
3628 }
3629
3630 void test_visitThrowStatement() {
3631 _assertSource(
3632 "throw e", AstFactory.throwExpression2(AstFactory.identifier3("e")));
3633 }
3634
3635 void test_visitTopLevelVariableDeclaration_multiple() {
3636 _assertSource(
3637 "var a;",
3638 AstFactory.topLevelVariableDeclaration2(
3639 Keyword.VAR, [AstFactory.variableDeclaration("a")]));
3640 }
3641
3642 void test_visitTopLevelVariableDeclaration_single() {
3643 _assertSource(
3644 "var a, b;",
3645 AstFactory.topLevelVariableDeclaration2(Keyword.VAR, [
3646 AstFactory.variableDeclaration("a"),
3647 AstFactory.variableDeclaration("b")
3648 ]));
3649 }
3650
3651 void test_visitTryStatement_catch() {
3652 _assertSource(
3653 "try {} on E {}",
3654 AstFactory.tryStatement2(AstFactory.block(),
3655 [AstFactory.catchClause3(AstFactory.typeName4("E"))]));
3656 }
3657
3658 void test_visitTryStatement_catches() {
3659 _assertSource(
3660 "try {} on E {} on F {}",
3661 AstFactory.tryStatement2(AstFactory.block(), [
3662 AstFactory.catchClause3(AstFactory.typeName4("E")),
3663 AstFactory.catchClause3(AstFactory.typeName4("F"))
3664 ]));
3665 }
3666
3667 void test_visitTryStatement_catchFinally() {
3668 _assertSource(
3669 "try {} on E {} finally {}",
3670 AstFactory.tryStatement3(
3671 AstFactory.block(),
3672 [AstFactory.catchClause3(AstFactory.typeName4("E"))],
3673 AstFactory.block()));
3674 }
3675
3676 void test_visitTryStatement_finally() {
3677 _assertSource("try {} finally {}",
3678 AstFactory.tryStatement(AstFactory.block(), AstFactory.block()));
3679 }
3680
3681 void test_visitTypeArgumentList_multiple() {
3682 _assertSource(
3683 "<E, F>",
3684 AstFactory.typeArgumentList(
3685 [AstFactory.typeName4("E"), AstFactory.typeName4("F")]));
3686 }
3687
3688 void test_visitTypeArgumentList_single() {
3689 _assertSource(
3690 "<E>", AstFactory.typeArgumentList([AstFactory.typeName4("E")]));
3691 }
3692
3693 void test_visitTypeName_multipleArgs() {
3694 _assertSource(
3695 "C<D, E>",
3696 AstFactory.typeName4(
3697 "C", [AstFactory.typeName4("D"), AstFactory.typeName4("E")]));
3698 }
3699
3700 void test_visitTypeName_nestedArg() {
3701 _assertSource(
3702 "C<D<E>>",
3703 AstFactory.typeName4("C", [
3704 AstFactory.typeName4("D", [AstFactory.typeName4("E")])
3705 ]));
3706 }
3707
3708 void test_visitTypeName_noArgs() {
3709 _assertSource("C", AstFactory.typeName4("C"));
3710 }
3711
3712 void test_visitTypeName_singleArg() {
3713 _assertSource(
3714 "C<D>", AstFactory.typeName4("C", [AstFactory.typeName4("D")]));
3715 }
3716
3717 void test_visitTypeParameter_withExtends() {
3718 _assertSource("E extends C",
3719 AstFactory.typeParameter2("E", AstFactory.typeName4("C")));
3720 }
3721
3722 void test_visitTypeParameter_withMetadata() {
3723 TypeParameter parameter = AstFactory.typeParameter("E");
3724 parameter.metadata
3725 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
3726 _assertSource("@deprecated E", parameter);
3727 }
3728
3729 void test_visitTypeParameter_withoutExtends() {
3730 _assertSource("E", AstFactory.typeParameter("E"));
3731 }
3732
3733 void test_visitTypeParameterList_multiple() {
3734 _assertSource("<E, F>", AstFactory.typeParameterList(["E", "F"]));
3735 }
3736
3737 void test_visitTypeParameterList_single() {
3738 _assertSource("<E>", AstFactory.typeParameterList(["E"]));
3739 }
3740
3741 void test_visitVariableDeclaration_initialized() {
3742 _assertSource("a = b",
3743 AstFactory.variableDeclaration2("a", AstFactory.identifier3("b")));
3744 }
3745
3746 void test_visitVariableDeclaration_uninitialized() {
3747 _assertSource("a", AstFactory.variableDeclaration("a"));
3748 }
3749
3750 void test_visitVariableDeclaration_withMetadata() {
3751 VariableDeclaration declaration = AstFactory.variableDeclaration("a");
3752 declaration.metadata
3753 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
3754 _assertSource("@deprecated a", declaration);
3755 }
3756
3757 void test_visitVariableDeclarationList_const_type() {
3758 _assertSource(
3759 "const C a, b",
3760 AstFactory.variableDeclarationList(
3761 Keyword.CONST, AstFactory.typeName4("C"), [
3762 AstFactory.variableDeclaration("a"),
3763 AstFactory.variableDeclaration("b")
3764 ]));
3765 }
3766
3767 void test_visitVariableDeclarationList_final_noType() {
3768 _assertSource(
3769 "final a, b",
3770 AstFactory.variableDeclarationList2(Keyword.FINAL, [
3771 AstFactory.variableDeclaration("a"),
3772 AstFactory.variableDeclaration("b")
3773 ]));
3774 }
3775
3776 void test_visitVariableDeclarationList_final_withMetadata() {
3777 VariableDeclarationList declarationList = AstFactory
3778 .variableDeclarationList2(Keyword.FINAL, [
3779 AstFactory.variableDeclaration("a"),
3780 AstFactory.variableDeclaration("b")
3781 ]);
3782 declarationList.metadata
3783 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
3784 _assertSource("@deprecated final a, b", declarationList);
3785 }
3786
3787 void test_visitVariableDeclarationList_type() {
3788 _assertSource(
3789 "C a, b",
3790 AstFactory.variableDeclarationList(null, AstFactory.typeName4("C"), [
3791 AstFactory.variableDeclaration("a"),
3792 AstFactory.variableDeclaration("b")
3793 ]));
3794 }
3795
3796 void test_visitVariableDeclarationList_var() {
3797 _assertSource(
3798 "var a, b",
3799 AstFactory.variableDeclarationList2(Keyword.VAR, [
3800 AstFactory.variableDeclaration("a"),
3801 AstFactory.variableDeclaration("b")
3802 ]));
3803 }
3804
3805 void test_visitVariableDeclarationStatement() {
3806 _assertSource(
3807 "C c;",
3808 AstFactory.variableDeclarationStatement(null, AstFactory.typeName4("C"),
3809 [AstFactory.variableDeclaration("c")]));
3810 }
3811
3812 void test_visitWhileStatement() {
3813 _assertSource(
3814 "while (c) {}",
3815 AstFactory.whileStatement(
3816 AstFactory.identifier3("c"), AstFactory.block()));
3817 }
3818
3819 void test_visitWithClause_multiple() {
3820 _assertSource(
3821 "with A, B, C",
3822 AstFactory.withClause([
3823 AstFactory.typeName4("A"),
3824 AstFactory.typeName4("B"),
3825 AstFactory.typeName4("C")
3826 ]));
3827 }
3828
3829 void test_visitWithClause_single() {
3830 _assertSource("with A", AstFactory.withClause([AstFactory.typeName4("A")]));
3831 }
3832
3833 void test_visitYieldStatement() {
3834 _assertSource(
3835 "yield e;", AstFactory.yieldStatement(AstFactory.identifier3("e")));
3836 }
3837
3838 void test_visitYieldStatement_each() {
3839 _assertSource("yield* e;",
3840 AstFactory.yieldEachStatement(AstFactory.identifier3("e")));
3841 }
3842
3843 /**
3844 * Assert that a `ToSourceVisitor` will produce the expected source when visit ing the given
3845 * node.
3846 *
3847 * @param expectedSource the source string that the visitor is expected to pro duce
3848 * @param node the AST node being visited to produce the actual source
3849 * @throws AFE if the visitor does not produce the expected source for the giv en node
3850 */
3851 void _assertSource(String expectedSource, AstNode node) {
3852 PrintStringWriter writer = new PrintStringWriter();
3853 node.accept(new ToSourceVisitor(writer));
3854 expect(writer.toString(), expectedSource);
3855 }
3856 }
3857
3858 @reflectiveTest
3859 class VariableDeclarationTest extends ParserTestCase {
3860 void test_getDocumentationComment_onGrandParent() {
3861 VariableDeclaration varDecl = AstFactory.variableDeclaration("a");
3862 TopLevelVariableDeclaration decl =
3863 AstFactory.topLevelVariableDeclaration2(Keyword.VAR, [varDecl]);
3864 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
3865 expect(varDecl.documentationComment, isNull);
3866 decl.documentationComment = comment;
3867 expect(varDecl.documentationComment, isNotNull);
3868 expect(decl.documentationComment, isNotNull);
3869 }
3870
3871 void test_getDocumentationComment_onNode() {
3872 VariableDeclaration decl = AstFactory.variableDeclaration("a");
3873 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
3874 decl.documentationComment = comment;
3875 expect(decl.documentationComment, isNotNull);
3876 }
3877 }
3878
3879 class WrapperKind extends Enum<WrapperKind> {
3880 static const WrapperKind PREFIXED_LEFT =
3881 const WrapperKind('PREFIXED_LEFT', 0);
3882
3883 static const WrapperKind PREFIXED_RIGHT =
3884 const WrapperKind('PREFIXED_RIGHT', 1);
3885
3886 static const WrapperKind PROPERTY_LEFT =
3887 const WrapperKind('PROPERTY_LEFT', 2);
3888
3889 static const WrapperKind PROPERTY_RIGHT =
3890 const WrapperKind('PROPERTY_RIGHT', 3);
3891
3892 static const WrapperKind NONE = const WrapperKind('NONE', 4);
3893
3894 static const List<WrapperKind> values = const [
3895 PREFIXED_LEFT,
3896 PREFIXED_RIGHT,
3897 PROPERTY_LEFT,
3898 PROPERTY_RIGHT,
3899 NONE
3900 ];
3901
3902 const WrapperKind(String name, int ordinal) : super(name, ordinal);
3903 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698