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

Side by Side Diff: pkg/analyzer/test/generated/element_resolver_test.dart

Issue 2551023005: Prepare for decoupling analyzer ASTs from element model. (Closed)
Patch Set: Created 4 years 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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.test.generated.element_resolver_test; 5 library analyzer.test.generated.element_resolver_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/resolution_accessors.dart';
8 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; 9 import 'package:analyzer/dart/ast/standard_ast_factory.dart';
9 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
10 import 'package:analyzer/dart/element/element.dart'; 11 import 'package:analyzer/dart/element/element.dart';
11 import 'package:analyzer/dart/element/type.dart'; 12 import 'package:analyzer/dart/element/type.dart';
12 import 'package:analyzer/file_system/memory_file_system.dart'; 13 import 'package:analyzer/file_system/memory_file_system.dart';
13 import 'package:analyzer/src/dart/element/element.dart'; 14 import 'package:analyzer/src/dart/element/element.dart';
14 import 'package:analyzer/src/generated/element_resolver.dart'; 15 import 'package:analyzer/src/generated/element_resolver.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 16 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/resolver.dart'; 17 import 'package:analyzer/src/generated/resolver.dart';
17 import 'package:analyzer/src/generated/source.dart'; 18 import 'package:analyzer/src/generated/source.dart';
(...skipping 24 matching lines...) Expand all
42 class A { 43 class A {
43 const A.named(); 44 const A.named();
44 } 45 }
45 '''); 46 ''');
46 _validateAnnotation('', '@A.named()', (SimpleIdentifier name1, 47 _validateAnnotation('', '@A.named()', (SimpleIdentifier name1,
47 SimpleIdentifier name2, 48 SimpleIdentifier name2,
48 SimpleIdentifier name3, 49 SimpleIdentifier name3,
49 Element annotationElement) { 50 Element annotationElement) {
50 expect(name1, isNotNull); 51 expect(name1, isNotNull);
51 expect(name1.staticElement, new isInstanceOf<ClassElement>()); 52 expect(name1.staticElement, new isInstanceOf<ClassElement>());
52 expect(name1.staticElement.displayName, 'A'); 53 expect(staticElementForIdentifier(name1).displayName, 'A');
53 expect(name2, isNotNull); 54 expect(name2, isNotNull);
54 expect(name2.staticElement, new isInstanceOf<ConstructorElement>()); 55 expect(name2.staticElement, new isInstanceOf<ConstructorElement>());
55 expect(name2.staticElement.displayName, 'named'); 56 expect(staticElementForIdentifier(name2).displayName, 'named');
56 expect(name3, isNull); 57 expect(name3, isNull);
57 if (annotationElement is ConstructorElement) { 58 if (annotationElement is ConstructorElement) {
58 expect(annotationElement, same(name2.staticElement)); 59 expect(annotationElement, same(name2.staticElement));
59 expect(annotationElement.enclosingElement, name1.staticElement); 60 expect(annotationElement.enclosingElement, name1.staticElement);
60 expect(annotationElement.displayName, 'named'); 61 expect(annotationElement.displayName, 'named');
61 expect(annotationElement.parameters, isEmpty); 62 expect(annotationElement.parameters, isEmpty);
62 } else { 63 } else {
63 fail('Expected "annotationElement" is ConstructorElement, ' 64 fail('Expected "annotationElement" is ConstructorElement, '
64 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 65 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
65 } 66 }
66 }); 67 });
67 } 68 }
68 69
69 void test_annotation_class_prefixed_namedConstructor() { 70 void test_annotation_class_prefixed_namedConstructor() {
70 addNamedSource( 71 addNamedSource(
71 '/a.dart', 72 '/a.dart',
72 r''' 73 r'''
73 class A { 74 class A {
74 const A.named(); 75 const A.named();
75 } 76 }
76 '''); 77 ''');
77 _validateAnnotation('as p', '@p.A.named()', (SimpleIdentifier name1, 78 _validateAnnotation('as p', '@p.A.named()', (SimpleIdentifier name1,
78 SimpleIdentifier name2, 79 SimpleIdentifier name2,
79 SimpleIdentifier name3, 80 SimpleIdentifier name3,
80 Element annotationElement) { 81 Element annotationElement) {
81 expect(name1, isNotNull); 82 expect(name1, isNotNull);
82 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); 83 expect(name1.staticElement, new isInstanceOf<PrefixElement>());
83 expect(name1.staticElement.displayName, 'p'); 84 expect(staticElementForIdentifier(name1).displayName, 'p');
84 expect(name2, isNotNull); 85 expect(name2, isNotNull);
85 expect(name2.staticElement, new isInstanceOf<ClassElement>()); 86 expect(name2.staticElement, new isInstanceOf<ClassElement>());
86 expect(name2.staticElement.displayName, 'A'); 87 expect(staticElementForIdentifier(name2).displayName, 'A');
87 expect(name3, isNotNull); 88 expect(name3, isNotNull);
88 expect(name3.staticElement, new isInstanceOf<ConstructorElement>()); 89 expect(name3.staticElement, new isInstanceOf<ConstructorElement>());
89 expect(name3.staticElement.displayName, 'named'); 90 expect(staticElementForIdentifier(name3).displayName, 'named');
90 if (annotationElement is ConstructorElement) { 91 if (annotationElement is ConstructorElement) {
91 expect(annotationElement, same(name3.staticElement)); 92 expect(annotationElement, same(name3.staticElement));
92 expect(annotationElement.enclosingElement, name2.staticElement); 93 expect(annotationElement.enclosingElement, name2.staticElement);
93 expect(annotationElement.displayName, 'named'); 94 expect(annotationElement.displayName, 'named');
94 expect(annotationElement.parameters, isEmpty); 95 expect(annotationElement.parameters, isEmpty);
95 } else { 96 } else {
96 fail('Expected "annotationElement" is ConstructorElement, ' 97 fail('Expected "annotationElement" is ConstructorElement, '
97 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 98 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
98 } 99 }
99 }); 100 });
100 } 101 }
101 102
102 void test_annotation_class_prefixed_staticConstField() { 103 void test_annotation_class_prefixed_staticConstField() {
103 addNamedSource( 104 addNamedSource(
104 '/a.dart', 105 '/a.dart',
105 r''' 106 r'''
106 class A { 107 class A {
107 static const V = 0; 108 static const V = 0;
108 } 109 }
109 '''); 110 ''');
110 _validateAnnotation('as p', '@p.A.V', (SimpleIdentifier name1, 111 _validateAnnotation('as p', '@p.A.V', (SimpleIdentifier name1,
111 SimpleIdentifier name2, 112 SimpleIdentifier name2,
112 SimpleIdentifier name3, 113 SimpleIdentifier name3,
113 Element annotationElement) { 114 Element annotationElement) {
114 expect(name1, isNotNull); 115 expect(name1, isNotNull);
115 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); 116 expect(name1.staticElement, new isInstanceOf<PrefixElement>());
116 expect(name1.staticElement.displayName, 'p'); 117 expect(staticElementForIdentifier(name1).displayName, 'p');
117 expect(name2, isNotNull); 118 expect(name2, isNotNull);
118 expect(name2.staticElement, new isInstanceOf<ClassElement>()); 119 expect(name2.staticElement, new isInstanceOf<ClassElement>());
119 expect(name2.staticElement.displayName, 'A'); 120 expect(staticElementForIdentifier(name2).displayName, 'A');
120 expect(name3, isNotNull); 121 expect(name3, isNotNull);
121 expect(name3.staticElement, new isInstanceOf<PropertyAccessorElement>()); 122 expect(name3.staticElement, new isInstanceOf<PropertyAccessorElement>());
122 expect(name3.staticElement.displayName, 'V'); 123 expect(staticElementForIdentifier(name3).displayName, 'V');
123 if (annotationElement is PropertyAccessorElement) { 124 if (annotationElement is PropertyAccessorElement) {
124 expect(annotationElement, same(name3.staticElement)); 125 expect(annotationElement, same(name3.staticElement));
125 expect(annotationElement.enclosingElement, name2.staticElement); 126 expect(annotationElement.enclosingElement, name2.staticElement);
126 expect(annotationElement.displayName, 'V'); 127 expect(annotationElement.displayName, 'V');
127 } else { 128 } else {
128 fail('Expected "annotationElement" is PropertyAccessorElement, ' 129 fail('Expected "annotationElement" is PropertyAccessorElement, '
129 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 130 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
130 } 131 }
131 }); 132 });
132 } 133 }
133 134
134 void test_annotation_class_prefixed_unnamedConstructor() { 135 void test_annotation_class_prefixed_unnamedConstructor() {
135 addNamedSource( 136 addNamedSource(
136 '/a.dart', 137 '/a.dart',
137 r''' 138 r'''
138 class A { 139 class A {
139 const A(); 140 const A();
140 } 141 }
141 '''); 142 ''');
142 _validateAnnotation('as p', '@p.A', (SimpleIdentifier name1, 143 _validateAnnotation('as p', '@p.A', (SimpleIdentifier name1,
143 SimpleIdentifier name2, 144 SimpleIdentifier name2,
144 SimpleIdentifier name3, 145 SimpleIdentifier name3,
145 Element annotationElement) { 146 Element annotationElement) {
146 expect(name1, isNotNull); 147 expect(name1, isNotNull);
147 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); 148 expect(name1.staticElement, new isInstanceOf<PrefixElement>());
148 expect(name1.staticElement.displayName, 'p'); 149 expect(staticElementForIdentifier(name1).displayName, 'p');
149 expect(name2, isNotNull); 150 expect(name2, isNotNull);
150 expect(name2.staticElement, new isInstanceOf<ClassElement>()); 151 expect(name2.staticElement, new isInstanceOf<ClassElement>());
151 expect(name2.staticElement.displayName, 'A'); 152 expect(staticElementForIdentifier(name2).displayName, 'A');
152 expect(name3, isNull); 153 expect(name3, isNull);
153 if (annotationElement is ConstructorElement) { 154 if (annotationElement is ConstructorElement) {
154 expect(annotationElement.enclosingElement, name2.staticElement); 155 expect(annotationElement.enclosingElement, name2.staticElement);
155 expect(annotationElement.displayName, ''); 156 expect(annotationElement.displayName, '');
156 expect(annotationElement.parameters, isEmpty); 157 expect(annotationElement.parameters, isEmpty);
157 } else { 158 } else {
158 fail('Expected "annotationElement" is ConstructorElement, ' 159 fail('Expected "annotationElement" is ConstructorElement, '
159 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 160 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
160 } 161 }
161 }); 162 });
162 } 163 }
163 164
164 void test_annotation_class_staticConstField() { 165 void test_annotation_class_staticConstField() {
165 addNamedSource( 166 addNamedSource(
166 '/a.dart', 167 '/a.dart',
167 r''' 168 r'''
168 class A { 169 class A {
169 static const V = 0; 170 static const V = 0;
170 } 171 }
171 '''); 172 ''');
172 _validateAnnotation('', '@A.V', (SimpleIdentifier name1, 173 _validateAnnotation('', '@A.V', (SimpleIdentifier name1,
173 SimpleIdentifier name2, 174 SimpleIdentifier name2,
174 SimpleIdentifier name3, 175 SimpleIdentifier name3,
175 Element annotationElement) { 176 Element annotationElement) {
176 expect(name1, isNotNull); 177 expect(name1, isNotNull);
177 expect(name1.staticElement, new isInstanceOf<ClassElement>()); 178 expect(name1.staticElement, new isInstanceOf<ClassElement>());
178 expect(name1.staticElement.displayName, 'A'); 179 expect(staticElementForIdentifier(name1).displayName, 'A');
179 expect(name2, isNotNull); 180 expect(name2, isNotNull);
180 expect(name2.staticElement, new isInstanceOf<PropertyAccessorElement>()); 181 expect(name2.staticElement, new isInstanceOf<PropertyAccessorElement>());
181 expect(name2.staticElement.displayName, 'V'); 182 expect(staticElementForIdentifier(name2).displayName, 'V');
182 expect(name3, isNull); 183 expect(name3, isNull);
183 if (annotationElement is PropertyAccessorElement) { 184 if (annotationElement is PropertyAccessorElement) {
184 expect(annotationElement, same(name2.staticElement)); 185 expect(annotationElement, same(name2.staticElement));
185 expect(annotationElement.enclosingElement, name1.staticElement); 186 expect(annotationElement.enclosingElement, name1.staticElement);
186 expect(annotationElement.displayName, 'V'); 187 expect(annotationElement.displayName, 'V');
187 } else { 188 } else {
188 fail('Expected "annotationElement" is PropertyAccessorElement, ' 189 fail('Expected "annotationElement" is PropertyAccessorElement, '
189 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 190 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
190 } 191 }
191 }); 192 });
192 } 193 }
193 194
194 void test_annotation_class_unnamedConstructor() { 195 void test_annotation_class_unnamedConstructor() {
195 addNamedSource( 196 addNamedSource(
196 '/a.dart', 197 '/a.dart',
197 r''' 198 r'''
198 class A { 199 class A {
199 const A(); 200 const A();
200 } 201 }
201 '''); 202 ''');
202 _validateAnnotation('', '@A', (SimpleIdentifier name1, 203 _validateAnnotation('', '@A', (SimpleIdentifier name1,
203 SimpleIdentifier name2, 204 SimpleIdentifier name2,
204 SimpleIdentifier name3, 205 SimpleIdentifier name3,
205 Element annotationElement) { 206 Element annotationElement) {
206 expect(name1, isNotNull); 207 expect(name1, isNotNull);
207 expect(name1.staticElement, new isInstanceOf<ClassElement>()); 208 expect(name1.staticElement, new isInstanceOf<ClassElement>());
208 expect(name1.staticElement.displayName, 'A'); 209 expect(staticElementForIdentifier(name1).displayName, 'A');
209 expect(name2, isNull); 210 expect(name2, isNull);
210 expect(name3, isNull); 211 expect(name3, isNull);
211 if (annotationElement is ConstructorElement) { 212 if (annotationElement is ConstructorElement) {
212 expect(annotationElement.enclosingElement, name1.staticElement); 213 expect(annotationElement.enclosingElement, name1.staticElement);
213 expect(annotationElement.displayName, ''); 214 expect(annotationElement.displayName, '');
214 expect(annotationElement.parameters, isEmpty); 215 expect(annotationElement.parameters, isEmpty);
215 } else { 216 } else {
216 fail('Expected "annotationElement" is ConstructorElement, ' 217 fail('Expected "annotationElement" is ConstructorElement, '
217 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 218 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
218 } 219 }
219 }); 220 });
220 } 221 }
221 222
222 void test_annotation_topLevelVariable() { 223 void test_annotation_topLevelVariable() {
223 addNamedSource( 224 addNamedSource(
224 '/a.dart', 225 '/a.dart',
225 r''' 226 r'''
226 const V = 0; 227 const V = 0;
227 '''); 228 ''');
228 _validateAnnotation('', '@V', (SimpleIdentifier name1, 229 _validateAnnotation('', '@V', (SimpleIdentifier name1,
229 SimpleIdentifier name2, 230 SimpleIdentifier name2,
230 SimpleIdentifier name3, 231 SimpleIdentifier name3,
231 Element annotationElement) { 232 Element annotationElement) {
232 expect(name1, isNotNull); 233 expect(name1, isNotNull);
233 expect(name1.staticElement, new isInstanceOf<PropertyAccessorElement>()); 234 expect(name1.staticElement, new isInstanceOf<PropertyAccessorElement>());
234 expect(name1.staticElement.displayName, 'V'); 235 expect(staticElementForIdentifier(name1).displayName, 'V');
235 expect(name2, isNull); 236 expect(name2, isNull);
236 expect(name3, isNull); 237 expect(name3, isNull);
237 if (annotationElement is PropertyAccessorElement) { 238 if (annotationElement is PropertyAccessorElement) {
238 expect(annotationElement, same(name1.staticElement)); 239 expect(annotationElement, same(name1.staticElement));
239 expect(annotationElement.enclosingElement, 240 expect(annotationElement.enclosingElement,
240 new isInstanceOf<CompilationUnitElement>()); 241 new isInstanceOf<CompilationUnitElement>());
241 expect(annotationElement.displayName, 'V'); 242 expect(annotationElement.displayName, 'V');
242 } else { 243 } else {
243 fail('Expected "annotationElement" is PropertyAccessorElement, ' 244 fail('Expected "annotationElement" is PropertyAccessorElement, '
244 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 245 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
245 } 246 }
246 }); 247 });
247 } 248 }
248 249
249 void test_annotation_topLevelVariable_prefixed() { 250 void test_annotation_topLevelVariable_prefixed() {
250 addNamedSource( 251 addNamedSource(
251 '/a.dart', 252 '/a.dart',
252 r''' 253 r'''
253 const V = 0; 254 const V = 0;
254 '''); 255 ''');
255 _validateAnnotation('as p', '@p.V', (SimpleIdentifier name1, 256 _validateAnnotation('as p', '@p.V', (SimpleIdentifier name1,
256 SimpleIdentifier name2, 257 SimpleIdentifier name2,
257 SimpleIdentifier name3, 258 SimpleIdentifier name3,
258 Element annotationElement) { 259 Element annotationElement) {
259 expect(name1, isNotNull); 260 expect(name1, isNotNull);
260 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); 261 expect(name1.staticElement, new isInstanceOf<PrefixElement>());
261 expect(name1.staticElement.displayName, 'p'); 262 expect(staticElementForIdentifier(name1).displayName, 'p');
262 expect(name2, isNotNull); 263 expect(name2, isNotNull);
263 expect(name2.staticElement, new isInstanceOf<PropertyAccessorElement>()); 264 expect(name2.staticElement, new isInstanceOf<PropertyAccessorElement>());
264 expect(name2.staticElement.displayName, 'V'); 265 expect(staticElementForIdentifier(name2).displayName, 'V');
265 expect(name3, isNull); 266 expect(name3, isNull);
266 if (annotationElement is PropertyAccessorElement) { 267 if (annotationElement is PropertyAccessorElement) {
267 expect(annotationElement, same(name2.staticElement)); 268 expect(annotationElement, same(name2.staticElement));
268 expect(annotationElement.enclosingElement, 269 expect(annotationElement.enclosingElement,
269 new isInstanceOf<CompilationUnitElement>()); 270 new isInstanceOf<CompilationUnitElement>());
270 expect(annotationElement.displayName, 'V'); 271 expect(annotationElement.displayName, 'V');
271 } else { 272 } else {
272 fail('Expected "annotationElement" is PropertyAccessorElement, ' 273 fail('Expected "annotationElement" is PropertyAccessorElement, '
273 'but (${annotationElement?.runtimeType}) $annotationElement found.') ; 274 'but (${annotationElement?.runtimeType}) $annotationElement found.') ;
274 } 275 }
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 ClassElementImpl classA = ElementFactory.classElement2("A"); 684 ClassElementImpl classA = ElementFactory.classElement2("A");
684 classA.fields = <FieldElement>[fieldElement]; 685 classA.fields = <FieldElement>[fieldElement];
685 FieldFormalParameter parameter = 686 FieldFormalParameter parameter =
686 AstTestFactory.fieldFormalParameter2(fieldName); 687 AstTestFactory.fieldFormalParameter2(fieldName);
687 FieldFormalParameterElementImpl parameterElement = 688 FieldFormalParameterElementImpl parameterElement =
688 ElementFactory.fieldFormalParameter(parameter.identifier); 689 ElementFactory.fieldFormalParameter(parameter.identifier);
689 parameterElement.field = fieldElement; 690 parameterElement.field = fieldElement;
690 parameterElement.type = intType; 691 parameterElement.type = intType;
691 parameter.identifier.staticElement = parameterElement; 692 parameter.identifier.staticElement = parameterElement;
692 _resolveInClass(parameter, classA); 693 _resolveInClass(parameter, classA);
693 expect(parameter.element.type, same(intType)); 694 expect(elementForFormalParameter(parameter).type, same(intType));
694 } 695 }
695 696
696 void test_visitImportDirective_noCombinators_noPrefix() { 697 void test_visitImportDirective_noCombinators_noPrefix() {
697 ImportDirective directive = AstTestFactory.importDirective3(null, null); 698 ImportDirective directive = AstTestFactory.importDirective3(null, null);
698 directive.element = ElementFactory.importFor( 699 directive.element = ElementFactory.importFor(
699 ElementFactory.library(_definingLibrary.context, "lib"), null); 700 ElementFactory.library(_definingLibrary.context, "lib"), null);
700 _resolveNode(directive); 701 _resolveNode(directive);
701 _listener.assertNoErrors(); 702 _listener.assertNoErrors();
702 } 703 }
703 704
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after
1290 innerScope = new LabelScope( 1291 innerScope = new LabelScope(
1291 outerScope, labelElement.name, labelTarget, labelElement); 1292 outerScope, labelElement.name, labelTarget, labelElement);
1292 } 1293 }
1293 _visitor.labelScope = innerScope; 1294 _visitor.labelScope = innerScope;
1294 statement.accept(_resolver); 1295 statement.accept(_resolver);
1295 } finally { 1296 } finally {
1296 _visitor.labelScope = outerScope; 1297 _visitor.labelScope = outerScope;
1297 } 1298 }
1298 } 1299 }
1299 } 1300 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698