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

Side by Side Diff: packages/analyzer/test/src/dart/ast/utilities_test.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library engine.ast_test; 5 library analyzer.test.src.dart.ast.utilities_test;
6 6
7 import 'package:analyzer/src/generated/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart';
11 import 'package:analyzer/src/dart/ast/utilities.dart';
12 import 'package:analyzer/src/dart/element/element.dart';
8 import 'package:analyzer/src/generated/java_core.dart'; 13 import 'package:analyzer/src/generated/java_core.dart';
9 import 'package:analyzer/src/generated/java_engine.dart' show Predicate; 14 import 'package:analyzer/src/generated/java_engine.dart' show Predicate;
10 import 'package:analyzer/src/generated/java_engine.dart'; 15 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'; 16 import 'package:analyzer/src/generated/testing/ast_factory.dart';
17 import 'package:analyzer/src/generated/testing/element_factory.dart';
13 import 'package:analyzer/src/generated/testing/token_factory.dart'; 18 import 'package:analyzer/src/generated/testing/token_factory.dart';
19 import 'package:test_reflective_loader/test_reflective_loader.dart';
14 import 'package:unittest/unittest.dart'; 20 import 'package:unittest/unittest.dart';
15 21
16 import '../reflective_tests.dart'; 22 import '../../../generated/parser_test.dart' show ParserTestCase;
17 import '../utils.dart'; 23 import '../../../generated/test_support.dart';
18 import 'parser_test.dart' show ParserTestCase; 24 import '../../../utils.dart';
19 import 'test_support.dart';
20 25
21 main() { 26 main() {
22 initializeTestEnvironment(); 27 initializeTestEnvironment();
23 runReflectiveTests(BreadthFirstVisitorTest); 28 defineReflectiveTests(ConstantEvaluatorTest);
24 runReflectiveTests(ClassDeclarationTest); 29 defineReflectiveTests(NodeLocatorTest);
25 runReflectiveTests(ClassTypeAliasTest); 30 defineReflectiveTests(NodeLocator2Test);
26 runReflectiveTests(ConstantEvaluatorTest); 31 defineReflectiveTests(ResolutionCopierTest);
27 runReflectiveTests(ConstructorDeclarationTest); 32 defineReflectiveTests(ToSourceVisitorTest);
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 } 33 }
96 34
97 @reflectiveTest 35 @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 { 36 class ConstantEvaluatorTest extends ParserTestCase {
224 void fail_constructor() { 37 void fail_constructor() {
225 Object value = _getConstantValue("?"); 38 Object value = _getConstantValue("?");
226 expect(value, null); 39 expect(value, null);
227 } 40 }
228 41
229 void fail_identifier_class() { 42 void fail_identifier_class() {
230 Object value = _getConstantValue("?"); 43 Object value = _getConstantValue("?");
231 expect(value, null); 44 expect(value, null);
232 } 45 }
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 void test_unary_negated_integer() { 330 void test_unary_negated_integer() {
518 Object value = _getConstantValue("-42"); 331 Object value = _getConstantValue("-42");
519 expect(value, -42); 332 expect(value, -42);
520 } 333 }
521 334
522 Object _getConstantValue(String source) => 335 Object _getConstantValue(String source) =>
523 parseExpression(source).accept(new ConstantEvaluator()); 336 parseExpression(source).accept(new ConstantEvaluator());
524 } 337 }
525 338
526 @reflectiveTest 339 @reflectiveTest
527 class ConstructorDeclarationTest extends EngineTestCase { 340 class NodeLocator2Test extends ParserTestCase {
528 void test_firstTokenAfterCommentAndMetadata_all_inverted() { 341 void test_onlyStartOffset() {
529 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); 342 String code = ' int vv; ';
530 externalKeyword.offset = 14; 343 // 012345678
531 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( 344 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code);
532 Keyword.CONST, 345 TopLevelVariableDeclaration declaration = unit.declarations[0];
533 Keyword.FACTORY, 346 VariableDeclarationList variableList = declaration.variables;
534 AstFactory.identifier3('int'), 347 Identifier typeName = variableList.type.name;
535 null, 348 SimpleIdentifier varName = variableList.variables[0].name;
536 null, 349 expect(new NodeLocator2(0).searchWithin(unit), same(unit));
537 null, 350 expect(new NodeLocator2(1).searchWithin(unit), same(typeName));
538 null); 351 expect(new NodeLocator2(2).searchWithin(unit), same(typeName));
539 declaration.externalKeyword = externalKeyword; 352 expect(new NodeLocator2(3).searchWithin(unit), same(typeName));
540 declaration.constKeyword.offset = 8; 353 expect(new NodeLocator2(4).searchWithin(unit), same(variableList));
541 Token factoryKeyword = declaration.factoryKeyword; 354 expect(new NodeLocator2(5).searchWithin(unit), same(varName));
542 factoryKeyword.offset = 0; 355 expect(new NodeLocator2(6).searchWithin(unit), same(varName));
543 expect(declaration.firstTokenAfterCommentAndMetadata, factoryKeyword); 356 expect(new NodeLocator2(7).searchWithin(unit), same(declaration));
357 expect(new NodeLocator2(8).searchWithin(unit), same(unit));
358 expect(new NodeLocator2(9).searchWithin(unit), isNull);
359 expect(new NodeLocator2(100).searchWithin(unit), isNull);
544 } 360 }
545 361
546 void test_firstTokenAfterCommentAndMetadata_all_normal() { 362 void test_startEndOffset() {
547 Token token = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); 363 String code = ' int vv; ';
548 token.offset = 0; 364 // 012345678
549 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( 365 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code);
550 Keyword.CONST, 366 TopLevelVariableDeclaration declaration = unit.declarations[0];
551 Keyword.FACTORY, 367 VariableDeclarationList variableList = declaration.variables;
552 AstFactory.identifier3('int'), 368 Identifier typeName = variableList.type.name;
553 null, 369 SimpleIdentifier varName = variableList.variables[0].name;
554 null, 370 expect(new NodeLocator2(-1, 2).searchWithin(unit), isNull);
555 null, 371 expect(new NodeLocator2(0, 2).searchWithin(unit), same(unit));
556 null); 372 expect(new NodeLocator2(1, 2).searchWithin(unit), same(typeName));
557 declaration.externalKeyword = token; 373 expect(new NodeLocator2(1, 3).searchWithin(unit), same(typeName));
558 declaration.constKeyword.offset = 9; 374 expect(new NodeLocator2(1, 4).searchWithin(unit), same(variableList));
559 declaration.factoryKeyword.offset = 15; 375 expect(new NodeLocator2(5, 6).searchWithin(unit), same(varName));
560 expect(declaration.firstTokenAfterCommentAndMetadata, token); 376 expect(new NodeLocator2(5, 7).searchWithin(unit), same(declaration));
561 } 377 expect(new NodeLocator2(5, 8).searchWithin(unit), same(unit));
562 378 expect(new NodeLocator2(5, 100).searchWithin(unit), isNull);
563 void test_firstTokenAfterCommentAndMetadata_constOnly() { 379 expect(new NodeLocator2(100, 200).searchWithin(unit), isNull);
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 } 380 }
596 } 381 }
597 382
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 383 @reflectiveTest
948 class NodeLocatorTest extends ParserTestCase { 384 class NodeLocatorTest extends ParserTestCase {
949 void test_range() { 385 void test_range() {
950 CompilationUnit unit = 386 CompilationUnit unit =
951 ParserTestCase.parseCompilationUnit("library myLib;"); 387 ParserTestCase.parseCompilationUnit("library myLib;");
952 _assertLocate( 388 _assertLocate(
953 unit, 4, 10, (node) => node is LibraryDirective, LibraryDirective); 389 unit, 4, 10, (node) => node is LibraryDirective, LibraryDirective);
954 } 390 }
955 391
956 void test_searchWithin_null() { 392 void test_searchWithin_null() {
(...skipping 20 matching lines...) Expand all
977 void test_searchWithin_offsetBeforeNode() { 413 void test_searchWithin_offsetBeforeNode() {
978 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r''' 414 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r'''
979 class A {} 415 class A {}
980 class B {}'''); 416 class B {}''');
981 NodeLocator locator = new NodeLocator(0, 0); 417 NodeLocator locator = new NodeLocator(0, 0);
982 AstNode node = locator.searchWithin(unit.declarations[1]); 418 AstNode node = locator.searchWithin(unit.declarations[1]);
983 expect(node, isNull); 419 expect(node, isNull);
984 } 420 }
985 421
986 void _assertLocate(CompilationUnit unit, int start, int end, 422 void _assertLocate(CompilationUnit unit, int start, int end,
987 Predicate<AstNode> predicate, Type expectedClass) { 423 Predicate<Object> predicate, Type expectedClass) {
988 NodeLocator locator = new NodeLocator(start, end); 424 NodeLocator locator = new NodeLocator(start, end);
989 AstNode node = locator.searchWithin(unit); 425 AstNode node = locator.searchWithin(unit);
990 expect(node, isNotNull); 426 expect(node, isNotNull);
991 expect(locator.foundNode, same(node)); 427 expect(locator.foundNode, same(node));
992 expect(node.offset <= start, isTrue, reason: "Node starts after range"); 428 expect(node.offset <= start, isTrue, reason: "Node starts after range");
993 expect(node.offset + node.length > end, isTrue, 429 expect(node.offset + node.length > end, isTrue,
994 reason: "Node ends before range"); 430 reason: "Node ends before range");
995 EngineTestCase.assertInstanceOf(predicate, expectedClass, node); 431 EngineTestCase.assertInstanceOf(predicate, expectedClass, node);
996 } 432 }
997 } 433 }
998 434
999 @reflectiveTest 435 @reflectiveTest
1000 class SimpleIdentifierTest extends ParserTestCase { 436 class ResolutionCopierTest extends EngineTestCase {
1001 void test_inDeclarationContext_catch_exception() { 437 void test_visitAdjacentStrings() {
1002 SimpleIdentifier identifier = 438 AdjacentStrings createNode() => new AdjacentStrings([
1003 AstFactory.catchClause("e").exceptionParameter; 439 new SimpleStringLiteral(null, 'hello'),
1004 expect(identifier.inDeclarationContext(), isTrue); 440 new SimpleStringLiteral(null, 'world')
1005 } 441 ]);
1006 442
1007 void test_inDeclarationContext_catch_stack() { 443 AdjacentStrings fromNode = createNode();
1008 SimpleIdentifier identifier = 444 DartType propagatedType = ElementFactory.classElement2("A").type;
1009 AstFactory.catchClause2("e", "s").stackTraceParameter; 445 fromNode.propagatedType = propagatedType;
1010 expect(identifier.inDeclarationContext(), isTrue); 446 DartType staticType = ElementFactory.classElement2("B").type;
1011 } 447 fromNode.staticType = staticType;
1012 448
1013 void test_inDeclarationContext_classDeclaration() { 449 AdjacentStrings toNode = createNode();
1014 SimpleIdentifier identifier = 450 ResolutionCopier.copyResolutionData(fromNode, toNode);
1015 AstFactory.classDeclaration(null, "C", null, null, null, null).name; 451 expect(toNode.propagatedType, same(propagatedType));
1016 expect(identifier.inDeclarationContext(), isTrue); 452 expect(toNode.staticType, same(staticType));
1017 } 453 }
1018 454
1019 void test_inDeclarationContext_classTypeAlias() { 455 void test_visitAnnotation() {
1020 SimpleIdentifier identifier = 456 String annotationName = "proxy";
1021 AstFactory.classTypeAlias("C", null, null, null, null, null).name; 457 Annotation fromNode =
1022 expect(identifier.inDeclarationContext(), isTrue); 458 AstFactory.annotation(AstFactory.identifier3(annotationName));
1023 } 459 Element element = ElementFactory.topLevelVariableElement2(annotationName);
1024 460 fromNode.element = element;
1025 void test_inDeclarationContext_constructorDeclaration() { 461 Annotation toNode =
1026 SimpleIdentifier identifier = AstFactory 462 AstFactory.annotation(AstFactory.identifier3(annotationName));
1027 .constructorDeclaration(AstFactory.identifier3("C"), "c", null, null) 463 ResolutionCopier.copyResolutionData(fromNode, toNode);
1028 .name; 464 expect(toNode.element, same(element));
1029 expect(identifier.inDeclarationContext(), isTrue); 465 }
1030 } 466
1031 467 void test_visitAsExpression() {
1032 void test_inDeclarationContext_declaredIdentifier() { 468 AsExpression fromNode = AstFactory.asExpression(
1033 DeclaredIdentifier declaredIdentifier = AstFactory.declaredIdentifier3("v"); 469 AstFactory.identifier3("x"), AstFactory.typeName4("A"));
1034 SimpleIdentifier identifier = declaredIdentifier.identifier; 470 DartType propagatedType = ElementFactory.classElement2("A").type;
1035 expect(identifier.inDeclarationContext(), isTrue); 471 fromNode.propagatedType = propagatedType;
1036 } 472 DartType staticType = ElementFactory.classElement2("B").type;
1037 473 fromNode.staticType = staticType;
1038 void test_inDeclarationContext_enumConstantDeclaration() { 474 AsExpression toNode = AstFactory.asExpression(
1039 EnumDeclaration enumDeclaration = 475 AstFactory.identifier3("x"), AstFactory.typeName4("A"));
1040 AstFactory.enumDeclaration2('MyEnum', ['CONST']); 476 ResolutionCopier.copyResolutionData(fromNode, toNode);
1041 SimpleIdentifier identifier = enumDeclaration.constants[0].name; 477 expect(toNode.propagatedType, same(propagatedType));
1042 expect(identifier.inDeclarationContext(), isTrue); 478 expect(toNode.staticType, same(staticType));
1043 } 479 }
1044 480
1045 void test_inDeclarationContext_enumDeclaration() { 481 void test_visitAssignmentExpression() {
1046 EnumDeclaration enumDeclaration = 482 AssignmentExpression fromNode = AstFactory.assignmentExpression(
1047 AstFactory.enumDeclaration2('MyEnum', ['A', 'B', 'C']); 483 AstFactory.identifier3("a"),
1048 SimpleIdentifier identifier = enumDeclaration.name; 484 TokenType.PLUS_EQ,
1049 expect(identifier.inDeclarationContext(), isTrue); 485 AstFactory.identifier3("b"));
1050 } 486 DartType propagatedType = ElementFactory.classElement2("C").type;
1051 487 MethodElement propagatedElement =
1052 void test_inDeclarationContext_fieldFormalParameter() { 488 ElementFactory.methodElement("+", propagatedType);
1053 SimpleIdentifier identifier = 489 fromNode.propagatedElement = propagatedElement;
1054 AstFactory.fieldFormalParameter2("p").identifier; 490 fromNode.propagatedType = propagatedType;
1055 expect(identifier.inDeclarationContext(), isFalse); 491 DartType staticType = ElementFactory.classElement2("C").type;
1056 } 492 MethodElement staticElement = ElementFactory.methodElement("+", staticType);
1057 493 fromNode.staticElement = staticElement;
1058 void test_inDeclarationContext_functionDeclaration() { 494 fromNode.staticType = staticType;
1059 SimpleIdentifier identifier = 495 AssignmentExpression toNode = AstFactory.assignmentExpression(
1060 AstFactory.functionDeclaration(null, null, "f", null).name; 496 AstFactory.identifier3("a"),
1061 expect(identifier.inDeclarationContext(), isTrue); 497 TokenType.PLUS_EQ,
1062 } 498 AstFactory.identifier3("b"));
1063 499 ResolutionCopier.copyResolutionData(fromNode, toNode);
1064 void test_inDeclarationContext_functionTypeAlias() { 500 expect(toNode.propagatedElement, same(propagatedElement));
1065 SimpleIdentifier identifier = 501 expect(toNode.propagatedType, same(propagatedType));
1066 AstFactory.typeAlias(null, "F", null, null).name; 502 expect(toNode.staticElement, same(staticElement));
1067 expect(identifier.inDeclarationContext(), isTrue); 503 expect(toNode.staticType, same(staticType));
1068 } 504 }
1069 505
1070 void test_inDeclarationContext_label_false() { 506 void test_visitBinaryExpression() {
1071 SimpleIdentifier identifier = 507 BinaryExpression fromNode = AstFactory.binaryExpression(
1072 AstFactory.namedExpression2("l", AstFactory.integer(0)).name.label; 508 AstFactory.identifier3("a"),
1073 expect(identifier.inDeclarationContext(), isFalse); 509 TokenType.PLUS,
1074 } 510 AstFactory.identifier3("b"));
1075 511 DartType propagatedType = ElementFactory.classElement2("C").type;
1076 void test_inDeclarationContext_label_true() { 512 MethodElement propagatedElement =
1077 Label label = AstFactory.label2("l"); 513 ElementFactory.methodElement("+", propagatedType);
1078 SimpleIdentifier identifier = label.label; 514 fromNode.propagatedElement = propagatedElement;
1079 AstFactory.labeledStatement([label], AstFactory.emptyStatement()); 515 fromNode.propagatedType = propagatedType;
1080 expect(identifier.inDeclarationContext(), isTrue); 516 DartType staticType = ElementFactory.classElement2("C").type;
1081 } 517 MethodElement staticElement = ElementFactory.methodElement("+", staticType);
1082 518 fromNode.staticElement = staticElement;
1083 void test_inDeclarationContext_methodDeclaration() { 519 fromNode.staticType = staticType;
1084 SimpleIdentifier identifier = AstFactory.identifier3("m"); 520 BinaryExpression toNode = AstFactory.binaryExpression(
1085 AstFactory.methodDeclaration2( 521 AstFactory.identifier3("a"),
1086 null, null, null, null, identifier, null, null); 522 TokenType.PLUS,
1087 expect(identifier.inDeclarationContext(), isTrue); 523 AstFactory.identifier3("b"));
1088 } 524 ResolutionCopier.copyResolutionData(fromNode, toNode);
1089 525 expect(toNode.propagatedElement, same(propagatedElement));
1090 void test_inDeclarationContext_prefix() { 526 expect(toNode.propagatedType, same(propagatedType));
1091 SimpleIdentifier identifier = 527 expect(toNode.staticElement, same(staticElement));
1092 AstFactory.importDirective3("uri", "pref").prefix; 528 expect(toNode.staticType, same(staticType));
1093 expect(identifier.inDeclarationContext(), isTrue); 529 }
1094 } 530
1095 531 void test_visitBooleanLiteral() {
1096 void test_inDeclarationContext_simpleFormalParameter() { 532 BooleanLiteral fromNode = AstFactory.booleanLiteral(true);
1097 SimpleIdentifier identifier = 533 DartType propagatedType = ElementFactory.classElement2("C").type;
1098 AstFactory.simpleFormalParameter3("p").identifier; 534 fromNode.propagatedType = propagatedType;
1099 expect(identifier.inDeclarationContext(), isTrue); 535 DartType staticType = ElementFactory.classElement2("C").type;
1100 } 536 fromNode.staticType = staticType;
1101 537 BooleanLiteral toNode = AstFactory.booleanLiteral(true);
1102 void test_inDeclarationContext_typeParameter_bound() { 538 ResolutionCopier.copyResolutionData(fromNode, toNode);
1103 TypeName bound = AstFactory.typeName4("A"); 539 expect(toNode.propagatedType, same(propagatedType));
1104 SimpleIdentifier identifier = bound.name as SimpleIdentifier; 540 expect(toNode.staticType, same(staticType));
1105 AstFactory.typeParameter2("E", bound); 541 }
1106 expect(identifier.inDeclarationContext(), isFalse); 542
1107 } 543 void test_visitCascadeExpression() {
1108 544 CascadeExpression fromNode = AstFactory.cascadeExpression(
1109 void test_inDeclarationContext_typeParameter_name() { 545 AstFactory.identifier3("a"), [AstFactory.identifier3("b")]);
1110 SimpleIdentifier identifier = AstFactory.typeParameter("E").name; 546 DartType propagatedType = ElementFactory.classElement2("C").type;
1111 expect(identifier.inDeclarationContext(), isTrue); 547 fromNode.propagatedType = propagatedType;
1112 } 548 DartType staticType = ElementFactory.classElement2("C").type;
1113 549 fromNode.staticType = staticType;
1114 void test_inDeclarationContext_variableDeclaration() { 550 CascadeExpression toNode = AstFactory.cascadeExpression(
1115 SimpleIdentifier identifier = AstFactory.variableDeclaration("v").name; 551 AstFactory.identifier3("a"), [AstFactory.identifier3("b")]);
1116 expect(identifier.inDeclarationContext(), isTrue); 552 ResolutionCopier.copyResolutionData(fromNode, toNode);
1117 } 553 expect(toNode.propagatedType, same(propagatedType));
1118 554 expect(toNode.staticType, same(staticType));
1119 void test_inGetterContext() { 555 }
1120 for (WrapperKind wrapper in WrapperKind.values) { 556
1121 for (AssignmentKind assignment in AssignmentKind.values) { 557 void test_visitCompilationUnit() {
1122 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment); 558 CompilationUnit fromNode = AstFactory.compilationUnit();
1123 if (assignment == AssignmentKind.SIMPLE_LEFT && 559 CompilationUnitElement element =
1124 wrapper != WrapperKind.PREFIXED_LEFT && 560 new CompilationUnitElementImpl("test.dart");
1125 wrapper != WrapperKind.PROPERTY_LEFT) { 561 fromNode.element = element;
1126 if (identifier.inGetterContext()) { 562 CompilationUnit toNode = AstFactory.compilationUnit();
1127 fail("Expected ${_topMostNode(identifier).toSource()} to be false"); 563 ResolutionCopier.copyResolutionData(fromNode, toNode);
1128 } 564 expect(toNode.element, same(element));
1129 } else { 565 }
1130 if (!identifier.inGetterContext()) { 566
1131 fail("Expected ${_topMostNode(identifier).toSource()} to be true"); 567 void test_visitConditionalExpression() {
1132 } 568 ConditionalExpression fromNode = AstFactory.conditionalExpression(
1133 } 569 AstFactory.identifier3("c"),
570 AstFactory.identifier3("a"),
571 AstFactory.identifier3("b"));
572 DartType propagatedType = ElementFactory.classElement2("C").type;
573 fromNode.propagatedType = propagatedType;
574 DartType staticType = ElementFactory.classElement2("C").type;
575 fromNode.staticType = staticType;
576 ConditionalExpression toNode = AstFactory.conditionalExpression(
577 AstFactory.identifier3("c"),
578 AstFactory.identifier3("a"),
579 AstFactory.identifier3("b"));
580 ResolutionCopier.copyResolutionData(fromNode, toNode);
581 expect(toNode.propagatedType, same(propagatedType));
582 expect(toNode.staticType, same(staticType));
583 }
584
585 void test_visitConstructorDeclaration() {
586 String className = "A";
587 String constructorName = "c";
588 ConstructorDeclaration fromNode = AstFactory.constructorDeclaration(
589 AstFactory.identifier3(className),
590 constructorName,
591 AstFactory.formalParameterList(),
592 null);
593 ConstructorElement element = ElementFactory.constructorElement2(
594 ElementFactory.classElement2(className), constructorName);
595 fromNode.element = element;
596 ConstructorDeclaration toNode = AstFactory.constructorDeclaration(
597 AstFactory.identifier3(className),
598 constructorName,
599 AstFactory.formalParameterList(),
600 null);
601 ResolutionCopier.copyResolutionData(fromNode, toNode);
602 expect(toNode.element, same(element));
603 }
604
605 void test_visitConstructorName() {
606 ConstructorName fromNode =
607 AstFactory.constructorName(AstFactory.typeName4("A"), "c");
608 ConstructorElement staticElement = ElementFactory.constructorElement2(
609 ElementFactory.classElement2("A"), "c");
610 fromNode.staticElement = staticElement;
611 ConstructorName toNode =
612 AstFactory.constructorName(AstFactory.typeName4("A"), "c");
613 ResolutionCopier.copyResolutionData(fromNode, toNode);
614 expect(toNode.staticElement, same(staticElement));
615 }
616
617 void test_visitDoubleLiteral() {
618 DoubleLiteral fromNode = AstFactory.doubleLiteral(1.0);
619 DartType propagatedType = ElementFactory.classElement2("C").type;
620 fromNode.propagatedType = propagatedType;
621 DartType staticType = ElementFactory.classElement2("C").type;
622 fromNode.staticType = staticType;
623 DoubleLiteral toNode = AstFactory.doubleLiteral(1.0);
624 ResolutionCopier.copyResolutionData(fromNode, toNode);
625 expect(toNode.propagatedType, same(propagatedType));
626 expect(toNode.staticType, same(staticType));
627 }
628
629 void test_visitExportDirective() {
630 ExportDirective fromNode = AstFactory.exportDirective2("dart:uri");
631 ExportElement element = new ExportElementImpl(-1);
632 fromNode.element = element;
633 ExportDirective toNode = AstFactory.exportDirective2("dart:uri");
634 ResolutionCopier.copyResolutionData(fromNode, toNode);
635 expect(toNode.element, same(element));
636 }
637
638 void test_visitFunctionExpression() {
639 FunctionExpression fromNode = AstFactory.functionExpression2(
640 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody());
641 MethodElement element = ElementFactory.methodElement(
642 "m", ElementFactory.classElement2("C").type);
643 fromNode.element = element;
644 DartType propagatedType = ElementFactory.classElement2("C").type;
645 fromNode.propagatedType = propagatedType;
646 DartType staticType = ElementFactory.classElement2("C").type;
647 fromNode.staticType = staticType;
648 FunctionExpression toNode = AstFactory.functionExpression2(
649 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody());
650 ResolutionCopier.copyResolutionData(fromNode, toNode);
651 expect(toNode.element, same(element));
652 expect(toNode.propagatedType, same(propagatedType));
653 expect(toNode.staticType, same(staticType));
654 }
655
656 void test_visitFunctionExpressionInvocation() {
657 FunctionExpressionInvocation fromNode =
658 AstFactory.functionExpressionInvocation(AstFactory.identifier3("f"));
659 MethodElement propagatedElement = ElementFactory.methodElement(
660 "m", ElementFactory.classElement2("C").type);
661 fromNode.propagatedElement = propagatedElement;
662 MethodElement staticElement = ElementFactory.methodElement(
663 "m", ElementFactory.classElement2("C").type);
664 fromNode.staticElement = staticElement;
665 FunctionExpressionInvocation toNode =
666 AstFactory.functionExpressionInvocation(AstFactory.identifier3("f"));
667 ClassElement elementT = ElementFactory.classElement2('T');
668 fromNode.typeArguments =
669 AstFactory.typeArgumentList(<TypeName>[AstFactory.typeName(elementT)]);
670 toNode.typeArguments =
671 AstFactory.typeArgumentList(<TypeName>[AstFactory.typeName4('T')]);
672
673 _copyAndVerifyInvocation(fromNode, toNode);
674
675 expect(toNode.propagatedElement, same(propagatedElement));
676 expect(toNode.staticElement, same(staticElement));
677 }
678
679 void test_visitImportDirective() {
680 ImportDirective fromNode = AstFactory.importDirective3("dart:uri", null);
681 ImportElement element = new ImportElementImpl(0);
682 fromNode.element = element;
683 ImportDirective toNode = AstFactory.importDirective3("dart:uri", null);
684 ResolutionCopier.copyResolutionData(fromNode, toNode);
685 expect(toNode.element, same(element));
686 }
687
688 void test_visitIndexExpression() {
689 IndexExpression fromNode = AstFactory.indexExpression(
690 AstFactory.identifier3("a"), AstFactory.integer(0));
691 MethodElement propagatedElement = ElementFactory.methodElement(
692 "m", ElementFactory.classElement2("C").type);
693 MethodElement staticElement = ElementFactory.methodElement(
694 "m", ElementFactory.classElement2("C").type);
695 AuxiliaryElements auxiliaryElements =
696 new AuxiliaryElements(staticElement, propagatedElement);
697 fromNode.auxiliaryElements = auxiliaryElements;
698 fromNode.propagatedElement = propagatedElement;
699 DartType propagatedType = ElementFactory.classElement2("C").type;
700 fromNode.propagatedType = propagatedType;
701 fromNode.staticElement = staticElement;
702 DartType staticType = ElementFactory.classElement2("C").type;
703 fromNode.staticType = staticType;
704 IndexExpression toNode = AstFactory.indexExpression(
705 AstFactory.identifier3("a"), AstFactory.integer(0));
706 ResolutionCopier.copyResolutionData(fromNode, toNode);
707 expect(toNode.auxiliaryElements, same(auxiliaryElements));
708 expect(toNode.propagatedElement, same(propagatedElement));
709 expect(toNode.propagatedType, same(propagatedType));
710 expect(toNode.staticElement, same(staticElement));
711 expect(toNode.staticType, same(staticType));
712 }
713
714 void test_visitInstanceCreationExpression() {
715 InstanceCreationExpression fromNode = AstFactory
716 .instanceCreationExpression2(Keyword.NEW, AstFactory.typeName4("C"));
717 DartType propagatedType = ElementFactory.classElement2("C").type;
718 fromNode.propagatedType = propagatedType;
719 ConstructorElement staticElement = ElementFactory.constructorElement2(
720 ElementFactory.classElement2("C"), null);
721 fromNode.staticElement = staticElement;
722 DartType staticType = ElementFactory.classElement2("C").type;
723 fromNode.staticType = staticType;
724 InstanceCreationExpression toNode = AstFactory.instanceCreationExpression2(
725 Keyword.NEW, AstFactory.typeName4("C"));
726 ResolutionCopier.copyResolutionData(fromNode, toNode);
727 expect(toNode.propagatedType, same(propagatedType));
728 expect(toNode.staticElement, same(staticElement));
729 expect(toNode.staticType, same(staticType));
730 }
731
732 void test_visitIntegerLiteral() {
733 IntegerLiteral fromNode = AstFactory.integer(2);
734 DartType propagatedType = ElementFactory.classElement2("C").type;
735 fromNode.propagatedType = propagatedType;
736 DartType staticType = ElementFactory.classElement2("C").type;
737 fromNode.staticType = staticType;
738 IntegerLiteral toNode = AstFactory.integer(2);
739 ResolutionCopier.copyResolutionData(fromNode, toNode);
740 expect(toNode.propagatedType, same(propagatedType));
741 expect(toNode.staticType, same(staticType));
742 }
743
744 void test_visitIsExpression() {
745 IsExpression fromNode = AstFactory.isExpression(
746 AstFactory.identifier3("x"), false, AstFactory.typeName4("A"));
747 DartType propagatedType = ElementFactory.classElement2("C").type;
748 fromNode.propagatedType = propagatedType;
749 DartType staticType = ElementFactory.classElement2("C").type;
750 fromNode.staticType = staticType;
751 IsExpression toNode = AstFactory.isExpression(
752 AstFactory.identifier3("x"), false, AstFactory.typeName4("A"));
753 ResolutionCopier.copyResolutionData(fromNode, toNode);
754 expect(toNode.propagatedType, same(propagatedType));
755 expect(toNode.staticType, same(staticType));
756 }
757
758 void test_visitLibraryIdentifier() {
759 LibraryIdentifier fromNode =
760 AstFactory.libraryIdentifier([AstFactory.identifier3("lib")]);
761 DartType propagatedType = ElementFactory.classElement2("C").type;
762 fromNode.propagatedType = propagatedType;
763 DartType staticType = ElementFactory.classElement2("C").type;
764 fromNode.staticType = staticType;
765 LibraryIdentifier toNode =
766 AstFactory.libraryIdentifier([AstFactory.identifier3("lib")]);
767 ResolutionCopier.copyResolutionData(fromNode, toNode);
768 expect(toNode.propagatedType, same(propagatedType));
769 expect(toNode.staticType, same(staticType));
770 }
771
772 void test_visitListLiteral() {
773 ListLiteral fromNode = AstFactory.listLiteral();
774 DartType propagatedType = ElementFactory.classElement2("C").type;
775 fromNode.propagatedType = propagatedType;
776 DartType staticType = ElementFactory.classElement2("C").type;
777 fromNode.staticType = staticType;
778 ListLiteral toNode = AstFactory.listLiteral();
779 ResolutionCopier.copyResolutionData(fromNode, toNode);
780 expect(toNode.propagatedType, same(propagatedType));
781 expect(toNode.staticType, same(staticType));
782 }
783
784 void test_visitMapLiteral() {
785 MapLiteral fromNode = AstFactory.mapLiteral2();
786 DartType propagatedType = ElementFactory.classElement2("C").type;
787 fromNode.propagatedType = propagatedType;
788 DartType staticType = ElementFactory.classElement2("C").type;
789 fromNode.staticType = staticType;
790 MapLiteral toNode = AstFactory.mapLiteral2();
791 ResolutionCopier.copyResolutionData(fromNode, toNode);
792 expect(toNode.propagatedType, same(propagatedType));
793 expect(toNode.staticType, same(staticType));
794 }
795
796 void test_visitMethodInvocation() {
797 MethodInvocation fromNode = AstFactory.methodInvocation2("m");
798 MethodInvocation toNode = AstFactory.methodInvocation2("m");
799 ClassElement elementT = ElementFactory.classElement2('T');
800 fromNode.typeArguments =
801 AstFactory.typeArgumentList(<TypeName>[AstFactory.typeName(elementT)]);
802 toNode.typeArguments =
803 AstFactory.typeArgumentList(<TypeName>[AstFactory.typeName4('T')]);
804 _copyAndVerifyInvocation(fromNode, toNode);
805 }
806
807 void test_visitNamedExpression() {
808 NamedExpression fromNode =
809 AstFactory.namedExpression2("n", AstFactory.integer(0));
810 DartType propagatedType = ElementFactory.classElement2("C").type;
811 fromNode.propagatedType = propagatedType;
812 DartType staticType = ElementFactory.classElement2("C").type;
813 fromNode.staticType = staticType;
814 NamedExpression toNode =
815 AstFactory.namedExpression2("n", AstFactory.integer(0));
816 ResolutionCopier.copyResolutionData(fromNode, toNode);
817 expect(toNode.propagatedType, same(propagatedType));
818 expect(toNode.staticType, same(staticType));
819 }
820
821 void test_visitNullLiteral() {
822 NullLiteral fromNode = AstFactory.nullLiteral();
823 DartType propagatedType = ElementFactory.classElement2("C").type;
824 fromNode.propagatedType = propagatedType;
825 DartType staticType = ElementFactory.classElement2("C").type;
826 fromNode.staticType = staticType;
827 NullLiteral toNode = AstFactory.nullLiteral();
828 ResolutionCopier.copyResolutionData(fromNode, toNode);
829 expect(toNode.propagatedType, same(propagatedType));
830 expect(toNode.staticType, same(staticType));
831 }
832
833 void test_visitParenthesizedExpression() {
834 ParenthesizedExpression fromNode =
835 AstFactory.parenthesizedExpression(AstFactory.integer(0));
836 DartType propagatedType = ElementFactory.classElement2("C").type;
837 fromNode.propagatedType = propagatedType;
838 DartType staticType = ElementFactory.classElement2("C").type;
839 fromNode.staticType = staticType;
840 ParenthesizedExpression toNode =
841 AstFactory.parenthesizedExpression(AstFactory.integer(0));
842 ResolutionCopier.copyResolutionData(fromNode, toNode);
843 expect(toNode.propagatedType, same(propagatedType));
844 expect(toNode.staticType, same(staticType));
845 }
846
847 void test_visitPartDirective() {
848 PartDirective fromNode = AstFactory.partDirective2("part.dart");
849 LibraryElement element = new LibraryElementImpl.forNode(
850 null, AstFactory.libraryIdentifier2(["lib"]));
851 fromNode.element = element;
852 PartDirective toNode = AstFactory.partDirective2("part.dart");
853 ResolutionCopier.copyResolutionData(fromNode, toNode);
854 expect(toNode.element, same(element));
855 }
856
857 void test_visitPartOfDirective() {
858 PartOfDirective fromNode =
859 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["lib"]));
860 LibraryElement element = new LibraryElementImpl.forNode(
861 null, AstFactory.libraryIdentifier2(["lib"]));
862 fromNode.element = element;
863 PartOfDirective toNode =
864 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["lib"]));
865 ResolutionCopier.copyResolutionData(fromNode, toNode);
866 expect(toNode.element, same(element));
867 }
868
869 void test_visitPostfixExpression() {
870 String variableName = "x";
871 PostfixExpression fromNode = AstFactory.postfixExpression(
872 AstFactory.identifier3(variableName), TokenType.PLUS_PLUS);
873 MethodElement propagatedElement = ElementFactory.methodElement(
874 "+", ElementFactory.classElement2("C").type);
875 fromNode.propagatedElement = propagatedElement;
876 DartType propagatedType = ElementFactory.classElement2("C").type;
877 fromNode.propagatedType = propagatedType;
878 MethodElement staticElement = ElementFactory.methodElement(
879 "+", ElementFactory.classElement2("C").type);
880 fromNode.staticElement = staticElement;
881 DartType staticType = ElementFactory.classElement2("C").type;
882 fromNode.staticType = staticType;
883 PostfixExpression toNode = AstFactory.postfixExpression(
884 AstFactory.identifier3(variableName), TokenType.PLUS_PLUS);
885 ResolutionCopier.copyResolutionData(fromNode, toNode);
886 expect(toNode.propagatedElement, same(propagatedElement));
887 expect(toNode.propagatedType, same(propagatedType));
888 expect(toNode.staticElement, same(staticElement));
889 expect(toNode.staticType, same(staticType));
890 }
891
892 void test_visitPrefixedIdentifier() {
893 PrefixedIdentifier fromNode = AstFactory.identifier5("p", "f");
894 DartType propagatedType = ElementFactory.classElement2("C").type;
895 fromNode.propagatedType = propagatedType;
896 DartType staticType = ElementFactory.classElement2("C").type;
897 fromNode.staticType = staticType;
898 PrefixedIdentifier toNode = AstFactory.identifier5("p", "f");
899 ResolutionCopier.copyResolutionData(fromNode, toNode);
900 expect(toNode.propagatedType, same(propagatedType));
901 expect(toNode.staticType, same(staticType));
902 }
903
904 void test_visitPrefixExpression() {
905 PrefixExpression fromNode = AstFactory.prefixExpression(
906 TokenType.PLUS_PLUS, AstFactory.identifier3("x"));
907 MethodElement propagatedElement = ElementFactory.methodElement(
908 "+", ElementFactory.classElement2("C").type);
909 DartType propagatedType = ElementFactory.classElement2("C").type;
910 fromNode.propagatedElement = propagatedElement;
911 fromNode.propagatedType = propagatedType;
912 DartType staticType = ElementFactory.classElement2("C").type;
913 MethodElement staticElement = ElementFactory.methodElement(
914 "+", ElementFactory.classElement2("C").type);
915 fromNode.staticElement = staticElement;
916 fromNode.staticType = staticType;
917 PrefixExpression toNode = AstFactory.prefixExpression(
918 TokenType.PLUS_PLUS, AstFactory.identifier3("x"));
919 ResolutionCopier.copyResolutionData(fromNode, toNode);
920 expect(toNode.propagatedElement, same(propagatedElement));
921 expect(toNode.propagatedType, same(propagatedType));
922 expect(toNode.staticElement, same(staticElement));
923 expect(toNode.staticType, same(staticType));
924 }
925
926 void test_visitPropertyAccess() {
927 PropertyAccess fromNode =
928 AstFactory.propertyAccess2(AstFactory.identifier3("x"), "y");
929 DartType propagatedType = ElementFactory.classElement2("C").type;
930 fromNode.propagatedType = propagatedType;
931 DartType staticType = ElementFactory.classElement2("C").type;
932 fromNode.staticType = staticType;
933 PropertyAccess toNode =
934 AstFactory.propertyAccess2(AstFactory.identifier3("x"), "y");
935 ResolutionCopier.copyResolutionData(fromNode, toNode);
936 expect(toNode.propagatedType, same(propagatedType));
937 expect(toNode.staticType, same(staticType));
938 }
939
940 void test_visitRedirectingConstructorInvocation() {
941 RedirectingConstructorInvocation fromNode =
942 AstFactory.redirectingConstructorInvocation();
943 ConstructorElement staticElement = ElementFactory.constructorElement2(
944 ElementFactory.classElement2("C"), null);
945 fromNode.staticElement = staticElement;
946 RedirectingConstructorInvocation toNode =
947 AstFactory.redirectingConstructorInvocation();
948 ResolutionCopier.copyResolutionData(fromNode, toNode);
949 expect(toNode.staticElement, same(staticElement));
950 }
951
952 void test_visitRethrowExpression() {
953 RethrowExpression fromNode = AstFactory.rethrowExpression();
954 DartType propagatedType = ElementFactory.classElement2("C").type;
955 fromNode.propagatedType = propagatedType;
956 DartType staticType = ElementFactory.classElement2("C").type;
957 fromNode.staticType = staticType;
958 RethrowExpression toNode = AstFactory.rethrowExpression();
959 ResolutionCopier.copyResolutionData(fromNode, toNode);
960 expect(toNode.propagatedType, same(propagatedType));
961 expect(toNode.staticType, same(staticType));
962 }
963
964 void test_visitSimpleIdentifier() {
965 SimpleIdentifier fromNode = AstFactory.identifier3("x");
966 MethodElement propagatedElement = ElementFactory.methodElement(
967 "m", ElementFactory.classElement2("C").type);
968 MethodElement staticElement = ElementFactory.methodElement(
969 "m", ElementFactory.classElement2("C").type);
970 AuxiliaryElements auxiliaryElements =
971 new AuxiliaryElements(staticElement, propagatedElement);
972 fromNode.auxiliaryElements = auxiliaryElements;
973 fromNode.propagatedElement = propagatedElement;
974 DartType propagatedType = ElementFactory.classElement2("C").type;
975 fromNode.propagatedType = propagatedType;
976 fromNode.staticElement = staticElement;
977 DartType staticType = ElementFactory.classElement2("C").type;
978 fromNode.staticType = staticType;
979 SimpleIdentifier toNode = AstFactory.identifier3("x");
980 ResolutionCopier.copyResolutionData(fromNode, toNode);
981 expect(toNode.auxiliaryElements, same(auxiliaryElements));
982 expect(toNode.propagatedElement, same(propagatedElement));
983 expect(toNode.propagatedType, same(propagatedType));
984 expect(toNode.staticElement, same(staticElement));
985 expect(toNode.staticType, same(staticType));
986 }
987
988 void test_visitSimpleStringLiteral() {
989 SimpleStringLiteral fromNode = AstFactory.string2("abc");
990 DartType propagatedType = ElementFactory.classElement2("C").type;
991 fromNode.propagatedType = propagatedType;
992 DartType staticType = ElementFactory.classElement2("C").type;
993 fromNode.staticType = staticType;
994 SimpleStringLiteral toNode = AstFactory.string2("abc");
995 ResolutionCopier.copyResolutionData(fromNode, toNode);
996 expect(toNode.propagatedType, same(propagatedType));
997 expect(toNode.staticType, same(staticType));
998 }
999
1000 void test_visitStringInterpolation() {
1001 StringInterpolation fromNode =
1002 AstFactory.string([AstFactory.interpolationString("a", "'a'")]);
1003 DartType propagatedType = ElementFactory.classElement2("C").type;
1004 fromNode.propagatedType = propagatedType;
1005 DartType staticType = ElementFactory.classElement2("C").type;
1006 fromNode.staticType = staticType;
1007 StringInterpolation toNode =
1008 AstFactory.string([AstFactory.interpolationString("a", "'a'")]);
1009 ResolutionCopier.copyResolutionData(fromNode, toNode);
1010 expect(toNode.propagatedType, same(propagatedType));
1011 expect(toNode.staticType, same(staticType));
1012 }
1013
1014 void test_visitSuperConstructorInvocation() {
1015 SuperConstructorInvocation fromNode =
1016 AstFactory.superConstructorInvocation();
1017 ConstructorElement staticElement = ElementFactory.constructorElement2(
1018 ElementFactory.classElement2("C"), null);
1019 fromNode.staticElement = staticElement;
1020 SuperConstructorInvocation toNode = AstFactory.superConstructorInvocation();
1021 ResolutionCopier.copyResolutionData(fromNode, toNode);
1022 expect(toNode.staticElement, same(staticElement));
1023 }
1024
1025 void test_visitSuperExpression() {
1026 SuperExpression fromNode = AstFactory.superExpression();
1027 DartType propagatedType = ElementFactory.classElement2("C").type;
1028 fromNode.propagatedType = propagatedType;
1029 DartType staticType = ElementFactory.classElement2("C").type;
1030 fromNode.staticType = staticType;
1031 SuperExpression toNode = AstFactory.superExpression();
1032 ResolutionCopier.copyResolutionData(fromNode, toNode);
1033 expect(toNode.propagatedType, same(propagatedType));
1034 expect(toNode.staticType, same(staticType));
1035 }
1036
1037 void test_visitSymbolLiteral() {
1038 SymbolLiteral fromNode = AstFactory.symbolLiteral(["s"]);
1039 DartType propagatedType = ElementFactory.classElement2("C").type;
1040 fromNode.propagatedType = propagatedType;
1041 DartType staticType = ElementFactory.classElement2("C").type;
1042 fromNode.staticType = staticType;
1043 SymbolLiteral toNode = AstFactory.symbolLiteral(["s"]);
1044 ResolutionCopier.copyResolutionData(fromNode, toNode);
1045 expect(toNode.propagatedType, same(propagatedType));
1046 expect(toNode.staticType, same(staticType));
1047 }
1048
1049 void test_visitThisExpression() {
1050 ThisExpression fromNode = AstFactory.thisExpression();
1051 DartType propagatedType = ElementFactory.classElement2("C").type;
1052 fromNode.propagatedType = propagatedType;
1053 DartType staticType = ElementFactory.classElement2("C").type;
1054 fromNode.staticType = staticType;
1055 ThisExpression toNode = AstFactory.thisExpression();
1056 ResolutionCopier.copyResolutionData(fromNode, toNode);
1057 expect(toNode.propagatedType, same(propagatedType));
1058 expect(toNode.staticType, same(staticType));
1059 }
1060
1061 void test_visitThrowExpression() {
1062 ThrowExpression fromNode = AstFactory.throwExpression();
1063 DartType propagatedType = ElementFactory.classElement2("C").type;
1064 fromNode.propagatedType = propagatedType;
1065 DartType staticType = ElementFactory.classElement2("C").type;
1066 fromNode.staticType = staticType;
1067 ThrowExpression toNode = AstFactory.throwExpression();
1068 ResolutionCopier.copyResolutionData(fromNode, toNode);
1069 expect(toNode.propagatedType, same(propagatedType));
1070 expect(toNode.staticType, same(staticType));
1071 }
1072
1073 void test_visitTypeName() {
1074 TypeName fromNode = AstFactory.typeName4("C");
1075 DartType type = ElementFactory.classElement2("C").type;
1076 fromNode.type = type;
1077 TypeName toNode = AstFactory.typeName4("C");
1078 ResolutionCopier.copyResolutionData(fromNode, toNode);
1079 expect(toNode.type, same(type));
1080 }
1081
1082 void _copyAndVerifyInvocation(
1083 InvocationExpression fromNode, InvocationExpression toNode) {
1084 DartType propagatedType = ElementFactory.classElement2("C").type;
1085 fromNode.propagatedType = propagatedType;
1086 DartType staticType = ElementFactory.classElement2("C").type;
1087 fromNode.staticType = staticType;
1088
1089 DartType propagatedInvokeType = ElementFactory.classElement2("C").type;
1090 fromNode.propagatedInvokeType = propagatedInvokeType;
1091 DartType staticInvokeType = ElementFactory.classElement2("C").type;
1092 fromNode.staticInvokeType = staticInvokeType;
1093
1094 ResolutionCopier.copyResolutionData(fromNode, toNode);
1095 expect(toNode.propagatedType, same(propagatedType));
1096 expect(toNode.staticType, same(staticType));
1097 expect(toNode.propagatedInvokeType, same(propagatedInvokeType));
1098 expect(toNode.staticInvokeType, same(staticInvokeType));
1099 List<TypeName> fromTypeArguments = toNode.typeArguments.arguments;
1100 List<TypeName> toTypeArguments = fromNode.typeArguments.arguments;
1101 if (fromTypeArguments != null) {
1102 for (int i = 0; i < fromTypeArguments.length; i++) {
1103 TypeName toArgument = fromTypeArguments[i];
1104 TypeName fromArgument = toTypeArguments[i];
1105 expect(toArgument.type, same(fromArgument.type));
1134 } 1106 }
1135 } 1107 }
1136 } 1108 }
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 } 1109 }
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 1110
1652 @reflectiveTest 1111 @reflectiveTest
1653 class ToSourceVisitorTest extends EngineTestCase { 1112 class ToSourceVisitorTest extends EngineTestCase {
1654 void test_visitAdjacentStrings() { 1113 void test_visitAdjacentStrings() {
1655 _assertSource( 1114 _assertSource(
1656 "'a' 'b'", 1115 "'a' 'b'",
1657 AstFactory.adjacentStrings( 1116 AstFactory.adjacentStrings(
1658 [AstFactory.string2("a"), AstFactory.string2("b")])); 1117 [AstFactory.string2("a"), AstFactory.string2("b")]));
1659 } 1118 }
1660 1119
(...skipping 20 matching lines...) Expand all
1681 "e as T", 1140 "e as T",
1682 AstFactory.asExpression( 1141 AstFactory.asExpression(
1683 AstFactory.identifier3("e"), AstFactory.typeName4("T"))); 1142 AstFactory.identifier3("e"), AstFactory.typeName4("T")));
1684 } 1143 }
1685 1144
1686 void test_visitAssertStatement() { 1145 void test_visitAssertStatement() {
1687 _assertSource( 1146 _assertSource(
1688 "assert (a);", AstFactory.assertStatement(AstFactory.identifier3("a"))); 1147 "assert (a);", AstFactory.assertStatement(AstFactory.identifier3("a")));
1689 } 1148 }
1690 1149
1150 void test_visitAssertStatement_withMessage() {
1151 _assertSource(
1152 "assert (a, b);",
1153 AstFactory.assertStatement(
1154 AstFactory.identifier3("a"), AstFactory.identifier3('b')));
1155 }
1156
1691 void test_visitAssignmentExpression() { 1157 void test_visitAssignmentExpression() {
1692 _assertSource( 1158 _assertSource(
1693 "a = b", 1159 "a = b",
1694 AstFactory.assignmentExpression(AstFactory.identifier3("a"), 1160 AstFactory.assignmentExpression(AstFactory.identifier3("a"),
1695 TokenType.EQ, AstFactory.identifier3("b"))); 1161 TokenType.EQ, AstFactory.identifier3("b")));
1696 } 1162 }
1697 1163
1698 void test_visitAwaitExpression() { 1164 void test_visitAwaitExpression() {
1699 _assertSource( 1165 _assertSource(
1700 "await e", AstFactory.awaitExpression(AstFactory.identifier3("e"))); 1166 "await e", AstFactory.awaitExpression(AstFactory.identifier3("e")));
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after
2283 } 1749 }
2284 1750
2285 void test_visitContinueStatement_label() { 1751 void test_visitContinueStatement_label() {
2286 _assertSource("continue l;", AstFactory.continueStatement("l")); 1752 _assertSource("continue l;", AstFactory.continueStatement("l"));
2287 } 1753 }
2288 1754
2289 void test_visitContinueStatement_noLabel() { 1755 void test_visitContinueStatement_noLabel() {
2290 _assertSource("continue;", AstFactory.continueStatement()); 1756 _assertSource("continue;", AstFactory.continueStatement());
2291 } 1757 }
2292 1758
1759 void test_visitDefaultFormalParameter_annotation() {
1760 DefaultFormalParameter parameter = AstFactory.positionalFormalParameter(
1761 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0));
1762 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
1763 _assertSource('@A p = 0', parameter);
1764 }
1765
2293 void test_visitDefaultFormalParameter_named_noValue() { 1766 void test_visitDefaultFormalParameter_named_noValue() {
2294 _assertSource( 1767 _assertSource(
2295 "p", 1768 "p",
2296 AstFactory.namedFormalParameter( 1769 AstFactory.namedFormalParameter(
2297 AstFactory.simpleFormalParameter3("p"), null)); 1770 AstFactory.simpleFormalParameter3("p"), null));
2298 } 1771 }
2299 1772
2300 void test_visitDefaultFormalParameter_named_value() { 1773 void test_visitDefaultFormalParameter_named_value() {
2301 _assertSource( 1774 _assertSource(
2302 "p : 0", 1775 "p : 0",
2303 AstFactory.namedFormalParameter( 1776 AstFactory.namedFormalParameter(
2304 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0))); 1777 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0)));
2305 } 1778 }
2306 1779
2307 void test_visitDefaultFormalParameter_positional_noValue() { 1780 void test_visitDefaultFormalParameter_positional_noValue() {
2308 _assertSource( 1781 _assertSource(
2309 "p", 1782 "p",
2310 AstFactory.positionalFormalParameter( 1783 AstFactory.positionalFormalParameter(
2311 AstFactory.simpleFormalParameter3("p"), null)); 1784 AstFactory.simpleFormalParameter3("p"), null));
2312 } 1785 }
2313 1786
2314 void test_visitDefaultFormalParameter_positional_value() { 1787 void test_visitDefaultFormalParameter_positional_value() {
2315 _assertSource( 1788 _assertSource(
2316 "p = 0", 1789 "p = 0",
2317 AstFactory.positionalFormalParameter( 1790 AstFactory.positionalFormalParameter(
2318 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0))); 1791 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0)));
2319 } 1792 }
2320 1793
2321 void test_visitDefaultFormalParameter_annotation() {
2322 DefaultFormalParameter parameter = AstFactory.positionalFormalParameter(
2323 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0));
2324 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
2325 _assertSource('@A p = 0', parameter);
2326 }
2327
2328 void test_visitDoStatement() { 1794 void test_visitDoStatement() {
2329 _assertSource( 1795 _assertSource(
2330 "do {} while (c);", 1796 "do {} while (c);",
2331 AstFactory.doStatement( 1797 AstFactory.doStatement(
2332 AstFactory.block(), AstFactory.identifier3("c"))); 1798 AstFactory.block(), AstFactory.identifier3("c")));
2333 } 1799 }
2334 1800
2335 void test_visitDoubleLiteral() { 1801 void test_visitDoubleLiteral() {
2336 _assertSource("4.2", AstFactory.doubleLiteral(4.2)); 1802 _assertSource("4.2", AstFactory.doubleLiteral(4.2));
2337 } 1803 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
2416 } 1882 }
2417 1883
2418 void test_visitFieldDeclaration_withMetadata() { 1884 void test_visitFieldDeclaration_withMetadata() {
2419 FieldDeclaration declaration = AstFactory.fieldDeclaration2( 1885 FieldDeclaration declaration = AstFactory.fieldDeclaration2(
2420 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]); 1886 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]);
2421 declaration.metadata 1887 declaration.metadata
2422 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); 1888 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2423 _assertSource("@deprecated var a;", declaration); 1889 _assertSource("@deprecated var a;", declaration);
2424 } 1890 }
2425 1891
1892 void test_visitFieldFormalParameter_annotation() {
1893 FieldFormalParameter parameter = AstFactory.fieldFormalParameter2('f');
1894 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
1895 _assertSource('@A this.f', parameter);
1896 }
1897
2426 void test_visitFieldFormalParameter_functionTyped() { 1898 void test_visitFieldFormalParameter_functionTyped() {
2427 _assertSource( 1899 _assertSource(
2428 "A this.a(b)", 1900 "A this.a(b)",
2429 AstFactory.fieldFormalParameter( 1901 AstFactory.fieldFormalParameter(
2430 null, 1902 null,
2431 AstFactory.typeName4("A"), 1903 AstFactory.typeName4("A"),
2432 "a", 1904 "a",
2433 AstFactory.formalParameterList( 1905 AstFactory.formalParameterList(
2434 [AstFactory.simpleFormalParameter3("b")]))); 1906 [AstFactory.simpleFormalParameter3("b")])));
2435 } 1907 }
(...skipping 24 matching lines...) Expand all
2460 "final A this.a", 1932 "final A this.a",
2461 AstFactory.fieldFormalParameter( 1933 AstFactory.fieldFormalParameter(
2462 Keyword.FINAL, AstFactory.typeName4("A"), "a")); 1934 Keyword.FINAL, AstFactory.typeName4("A"), "a"));
2463 } 1935 }
2464 1936
2465 void test_visitFieldFormalParameter_type() { 1937 void test_visitFieldFormalParameter_type() {
2466 _assertSource("A this.a", 1938 _assertSource("A this.a",
2467 AstFactory.fieldFormalParameter(null, AstFactory.typeName4("A"), "a")); 1939 AstFactory.fieldFormalParameter(null, AstFactory.typeName4("A"), "a"));
2468 } 1940 }
2469 1941
2470 void test_visitFieldFormalParameter_annotation() {
2471 FieldFormalParameter parameter = AstFactory.fieldFormalParameter2('f');
2472 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
2473 _assertSource('@A this.f', parameter);
2474 }
2475
2476 void test_visitForEachStatement_declared() { 1942 void test_visitForEachStatement_declared() {
2477 _assertSource( 1943 _assertSource(
2478 "for (var a in b) {}", 1944 "for (var a in b) {}",
2479 AstFactory.forEachStatement(AstFactory.declaredIdentifier3("a"), 1945 AstFactory.forEachStatement(AstFactory.declaredIdentifier3("a"),
2480 AstFactory.identifier3("b"), AstFactory.block())); 1946 AstFactory.identifier3("b"), AstFactory.block()));
2481 } 1947 }
2482 1948
2483 void test_visitForEachStatement_variable() { 1949 void test_visitForEachStatement_variable() {
2484 _assertSource( 1950 _assertSource(
2485 "for (a in b) {}", 1951 "for (a in b) {}",
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
2900 } 2366 }
2901 2367
2902 void test_visitFunctionTypeAlias_withMetadata() { 2368 void test_visitFunctionTypeAlias_withMetadata() {
2903 FunctionTypeAlias declaration = AstFactory.typeAlias( 2369 FunctionTypeAlias declaration = AstFactory.typeAlias(
2904 AstFactory.typeName4("A"), "F", null, AstFactory.formalParameterList()); 2370 AstFactory.typeName4("A"), "F", null, AstFactory.formalParameterList());
2905 declaration.metadata 2371 declaration.metadata
2906 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); 2372 .add(AstFactory.annotation(AstFactory.identifier3("deprecated")));
2907 _assertSource("@deprecated typedef A F();", declaration); 2373 _assertSource("@deprecated typedef A F();", declaration);
2908 } 2374 }
2909 2375
2376 void test_visitFunctionTypedFormalParameter_annotation() {
2377 FunctionTypedFormalParameter parameter =
2378 AstFactory.functionTypedFormalParameter(null, "f");
2379 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
2380 _assertSource('@A f()', parameter);
2381 }
2382
2910 void test_visitFunctionTypedFormalParameter_noType() { 2383 void test_visitFunctionTypedFormalParameter_noType() {
2911 _assertSource("f()", AstFactory.functionTypedFormalParameter(null, "f")); 2384 _assertSource("f()", AstFactory.functionTypedFormalParameter(null, "f"));
2912 } 2385 }
2913 2386
2914 void test_visitFunctionTypedFormalParameter_annotation() {
2915 FunctionTypedFormalParameter parameter = AstFactory.functionTypedFormalParam eter(null, "f");
2916 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
2917 _assertSource('@A f()', parameter);
2918 }
2919
2920 void test_visitFunctionTypedFormalParameter_type() { 2387 void test_visitFunctionTypedFormalParameter_type() {
2921 _assertSource( 2388 _assertSource(
2922 "T f()", 2389 "T f()",
2923 AstFactory.functionTypedFormalParameter( 2390 AstFactory.functionTypedFormalParameter(
2924 AstFactory.typeName4("T"), "f")); 2391 AstFactory.typeName4("T"), "f"));
2925 } 2392 }
2926 2393
2927 void test_visitFunctionTypedFormalParameter_typeParameters() { 2394 void test_visitFunctionTypedFormalParameter_typeParameters() {
2928 _assertSource( 2395 _assertSource(
2929 "T f<E>()", 2396 "T f<E>()",
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after
3517 2984
3518 void test_visitReturnStatement_noExpression() { 2985 void test_visitReturnStatement_noExpression() {
3519 _assertSource("return;", AstFactory.returnStatement()); 2986 _assertSource("return;", AstFactory.returnStatement());
3520 } 2987 }
3521 2988
3522 void test_visitScriptTag() { 2989 void test_visitScriptTag() {
3523 String scriptTag = "!#/bin/dart.exe"; 2990 String scriptTag = "!#/bin/dart.exe";
3524 _assertSource(scriptTag, AstFactory.scriptTag(scriptTag)); 2991 _assertSource(scriptTag, AstFactory.scriptTag(scriptTag));
3525 } 2992 }
3526 2993
2994 void test_visitSimpleFormalParameter_annotation() {
2995 SimpleFormalParameter parameter = AstFactory.simpleFormalParameter3('x');
2996 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
2997 _assertSource('@A x', parameter);
2998 }
2999
3527 void test_visitSimpleFormalParameter_keyword() { 3000 void test_visitSimpleFormalParameter_keyword() {
3528 _assertSource("var a", AstFactory.simpleFormalParameter(Keyword.VAR, "a")); 3001 _assertSource("var a", AstFactory.simpleFormalParameter(Keyword.VAR, "a"));
3529 } 3002 }
3530 3003
3531 void test_visitSimpleFormalParameter_keyword_type() { 3004 void test_visitSimpleFormalParameter_keyword_type() {
3532 _assertSource( 3005 _assertSource(
3533 "final A a", 3006 "final A a",
3534 AstFactory.simpleFormalParameter2( 3007 AstFactory.simpleFormalParameter2(
3535 Keyword.FINAL, AstFactory.typeName4("A"), "a")); 3008 Keyword.FINAL, AstFactory.typeName4("A"), "a"));
3536 } 3009 }
3537 3010
3538 void test_visitSimpleFormalParameter_type() { 3011 void test_visitSimpleFormalParameter_type() {
3539 _assertSource("A a", 3012 _assertSource("A a",
3540 AstFactory.simpleFormalParameter4(AstFactory.typeName4("A"), "a")); 3013 AstFactory.simpleFormalParameter4(AstFactory.typeName4("A"), "a"));
3541 } 3014 }
3542 3015
3543 void test_visitSimpleFormalParameter_annotation() {
3544 SimpleFormalParameter parameter = AstFactory.simpleFormalParameter3('x');
3545 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A")));
3546 _assertSource('@A x', parameter);
3547 }
3548
3549 void test_visitSimpleIdentifier() { 3016 void test_visitSimpleIdentifier() {
3550 _assertSource("a", AstFactory.identifier3("a")); 3017 _assertSource("a", AstFactory.identifier3("a"));
3551 } 3018 }
3552 3019
3553 void test_visitSimpleStringLiteral() { 3020 void test_visitSimpleStringLiteral() {
3554 _assertSource("'a'", AstFactory.string2("a")); 3021 _assertSource("'a'", AstFactory.string2("a"));
3555 } 3022 }
3556 3023
3557 void test_visitStringInterpolation() { 3024 void test_visitStringInterpolation() {
3558 _assertSource( 3025 _assertSource(
(...skipping 29 matching lines...) Expand all
3588 void test_visitSwitchCase_multipleStatements() { 3055 void test_visitSwitchCase_multipleStatements() {
3589 _assertSource( 3056 _assertSource(
3590 "case a: {} {}", 3057 "case a: {} {}",
3591 AstFactory.switchCase(AstFactory.identifier3("a"), 3058 AstFactory.switchCase(AstFactory.identifier3("a"),
3592 [AstFactory.block(), AstFactory.block()])); 3059 [AstFactory.block(), AstFactory.block()]));
3593 } 3060 }
3594 3061
3595 void test_visitSwitchCase_noLabels() { 3062 void test_visitSwitchCase_noLabels() {
3596 _assertSource( 3063 _assertSource(
3597 "case a: {}", 3064 "case a: {}",
3598 AstFactory.switchCase( 3065 AstFactory
3599 AstFactory.identifier3("a"), [AstFactory.block()])); 3066 .switchCase(AstFactory.identifier3("a"), [AstFactory.block()]));
3600 } 3067 }
3601 3068
3602 void test_visitSwitchCase_singleLabel() { 3069 void test_visitSwitchCase_singleLabel() {
3603 _assertSource( 3070 _assertSource(
3604 "l1: case a: {}", 3071 "l1: case a: {}",
3605 AstFactory.switchCase2([AstFactory.label2("l1")], 3072 AstFactory.switchCase2([AstFactory.label2("l1")],
3606 AstFactory.identifier3("a"), [AstFactory.block()])); 3073 AstFactory.identifier3("a"), [AstFactory.block()]));
3607 } 3074 }
3608 3075
3609 void test_visitSwitchDefault_multipleLabels() { 3076 void test_visitSwitchDefault_multipleLabels() {
(...skipping 10 matching lines...) Expand all
3620 } 3087 }
3621 3088
3622 void test_visitSwitchDefault_noLabels() { 3089 void test_visitSwitchDefault_noLabels() {
3623 _assertSource( 3090 _assertSource(
3624 "default: {}", AstFactory.switchDefault2([AstFactory.block()])); 3091 "default: {}", AstFactory.switchDefault2([AstFactory.block()]));
3625 } 3092 }
3626 3093
3627 void test_visitSwitchDefault_singleLabel() { 3094 void test_visitSwitchDefault_singleLabel() {
3628 _assertSource( 3095 _assertSource(
3629 "l1: default: {}", 3096 "l1: default: {}",
3630 AstFactory.switchDefault( 3097 AstFactory
3631 [AstFactory.label2("l1")], [AstFactory.block()])); 3098 .switchDefault([AstFactory.label2("l1")], [AstFactory.block()]));
3632 } 3099 }
3633 3100
3634 void test_visitSwitchStatement() { 3101 void test_visitSwitchStatement() {
3635 _assertSource( 3102 _assertSource(
3636 "switch (a) {case 'b': {} default: {}}", 3103 "switch (a) {case 'b': {} default: {}}",
3637 AstFactory.switchStatement(AstFactory.identifier3("a"), [ 3104 AstFactory.switchStatement(AstFactory.identifier3("a"), [
3638 AstFactory.switchCase(AstFactory.string2("b"), [AstFactory.block()]), 3105 AstFactory.switchCase(AstFactory.string2("b"), [AstFactory.block()]),
3639 AstFactory.switchDefault2([AstFactory.block()]) 3106 AstFactory.switchDefault2([AstFactory.block()])
3640 ])); 3107 ]));
3641 } 3108 }
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
3872 * @param expectedSource the source string that the visitor is expected to pro duce 3339 * @param expectedSource the source string that the visitor is expected to pro duce
3873 * @param node the AST node being visited to produce the actual source 3340 * @param node the AST node being visited to produce the actual source
3874 * @throws AFE if the visitor does not produce the expected source for the giv en node 3341 * @throws AFE if the visitor does not produce the expected source for the giv en node
3875 */ 3342 */
3876 void _assertSource(String expectedSource, AstNode node) { 3343 void _assertSource(String expectedSource, AstNode node) {
3877 PrintStringWriter writer = new PrintStringWriter(); 3344 PrintStringWriter writer = new PrintStringWriter();
3878 node.accept(new ToSourceVisitor(writer)); 3345 node.accept(new ToSourceVisitor(writer));
3879 expect(writer.toString(), expectedSource); 3346 expect(writer.toString(), expectedSource);
3880 } 3347 }
3881 } 3348 }
3882
3883 @reflectiveTest
3884 class VariableDeclarationTest extends ParserTestCase {
3885 void test_getDocumentationComment_onGrandParent() {
3886 VariableDeclaration varDecl = AstFactory.variableDeclaration("a");
3887 TopLevelVariableDeclaration decl =
3888 AstFactory.topLevelVariableDeclaration2(Keyword.VAR, [varDecl]);
3889 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
3890 expect(varDecl.documentationComment, isNull);
3891 decl.documentationComment = comment;
3892 expect(varDecl.documentationComment, isNotNull);
3893 expect(decl.documentationComment, isNotNull);
3894 }
3895
3896 void test_getDocumentationComment_onNode() {
3897 VariableDeclaration decl = AstFactory.variableDeclaration("a");
3898 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
3899 decl.documentationComment = comment;
3900 expect(decl.documentationComment, isNotNull);
3901 }
3902 }
3903
3904 class WrapperKind extends Enum<WrapperKind> {
3905 static const WrapperKind PREFIXED_LEFT =
3906 const WrapperKind('PREFIXED_LEFT', 0);
3907
3908 static const WrapperKind PREFIXED_RIGHT =
3909 const WrapperKind('PREFIXED_RIGHT', 1);
3910
3911 static const WrapperKind PROPERTY_LEFT =
3912 const WrapperKind('PROPERTY_LEFT', 2);
3913
3914 static const WrapperKind PROPERTY_RIGHT =
3915 const WrapperKind('PROPERTY_RIGHT', 3);
3916
3917 static const WrapperKind NONE = const WrapperKind('NONE', 4);
3918
3919 static const List<WrapperKind> values = const [
3920 PREFIXED_LEFT,
3921 PREFIXED_RIGHT,
3922 PROPERTY_LEFT,
3923 PROPERTY_RIGHT,
3924 NONE
3925 ];
3926
3927 const WrapperKind(String name, int ordinal) : super(name, ordinal);
3928 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/src/dart/ast/test_all.dart ('k') | packages/analyzer/test/src/dart/constant/evaluation_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698