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

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

Issue 1612933006: Moved the AST related tests to their new locations (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 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 analyzer.test.generated.ast_test; 5 library analyzer.test.src.dart.ast.utilities_test;
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/visitor.dart';
9 import 'package:analyzer/src/dart/ast/utilities.dart'; 8 import 'package:analyzer/src/dart/ast/utilities.dart';
10 import 'package:analyzer/src/generated/java_core.dart'; 9 import 'package:analyzer/src/generated/java_core.dart';
11 import 'package:analyzer/src/generated/java_engine.dart' show Predicate; 10 import 'package:analyzer/src/generated/java_engine.dart' show Predicate;
12 import 'package:analyzer/src/generated/java_engine.dart'; 11 import 'package:analyzer/src/generated/java_engine.dart';
13 import 'package:analyzer/src/generated/scanner.dart'; 12 import 'package:analyzer/src/generated/scanner.dart';
14 import 'package:analyzer/src/generated/testing/ast_factory.dart'; 13 import 'package:analyzer/src/generated/testing/ast_factory.dart';
15 import 'package:analyzer/src/generated/testing/token_factory.dart'; 14 import 'package:analyzer/src/generated/testing/token_factory.dart';
16 import 'package:unittest/unittest.dart'; 15 import 'package:unittest/unittest.dart';
17 16
18 import '../reflective_tests.dart'; 17 import '../../../generated/parser_test.dart' show ParserTestCase;
19 import '../utils.dart'; 18 import '../../../generated/test_support.dart';
20 import 'parser_test.dart' show ParserTestCase; 19 import '../../../reflective_tests.dart';
21 import 'test_support.dart'; 20 import '../../../utils.dart';
22 21
23 main() { 22 main() {
24 initializeTestEnvironment(); 23 initializeTestEnvironment();
25 runReflectiveTests(BreadthFirstVisitorTest);
26 runReflectiveTests(ClassDeclarationTest);
27 runReflectiveTests(ClassTypeAliasTest);
28 runReflectiveTests(ConstantEvaluatorTest); 24 runReflectiveTests(ConstantEvaluatorTest);
29 runReflectiveTests(ConstructorDeclarationTest);
30 runReflectiveTests(FieldFormalParameterTest);
31 runReflectiveTests(IndexExpressionTest);
32 runReflectiveTests(NodeListTest);
33 runReflectiveTests(NodeLocatorTest); 25 runReflectiveTests(NodeLocatorTest);
34 runReflectiveTests(NodeLocator2Test); 26 runReflectiveTests(NodeLocator2Test);
35 runReflectiveTests(SimpleIdentifierTest);
36 runReflectiveTests(SimpleStringLiteralTest);
37 runReflectiveTests(StringInterpolationTest);
38 runReflectiveTests(ToSourceVisitorTest); 27 runReflectiveTests(ToSourceVisitorTest);
39 runReflectiveTests(VariableDeclarationTest);
40 }
41
42 class AssignmentKind extends Enum<AssignmentKind> {
43 static const AssignmentKind BINARY = const AssignmentKind('BINARY', 0);
44
45 static const AssignmentKind COMPOUND_LEFT =
46 const AssignmentKind('COMPOUND_LEFT', 1);
47
48 static const AssignmentKind COMPOUND_RIGHT =
49 const AssignmentKind('COMPOUND_RIGHT', 2);
50
51 static const AssignmentKind POSTFIX_INC =
52 const AssignmentKind('POSTFIX_INC', 3);
53
54 static const AssignmentKind PREFIX_DEC =
55 const AssignmentKind('PREFIX_DEC', 4);
56
57 static const AssignmentKind PREFIX_INC =
58 const AssignmentKind('PREFIX_INC', 5);
59
60 static const AssignmentKind PREFIX_NOT =
61 const AssignmentKind('PREFIX_NOT', 6);
62
63 static const AssignmentKind SIMPLE_LEFT =
64 const AssignmentKind('SIMPLE_LEFT', 7);
65
66 static const AssignmentKind SIMPLE_RIGHT =
67 const AssignmentKind('SIMPLE_RIGHT', 8);
68
69 static const AssignmentKind NONE = const AssignmentKind('NONE', 9);
70
71 static const List<AssignmentKind> values = const [
72 BINARY,
73 COMPOUND_LEFT,
74 COMPOUND_RIGHT,
75 POSTFIX_INC,
76 PREFIX_DEC,
77 PREFIX_INC,
78 PREFIX_NOT,
79 SIMPLE_LEFT,
80 SIMPLE_RIGHT,
81 NONE
82 ];
83
84 const AssignmentKind(String name, int ordinal) : super(name, ordinal);
85 }
86
87 class BreadthFirstVisitor_BreadthFirstVisitorTest_testIt
88 extends BreadthFirstVisitor<Object> {
89 List<AstNode> nodes;
90
91 BreadthFirstVisitor_BreadthFirstVisitorTest_testIt(this.nodes) : super();
92
93 @override
94 Object visitNode(AstNode node) {
95 nodes.add(node);
96 return super.visitNode(node);
97 }
98 } 28 }
99 29
100 @reflectiveTest 30 @reflectiveTest
101 class BreadthFirstVisitorTest extends ParserTestCase {
102 void test_it() {
103 String source = r'''
104 class A {
105 bool get g => true;
106 }
107 class B {
108 int f() {
109 num q() {
110 return 3;
111 }
112 return q() + 4;
113 }
114 }
115 A f(var p) {
116 if ((p as A).g) {
117 return p;
118 } else {
119 return null;
120 }
121 }''';
122 CompilationUnit unit = ParserTestCase.parseCompilationUnit(source);
123 List<AstNode> nodes = new List<AstNode>();
124 BreadthFirstVisitor<Object> visitor =
125 new BreadthFirstVisitor_BreadthFirstVisitorTest_testIt(nodes);
126 visitor.visitAllNodes(unit);
127 expect(nodes, hasLength(59));
128 EngineTestCase.assertInstanceOf(
129 (obj) => obj is CompilationUnit, CompilationUnit, nodes[0]);
130 EngineTestCase.assertInstanceOf(
131 (obj) => obj is ClassDeclaration, ClassDeclaration, nodes[2]);
132 EngineTestCase.assertInstanceOf(
133 (obj) => obj is FunctionDeclaration, FunctionDeclaration, nodes[3]);
134 EngineTestCase.assertInstanceOf(
135 (obj) => obj is FunctionDeclarationStatement,
136 FunctionDeclarationStatement,
137 nodes[27]);
138 EngineTestCase.assertInstanceOf(
139 (obj) => obj is IntegerLiteral, IntegerLiteral, nodes[58]);
140 //3
141 }
142 }
143
144 @reflectiveTest
145 class ClassDeclarationTest extends ParserTestCase {
146 void test_getConstructor() {
147 List<ConstructorInitializer> initializers =
148 new List<ConstructorInitializer>();
149 ConstructorDeclaration defaultConstructor =
150 AstFactory.constructorDeclaration(AstFactory.identifier3("Test"), null,
151 AstFactory.formalParameterList(), initializers);
152 ConstructorDeclaration aConstructor = AstFactory.constructorDeclaration(
153 AstFactory.identifier3("Test"),
154 "a",
155 AstFactory.formalParameterList(),
156 initializers);
157 ConstructorDeclaration bConstructor = AstFactory.constructorDeclaration(
158 AstFactory.identifier3("Test"),
159 "b",
160 AstFactory.formalParameterList(),
161 initializers);
162 ClassDeclaration clazz = AstFactory.classDeclaration(null, "Test", null,
163 null, null, null, [defaultConstructor, aConstructor, bConstructor]);
164 expect(clazz.getConstructor(null), same(defaultConstructor));
165 expect(clazz.getConstructor("a"), same(aConstructor));
166 expect(clazz.getConstructor("b"), same(bConstructor));
167 expect(clazz.getConstructor("noSuchConstructor"), same(null));
168 }
169
170 void test_getField() {
171 VariableDeclaration aVar = AstFactory.variableDeclaration("a");
172 VariableDeclaration bVar = AstFactory.variableDeclaration("b");
173 VariableDeclaration cVar = AstFactory.variableDeclaration("c");
174 ClassDeclaration clazz =
175 AstFactory.classDeclaration(null, "Test", null, null, null, null, [
176 AstFactory.fieldDeclaration2(false, null, [aVar]),
177 AstFactory.fieldDeclaration2(false, null, [bVar, cVar])
178 ]);
179 expect(clazz.getField("a"), same(aVar));
180 expect(clazz.getField("b"), same(bVar));
181 expect(clazz.getField("c"), same(cVar));
182 expect(clazz.getField("noSuchField"), same(null));
183 }
184
185 void test_getMethod() {
186 MethodDeclaration aMethod = AstFactory.methodDeclaration(null, null, null,
187 null, AstFactory.identifier3("a"), AstFactory.formalParameterList());
188 MethodDeclaration bMethod = AstFactory.methodDeclaration(null, null, null,
189 null, AstFactory.identifier3("b"), AstFactory.formalParameterList());
190 ClassDeclaration clazz = AstFactory.classDeclaration(
191 null, "Test", null, null, null, null, [aMethod, bMethod]);
192 expect(clazz.getMethod("a"), same(aMethod));
193 expect(clazz.getMethod("b"), same(bMethod));
194 expect(clazz.getMethod("noSuchMethod"), same(null));
195 }
196
197 void test_isAbstract() {
198 expect(
199 AstFactory
200 .classDeclaration(null, "A", null, null, null, null)
201 .isAbstract,
202 isFalse);
203 expect(
204 AstFactory
205 .classDeclaration(Keyword.ABSTRACT, "B", null, null, null, null)
206 .isAbstract,
207 isTrue);
208 }
209 }
210
211 @reflectiveTest
212 class ClassTypeAliasTest extends ParserTestCase {
213 void test_isAbstract() {
214 expect(
215 AstFactory.classTypeAlias("A", null, null, null, null, null).isAbstract,
216 isFalse);
217 expect(
218 AstFactory
219 .classTypeAlias("B", null, Keyword.ABSTRACT, null, null, null)
220 .isAbstract,
221 isTrue);
222 }
223 }
224
225 @reflectiveTest
226 class ConstantEvaluatorTest extends ParserTestCase { 31 class ConstantEvaluatorTest extends ParserTestCase {
227 void fail_constructor() { 32 void fail_constructor() {
228 Object value = _getConstantValue("?"); 33 Object value = _getConstantValue("?");
229 expect(value, null); 34 expect(value, null);
230 } 35 }
231 36
232 void fail_identifier_class() { 37 void fail_identifier_class() {
233 Object value = _getConstantValue("?"); 38 Object value = _getConstantValue("?");
234 expect(value, null); 39 expect(value, null);
235 } 40 }
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 void test_unary_negated_integer() { 325 void test_unary_negated_integer() {
521 Object value = _getConstantValue("-42"); 326 Object value = _getConstantValue("-42");
522 expect(value, -42); 327 expect(value, -42);
523 } 328 }
524 329
525 Object _getConstantValue(String source) => 330 Object _getConstantValue(String source) =>
526 parseExpression(source).accept(new ConstantEvaluator()); 331 parseExpression(source).accept(new ConstantEvaluator());
527 } 332 }
528 333
529 @reflectiveTest 334 @reflectiveTest
530 class ConstructorDeclarationTest extends EngineTestCase {
531 void test_firstTokenAfterCommentAndMetadata_all_inverted() {
532 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
533 externalKeyword.offset = 14;
534 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
535 Keyword.CONST,
536 Keyword.FACTORY,
537 AstFactory.identifier3('int'),
538 null,
539 null,
540 null,
541 null);
542 declaration.externalKeyword = externalKeyword;
543 declaration.constKeyword.offset = 8;
544 Token factoryKeyword = declaration.factoryKeyword;
545 factoryKeyword.offset = 0;
546 expect(declaration.firstTokenAfterCommentAndMetadata, factoryKeyword);
547 }
548
549 void test_firstTokenAfterCommentAndMetadata_all_normal() {
550 Token token = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
551 token.offset = 0;
552 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
553 Keyword.CONST,
554 Keyword.FACTORY,
555 AstFactory.identifier3('int'),
556 null,
557 null,
558 null,
559 null);
560 declaration.externalKeyword = token;
561 declaration.constKeyword.offset = 9;
562 declaration.factoryKeyword.offset = 15;
563 expect(declaration.firstTokenAfterCommentAndMetadata, token);
564 }
565
566 void test_firstTokenAfterCommentAndMetadata_constOnly() {
567 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
568 Keyword.CONST,
569 null,
570 AstFactory.identifier3('int'),
571 null,
572 null,
573 null,
574 null);
575 expect(declaration.firstTokenAfterCommentAndMetadata,
576 declaration.constKeyword);
577 }
578
579 void test_firstTokenAfterCommentAndMetadata_externalOnly() {
580 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
581 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
582 null, null, AstFactory.identifier3('int'), null, null, null, null);
583 declaration.externalKeyword = externalKeyword;
584 expect(declaration.firstTokenAfterCommentAndMetadata, externalKeyword);
585 }
586
587 void test_firstTokenAfterCommentAndMetadata_factoryOnly() {
588 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2(
589 null,
590 Keyword.FACTORY,
591 AstFactory.identifier3('int'),
592 null,
593 null,
594 null,
595 null);
596 expect(declaration.firstTokenAfterCommentAndMetadata,
597 declaration.factoryKeyword);
598 }
599 }
600
601 @reflectiveTest
602 class FieldFormalParameterTest extends EngineTestCase {
603 void test_endToken_noParameters() {
604 FieldFormalParameter parameter = AstFactory.fieldFormalParameter2('field');
605 expect(parameter.endToken, parameter.identifier.endToken);
606 }
607
608 void test_endToken_parameters() {
609 FieldFormalParameter parameter = AstFactory.fieldFormalParameter(
610 null, null, 'field', AstFactory.formalParameterList([]));
611 expect(parameter.endToken, parameter.parameters.endToken);
612 }
613 }
614
615 @reflectiveTest
616 class IndexExpressionTest extends EngineTestCase {
617 void test_inGetterContext_assignment_compound_left() {
618 IndexExpression expression = AstFactory.indexExpression(
619 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
620 // a[b] += c
621 AstFactory.assignmentExpression(
622 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c"));
623 expect(expression.inGetterContext(), isTrue);
624 }
625
626 void test_inGetterContext_assignment_simple_left() {
627 IndexExpression expression = AstFactory.indexExpression(
628 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
629 // a[b] = c
630 AstFactory.assignmentExpression(
631 expression, TokenType.EQ, AstFactory.identifier3("c"));
632 expect(expression.inGetterContext(), isFalse);
633 }
634
635 void test_inGetterContext_nonAssignment() {
636 IndexExpression expression = AstFactory.indexExpression(
637 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
638 // a[b] + c
639 AstFactory.binaryExpression(
640 expression, TokenType.PLUS, AstFactory.identifier3("c"));
641 expect(expression.inGetterContext(), isTrue);
642 }
643
644 void test_inSetterContext_assignment_compound_left() {
645 IndexExpression expression = AstFactory.indexExpression(
646 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
647 // a[b] += c
648 AstFactory.assignmentExpression(
649 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c"));
650 expect(expression.inSetterContext(), isTrue);
651 }
652
653 void test_inSetterContext_assignment_compound_right() {
654 IndexExpression expression = AstFactory.indexExpression(
655 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
656 // c += a[b]
657 AstFactory.assignmentExpression(
658 AstFactory.identifier3("c"), TokenType.PLUS_EQ, expression);
659 expect(expression.inSetterContext(), isFalse);
660 }
661
662 void test_inSetterContext_assignment_simple_left() {
663 IndexExpression expression = AstFactory.indexExpression(
664 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
665 // a[b] = c
666 AstFactory.assignmentExpression(
667 expression, TokenType.EQ, AstFactory.identifier3("c"));
668 expect(expression.inSetterContext(), isTrue);
669 }
670
671 void test_inSetterContext_assignment_simple_right() {
672 IndexExpression expression = AstFactory.indexExpression(
673 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
674 // c = a[b]
675 AstFactory.assignmentExpression(
676 AstFactory.identifier3("c"), TokenType.EQ, expression);
677 expect(expression.inSetterContext(), isFalse);
678 }
679
680 void test_inSetterContext_nonAssignment() {
681 IndexExpression expression = AstFactory.indexExpression(
682 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
683 AstFactory.binaryExpression(
684 expression, TokenType.PLUS, AstFactory.identifier3("c"));
685 // a[b] + cc
686 expect(expression.inSetterContext(), isFalse);
687 }
688
689 void test_inSetterContext_postfix() {
690 IndexExpression expression = AstFactory.indexExpression(
691 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
692 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS);
693 // a[b]++
694 expect(expression.inSetterContext(), isTrue);
695 }
696
697 void test_inSetterContext_prefix_bang() {
698 IndexExpression expression = AstFactory.indexExpression(
699 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
700 // !a[b]
701 AstFactory.prefixExpression(TokenType.BANG, expression);
702 expect(expression.inSetterContext(), isFalse);
703 }
704
705 void test_inSetterContext_prefix_minusMinus() {
706 IndexExpression expression = AstFactory.indexExpression(
707 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
708 // --a[b]
709 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression);
710 expect(expression.inSetterContext(), isTrue);
711 }
712
713 void test_inSetterContext_prefix_plusPlus() {
714 IndexExpression expression = AstFactory.indexExpression(
715 AstFactory.identifier3("a"), AstFactory.identifier3("b"));
716 // ++a[b]
717 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression);
718 expect(expression.inSetterContext(), isTrue);
719 }
720 }
721
722 @reflectiveTest
723 class NodeListTest extends EngineTestCase {
724 void test_add() {
725 AstNode parent = AstFactory.argumentList();
726 AstNode firstNode = AstFactory.booleanLiteral(true);
727 AstNode secondNode = AstFactory.booleanLiteral(false);
728 NodeList<AstNode> list = new NodeList<AstNode>(parent);
729 list.insert(0, secondNode);
730 list.insert(0, firstNode);
731 expect(list, hasLength(2));
732 expect(list[0], same(firstNode));
733 expect(list[1], same(secondNode));
734 expect(firstNode.parent, same(parent));
735 expect(secondNode.parent, same(parent));
736 AstNode thirdNode = AstFactory.booleanLiteral(false);
737 list.insert(1, thirdNode);
738 expect(list, hasLength(3));
739 expect(list[0], same(firstNode));
740 expect(list[1], same(thirdNode));
741 expect(list[2], same(secondNode));
742 expect(firstNode.parent, same(parent));
743 expect(secondNode.parent, same(parent));
744 expect(thirdNode.parent, same(parent));
745 }
746
747 void test_add_negative() {
748 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
749 try {
750 list.insert(-1, AstFactory.booleanLiteral(true));
751 fail("Expected IndexOutOfBoundsException");
752 } on RangeError {
753 // Expected
754 }
755 }
756
757 void test_add_tooBig() {
758 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
759 try {
760 list.insert(1, AstFactory.booleanLiteral(true));
761 fail("Expected IndexOutOfBoundsException");
762 } on RangeError {
763 // Expected
764 }
765 }
766
767 void test_addAll() {
768 AstNode parent = AstFactory.argumentList();
769 List<AstNode> firstNodes = new List<AstNode>();
770 AstNode firstNode = AstFactory.booleanLiteral(true);
771 AstNode secondNode = AstFactory.booleanLiteral(false);
772 firstNodes.add(firstNode);
773 firstNodes.add(secondNode);
774 NodeList<AstNode> list = new NodeList<AstNode>(parent);
775 list.addAll(firstNodes);
776 expect(list, hasLength(2));
777 expect(list[0], same(firstNode));
778 expect(list[1], same(secondNode));
779 expect(firstNode.parent, same(parent));
780 expect(secondNode.parent, same(parent));
781 List<AstNode> secondNodes = new List<AstNode>();
782 AstNode thirdNode = AstFactory.booleanLiteral(true);
783 AstNode fourthNode = AstFactory.booleanLiteral(false);
784 secondNodes.add(thirdNode);
785 secondNodes.add(fourthNode);
786 list.addAll(secondNodes);
787 expect(list, hasLength(4));
788 expect(list[0], same(firstNode));
789 expect(list[1], same(secondNode));
790 expect(list[2], same(thirdNode));
791 expect(list[3], same(fourthNode));
792 expect(firstNode.parent, same(parent));
793 expect(secondNode.parent, same(parent));
794 expect(thirdNode.parent, same(parent));
795 expect(fourthNode.parent, same(parent));
796 }
797
798 void test_creation() {
799 AstNode owner = AstFactory.argumentList();
800 NodeList<AstNode> list = new NodeList<AstNode>(owner);
801 expect(list, isNotNull);
802 expect(list, hasLength(0));
803 expect(list.owner, same(owner));
804 }
805
806 void test_get_negative() {
807 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
808 try {
809 list[-1];
810 fail("Expected IndexOutOfBoundsException");
811 } on RangeError {
812 // Expected
813 }
814 }
815
816 void test_get_tooBig() {
817 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
818 try {
819 list[1];
820 fail("Expected IndexOutOfBoundsException");
821 } on RangeError {
822 // Expected
823 }
824 }
825
826 void test_getBeginToken_empty() {
827 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
828 expect(list.beginToken, isNull);
829 }
830
831 void test_getBeginToken_nonEmpty() {
832 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
833 AstNode node =
834 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true));
835 list.add(node);
836 expect(list.beginToken, same(node.beginToken));
837 }
838
839 void test_getEndToken_empty() {
840 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
841 expect(list.endToken, isNull);
842 }
843
844 void test_getEndToken_nonEmpty() {
845 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
846 AstNode node =
847 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true));
848 list.add(node);
849 expect(list.endToken, same(node.endToken));
850 }
851
852 void test_indexOf() {
853 List<AstNode> nodes = new List<AstNode>();
854 AstNode firstNode = AstFactory.booleanLiteral(true);
855 AstNode secondNode = AstFactory.booleanLiteral(false);
856 AstNode thirdNode = AstFactory.booleanLiteral(true);
857 AstNode fourthNode = AstFactory.booleanLiteral(false);
858 nodes.add(firstNode);
859 nodes.add(secondNode);
860 nodes.add(thirdNode);
861 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
862 list.addAll(nodes);
863 expect(list, hasLength(3));
864 expect(list.indexOf(firstNode), 0);
865 expect(list.indexOf(secondNode), 1);
866 expect(list.indexOf(thirdNode), 2);
867 expect(list.indexOf(fourthNode), -1);
868 expect(list.indexOf(null), -1);
869 }
870
871 void test_remove() {
872 List<AstNode> nodes = new List<AstNode>();
873 AstNode firstNode = AstFactory.booleanLiteral(true);
874 AstNode secondNode = AstFactory.booleanLiteral(false);
875 AstNode thirdNode = AstFactory.booleanLiteral(true);
876 nodes.add(firstNode);
877 nodes.add(secondNode);
878 nodes.add(thirdNode);
879 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
880 list.addAll(nodes);
881 expect(list, hasLength(3));
882 expect(list.removeAt(1), same(secondNode));
883 expect(list, hasLength(2));
884 expect(list[0], same(firstNode));
885 expect(list[1], same(thirdNode));
886 }
887
888 void test_remove_negative() {
889 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
890 try {
891 list.removeAt(-1);
892 fail("Expected IndexOutOfBoundsException");
893 } on RangeError {
894 // Expected
895 }
896 }
897
898 void test_remove_tooBig() {
899 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
900 try {
901 list.removeAt(1);
902 fail("Expected IndexOutOfBoundsException");
903 } on RangeError {
904 // Expected
905 }
906 }
907
908 void test_set() {
909 List<AstNode> nodes = new List<AstNode>();
910 AstNode firstNode = AstFactory.booleanLiteral(true);
911 AstNode secondNode = AstFactory.booleanLiteral(false);
912 AstNode thirdNode = AstFactory.booleanLiteral(true);
913 nodes.add(firstNode);
914 nodes.add(secondNode);
915 nodes.add(thirdNode);
916 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
917 list.addAll(nodes);
918 expect(list, hasLength(3));
919 AstNode fourthNode = AstFactory.integer(0);
920 expect(javaListSet(list, 1, fourthNode), same(secondNode));
921 expect(list, hasLength(3));
922 expect(list[0], same(firstNode));
923 expect(list[1], same(fourthNode));
924 expect(list[2], same(thirdNode));
925 }
926
927 void test_set_negative() {
928 AstNode node = AstFactory.booleanLiteral(true);
929 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
930 try {
931 javaListSet(list, -1, node);
932 fail("Expected IndexOutOfBoundsException");
933 } on RangeError {
934 // Expected
935 }
936 }
937
938 void test_set_tooBig() {
939 AstNode node = AstFactory.booleanLiteral(true);
940 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList());
941 try {
942 javaListSet(list, 1, node);
943 fail("Expected IndexOutOfBoundsException");
944 } on RangeError {
945 // Expected
946 }
947 }
948 }
949
950 @reflectiveTest
951 class NodeLocator2Test extends ParserTestCase { 335 class NodeLocator2Test extends ParserTestCase {
952 void test_onlyStartOffset() { 336 void test_onlyStartOffset() {
953 String code = ' int vv; '; 337 String code = ' int vv; ';
954 // 012345678 338 // 012345678
955 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code); 339 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code);
956 TopLevelVariableDeclaration declaration = unit.declarations[0]; 340 TopLevelVariableDeclaration declaration = unit.declarations[0];
957 VariableDeclarationList variableList = declaration.variables; 341 VariableDeclarationList variableList = declaration.variables;
958 Identifier typeName = variableList.type.name; 342 Identifier typeName = variableList.type.name;
959 SimpleIdentifier varName = variableList.variables[0].name; 343 SimpleIdentifier varName = variableList.variables[0].name;
960 expect(new NodeLocator2(0).searchWithin(unit), same(unit)); 344 expect(new NodeLocator2(0).searchWithin(unit), same(unit));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 expect(node, isNotNull); 421 expect(node, isNotNull);
1038 expect(locator.foundNode, same(node)); 422 expect(locator.foundNode, same(node));
1039 expect(node.offset <= start, isTrue, reason: "Node starts after range"); 423 expect(node.offset <= start, isTrue, reason: "Node starts after range");
1040 expect(node.offset + node.length > end, isTrue, 424 expect(node.offset + node.length > end, isTrue,
1041 reason: "Node ends before range"); 425 reason: "Node ends before range");
1042 EngineTestCase.assertInstanceOf(predicate, expectedClass, node); 426 EngineTestCase.assertInstanceOf(predicate, expectedClass, node);
1043 } 427 }
1044 } 428 }
1045 429
1046 @reflectiveTest 430 @reflectiveTest
1047 class SimpleIdentifierTest extends ParserTestCase {
1048 void test_inDeclarationContext_catch_exception() {
1049 SimpleIdentifier identifier =
1050 AstFactory.catchClause("e").exceptionParameter;
1051 expect(identifier.inDeclarationContext(), isTrue);
1052 }
1053
1054 void test_inDeclarationContext_catch_stack() {
1055 SimpleIdentifier identifier =
1056 AstFactory.catchClause2("e", "s").stackTraceParameter;
1057 expect(identifier.inDeclarationContext(), isTrue);
1058 }
1059
1060 void test_inDeclarationContext_classDeclaration() {
1061 SimpleIdentifier identifier =
1062 AstFactory.classDeclaration(null, "C", null, null, null, null).name;
1063 expect(identifier.inDeclarationContext(), isTrue);
1064 }
1065
1066 void test_inDeclarationContext_classTypeAlias() {
1067 SimpleIdentifier identifier =
1068 AstFactory.classTypeAlias("C", null, null, null, null, null).name;
1069 expect(identifier.inDeclarationContext(), isTrue);
1070 }
1071
1072 void test_inDeclarationContext_constructorDeclaration() {
1073 SimpleIdentifier identifier = AstFactory
1074 .constructorDeclaration(AstFactory.identifier3("C"), "c", null, null)
1075 .name;
1076 expect(identifier.inDeclarationContext(), isTrue);
1077 }
1078
1079 void test_inDeclarationContext_declaredIdentifier() {
1080 DeclaredIdentifier declaredIdentifier = AstFactory.declaredIdentifier3("v");
1081 SimpleIdentifier identifier = declaredIdentifier.identifier;
1082 expect(identifier.inDeclarationContext(), isTrue);
1083 }
1084
1085 void test_inDeclarationContext_enumConstantDeclaration() {
1086 EnumDeclaration enumDeclaration =
1087 AstFactory.enumDeclaration2('MyEnum', ['CONST']);
1088 SimpleIdentifier identifier = enumDeclaration.constants[0].name;
1089 expect(identifier.inDeclarationContext(), isTrue);
1090 }
1091
1092 void test_inDeclarationContext_enumDeclaration() {
1093 EnumDeclaration enumDeclaration =
1094 AstFactory.enumDeclaration2('MyEnum', ['A', 'B', 'C']);
1095 SimpleIdentifier identifier = enumDeclaration.name;
1096 expect(identifier.inDeclarationContext(), isTrue);
1097 }
1098
1099 void test_inDeclarationContext_fieldFormalParameter() {
1100 SimpleIdentifier identifier =
1101 AstFactory.fieldFormalParameter2("p").identifier;
1102 expect(identifier.inDeclarationContext(), isFalse);
1103 }
1104
1105 void test_inDeclarationContext_functionDeclaration() {
1106 SimpleIdentifier identifier =
1107 AstFactory.functionDeclaration(null, null, "f", null).name;
1108 expect(identifier.inDeclarationContext(), isTrue);
1109 }
1110
1111 void test_inDeclarationContext_functionTypeAlias() {
1112 SimpleIdentifier identifier =
1113 AstFactory.typeAlias(null, "F", null, null).name;
1114 expect(identifier.inDeclarationContext(), isTrue);
1115 }
1116
1117 void test_inDeclarationContext_label_false() {
1118 SimpleIdentifier identifier =
1119 AstFactory.namedExpression2("l", AstFactory.integer(0)).name.label;
1120 expect(identifier.inDeclarationContext(), isFalse);
1121 }
1122
1123 void test_inDeclarationContext_label_true() {
1124 Label label = AstFactory.label2("l");
1125 SimpleIdentifier identifier = label.label;
1126 AstFactory.labeledStatement([label], AstFactory.emptyStatement());
1127 expect(identifier.inDeclarationContext(), isTrue);
1128 }
1129
1130 void test_inDeclarationContext_methodDeclaration() {
1131 SimpleIdentifier identifier = AstFactory.identifier3("m");
1132 AstFactory.methodDeclaration2(
1133 null, null, null, null, identifier, null, null);
1134 expect(identifier.inDeclarationContext(), isTrue);
1135 }
1136
1137 void test_inDeclarationContext_prefix() {
1138 SimpleIdentifier identifier =
1139 AstFactory.importDirective3("uri", "pref").prefix;
1140 expect(identifier.inDeclarationContext(), isTrue);
1141 }
1142
1143 void test_inDeclarationContext_simpleFormalParameter() {
1144 SimpleIdentifier identifier =
1145 AstFactory.simpleFormalParameter3("p").identifier;
1146 expect(identifier.inDeclarationContext(), isTrue);
1147 }
1148
1149 void test_inDeclarationContext_typeParameter_bound() {
1150 TypeName bound = AstFactory.typeName4("A");
1151 SimpleIdentifier identifier = bound.name as SimpleIdentifier;
1152 AstFactory.typeParameter2("E", bound);
1153 expect(identifier.inDeclarationContext(), isFalse);
1154 }
1155
1156 void test_inDeclarationContext_typeParameter_name() {
1157 SimpleIdentifier identifier = AstFactory.typeParameter("E").name;
1158 expect(identifier.inDeclarationContext(), isTrue);
1159 }
1160
1161 void test_inDeclarationContext_variableDeclaration() {
1162 SimpleIdentifier identifier = AstFactory.variableDeclaration("v").name;
1163 expect(identifier.inDeclarationContext(), isTrue);
1164 }
1165
1166 void test_inGetterContext() {
1167 for (WrapperKind wrapper in WrapperKind.values) {
1168 for (AssignmentKind assignment in AssignmentKind.values) {
1169 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment);
1170 if (assignment == AssignmentKind.SIMPLE_LEFT &&
1171 wrapper != WrapperKind.PREFIXED_LEFT &&
1172 wrapper != WrapperKind.PROPERTY_LEFT) {
1173 if (identifier.inGetterContext()) {
1174 fail("Expected ${_topMostNode(identifier).toSource()} to be false");
1175 }
1176 } else {
1177 if (!identifier.inGetterContext()) {
1178 fail("Expected ${_topMostNode(identifier).toSource()} to be true");
1179 }
1180 }
1181 }
1182 }
1183 }
1184
1185 void test_inGetterContext_forEachLoop() {
1186 SimpleIdentifier identifier = AstFactory.identifier3("a");
1187 Expression iterator = AstFactory.listLiteral();
1188 Statement body = AstFactory.block();
1189 AstFactory.forEachStatement2(identifier, iterator, body);
1190 expect(identifier.inGetterContext(), isFalse);
1191 }
1192
1193 void test_inReferenceContext() {
1194 SimpleIdentifier identifier = AstFactory.identifier3("id");
1195 AstFactory.namedExpression(
1196 AstFactory.label(identifier), AstFactory.identifier3("_"));
1197 expect(identifier.inGetterContext(), isFalse);
1198 expect(identifier.inSetterContext(), isFalse);
1199 }
1200
1201 void test_inSetterContext() {
1202 for (WrapperKind wrapper in WrapperKind.values) {
1203 for (AssignmentKind assignment in AssignmentKind.values) {
1204 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment);
1205 if (wrapper == WrapperKind.PREFIXED_LEFT ||
1206 wrapper == WrapperKind.PROPERTY_LEFT ||
1207 assignment == AssignmentKind.BINARY ||
1208 assignment == AssignmentKind.COMPOUND_RIGHT ||
1209 assignment == AssignmentKind.PREFIX_NOT ||
1210 assignment == AssignmentKind.SIMPLE_RIGHT ||
1211 assignment == AssignmentKind.NONE) {
1212 if (identifier.inSetterContext()) {
1213 fail("Expected ${_topMostNode(identifier).toSource()} to be false");
1214 }
1215 } else {
1216 if (!identifier.inSetterContext()) {
1217 fail("Expected ${_topMostNode(identifier).toSource()} to be true");
1218 }
1219 }
1220 }
1221 }
1222 }
1223
1224 void test_inSetterContext_forEachLoop() {
1225 SimpleIdentifier identifier = AstFactory.identifier3("a");
1226 Expression iterator = AstFactory.listLiteral();
1227 Statement body = AstFactory.block();
1228 AstFactory.forEachStatement2(identifier, iterator, body);
1229 expect(identifier.inSetterContext(), isTrue);
1230 }
1231
1232 void test_isQualified_inMethodInvocation_noTarget() {
1233 MethodInvocation invocation =
1234 AstFactory.methodInvocation2("test", [AstFactory.identifier3("arg0")]);
1235 SimpleIdentifier identifier = invocation.methodName;
1236 expect(identifier.isQualified, isFalse);
1237 }
1238
1239 void test_isQualified_inMethodInvocation_withTarget() {
1240 MethodInvocation invocation = AstFactory.methodInvocation(
1241 AstFactory.identifier3("target"),
1242 "test",
1243 [AstFactory.identifier3("arg0")]);
1244 SimpleIdentifier identifier = invocation.methodName;
1245 expect(identifier.isQualified, isTrue);
1246 }
1247
1248 void test_isQualified_inPrefixedIdentifier_name() {
1249 SimpleIdentifier identifier = AstFactory.identifier3("test");
1250 AstFactory.identifier4("prefix", identifier);
1251 expect(identifier.isQualified, isTrue);
1252 }
1253
1254 void test_isQualified_inPrefixedIdentifier_prefix() {
1255 SimpleIdentifier identifier = AstFactory.identifier3("test");
1256 AstFactory.identifier(identifier, AstFactory.identifier3("name"));
1257 expect(identifier.isQualified, isFalse);
1258 }
1259
1260 void test_isQualified_inPropertyAccess_name() {
1261 SimpleIdentifier identifier = AstFactory.identifier3("test");
1262 AstFactory.propertyAccess(AstFactory.identifier3("target"), identifier);
1263 expect(identifier.isQualified, isTrue);
1264 }
1265
1266 void test_isQualified_inPropertyAccess_target() {
1267 SimpleIdentifier identifier = AstFactory.identifier3("test");
1268 AstFactory.propertyAccess(identifier, AstFactory.identifier3("name"));
1269 expect(identifier.isQualified, isFalse);
1270 }
1271
1272 void test_isQualified_inReturnStatement() {
1273 SimpleIdentifier identifier = AstFactory.identifier3("test");
1274 AstFactory.returnStatement2(identifier);
1275 expect(identifier.isQualified, isFalse);
1276 }
1277
1278 SimpleIdentifier _createIdentifier(
1279 WrapperKind wrapper, AssignmentKind assignment) {
1280 SimpleIdentifier identifier = AstFactory.identifier3("a");
1281 Expression expression = identifier;
1282 while (true) {
1283 if (wrapper == WrapperKind.PREFIXED_LEFT) {
1284 expression =
1285 AstFactory.identifier(identifier, AstFactory.identifier3("_"));
1286 } else if (wrapper == WrapperKind.PREFIXED_RIGHT) {
1287 expression =
1288 AstFactory.identifier(AstFactory.identifier3("_"), identifier);
1289 } else if (wrapper == WrapperKind.PROPERTY_LEFT) {
1290 expression = AstFactory.propertyAccess2(expression, "_");
1291 } else if (wrapper == WrapperKind.PROPERTY_RIGHT) {
1292 expression =
1293 AstFactory.propertyAccess(AstFactory.identifier3("_"), identifier);
1294 } else if (wrapper == WrapperKind.NONE) {}
1295 break;
1296 }
1297 while (true) {
1298 if (assignment == AssignmentKind.BINARY) {
1299 AstFactory.binaryExpression(
1300 expression, TokenType.PLUS, AstFactory.identifier3("_"));
1301 } else if (assignment == AssignmentKind.COMPOUND_LEFT) {
1302 AstFactory.assignmentExpression(
1303 expression, TokenType.PLUS_EQ, AstFactory.identifier3("_"));
1304 } else if (assignment == AssignmentKind.COMPOUND_RIGHT) {
1305 AstFactory.assignmentExpression(
1306 AstFactory.identifier3("_"), TokenType.PLUS_EQ, expression);
1307 } else if (assignment == AssignmentKind.POSTFIX_INC) {
1308 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS);
1309 } else if (assignment == AssignmentKind.PREFIX_DEC) {
1310 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression);
1311 } else if (assignment == AssignmentKind.PREFIX_INC) {
1312 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression);
1313 } else if (assignment == AssignmentKind.PREFIX_NOT) {
1314 AstFactory.prefixExpression(TokenType.BANG, expression);
1315 } else if (assignment == AssignmentKind.SIMPLE_LEFT) {
1316 AstFactory.assignmentExpression(
1317 expression, TokenType.EQ, AstFactory.identifier3("_"));
1318 } else if (assignment == AssignmentKind.SIMPLE_RIGHT) {
1319 AstFactory.assignmentExpression(
1320 AstFactory.identifier3("_"), TokenType.EQ, expression);
1321 } else if (assignment == AssignmentKind.NONE) {}
1322 break;
1323 }
1324 return identifier;
1325 }
1326
1327 /**
1328 * Return the top-most node in the AST structure containing the given identifi er.
1329 *
1330 * @param identifier the identifier in the AST structure being traversed
1331 * @return the root of the AST structure containing the identifier
1332 */
1333 AstNode _topMostNode(SimpleIdentifier identifier) {
1334 AstNode child = identifier;
1335 AstNode parent = identifier.parent;
1336 while (parent != null) {
1337 child = parent;
1338 parent = parent.parent;
1339 }
1340 return child;
1341 }
1342 }
1343
1344 @reflectiveTest
1345 class SimpleStringLiteralTest extends ParserTestCase {
1346 void test_contentsEnd() {
1347 expect(
1348 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X")
1349 .contentsEnd,
1350 2);
1351 expect(
1352 new SimpleStringLiteral(TokenFactory.tokenFromString('"X"'), "X")
1353 .contentsEnd,
1354 2);
1355
1356 expect(
1357 new SimpleStringLiteral(TokenFactory.tokenFromString('"""X"""'), "X")
1358 .contentsEnd,
1359 4);
1360 expect(
1361 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1362 .contentsEnd,
1363 4);
1364 expect(
1365 new SimpleStringLiteral(
1366 TokenFactory.tokenFromString("''' \nX'''"), "X")
1367 .contentsEnd,
1368 7);
1369
1370 expect(
1371 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1372 .contentsEnd,
1373 3);
1374 expect(
1375 new SimpleStringLiteral(TokenFactory.tokenFromString('r"X"'), "X")
1376 .contentsEnd,
1377 3);
1378
1379 expect(
1380 new SimpleStringLiteral(TokenFactory.tokenFromString('r"""X"""'), "X")
1381 .contentsEnd,
1382 5);
1383 expect(
1384 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1385 .contentsEnd,
1386 5);
1387 expect(
1388 new SimpleStringLiteral(
1389 TokenFactory.tokenFromString("r''' \nX'''"), "X")
1390 .contentsEnd,
1391 8);
1392 }
1393
1394 void test_contentsOffset() {
1395 expect(
1396 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X")
1397 .contentsOffset,
1398 1);
1399 expect(
1400 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X")
1401 .contentsOffset,
1402 1);
1403 expect(
1404 new SimpleStringLiteral(
1405 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X")
1406 .contentsOffset,
1407 3);
1408 expect(
1409 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1410 .contentsOffset,
1411 3);
1412 expect(
1413 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1414 .contentsOffset,
1415 2);
1416 expect(
1417 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X")
1418 .contentsOffset,
1419 2);
1420 expect(
1421 new SimpleStringLiteral(
1422 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X")
1423 .contentsOffset,
1424 4);
1425 expect(
1426 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1427 .contentsOffset,
1428 4);
1429 // leading whitespace
1430 expect(
1431 new SimpleStringLiteral(
1432 TokenFactory.tokenFromString("''' \ \nX''"), "X")
1433 .contentsOffset,
1434 6);
1435 expect(
1436 new SimpleStringLiteral(
1437 TokenFactory.tokenFromString('r""" \ \nX"""'), "X")
1438 .contentsOffset,
1439 7);
1440 }
1441
1442 void test_isMultiline() {
1443 expect(
1444 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X")
1445 .isMultiline,
1446 isFalse);
1447 expect(
1448 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1449 .isMultiline,
1450 isFalse);
1451 expect(
1452 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X")
1453 .isMultiline,
1454 isFalse);
1455 expect(
1456 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X")
1457 .isMultiline,
1458 isFalse);
1459 expect(
1460 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1461 .isMultiline,
1462 isTrue);
1463 expect(
1464 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1465 .isMultiline,
1466 isTrue);
1467 expect(
1468 new SimpleStringLiteral(
1469 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X")
1470 .isMultiline,
1471 isTrue);
1472 expect(
1473 new SimpleStringLiteral(
1474 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X")
1475 .isMultiline,
1476 isTrue);
1477 }
1478
1479 void test_isRaw() {
1480 expect(
1481 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X").isRaw,
1482 isFalse);
1483 expect(
1484 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X")
1485 .isRaw,
1486 isFalse);
1487 expect(
1488 new SimpleStringLiteral(
1489 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X")
1490 .isRaw,
1491 isFalse);
1492 expect(
1493 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X")
1494 .isRaw,
1495 isFalse);
1496 expect(
1497 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X")
1498 .isRaw,
1499 isTrue);
1500 expect(
1501 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X")
1502 .isRaw,
1503 isTrue);
1504 expect(
1505 new SimpleStringLiteral(
1506 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X")
1507 .isRaw,
1508 isTrue);
1509 expect(
1510 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X")
1511 .isRaw,
1512 isTrue);
1513 }
1514
1515 void test_isSingleQuoted() {
1516 // '
1517 {
1518 var token = TokenFactory.tokenFromString("'X'");
1519 var node = new SimpleStringLiteral(token, null);
1520 expect(node.isSingleQuoted, isTrue);
1521 }
1522 // '''
1523 {
1524 var token = TokenFactory.tokenFromString("'''X'''");
1525 var node = new SimpleStringLiteral(token, null);
1526 expect(node.isSingleQuoted, isTrue);
1527 }
1528 // "
1529 {
1530 var token = TokenFactory.tokenFromString('"X"');
1531 var node = new SimpleStringLiteral(token, null);
1532 expect(node.isSingleQuoted, isFalse);
1533 }
1534 // """
1535 {
1536 var token = TokenFactory.tokenFromString('"""X"""');
1537 var node = new SimpleStringLiteral(token, null);
1538 expect(node.isSingleQuoted, isFalse);
1539 }
1540 }
1541
1542 void test_isSingleQuoted_raw() {
1543 // r'
1544 {
1545 var token = TokenFactory.tokenFromString("r'X'");
1546 var node = new SimpleStringLiteral(token, null);
1547 expect(node.isSingleQuoted, isTrue);
1548 }
1549 // r'''
1550 {
1551 var token = TokenFactory.tokenFromString("r'''X'''");
1552 var node = new SimpleStringLiteral(token, null);
1553 expect(node.isSingleQuoted, isTrue);
1554 }
1555 // r"
1556 {
1557 var token = TokenFactory.tokenFromString('r"X"');
1558 var node = new SimpleStringLiteral(token, null);
1559 expect(node.isSingleQuoted, isFalse);
1560 }
1561 // r"""
1562 {
1563 var token = TokenFactory.tokenFromString('r"""X"""');
1564 var node = new SimpleStringLiteral(token, null);
1565 expect(node.isSingleQuoted, isFalse);
1566 }
1567 }
1568
1569 void test_simple() {
1570 Token token = TokenFactory.tokenFromString("'value'");
1571 SimpleStringLiteral stringLiteral = new SimpleStringLiteral(token, "value");
1572 expect(stringLiteral.literal, same(token));
1573 expect(stringLiteral.beginToken, same(token));
1574 expect(stringLiteral.endToken, same(token));
1575 expect(stringLiteral.value, "value");
1576 }
1577 }
1578
1579 @reflectiveTest
1580 class StringInterpolationTest extends ParserTestCase {
1581 void test_contentsOffsetEnd() {
1582 AstFactory.interpolationExpression(AstFactory.identifier3('bb'));
1583 // 'a${bb}ccc'
1584 {
1585 var ae = AstFactory.interpolationString("'a", "a");
1586 var cToken = new StringToken(TokenType.STRING, "ccc'", 10);
1587 var cElement = new InterpolationString(cToken, 'ccc');
1588 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1589 expect(node.contentsOffset, 1);
1590 expect(node.contentsEnd, 10 + 4 - 1);
1591 }
1592 // '''a${bb}ccc'''
1593 {
1594 var ae = AstFactory.interpolationString("'''a", "a");
1595 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10);
1596 var cElement = new InterpolationString(cToken, 'ccc');
1597 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1598 expect(node.contentsOffset, 3);
1599 expect(node.contentsEnd, 10 + 4 - 1);
1600 }
1601 // """a${bb}ccc"""
1602 {
1603 var ae = AstFactory.interpolationString('"""a', "a");
1604 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10);
1605 var cElement = new InterpolationString(cToken, 'ccc');
1606 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1607 expect(node.contentsOffset, 3);
1608 expect(node.contentsEnd, 10 + 4 - 1);
1609 }
1610 // r'a${bb}ccc'
1611 {
1612 var ae = AstFactory.interpolationString("r'a", "a");
1613 var cToken = new StringToken(TokenType.STRING, "ccc'", 10);
1614 var cElement = new InterpolationString(cToken, 'ccc');
1615 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1616 expect(node.contentsOffset, 2);
1617 expect(node.contentsEnd, 10 + 4 - 1);
1618 }
1619 // r'''a${bb}ccc'''
1620 {
1621 var ae = AstFactory.interpolationString("r'''a", "a");
1622 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10);
1623 var cElement = new InterpolationString(cToken, 'ccc');
1624 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1625 expect(node.contentsOffset, 4);
1626 expect(node.contentsEnd, 10 + 4 - 1);
1627 }
1628 // r"""a${bb}ccc"""
1629 {
1630 var ae = AstFactory.interpolationString('r"""a', "a");
1631 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10);
1632 var cElement = new InterpolationString(cToken, 'ccc');
1633 StringInterpolation node = AstFactory.string([ae, ae, cElement]);
1634 expect(node.contentsOffset, 4);
1635 expect(node.contentsEnd, 10 + 4 - 1);
1636 }
1637 }
1638
1639 void test_isMultiline() {
1640 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb'));
1641 // '
1642 {
1643 var a = AstFactory.interpolationString("'a", "a");
1644 var c = AstFactory.interpolationString("ccc'", "ccc");
1645 StringInterpolation node = AstFactory.string([a, b, c]);
1646 expect(node.isMultiline, isFalse);
1647 }
1648 // '''
1649 {
1650 var a = AstFactory.interpolationString("'''a", "a");
1651 var c = AstFactory.interpolationString("ccc'''", "ccc");
1652 StringInterpolation node = AstFactory.string([a, b, c]);
1653 expect(node.isMultiline, isTrue);
1654 }
1655 // "
1656 {
1657 var a = AstFactory.interpolationString('"a', "a");
1658 var c = AstFactory.interpolationString('ccc"', "ccc");
1659 StringInterpolation node = AstFactory.string([a, b, c]);
1660 expect(node.isMultiline, isFalse);
1661 }
1662 // """
1663 {
1664 var a = AstFactory.interpolationString('"""a', "a");
1665 var c = AstFactory.interpolationString('ccc"""', "ccc");
1666 StringInterpolation node = AstFactory.string([a, b, c]);
1667 expect(node.isMultiline, isTrue);
1668 }
1669 }
1670
1671 void test_isRaw() {
1672 StringInterpolation node = AstFactory.string();
1673 expect(node.isRaw, isFalse);
1674 }
1675
1676 void test_isSingleQuoted() {
1677 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb'));
1678 // "
1679 {
1680 var a = AstFactory.interpolationString('"a', "a");
1681 var c = AstFactory.interpolationString('ccc"', "ccc");
1682 StringInterpolation node = AstFactory.string([a, b, c]);
1683 expect(node.isSingleQuoted, isFalse);
1684 }
1685 // """
1686 {
1687 var a = AstFactory.interpolationString('"""a', "a");
1688 var c = AstFactory.interpolationString('ccc"""', "ccc");
1689 StringInterpolation node = AstFactory.string([a, b, c]);
1690 expect(node.isSingleQuoted, isFalse);
1691 }
1692 // '
1693 {
1694 var a = AstFactory.interpolationString("'a", "a");
1695 var c = AstFactory.interpolationString("ccc'", "ccc");
1696 StringInterpolation node = AstFactory.string([a, b, c]);
1697 expect(node.isSingleQuoted, isTrue);
1698 }
1699 // '''
1700 {
1701 var a = AstFactory.interpolationString("'''a", "a");
1702 var c = AstFactory.interpolationString("ccc'''", "ccc");
1703 StringInterpolation node = AstFactory.string([a, b, c]);
1704 expect(node.isSingleQuoted, isTrue);
1705 }
1706 }
1707 }
1708
1709 @reflectiveTest
1710 class ToSourceVisitorTest extends EngineTestCase { 431 class ToSourceVisitorTest extends EngineTestCase {
1711 void test_visitAdjacentStrings() { 432 void test_visitAdjacentStrings() {
1712 _assertSource( 433 _assertSource(
1713 "'a' 'b'", 434 "'a' 'b'",
1714 AstFactory.adjacentStrings( 435 AstFactory.adjacentStrings(
1715 [AstFactory.string2("a"), AstFactory.string2("b")])); 436 [AstFactory.string2("a"), AstFactory.string2("b")]));
1716 } 437 }
1717 438
1718 void test_visitAnnotation_constant() { 439 void test_visitAnnotation_constant() {
1719 _assertSource("@A", AstFactory.annotation(AstFactory.identifier3("A"))); 440 _assertSource("@A", AstFactory.annotation(AstFactory.identifier3("A")));
(...skipping 2217 matching lines...) Expand 10 before | Expand all | Expand 10 after
3937 * @param expectedSource the source string that the visitor is expected to pro duce 2658 * @param expectedSource the source string that the visitor is expected to pro duce
3938 * @param node the AST node being visited to produce the actual source 2659 * @param node the AST node being visited to produce the actual source
3939 * @throws AFE if the visitor does not produce the expected source for the giv en node 2660 * @throws AFE if the visitor does not produce the expected source for the giv en node
3940 */ 2661 */
3941 void _assertSource(String expectedSource, AstNode node) { 2662 void _assertSource(String expectedSource, AstNode node) {
3942 PrintStringWriter writer = new PrintStringWriter(); 2663 PrintStringWriter writer = new PrintStringWriter();
3943 node.accept(new ToSourceVisitor(writer)); 2664 node.accept(new ToSourceVisitor(writer));
3944 expect(writer.toString(), expectedSource); 2665 expect(writer.toString(), expectedSource);
3945 } 2666 }
3946 } 2667 }
3947
3948 @reflectiveTest
3949 class VariableDeclarationTest extends ParserTestCase {
3950 void test_getDocumentationComment_onGrandParent() {
3951 VariableDeclaration varDecl = AstFactory.variableDeclaration("a");
3952 TopLevelVariableDeclaration decl =
3953 AstFactory.topLevelVariableDeclaration2(Keyword.VAR, [varDecl]);
3954 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
3955 expect(varDecl.documentationComment, isNull);
3956 decl.documentationComment = comment;
3957 expect(varDecl.documentationComment, isNotNull);
3958 expect(decl.documentationComment, isNotNull);
3959 }
3960
3961 void test_getDocumentationComment_onNode() {
3962 VariableDeclaration decl = AstFactory.variableDeclaration("a");
3963 Comment comment = Comment.createDocumentationComment(new List<Token>(0));
3964 decl.documentationComment = comment;
3965 expect(decl.documentationComment, isNotNull);
3966 }
3967 }
3968
3969 class WrapperKind extends Enum<WrapperKind> {
3970 static const WrapperKind PREFIXED_LEFT =
3971 const WrapperKind('PREFIXED_LEFT', 0);
3972
3973 static const WrapperKind PREFIXED_RIGHT =
3974 const WrapperKind('PREFIXED_RIGHT', 1);
3975
3976 static const WrapperKind PROPERTY_LEFT =
3977 const WrapperKind('PROPERTY_LEFT', 2);
3978
3979 static const WrapperKind PROPERTY_RIGHT =
3980 const WrapperKind('PROPERTY_RIGHT', 3);
3981
3982 static const WrapperKind NONE = const WrapperKind('NONE', 4);
3983
3984 static const List<WrapperKind> values = const [
3985 PREFIXED_LEFT,
3986 PREFIXED_RIGHT,
3987 PROPERTY_LEFT,
3988 PROPERTY_RIGHT,
3989 NONE
3990 ];
3991
3992 const WrapperKind(String name, int ordinal) : super(name, ordinal);
3993 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/src/dart/ast/test_all.dart ('k') | pkg/analyzer/test/src/dart/element/element_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698