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

Side by Side Diff: pkg/analyzer/test/dart/element/builder_test.dart

Issue 2431783002: Add (shared) tests for ApiElementBuilder and ElementBuilder. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/builder.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.dart.element.builder_test; 5 library analyzer.test.dart.element.builder_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/token.dart'; 8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/src/dart/ast/ast.dart'; 10 import 'package:analyzer/src/dart/ast/ast.dart';
11 import 'package:analyzer/src/dart/element/builder.dart'; 11 import 'package:analyzer/src/dart/element/builder.dart';
12 import 'package:analyzer/src/dart/element/element.dart'; 12 import 'package:analyzer/src/dart/element/element.dart';
13 import 'package:analyzer/src/generated/engine.dart'; 13 import 'package:analyzer/src/generated/engine.dart';
14 import 'package:analyzer/src/generated/resolver.dart'; 14 import 'package:analyzer/src/generated/resolver.dart';
15 import 'package:analyzer/src/generated/source.dart'; 15 import 'package:analyzer/src/generated/source.dart';
16 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'; 17 import 'package:analyzer/src/generated/testing/element_factory.dart';
18 import 'package:analyzer/src/generated/testing/token_factory.dart'; 18 import 'package:analyzer/src/generated/testing/token_factory.dart';
19 import 'package:analyzer/src/generated/utilities_dart.dart'; 19 import 'package:analyzer/src/generated/utilities_dart.dart';
20 import 'package:test/test.dart'; 20 import 'package:test/test.dart';
21 import 'package:test_reflective_loader/test_reflective_loader.dart'; 21 import 'package:test_reflective_loader/test_reflective_loader.dart';
22 22
23 import '../../generated/parser_test.dart'; 23 import '../../generated/parser_test.dart';
24 import '../../generated/test_support.dart'; 24 import '../../generated/test_support.dart';
25 25
26 main() { 26 main() {
27 defineReflectiveSuite(() { 27 defineReflectiveSuite(() {
28 defineReflectiveTests(ApiElementBuilderTest);
28 defineReflectiveTests(ElementBuilderTest); 29 defineReflectiveTests(ElementBuilderTest);
29 }); 30 });
30 } 31 }
31 32
32 @reflectiveTest 33 @reflectiveTest
33 class ElementBuilderTest extends ParserTestCase { 34 class ApiElementBuilderTest extends _BaseTest with _ApiElementBuilderTestMixin {
34 CompilationUnitElement compilationUnitElement; 35 @override
35 CompilationUnit compilationUnit; 36 AstVisitor createElementBuilder(ElementHolder holder) {
37 return new ApiElementBuilder(holder, compilationUnitElement);
38 }
36 39
40 void test_api_class_field() {
41 List<FieldElement> fields = buildElementsForText(r'''
42 class C {
43 var a = 42;
44 var b = () {
45 int v = 0;
46 localFunction() {}
47 };
48 }
49 ''').types[0].fields;
50 expect(fields, hasLength(2));
51 {
52 FieldElement a = fields[0];
53 expect(a.displayName, 'a');
54 expect(a.initializer, isNull);
55 }
56 {
57 FieldElement b = fields[1];
58 expect(b.displayName, 'b');
59 expect(b.initializer, isNull);
60 }
61 }
62
63 void test_api_class_method_blockBody() {
64 MethodElement method = buildElementsForText(r'''
65 class C {
66 void m(int a, {int b: 42}) {
67 int v = 0;
68 localFunction() {}
69 }
70 }
71 ''').types[0].methods[0];
72 {
73 expect(method.parameters, hasLength(2));
74 expect(method.parameters[0].displayName, 'a');
75 expect(method.parameters[0].initializer, isNull);
76 expect(method.parameters[1].displayName, 'b');
77 expect(method.parameters[1].initializer, isNull);
78 }
79 expect(method.localVariables, isEmpty);
80 expect(method.functions, isEmpty);
81 }
82
83 void test_api_topLevelFunction_blockBody() {
84 FunctionElement function = buildElementsForText(r'''
85 void topLevelFunction() {
86 int v = 0;
87 localFunction() {}
88 }
89 ''').functions[0];
90 expect(function.localVariables, isEmpty);
91 expect(function.functions, isEmpty);
92 }
93
94 void test_api_topLevelFunction_expressionBody() {
95 FunctionElement function = buildElementsForText(r'''
96 topLevelFunction() => () {
97 int localVar = 0;
98 };
99 ''').functions[0];
100 expect(function.localVariables, isEmpty);
101 expect(function.functions, isEmpty);
102 }
103
104 void test_api_topLevelFunction_parameters() {
105 FunctionElement function = buildElementsForText(r'''
106 void topLevelFunction(int a, int b(double b2), {c: () {int c2; c3() {} }}) {
107 }
108 ''').functions[0];
109 List<ParameterElement> parameters = function.parameters;
110 expect(parameters, hasLength(3));
111 {
112 ParameterElement a = parameters[0];
113 expect(a.displayName, 'a');
114 expect(a.initializer, isNull);
115 }
116 {
117 ParameterElement b = parameters[1];
118 expect(b.displayName, 'b');
119 expect(b.initializer, isNull);
120 expect(b.parameters, hasLength(1));
121 expect(b.parameters[0].displayName, 'b2');
122 }
123 {
124 var c = parameters[2] as DefaultParameterElementImpl;
125 expect(c.displayName, 'c');
126 expect(c.initializer, isNull);
127 }
128 }
129
130 void test_api_topLevelVariable() {
131 List<TopLevelVariableElement> variables = buildElementsForText(r'''
132 var A = 42;
133 var B = () {
134 int v = 0;
135 localFunction(int _) {}
136 };
137 ''').topLevelVariables;
138 expect(variables, hasLength(2));
139 {
140 TopLevelVariableElement a = variables[0];
141 expect(a.displayName, 'A');
142 expect(a.initializer, isNull);
143 }
144 {
145 TopLevelVariableElement b = variables[1];
146 expect(b.displayName, 'B');
147 expect(b.initializer, isNull);
148 }
149 }
150 }
151
152 @reflectiveTest
153 class ElementBuilderTest extends _BaseTest with _ApiElementBuilderTestMixin {
37 /** 154 /**
38 * Parse the given [code], pass it through [ElementBuilder], and return the 155 * Parse the given [code], pass it through [ElementBuilder], and return the
39 * resulting [ElementHolder]. 156 * resulting [ElementHolder].
40 */ 157 */
41 ElementHolder buildElementsForText(String code) { 158 ElementHolder buildElementsForText(String code) {
42 TestLogger logger = new TestLogger(); 159 ElementHolder holder = new ElementHolder();
43 AnalysisEngine.instance.logger = logger; 160 ElementBuilder builder = new ElementBuilder(holder, compilationUnitElement);
44 try { 161 _visitAstOfCode(code, builder);
45 compilationUnit = ParserTestCase.parseCompilationUnit(code); 162 return holder;
46 ElementHolder holder = new ElementHolder();
47 ElementBuilder builder =
48 new ElementBuilder(holder, compilationUnitElement);
49 compilationUnit.accept(builder);
50 return holder;
51 } finally {
52 expect(logger.log, hasLength(0));
53 AnalysisEngine.instance.logger = Logger.NULL;
54 }
55 } 163 }
56 164
57 /** 165 AstVisitor createElementBuilder(ElementHolder holder) {
58 * Verify that the given [metadata] has exactly one annotation, and that its 166 return new ElementBuilder(holder, compilationUnitElement);
59 * [ElementAnnotationImpl] is unresolved.
60 */
61 void checkAnnotation(NodeList<Annotation> metadata) {
62 expect(metadata, hasLength(1));
63 expect(metadata[0], new isInstanceOf<AnnotationImpl>());
64 AnnotationImpl annotation = metadata[0];
65 expect(annotation.elementAnnotation,
66 new isInstanceOf<ElementAnnotationImpl>());
67 ElementAnnotationImpl elementAnnotation = annotation.elementAnnotation;
68 expect(elementAnnotation.element, isNull); // Not yet resolved
69 expect(elementAnnotation.compilationUnit, isNotNull);
70 expect(elementAnnotation.compilationUnit, compilationUnitElement);
71 }
72
73 /**
74 * Verify that the given [element] has exactly one annotation, and that its
75 * [ElementAnnotationImpl] is unresolved.
76 */
77 void checkMetadata(Element element) {
78 expect(element.metadata, hasLength(1));
79 expect(element.metadata[0], new isInstanceOf<ElementAnnotationImpl>());
80 ElementAnnotationImpl elementAnnotation = element.metadata[0];
81 expect(elementAnnotation.element, isNull); // Not yet resolved
82 expect(elementAnnotation.compilationUnit, isNotNull);
83 expect(elementAnnotation.compilationUnit, compilationUnitElement);
84 } 167 }
85 168
86 void fail_visitMethodDeclaration_setter_duplicate() { 169 void fail_visitMethodDeclaration_setter_duplicate() {
87 // https://github.com/dart-lang/sdk/issues/25601 170 // https://github.com/dart-lang/sdk/issues/25601
88 String code = r''' 171 String code = r'''
89 class C { 172 class C {
90 set zzz(x) {} 173 set zzz(x) {}
91 set zzz(y) {} 174 set zzz(y) {}
92 } 175 }
93 '''; 176 ''';
94 ClassElement classElement = buildElementsForText(code).types[0]; 177 ClassElement classElement = buildElementsForText(code).types[0];
95 for (PropertyAccessorElement accessor in classElement.accessors) { 178 for (PropertyAccessorElement accessor in classElement.accessors) {
96 expect(accessor.variable.setter, same(accessor)); 179 expect(accessor.variable.setter, same(accessor));
97 } 180 }
98 } 181 }
99 182
100 @override 183 @override
101 void setUp() { 184 void setUp() {
102 super.setUp(); 185 super.setUp();
103 compilationUnitElement = new CompilationUnitElementImpl('test.dart'); 186 compilationUnitElement = new CompilationUnitElementImpl('test.dart');
104 } 187 }
105 188
106 void test_metadata_fieldDeclaration() { 189 void test_visitDefaultFormalParameter_noType() {
107 List<FieldElement> fields = 190 // p = 0
108 buildElementsForText('class C { @a int x, y; }').types[0].fields; 191 String parameterName = 'p';
109 checkMetadata(fields[0]); 192 DefaultFormalParameter formalParameter =
110 checkMetadata(fields[1]); 193 AstFactory.positionalFormalParameter(
111 expect(fields[0].metadata, same(fields[1].metadata)); 194 AstFactory.simpleFormalParameter3(parameterName),
195 AstFactory.integer(0));
196 formalParameter.beginToken.offset = 50;
197 formalParameter.endToken.offset = 80;
198
199 ElementHolder holder = buildElementsForAst(formalParameter);
200 List<ParameterElement> parameters = holder.parameters;
201 expect(parameters, hasLength(1));
202 ParameterElement parameter = parameters[0];
203 assertHasCodeRange(parameter, 50, 31);
204 expect(parameter.hasImplicitType, isTrue);
205 expect(parameter.initializer, isNotNull);
206 expect(parameter.initializer.type, isNotNull);
207 expect(parameter.initializer.hasImplicitReturnType, isTrue);
208 expect(parameter.isConst, isFalse);
209 expect(parameter.isDeprecated, isFalse);
210 expect(parameter.isFinal, isFalse);
211 expect(parameter.isInitializingFormal, isFalse);
212 expect(parameter.isOverride, isFalse);
213 expect(parameter.isPrivate, isFalse);
214 expect(parameter.isPublic, isTrue);
215 expect(parameter.isSynthetic, isFalse);
216 expect(parameter.name, parameterName);
217 }
218
219 void test_visitDefaultFormalParameter_type() {
220 // E p = 0
221 String parameterName = 'p';
222 DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter(
223 AstFactory.simpleFormalParameter4(
224 AstFactory.typeName4('E'), parameterName),
225 AstFactory.integer(0));
226
227 ElementHolder holder = buildElementsForAst(formalParameter);
228 List<ParameterElement> parameters = holder.parameters;
229 expect(parameters, hasLength(1));
230 ParameterElement parameter = parameters[0];
231 expect(parameter.hasImplicitType, isFalse);
232 expect(parameter.initializer, isNotNull);
233 expect(parameter.initializer.type, isNotNull);
234 expect(parameter.initializer.hasImplicitReturnType, isTrue);
235 expect(parameter.isConst, isFalse);
236 expect(parameter.isDeprecated, isFalse);
237 expect(parameter.isFinal, isFalse);
238 expect(parameter.isInitializingFormal, isFalse);
239 expect(parameter.isOverride, isFalse);
240 expect(parameter.isPrivate, isFalse);
241 expect(parameter.isPublic, isTrue);
242 expect(parameter.isSynthetic, isFalse);
243 expect(parameter.name, parameterName);
112 } 244 }
113 245
114 void test_metadata_localVariableDeclaration() { 246 void test_metadata_localVariableDeclaration() {
115 List<LocalVariableElement> localVariables = 247 List<LocalVariableElement> localVariables =
116 buildElementsForText('f() { @a int x, y; }') 248 buildElementsForText('f() { @a int x, y; }')
117 .functions[0] 249 .functions[0]
118 .localVariables; 250 .localVariables;
119 checkMetadata(localVariables[0]); 251 checkMetadata(localVariables[0]);
120 checkMetadata(localVariables[1]); 252 checkMetadata(localVariables[1]);
121 expect(localVariables[0].metadata, same(localVariables[1].metadata)); 253 expect(localVariables[0].metadata, same(localVariables[1].metadata));
122 } 254 }
123 255
124 void test_metadata_topLevelVariableDeclaration() {
125 List<TopLevelVariableElement> topLevelVariables =
126 buildElementsForText('@a int x, y;').topLevelVariables;
127 checkMetadata(topLevelVariables[0]);
128 checkMetadata(topLevelVariables[1]);
129 expect(topLevelVariables[0].metadata, same(topLevelVariables[1].metadata));
130 }
131
132 void test_metadata_visitClassDeclaration() {
133 ClassElement classElement = buildElementsForText('@a class C {}').types[0];
134 checkMetadata(classElement);
135 }
136
137 void test_metadata_visitClassTypeAlias() {
138 ClassElement classElement =
139 buildElementsForText('@a class C = D with E;').types[0];
140 checkMetadata(classElement);
141 }
142
143 void test_metadata_visitConstructorDeclaration() {
144 ConstructorElement constructorElement =
145 buildElementsForText('class C { @a C(); }').types[0].constructors[0];
146 checkMetadata(constructorElement);
147 }
148
149 void test_metadata_visitDeclaredIdentifier() { 256 void test_metadata_visitDeclaredIdentifier() {
150 LocalVariableElement localVariableElement = 257 LocalVariableElement localVariableElement =
151 buildElementsForText('f() { for (@a var x in y) {} }') 258 buildElementsForText('f() { for (@a var x in y) {} }')
152 .functions[0] 259 .functions[0]
153 .localVariables[0]; 260 .localVariables[0];
154 checkMetadata(localVariableElement); 261 checkMetadata(localVariableElement);
155 } 262 }
156 263
157 void test_metadata_visitDefaultFormalParameter_fieldFormalParameter() {
158 ParameterElement parameterElement =
159 buildElementsForText('class C { var x; C([@a this.x = null]); }')
160 .types[0]
161 .constructors[0]
162 .parameters[0];
163 checkMetadata(parameterElement);
164 }
165
166 void
167 test_metadata_visitDefaultFormalParameter_functionTypedFormalParameter() {
168 ParameterElement parameterElement =
169 buildElementsForText('f([@a g() = null]) {}').functions[0].parameters[
170 0];
171 checkMetadata(parameterElement);
172 }
173
174 void test_metadata_visitDefaultFormalParameter_simpleFormalParameter() {
175 ParameterElement parameterElement =
176 buildElementsForText('f([@a gx = null]) {}').functions[0].parameters[0];
177 checkMetadata(parameterElement);
178 }
179
180 void test_metadata_visitEnumDeclaration() {
181 ClassElement classElement =
182 buildElementsForText('@a enum E { v }').enums[0];
183 checkMetadata(classElement);
184 }
185
186 void test_metadata_visitExportDirective() {
187 buildElementsForText('@a export "foo.dart";');
188 expect(compilationUnit.directives[0], new isInstanceOf<ExportDirective>());
189 ExportDirective exportDirective = compilationUnit.directives[0];
190 checkAnnotation(exportDirective.metadata);
191 }
192
193 void test_metadata_visitFieldFormalParameter() {
194 ParameterElement parameterElement =
195 buildElementsForText('class C { var x; C(@a this.x); }')
196 .types[0]
197 .constructors[0]
198 .parameters[0];
199 checkMetadata(parameterElement);
200 }
201
202 void test_metadata_visitFunctionDeclaration_function() {
203 FunctionElement functionElement =
204 buildElementsForText('@a f() {}').functions[0];
205 checkMetadata(functionElement);
206 }
207
208 void test_metadata_visitFunctionDeclaration_getter() {
209 PropertyAccessorElement propertyAccessorElement =
210 buildElementsForText('@a get f => null;').accessors[0];
211 checkMetadata(propertyAccessorElement);
212 }
213
214 void test_metadata_visitFunctionDeclaration_setter() {
215 PropertyAccessorElement propertyAccessorElement =
216 buildElementsForText('@a set f(value) {}').accessors[0];
217 checkMetadata(propertyAccessorElement);
218 }
219
220 void test_metadata_visitFunctionTypeAlias() {
221 FunctionTypeAliasElement functionTypeAliasElement =
222 buildElementsForText('@a typedef F();').typeAliases[0];
223 checkMetadata(functionTypeAliasElement);
224 }
225
226 void test_metadata_visitFunctionTypedFormalParameter() {
227 ParameterElement parameterElement =
228 buildElementsForText('f(@a g()) {}').functions[0].parameters[0];
229 checkMetadata(parameterElement);
230 }
231
232 void test_metadata_visitImportDirective() {
233 buildElementsForText('@a import "foo.dart";');
234 expect(compilationUnit.directives[0], new isInstanceOf<ImportDirective>());
235 ImportDirective importDirective = compilationUnit.directives[0];
236 checkAnnotation(importDirective.metadata);
237 }
238
239 void test_metadata_visitLibraryDirective() {
240 buildElementsForText('@a library L;');
241 expect(compilationUnit.directives[0], new isInstanceOf<LibraryDirective>());
242 LibraryDirective libraryDirective = compilationUnit.directives[0];
243 checkAnnotation(libraryDirective.metadata);
244 }
245
246 void test_metadata_visitMethodDeclaration_getter() {
247 PropertyAccessorElement propertyAccessorElement =
248 buildElementsForText('class C { @a get m => null; }')
249 .types[0]
250 .accessors[0];
251 checkMetadata(propertyAccessorElement);
252 }
253
254 void test_metadata_visitMethodDeclaration_method() {
255 MethodElement methodElement =
256 buildElementsForText('class C { @a m() {} }').types[0].methods[0];
257 checkMetadata(methodElement);
258 }
259
260 void test_metadata_visitMethodDeclaration_setter() {
261 PropertyAccessorElement propertyAccessorElement =
262 buildElementsForText('class C { @a set f(value) {} }')
263 .types[0]
264 .accessors[0];
265 checkMetadata(propertyAccessorElement);
266 }
267
268 void test_metadata_visitPartDirective() {
269 buildElementsForText('@a part "foo.dart";');
270 expect(compilationUnit.directives[0], new isInstanceOf<PartDirective>());
271 PartDirective partDirective = compilationUnit.directives[0];
272 checkAnnotation(partDirective.metadata);
273 }
274
275 void test_metadata_visitPartOfDirective() {
276 // We don't build ElementAnnotation objects for `part of` directives, since
277 // analyzer ignores them in favor of annotations on the library directive.
278 buildElementsForText('@a part of L;');
279 expect(compilationUnit.directives[0], new isInstanceOf<PartOfDirective>());
280 PartOfDirective partOfDirective = compilationUnit.directives[0];
281 expect(partOfDirective.metadata, hasLength(1));
282 expect(partOfDirective.metadata[0].elementAnnotation, isNull);
283 }
284
285 void test_metadata_visitSimpleFormalParameter() {
286 ParameterElement parameterElement =
287 buildElementsForText('f(@a x) {}').functions[0].parameters[0];
288 checkMetadata(parameterElement);
289 }
290
291 void test_metadata_visitTypeParameter() {
292 TypeParameterElement typeParameterElement =
293 buildElementsForText('class C<@a T> {}').types[0].typeParameters[0];
294 checkMetadata(typeParameterElement);
295 }
296
297 void test_visitCatchClause() { 264 void test_visitCatchClause() {
298 List<LocalVariableElement> variables = 265 List<LocalVariableElement> variables =
299 buildElementsForText('f() { try {} catch (e, s) {} }') 266 buildElementsForText('f() { try {} catch (e, s) {} }')
300 .functions[0] 267 .functions[0]
301 .localVariables; 268 .localVariables;
302 String exceptionParameterName = "e"; 269 String exceptionParameterName = "e";
303 String stackParameterName = "s"; 270 String stackParameterName = "s";
304 expect(variables, hasLength(2)); 271 expect(variables, hasLength(2));
305 272
306 LocalVariableElement exceptionVariable = variables[0]; 273 LocalVariableElement exceptionVariable = variables[0];
(...skipping 22 matching lines...) Expand all
329 .functions[0] 296 .functions[0]
330 .localVariables; 297 .localVariables;
331 String exceptionParameterName = "e"; 298 String exceptionParameterName = "e";
332 expect(variables, hasLength(1)); 299 expect(variables, hasLength(1));
333 VariableElement exceptionVariable = variables[0]; 300 VariableElement exceptionVariable = variables[0];
334 expect(exceptionVariable, isNotNull); 301 expect(exceptionVariable, isNotNull);
335 expect(exceptionVariable.name, exceptionParameterName); 302 expect(exceptionVariable.name, exceptionParameterName);
336 expect(exceptionVariable.hasImplicitType, isFalse); 303 expect(exceptionVariable.hasImplicitType, isFalse);
337 } 304 }
338 305
306 void test_visitCompilationUnit_codeRange() {
307 TopLevelVariableDeclaration topLevelVariableDeclaration = AstFactory
308 .topLevelVariableDeclaration(null, AstFactory.typeName4('int'),
309 [AstFactory.variableDeclaration('V')]);
310 CompilationUnit unit = new CompilationUnit(
311 topLevelVariableDeclaration.beginToken,
312 null,
313 [],
314 [topLevelVariableDeclaration],
315 topLevelVariableDeclaration.endToken);
316 ElementHolder holder = new ElementHolder();
317 ElementBuilder builder = _makeBuilder(holder);
318 unit.beginToken.offset = 10;
319 unit.endToken.offset = 40;
320 unit.accept(builder);
321
322 assertHasCodeRange(compilationUnitElement, 0, 41);
323 }
324
325 void test_visitDeclaredIdentifier_noType() {
326 LocalVariableElement variable =
327 buildElementsForText('f() { for (var i in []) {} }')
328 .functions[0]
329 .localVariables[0];
330 assertHasCodeRange(variable, 11, 5);
331 expect(variable, isNotNull);
332 expect(variable.hasImplicitType, isTrue);
333 expect(variable.isConst, isFalse);
334 expect(variable.isDeprecated, isFalse);
335 expect(variable.isFinal, isFalse);
336 expect(variable.isOverride, isFalse);
337 expect(variable.isPrivate, isFalse);
338 expect(variable.isPublic, isTrue);
339 expect(variable.isSynthetic, isFalse);
340 expect(variable.name, 'i');
341 }
342
343 void test_visitDeclaredIdentifier_type() {
344 LocalVariableElement variable =
345 buildElementsForText('f() { for (int i in []) {} }')
346 .functions[0]
347 .localVariables[0];
348 assertHasCodeRange(variable, 11, 5);
349 expect(variable.hasImplicitType, isFalse);
350 expect(variable.isConst, isFalse);
351 expect(variable.isDeprecated, isFalse);
352 expect(variable.isFinal, isFalse);
353 expect(variable.isOverride, isFalse);
354 expect(variable.isPrivate, isFalse);
355 expect(variable.isPublic, isTrue);
356 expect(variable.isSynthetic, isFalse);
357 expect(variable.name, 'i');
358 }
359
360 void test_visitFunctionExpression() {
361 ElementHolder holder = new ElementHolder();
362 ElementBuilder builder = _makeBuilder(holder);
363 FunctionExpression expression = AstFactory.functionExpression2(
364 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2());
365 expression.accept(builder);
366 List<FunctionElement> functions = holder.functions;
367 expect(functions, hasLength(1));
368 FunctionElement function = functions[0];
369 expect(function, isNotNull);
370 expect(expression.element, same(function));
371 expect(function.hasImplicitReturnType, isTrue);
372 expect(function.isSynthetic, isFalse);
373 expect(function.typeParameters, hasLength(0));
374 }
375
376 void test_visitFunctionExpression_inBlockBody() {
377 List<FunctionElement> functions =
378 buildElementsForText('f() { return () => 42; }').functions[0].functions;
379 expect(functions, hasLength(1));
380 FunctionElement function = functions[0];
381 expect(function, isNotNull);
382 expect(function.hasImplicitReturnType, isTrue);
383 expect(function.isSynthetic, isFalse);
384 expect(function.typeParameters, hasLength(0));
385 }
386
387 void test_visitFunctionExpression_inExpressionBody() {
388 List<FunctionElement> functions =
389 buildElementsForText('f() => () => 42;').functions[0].functions;
390 expect(functions, hasLength(1));
391 FunctionElement function = functions[0];
392 expect(function, isNotNull);
393 expect(function.hasImplicitReturnType, isTrue);
394 expect(function.isSynthetic, isFalse);
395 expect(function.typeParameters, hasLength(0));
396 }
397
398 void test_visitFunctionTypeAlias() {
399 ElementHolder holder = new ElementHolder();
400 ElementBuilder builder = _makeBuilder(holder);
401 String aliasName = "F";
402 String parameterName = "E";
403 FunctionTypeAlias aliasNode = AstFactory.typeAlias(
404 null, aliasName, AstFactory.typeParameterList([parameterName]), null);
405 aliasNode.documentationComment = AstFactory.documentationComment(
406 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
407 aliasNode.endToken.offset = 80;
408 aliasNode.accept(builder);
409
410 List<FunctionTypeAliasElement> aliases = holder.typeAliases;
411 expect(aliases, hasLength(1));
412 FunctionTypeAliasElement alias = aliases[0];
413 expect(alias, isNotNull);
414 assertHasCodeRange(alias, 50, 31);
415 expect(alias.documentationComment, '/// aaa');
416 expect(alias.name, aliasName);
417 expect(alias.parameters, hasLength(0));
418 List<TypeParameterElement> typeParameters = alias.typeParameters;
419 expect(typeParameters, hasLength(1));
420 TypeParameterElement typeParameter = typeParameters[0];
421 expect(typeParameter, isNotNull);
422 expect(typeParameter.name, parameterName);
423 }
424
425 void test_visitFunctionTypedFormalParameter() {
426 ElementHolder holder = new ElementHolder();
427 ElementBuilder builder = _makeBuilder(holder);
428 String parameterName = "p";
429 FunctionTypedFormalParameter formalParameter =
430 AstFactory.functionTypedFormalParameter(null, parameterName);
431 _useParameterInMethod(formalParameter, 100, 110);
432 formalParameter.accept(builder);
433 List<ParameterElement> parameters = holder.parameters;
434 expect(parameters, hasLength(1));
435 ParameterElement parameter = parameters[0];
436 expect(parameter, isNotNull);
437 expect(parameter.name, parameterName);
438 expect(parameter.initializer, isNull);
439 expect(parameter.isConst, isFalse);
440 expect(parameter.isFinal, isFalse);
441 expect(parameter.isSynthetic, isFalse);
442 expect(parameter.parameterKind, ParameterKind.REQUIRED);
443 _assertVisibleRange(parameter, 100, 110);
444 }
445
446 void test_visitFunctionTypedFormalParameter_withTypeParameters() {
447 ElementHolder holder = new ElementHolder();
448 ElementBuilder builder = _makeBuilder(holder);
449 String parameterName = "p";
450 FunctionTypedFormalParameter formalParameter =
451 AstFactory.functionTypedFormalParameter(null, parameterName);
452 formalParameter.typeParameters = AstFactory.typeParameterList(['F']);
453 _useParameterInMethod(formalParameter, 100, 110);
454 formalParameter.accept(builder);
455 List<ParameterElement> parameters = holder.parameters;
456 expect(parameters, hasLength(1));
457 ParameterElement parameter = parameters[0];
458 expect(parameter, isNotNull);
459 expect(parameter.name, parameterName);
460 expect(parameter.initializer, isNull);
461 expect(parameter.isConst, isFalse);
462 expect(parameter.isFinal, isFalse);
463 expect(parameter.isSynthetic, isFalse);
464 expect(parameter.parameterKind, ParameterKind.REQUIRED);
465 expect(parameter.typeParameters, hasLength(1));
466 _assertVisibleRange(parameter, 100, 110);
467 }
468
469 void test_visitLabeledStatement() {
470 List<LabelElement> labels =
471 buildElementsForText('f() { l: print(42); }').functions[0].labels;
472 expect(labels, hasLength(1));
473 LabelElement label = labels[0];
474 expect(label, isNotNull);
475 expect(label.name, 'l');
476 expect(label.isSynthetic, isFalse);
477 }
478
479 void test_visitMethodDeclaration_withMembers() {
480 MethodElement method = buildElementsForText(
481 'class C { m(p) { var v; try { l: return; } catch (e) {} } }')
482 .types[0]
483 .methods[0];
484 String methodName = "m";
485 String parameterName = "p";
486 String localVariableName = "v";
487 String labelName = "l";
488 String exceptionParameterName = "e";
489 expect(method, isNotNull);
490 expect(method.hasImplicitReturnType, isTrue);
491 expect(method.name, methodName);
492 expect(method.typeParameters, hasLength(0));
493 expect(method.isAbstract, isFalse);
494 expect(method.isExternal, isFalse);
495 expect(method.isStatic, isFalse);
496 expect(method.isSynthetic, isFalse);
497 List<VariableElement> parameters = method.parameters;
498 expect(parameters, hasLength(1));
499 VariableElement parameter = parameters[0];
500 expect(parameter, isNotNull);
501 expect(parameter.name, parameterName);
502 List<VariableElement> localVariables = method.localVariables;
503 expect(localVariables, hasLength(2));
504 VariableElement firstVariable = localVariables[0];
505 VariableElement secondVariable = localVariables[1];
506 expect(firstVariable, isNotNull);
507 expect(secondVariable, isNotNull);
508 expect(
509 (firstVariable.name == localVariableName &&
510 secondVariable.name == exceptionParameterName) ||
511 (firstVariable.name == exceptionParameterName &&
512 secondVariable.name == localVariableName),
513 isTrue);
514 List<LabelElement> labels = method.labels;
515 expect(labels, hasLength(1));
516 LabelElement label = labels[0];
517 expect(label, isNotNull);
518 expect(label.name, labelName);
519 }
520
521 void test_visitNamedFormalParameter() {
522 ElementHolder holder = new ElementHolder();
523 ElementBuilder builder = _makeBuilder(holder);
524 String parameterName = "p";
525 DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter(
526 AstFactory.simpleFormalParameter3(parameterName),
527 AstFactory.identifier3("42"));
528 _useParameterInMethod(formalParameter, 100, 110);
529 formalParameter.beginToken.offset = 50;
530 formalParameter.endToken.offset = 80;
531 formalParameter.accept(builder);
532 List<ParameterElement> parameters = holder.parameters;
533 expect(parameters, hasLength(1));
534 ParameterElement parameter = parameters[0];
535 expect(parameter, isNotNull);
536 assertHasCodeRange(parameter, 50, 32);
537 expect(parameter.name, parameterName);
538 expect(parameter.isConst, isFalse);
539 expect(parameter.isFinal, isFalse);
540 expect(parameter.isSynthetic, isFalse);
541 expect(parameter.parameterKind, ParameterKind.NAMED);
542 _assertVisibleRange(parameter, 100, 110);
543 expect(parameter.defaultValueCode, "42");
544 FunctionElement initializer = parameter.initializer;
545 expect(initializer, isNotNull);
546 expect(initializer.isSynthetic, isTrue);
547 expect(initializer.hasImplicitReturnType, isTrue);
548 }
549
550 void test_visitSimpleFormalParameter_noType() {
551 // p
552 ElementHolder holder = new ElementHolder();
553 ElementBuilder builder = _makeBuilder(holder);
554 String parameterName = "p";
555 SimpleFormalParameter formalParameter =
556 AstFactory.simpleFormalParameter3(parameterName);
557 _useParameterInMethod(formalParameter, 100, 110);
558 formalParameter.accept(builder);
559 List<ParameterElement> parameters = holder.parameters;
560 expect(parameters, hasLength(1));
561 ParameterElement parameter = parameters[0];
562 expect(parameter, isNotNull);
563 expect(parameter.hasImplicitType, isTrue);
564 expect(parameter.initializer, isNull);
565 expect(parameter.isConst, isFalse);
566 expect(parameter.isFinal, isFalse);
567 expect(parameter.isSynthetic, isFalse);
568 expect(parameter.name, parameterName);
569 expect(parameter.parameterKind, ParameterKind.REQUIRED);
570 _assertVisibleRange(parameter, 100, 110);
571 }
572
573 void test_visitSimpleFormalParameter_type() {
574 // T p
575 ElementHolder holder = new ElementHolder();
576 ElementBuilder builder = _makeBuilder(holder);
577 String parameterName = "p";
578 SimpleFormalParameter formalParameter = AstFactory.simpleFormalParameter4(
579 AstFactory.typeName4('T'), parameterName);
580 _useParameterInMethod(formalParameter, 100, 110);
581 formalParameter.accept(builder);
582 List<ParameterElement> parameters = holder.parameters;
583 expect(parameters, hasLength(1));
584 ParameterElement parameter = parameters[0];
585 expect(parameter, isNotNull);
586 expect(parameter.hasImplicitType, isFalse);
587 expect(parameter.initializer, isNull);
588 expect(parameter.isConst, isFalse);
589 expect(parameter.isFinal, isFalse);
590 expect(parameter.isSynthetic, isFalse);
591 expect(parameter.name, parameterName);
592 expect(parameter.parameterKind, ParameterKind.REQUIRED);
593 _assertVisibleRange(parameter, 100, 110);
594 }
595
596 void test_visitVariableDeclaration_inConstructor() {
597 List<ConstructorElement> constructors =
598 buildElementsForText('class C { C() { var v = 1; } }')
599 .types[0]
600 .constructors;
601 expect(constructors, hasLength(1));
602 List<LocalVariableElement> variableElements =
603 constructors[0].localVariables;
604 expect(variableElements, hasLength(1));
605 LocalVariableElement variableElement = variableElements[0];
606 assertHasCodeRange(variableElement, 16, 10);
607 expect(variableElement.hasImplicitType, isTrue);
608 expect(variableElement.name, 'v');
609 _assertVisibleRange(variableElement, 14, 28);
610 }
611
612 void test_visitVariableDeclaration_inForEachStatement() {
613 ElementHolder holder = new ElementHolder();
614 ElementBuilder builder = _makeBuilder(holder);
615 //
616 // m() { for (var v in []) }
617 //
618 String variableName = "v";
619 Statement statement = AstFactory.forEachStatement(
620 AstFactory.declaredIdentifier3('v'),
621 AstFactory.listLiteral(),
622 AstFactory.block());
623 _setNodeSourceRange(statement, 100, 110);
624 MethodDeclaration method = AstFactory.methodDeclaration2(
625 null,
626 null,
627 null,
628 null,
629 AstFactory.identifier3("m"),
630 AstFactory.formalParameterList(),
631 AstFactory.blockFunctionBody2([statement]));
632 _setBlockBodySourceRange(method.body, 200, 220);
633 method.accept(builder);
634
635 List<MethodElement> methods = holder.methods;
636 expect(methods, hasLength(1));
637 List<LocalVariableElement> variableElements = methods[0].localVariables;
638 expect(variableElements, hasLength(1));
639 LocalVariableElement variableElement = variableElements[0];
640 expect(variableElement.name, variableName);
641 _assertVisibleRange(variableElement, 100, 110);
642 }
643
644 void test_visitVariableDeclaration_inForStatement() {
645 ElementHolder holder = new ElementHolder();
646 ElementBuilder builder = _makeBuilder(holder);
647 //
648 // m() { for (T v;;) }
649 //
650 String variableName = "v";
651 ForStatement statement = AstFactory.forStatement2(
652 AstFactory.variableDeclarationList(null, AstFactory.typeName4('T'),
653 [AstFactory.variableDeclaration('v')]),
654 null,
655 null,
656 AstFactory.block());
657 _setNodeSourceRange(statement, 100, 110);
658 MethodDeclaration method = AstFactory.methodDeclaration2(
659 null,
660 null,
661 null,
662 null,
663 AstFactory.identifier3("m"),
664 AstFactory.formalParameterList(),
665 AstFactory.blockFunctionBody2([statement]));
666 _setBlockBodySourceRange(method.body, 200, 220);
667 method.accept(builder);
668
669 List<MethodElement> methods = holder.methods;
670 expect(methods, hasLength(1));
671 List<LocalVariableElement> variableElements = methods[0].localVariables;
672 expect(variableElements, hasLength(1));
673 LocalVariableElement variableElement = variableElements[0];
674 expect(variableElement.name, variableName);
675 _assertVisibleRange(variableElement, 100, 110);
676 }
677
678 void test_visitVariableDeclaration_inMethod() {
679 ElementHolder holder = new ElementHolder();
680 ElementBuilder builder = _makeBuilder(holder);
681 //
682 // m() {T v;}
683 //
684 String variableName = "v";
685 VariableDeclaration variable =
686 AstFactory.variableDeclaration2(variableName, null);
687 Statement statement = AstFactory.variableDeclarationStatement(
688 null, AstFactory.typeName4('T'), [variable]);
689 MethodDeclaration method = AstFactory.methodDeclaration2(
690 null,
691 null,
692 null,
693 null,
694 AstFactory.identifier3("m"),
695 AstFactory.formalParameterList(),
696 AstFactory.blockFunctionBody2([statement]));
697 _setBlockBodySourceRange(method.body, 100, 110);
698 method.accept(builder);
699
700 List<MethodElement> methods = holder.methods;
701 expect(methods, hasLength(1));
702 List<LocalVariableElement> variableElements = methods[0].localVariables;
703 expect(variableElements, hasLength(1));
704 LocalVariableElement variableElement = variableElements[0];
705 expect(variableElement.hasImplicitType, isFalse);
706 expect(variableElement.name, variableName);
707 _assertVisibleRange(variableElement, 100, 110);
708 }
709
710 void test_visitVariableDeclaration_localNestedInFunction() {
711 ElementHolder holder = new ElementHolder();
712 ElementBuilder builder = _makeBuilder(holder);
713 //
714 // var f = () {var v;};
715 //
716 String variableName = "v";
717 VariableDeclaration variable =
718 AstFactory.variableDeclaration2(variableName, null);
719 Statement statement =
720 AstFactory.variableDeclarationStatement2(null, [variable]);
721 Expression initializer = AstFactory.functionExpression2(
722 AstFactory.formalParameterList(),
723 AstFactory.blockFunctionBody2([statement]));
724 String fieldName = "f";
725 VariableDeclaration field =
726 AstFactory.variableDeclaration2(fieldName, initializer);
727 FieldDeclaration fieldDeclaration =
728 AstFactory.fieldDeclaration2(false, null, [field]);
729 fieldDeclaration.accept(builder);
730
731 List<FieldElement> variables = holder.fields;
732 expect(variables, hasLength(1));
733 FieldElement fieldElement = variables[0];
734 expect(fieldElement, isNotNull);
735 FunctionElement initializerElement = fieldElement.initializer;
736 expect(initializerElement, isNotNull);
737 expect(initializerElement.hasImplicitReturnType, isTrue);
738 List<FunctionElement> functionElements = initializerElement.functions;
739 expect(functionElements, hasLength(1));
740 List<LocalVariableElement> variableElements =
741 functionElements[0].localVariables;
742 expect(variableElements, hasLength(1));
743 LocalVariableElement variableElement = variableElements[0];
744 expect(variableElement.hasImplicitType, isTrue);
745 expect(variableElement.isConst, isFalse);
746 expect(variableElement.isFinal, isFalse);
747 expect(variableElement.isSynthetic, isFalse);
748 expect(variableElement.name, variableName);
749 }
750
751 void test_visitVariableDeclaration_noInitializer() {
752 // var v;
753 ElementHolder holder = new ElementHolder();
754 ElementBuilder builder = _makeBuilder(holder);
755 String variableName = "v";
756 VariableDeclaration variableDeclaration =
757 AstFactory.variableDeclaration2(variableName, null);
758 AstFactory.variableDeclarationList2(null, [variableDeclaration]);
759 variableDeclaration.accept(builder);
760
761 List<TopLevelVariableElement> variables = holder.topLevelVariables;
762 expect(variables, hasLength(1));
763 TopLevelVariableElement variable = variables[0];
764 expect(variable, isNotNull);
765 expect(variable.hasImplicitType, isTrue);
766 expect(variable.initializer, isNull);
767 expect(variable.name, variableName);
768 expect(variable.isConst, isFalse);
769 expect(variable.isFinal, isFalse);
770 expect(variable.isSynthetic, isFalse);
771 expect(variable.getter, isNotNull);
772 expect(variable.setter, isNotNull);
773 }
774
775 void test_visitVariableDeclaration_top() {
776 // final a, b;
777 ElementHolder holder = new ElementHolder();
778 ElementBuilder builder = _makeBuilder(holder);
779 VariableDeclaration variableDeclaration1 =
780 AstFactory.variableDeclaration('a');
781 VariableDeclaration variableDeclaration2 =
782 AstFactory.variableDeclaration('b');
783 TopLevelVariableDeclaration topLevelVariableDeclaration = AstFactory
784 .topLevelVariableDeclaration(
785 Keyword.FINAL, null, [variableDeclaration1, variableDeclaration2]);
786 topLevelVariableDeclaration.documentationComment = AstFactory
787 .documentationComment(
788 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
789
790 topLevelVariableDeclaration.accept(builder);
791 List<TopLevelVariableElement> variables = holder.topLevelVariables;
792 expect(variables, hasLength(2));
793
794 TopLevelVariableElement variable1 = variables[0];
795 expect(variable1, isNotNull);
796 expect(variable1.documentationComment, '/// aaa');
797
798 TopLevelVariableElement variable2 = variables[1];
799 expect(variable2, isNotNull);
800 expect(variable2.documentationComment, '/// aaa');
801 }
802
803 void test_visitVariableDeclaration_top_const_hasInitializer() {
804 // const v = 42;
805 ElementHolder holder = new ElementHolder();
806 ElementBuilder builder = _makeBuilder(holder);
807 String variableName = "v";
808 VariableDeclaration variableDeclaration =
809 AstFactory.variableDeclaration2(variableName, AstFactory.integer(42));
810 AstFactory.variableDeclarationList2(Keyword.CONST, [variableDeclaration]);
811 variableDeclaration.accept(builder);
812
813 List<TopLevelVariableElement> variables = holder.topLevelVariables;
814 expect(variables, hasLength(1));
815 TopLevelVariableElement variable = variables[0];
816 expect(variable, new isInstanceOf<ConstTopLevelVariableElementImpl>());
817 expect(variable.initializer, isNotNull);
818 expect(variable.initializer.type, isNotNull);
819 expect(variable.initializer.hasImplicitReturnType, isTrue);
820 expect(variable.name, variableName);
821 expect(variable.hasImplicitType, isTrue);
822 expect(variable.isConst, isTrue);
823 expect(variable.isFinal, isFalse);
824 expect(variable.isSynthetic, isFalse);
825 expect(variable.getter, isNotNull);
826 expect(variable.setter, isNull);
827 }
828
829 void test_visitVariableDeclaration_top_final() {
830 // final v;
831 ElementHolder holder = new ElementHolder();
832 ElementBuilder builder = _makeBuilder(holder);
833 String variableName = "v";
834 VariableDeclaration variableDeclaration =
835 AstFactory.variableDeclaration2(variableName, null);
836 AstFactory.variableDeclarationList2(Keyword.FINAL, [variableDeclaration]);
837 variableDeclaration.accept(builder);
838 List<TopLevelVariableElement> variables = holder.topLevelVariables;
839 expect(variables, hasLength(1));
840 TopLevelVariableElement variable = variables[0];
841 expect(variable, isNotNull);
842 expect(variable.hasImplicitType, isTrue);
843 expect(variable.initializer, isNull);
844 expect(variable.name, variableName);
845 expect(variable.isConst, isFalse);
846 expect(variable.isFinal, isTrue);
847 expect(variable.isSynthetic, isFalse);
848 expect(variable.getter, isNotNull);
849 expect(variable.setter, isNull);
850 }
851
852 ElementBuilder _makeBuilder(ElementHolder holder) =>
853 new ElementBuilder(holder, compilationUnitElement);
854
855 void _setBlockBodySourceRange(BlockFunctionBody body, int offset, int end) {
856 _setNodeSourceRange(body.block, offset, end);
857 }
858
859 void _setNodeSourceRange(AstNode node, int offset, int end) {
860 node.beginToken.offset = offset;
861 Token endToken = node.endToken;
862 endToken.offset = end - endToken.length;
863 }
864
865 void _useParameterInMethod(
866 FormalParameter formalParameter, int blockOffset, int blockEnd) {
867 Block block = AstFactory.block();
868 block.leftBracket.offset = blockOffset;
869 block.rightBracket.offset = blockEnd - 1;
870 BlockFunctionBody body = AstFactory.blockFunctionBody(block);
871 AstFactory.methodDeclaration2(
872 null,
873 null,
874 null,
875 null,
876 AstFactory.identifier3("main"),
877 AstFactory.formalParameterList([formalParameter]),
878 body);
879 }
880 }
881
882 /**
883 * Mixin with test methods for testing element building in [ApiElementBuilder].
884 * It is used to test the [ApiElementBuilder] itself, and its usage by
885 * [ElementBuilder].
886 */
887 abstract class _ApiElementBuilderTestMixin {
888 CompilationUnit get compilationUnit;
889
890 void assertHasCodeRange(Element element, int offset, int length);
891
892 /**
893 * Build elements using [ApiElementBuilder].
894 */
895 ElementHolder buildElementsForAst(AstNode node);
896
897 /**
898 * Parse the given [code], and build elements using [ApiElementBuilder].
899 */
900 ElementHolder buildElementsForText(String code);
901
902 /**
903 * Verify that the given [metadata] has exactly one annotation, and that its
904 * [ElementAnnotationImpl] is unresolved.
905 */
906 void checkAnnotation(NodeList<Annotation> metadata);
907
908 /**
909 * Verify that the given [element] has exactly one annotation, and that its
910 * [ElementAnnotationImpl] is unresolved.
911 */
912 void checkMetadata(Element element);
913
914 void test_metadata_fieldDeclaration() {
915 List<FieldElement> fields =
916 buildElementsForText('class C { @a int x, y; }').types[0].fields;
917 checkMetadata(fields[0]);
918 checkMetadata(fields[1]);
919 expect(fields[0].metadata, same(fields[1].metadata));
920 }
921
922 void test_metadata_topLevelVariableDeclaration() {
923 List<TopLevelVariableElement> topLevelVariables =
924 buildElementsForText('@a int x, y;').topLevelVariables;
925 checkMetadata(topLevelVariables[0]);
926 checkMetadata(topLevelVariables[1]);
927 expect(topLevelVariables[0].metadata, same(topLevelVariables[1].metadata));
928 }
929
930 void test_metadata_visitClassDeclaration() {
931 ClassElement classElement = buildElementsForText('@a class C {}').types[0];
932 checkMetadata(classElement);
933 }
934
935 void test_metadata_visitClassTypeAlias() {
936 ClassElement classElement =
937 buildElementsForText('@a class C = D with E;').types[0];
938 checkMetadata(classElement);
939 }
940
941 void test_metadata_visitConstructorDeclaration() {
942 ConstructorElement constructorElement =
943 buildElementsForText('class C { @a C(); }').types[0].constructors[0];
944 checkMetadata(constructorElement);
945 }
946
947 void test_metadata_visitDefaultFormalParameter_fieldFormalParameter() {
948 ParameterElement parameterElement =
949 buildElementsForText('class C { var x; C([@a this.x = null]); }')
950 .types[0]
951 .constructors[0]
952 .parameters[0];
953 checkMetadata(parameterElement);
954 }
955
956 void
957 test_metadata_visitDefaultFormalParameter_functionTypedFormalParameter() {
958 ParameterElement parameterElement =
959 buildElementsForText('f([@a g() = null]) {}').functions[0].parameters[
960 0];
961 checkMetadata(parameterElement);
962 }
963
964 void test_metadata_visitDefaultFormalParameter_simpleFormalParameter() {
965 ParameterElement parameterElement =
966 buildElementsForText('f([@a gx = null]) {}').functions[0].parameters[0];
967 checkMetadata(parameterElement);
968 }
969
970 void test_metadata_visitEnumDeclaration() {
971 ClassElement classElement =
972 buildElementsForText('@a enum E { v }').enums[0];
973 checkMetadata(classElement);
974 }
975
976 void test_metadata_visitExportDirective() {
977 buildElementsForText('@a export "foo.dart";');
978 expect(compilationUnit.directives[0], new isInstanceOf<ExportDirective>());
979 ExportDirective exportDirective = compilationUnit.directives[0];
980 checkAnnotation(exportDirective.metadata);
981 }
982
983 void test_metadata_visitFieldFormalParameter() {
984 ParameterElement parameterElement =
985 buildElementsForText('class C { var x; C(@a this.x); }')
986 .types[0]
987 .constructors[0]
988 .parameters[0];
989 checkMetadata(parameterElement);
990 }
991
992 void test_metadata_visitFunctionDeclaration_function() {
993 FunctionElement functionElement =
994 buildElementsForText('@a f() {}').functions[0];
995 checkMetadata(functionElement);
996 }
997
998 void test_metadata_visitFunctionDeclaration_getter() {
999 PropertyAccessorElement propertyAccessorElement =
1000 buildElementsForText('@a get f => null;').accessors[0];
1001 checkMetadata(propertyAccessorElement);
1002 }
1003
1004 void test_metadata_visitFunctionDeclaration_setter() {
1005 PropertyAccessorElement propertyAccessorElement =
1006 buildElementsForText('@a set f(value) {}').accessors[0];
1007 checkMetadata(propertyAccessorElement);
1008 }
1009
1010 void test_metadata_visitFunctionTypeAlias() {
1011 FunctionTypeAliasElement functionTypeAliasElement =
1012 buildElementsForText('@a typedef F();').typeAliases[0];
1013 checkMetadata(functionTypeAliasElement);
1014 }
1015
1016 void test_metadata_visitFunctionTypedFormalParameter() {
1017 ParameterElement parameterElement =
1018 buildElementsForText('f(@a g()) {}').functions[0].parameters[0];
1019 checkMetadata(parameterElement);
1020 }
1021
1022 void test_metadata_visitImportDirective() {
1023 buildElementsForText('@a import "foo.dart";');
1024 expect(compilationUnit.directives[0], new isInstanceOf<ImportDirective>());
1025 ImportDirective importDirective = compilationUnit.directives[0];
1026 checkAnnotation(importDirective.metadata);
1027 }
1028
1029 void test_metadata_visitLibraryDirective() {
1030 buildElementsForText('@a library L;');
1031 expect(compilationUnit.directives[0], new isInstanceOf<LibraryDirective>());
1032 LibraryDirective libraryDirective = compilationUnit.directives[0];
1033 checkAnnotation(libraryDirective.metadata);
1034 }
1035
1036 void test_metadata_visitMethodDeclaration_getter() {
1037 PropertyAccessorElement propertyAccessorElement =
1038 buildElementsForText('class C { @a get m => null; }')
1039 .types[0]
1040 .accessors[0];
1041 checkMetadata(propertyAccessorElement);
1042 }
1043
1044 void test_metadata_visitMethodDeclaration_method() {
1045 MethodElement methodElement =
1046 buildElementsForText('class C { @a m() {} }').types[0].methods[0];
1047 checkMetadata(methodElement);
1048 }
1049
1050 void test_metadata_visitMethodDeclaration_setter() {
1051 PropertyAccessorElement propertyAccessorElement =
1052 buildElementsForText('class C { @a set f(value) {} }')
1053 .types[0]
1054 .accessors[0];
1055 checkMetadata(propertyAccessorElement);
1056 }
1057
1058 void test_metadata_visitPartDirective() {
1059 buildElementsForText('@a part "foo.dart";');
1060 expect(compilationUnit.directives[0], new isInstanceOf<PartDirective>());
1061 PartDirective partDirective = compilationUnit.directives[0];
1062 checkAnnotation(partDirective.metadata);
1063 }
1064
1065 void test_metadata_visitPartOfDirective() {
1066 // We don't build ElementAnnotation objects for `part of` directives, since
1067 // analyzer ignores them in favor of annotations on the library directive.
1068 buildElementsForText('@a part of L;');
1069 expect(compilationUnit.directives[0], new isInstanceOf<PartOfDirective>());
1070 PartOfDirective partOfDirective = compilationUnit.directives[0];
1071 expect(partOfDirective.metadata, hasLength(1));
1072 expect(partOfDirective.metadata[0].elementAnnotation, isNull);
1073 }
1074
1075 void test_metadata_visitSimpleFormalParameter() {
1076 ParameterElement parameterElement =
1077 buildElementsForText('f(@a x) {}').functions[0].parameters[0];
1078 checkMetadata(parameterElement);
1079 }
1080
1081 void test_metadata_visitTypeParameter() {
1082 TypeParameterElement typeParameterElement =
1083 buildElementsForText('class C<@a T> {}').types[0].typeParameters[0];
1084 checkMetadata(typeParameterElement);
1085 }
1086
339 void test_visitClassDeclaration_abstract() { 1087 void test_visitClassDeclaration_abstract() {
340 ElementHolder holder = new ElementHolder(); 1088 List<ClassElement> types =
341 ElementBuilder builder = _makeBuilder(holder); 1089 buildElementsForText('abstract class C {}').types;
342 String className = "C";
343 ClassDeclaration classDeclaration = AstFactory.classDeclaration(
344 Keyword.ABSTRACT, className, null, null, null, null);
345 classDeclaration.accept(builder);
346 List<ClassElement> types = holder.types;
347 expect(types, hasLength(1)); 1090 expect(types, hasLength(1));
348 ClassElement type = types[0]; 1091 ClassElement type = types[0];
349 expect(type, isNotNull); 1092 expect(type, isNotNull);
350 expect(type.name, className); 1093 expect(type.name, 'C');
351 List<TypeParameterElement> typeParameters = type.typeParameters; 1094 List<TypeParameterElement> typeParameters = type.typeParameters;
352 expect(typeParameters, hasLength(0)); 1095 expect(typeParameters, hasLength(0));
353 expect(type.isAbstract, isTrue); 1096 expect(type.isAbstract, isTrue);
354 expect(type.isMixinApplication, isFalse); 1097 expect(type.isMixinApplication, isFalse);
355 expect(type.isSynthetic, isFalse); 1098 expect(type.isSynthetic, isFalse);
356 } 1099 }
357 1100
358 void test_visitClassDeclaration_invalidFunctionInAnnotation_class() { 1101 void test_visitClassDeclaration_invalidFunctionInAnnotation_class() {
359 // https://github.com/dart-lang/sdk/issues/25696 1102 // https://github.com/dart-lang/sdk/issues/25696
360 String code = r''' 1103 String code = r'''
(...skipping 20 matching lines...) Expand all
381 '''; 1124 ''';
382 ElementHolder holder = buildElementsForText(code); 1125 ElementHolder holder = buildElementsForText(code);
383 ClassElement elementC = holder.types[1]; 1126 ClassElement elementC = holder.types[1];
384 expect(elementC, isNotNull); 1127 expect(elementC, isNotNull);
385 MethodElement methodM = elementC.methods[0]; 1128 MethodElement methodM = elementC.methods[0];
386 expect(methodM, isNotNull); 1129 expect(methodM, isNotNull);
387 expect(methodM.functions, isEmpty); 1130 expect(methodM.functions, isEmpty);
388 } 1131 }
389 1132
390 void test_visitClassDeclaration_minimal() { 1133 void test_visitClassDeclaration_minimal() {
391 ElementHolder holder = new ElementHolder();
392 ElementBuilder builder = _makeBuilder(holder);
393 String className = "C"; 1134 String className = "C";
394 ClassDeclaration classDeclaration = 1135 ClassDeclaration classDeclaration =
395 AstFactory.classDeclaration(null, className, null, null, null, null); 1136 AstFactory.classDeclaration(null, className, null, null, null, null);
396 classDeclaration.documentationComment = AstFactory.documentationComment( 1137 classDeclaration.documentationComment = AstFactory.documentationComment(
397 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1138 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
398 classDeclaration.endToken.offset = 80; 1139 classDeclaration.endToken.offset = 80;
399 classDeclaration.accept(builder); 1140
1141 ElementHolder holder = buildElementsForAst(classDeclaration);
400 List<ClassElement> types = holder.types; 1142 List<ClassElement> types = holder.types;
401 expect(types, hasLength(1)); 1143 expect(types, hasLength(1));
402 ClassElement type = types[0]; 1144 ClassElement type = types[0];
403 expect(type, isNotNull); 1145 expect(type, isNotNull);
404 expect(type.name, className); 1146 expect(type.name, className);
405 List<TypeParameterElement> typeParameters = type.typeParameters; 1147 List<TypeParameterElement> typeParameters = type.typeParameters;
406 expect(typeParameters, hasLength(0)); 1148 expect(typeParameters, hasLength(0));
407 expect(type.isAbstract, isFalse); 1149 expect(type.isAbstract, isFalse);
408 expect(type.isMixinApplication, isFalse); 1150 expect(type.isMixinApplication, isFalse);
409 expect(type.isSynthetic, isFalse); 1151 expect(type.isSynthetic, isFalse);
410 expect(type.documentationComment, '/// aaa'); 1152 expect(type.documentationComment, '/// aaa');
411 _assertHasCodeRange(type, 50, 31); 1153 assertHasCodeRange(type, 50, 31);
412 } 1154 }
413 1155
414 void test_visitClassDeclaration_parameterized() { 1156 void test_visitClassDeclaration_parameterized() {
415 ElementHolder holder = new ElementHolder();
416 ElementBuilder builder = _makeBuilder(holder);
417 String className = "C"; 1157 String className = "C";
418 String firstVariableName = "E"; 1158 String firstVariableName = "E";
419 String secondVariableName = "F"; 1159 String secondVariableName = "F";
420 ClassDeclaration classDeclaration = AstFactory.classDeclaration( 1160 ClassDeclaration classDeclaration = AstFactory.classDeclaration(
421 null, 1161 null,
422 className, 1162 className,
423 AstFactory.typeParameterList([firstVariableName, secondVariableName]), 1163 AstFactory.typeParameterList([firstVariableName, secondVariableName]),
424 null, 1164 null,
425 null, 1165 null,
426 null); 1166 null);
427 classDeclaration.accept(builder); 1167
1168 ElementHolder holder = buildElementsForAst(classDeclaration);
428 List<ClassElement> types = holder.types; 1169 List<ClassElement> types = holder.types;
429 expect(types, hasLength(1)); 1170 expect(types, hasLength(1));
430 ClassElement type = types[0]; 1171 ClassElement type = types[0];
431 expect(type, isNotNull); 1172 expect(type, isNotNull);
432 expect(type.name, className); 1173 expect(type.name, className);
433 List<TypeParameterElement> typeParameters = type.typeParameters; 1174 List<TypeParameterElement> typeParameters = type.typeParameters;
434 expect(typeParameters, hasLength(2)); 1175 expect(typeParameters, hasLength(2));
435 expect(typeParameters[0].name, firstVariableName); 1176 expect(typeParameters[0].name, firstVariableName);
436 expect(typeParameters[1].name, secondVariableName); 1177 expect(typeParameters[1].name, secondVariableName);
437 expect(type.isAbstract, isFalse); 1178 expect(type.isAbstract, isFalse);
438 expect(type.isMixinApplication, isFalse); 1179 expect(type.isMixinApplication, isFalse);
439 expect(type.isSynthetic, isFalse); 1180 expect(type.isSynthetic, isFalse);
440 } 1181 }
441 1182
442 void test_visitClassDeclaration_withMembers() { 1183 void test_visitClassDeclaration_withMembers() {
443 ElementHolder holder = new ElementHolder();
444 ElementBuilder builder = _makeBuilder(holder);
445 String className = "C"; 1184 String className = "C";
446 String typeParameterName = "E"; 1185 String typeParameterName = "E";
447 String fieldName = "f"; 1186 String fieldName = "f";
448 String methodName = "m"; 1187 String methodName = "m";
449 ClassDeclaration classDeclaration = AstFactory.classDeclaration( 1188 ClassDeclaration classDeclaration = AstFactory.classDeclaration(
450 null, 1189 null,
451 className, 1190 className,
452 AstFactory.typeParameterList([typeParameterName]), 1191 AstFactory.typeParameterList([typeParameterName]),
453 null, 1192 null,
454 null, 1193 null,
455 null, [ 1194 null, [
456 AstFactory.fieldDeclaration2( 1195 AstFactory.fieldDeclaration2(
457 false, null, [AstFactory.variableDeclaration(fieldName)]), 1196 false, null, [AstFactory.variableDeclaration(fieldName)]),
458 AstFactory.methodDeclaration2( 1197 AstFactory.methodDeclaration2(
459 null, 1198 null,
460 null, 1199 null,
461 null, 1200 null,
462 null, 1201 null,
463 AstFactory.identifier3(methodName), 1202 AstFactory.identifier3(methodName),
464 AstFactory.formalParameterList(), 1203 AstFactory.formalParameterList(),
465 AstFactory.blockFunctionBody2()) 1204 AstFactory.blockFunctionBody2())
466 ]); 1205 ]);
467 classDeclaration.accept(builder); 1206
1207 ElementHolder holder = buildElementsForAst(classDeclaration);
468 List<ClassElement> types = holder.types; 1208 List<ClassElement> types = holder.types;
469 expect(types, hasLength(1)); 1209 expect(types, hasLength(1));
470 ClassElement type = types[0]; 1210 ClassElement type = types[0];
471 expect(type, isNotNull); 1211 expect(type, isNotNull);
472 expect(type.name, className); 1212 expect(type.name, className);
473 expect(type.isAbstract, isFalse); 1213 expect(type.isAbstract, isFalse);
474 expect(type.isMixinApplication, isFalse); 1214 expect(type.isMixinApplication, isFalse);
475 expect(type.isSynthetic, isFalse); 1215 expect(type.isSynthetic, isFalse);
476 List<TypeParameterElement> typeParameters = type.typeParameters; 1216 List<TypeParameterElement> typeParameters = type.typeParameters;
477 expect(typeParameters, hasLength(1)); 1217 expect(typeParameters, hasLength(1));
478 TypeParameterElement typeParameter = typeParameters[0]; 1218 TypeParameterElement typeParameter = typeParameters[0];
479 expect(typeParameter, isNotNull); 1219 expect(typeParameter, isNotNull);
480 expect(typeParameter.name, typeParameterName); 1220 expect(typeParameter.name, typeParameterName);
481 List<FieldElement> fields = type.fields; 1221 List<FieldElement> fields = type.fields;
482 expect(fields, hasLength(1)); 1222 expect(fields, hasLength(1));
483 FieldElement field = fields[0]; 1223 FieldElement field = fields[0];
484 expect(field, isNotNull); 1224 expect(field, isNotNull);
485 expect(field.name, fieldName); 1225 expect(field.name, fieldName);
486 List<MethodElement> methods = type.methods; 1226 List<MethodElement> methods = type.methods;
487 expect(methods, hasLength(1)); 1227 expect(methods, hasLength(1));
488 MethodElement method = methods[0]; 1228 MethodElement method = methods[0];
489 expect(method, isNotNull); 1229 expect(method, isNotNull);
490 expect(method.name, methodName); 1230 expect(method.name, methodName);
491 } 1231 }
492 1232
493 void test_visitClassTypeAlias() { 1233 void test_visitClassTypeAlias() {
494 // class B {} 1234 // class B {}
495 // class M {} 1235 // class M {}
496 // class C = B with M 1236 // class C = B with M
497 ElementHolder holder = new ElementHolder();
498 ElementBuilder builder = _makeBuilder(holder);
499 ClassElementImpl classB = ElementFactory.classElement2('B', []); 1237 ClassElementImpl classB = ElementFactory.classElement2('B', []);
500 ConstructorElementImpl constructorB = 1238 ConstructorElementImpl constructorB =
501 ElementFactory.constructorElement2(classB, '', []); 1239 ElementFactory.constructorElement2(classB, '', []);
502 constructorB.setModifier(Modifier.SYNTHETIC, true); 1240 constructorB.setModifier(Modifier.SYNTHETIC, true);
503 classB.constructors = [constructorB]; 1241 classB.constructors = [constructorB];
504 ClassElement classM = ElementFactory.classElement2('M', []); 1242 ClassElement classM = ElementFactory.classElement2('M', []);
505 WithClause withClause = 1243 WithClause withClause =
506 AstFactory.withClause([AstFactory.typeName(classM, [])]); 1244 AstFactory.withClause([AstFactory.typeName(classM, [])]);
507 ClassTypeAlias alias = AstFactory.classTypeAlias( 1245 ClassTypeAlias alias = AstFactory.classTypeAlias(
508 'C', null, null, AstFactory.typeName(classB, []), withClause, null); 1246 'C', null, null, AstFactory.typeName(classB, []), withClause, null);
509 alias.accept(builder); 1247
1248 ElementHolder holder = buildElementsForAst(alias);
510 List<ClassElement> types = holder.types; 1249 List<ClassElement> types = holder.types;
511 expect(types, hasLength(1)); 1250 expect(types, hasLength(1));
512 ClassElement type = types[0]; 1251 ClassElement type = types[0];
513 expect(alias.element, same(type)); 1252 expect(alias.element, same(type));
514 expect(type.name, equals('C')); 1253 expect(type.name, equals('C'));
515 expect(type.isAbstract, isFalse); 1254 expect(type.isAbstract, isFalse);
516 expect(type.isMixinApplication, isTrue); 1255 expect(type.isMixinApplication, isTrue);
517 expect(type.isSynthetic, isFalse); 1256 expect(type.isSynthetic, isFalse);
518 expect(type.typeParameters, isEmpty); 1257 expect(type.typeParameters, isEmpty);
519 expect(type.fields, isEmpty); 1258 expect(type.fields, isEmpty);
520 expect(type.methods, isEmpty); 1259 expect(type.methods, isEmpty);
521 } 1260 }
522 1261
523 void test_visitClassTypeAlias_abstract() { 1262 void test_visitClassTypeAlias_abstract() {
524 // class B {} 1263 // class B {}
525 // class M {} 1264 // class M {}
526 // abstract class C = B with M 1265 // abstract class C = B with M
527 ElementHolder holder = new ElementHolder();
528 ElementBuilder builder = _makeBuilder(holder);
529 ClassElementImpl classB = ElementFactory.classElement2('B', []); 1266 ClassElementImpl classB = ElementFactory.classElement2('B', []);
530 ConstructorElementImpl constructorB = 1267 ConstructorElementImpl constructorB =
531 ElementFactory.constructorElement2(classB, '', []); 1268 ElementFactory.constructorElement2(classB, '', []);
532 constructorB.setModifier(Modifier.SYNTHETIC, true); 1269 constructorB.setModifier(Modifier.SYNTHETIC, true);
533 classB.constructors = [constructorB]; 1270 classB.constructors = [constructorB];
534 ClassElement classM = ElementFactory.classElement2('M', []); 1271 ClassElement classM = ElementFactory.classElement2('M', []);
535 WithClause withClause = 1272 WithClause withClause =
536 AstFactory.withClause([AstFactory.typeName(classM, [])]); 1273 AstFactory.withClause([AstFactory.typeName(classM, [])]);
537 ClassTypeAlias classCAst = AstFactory.classTypeAlias('C', null, 1274 ClassTypeAlias alias = AstFactory.classTypeAlias('C', null,
538 Keyword.ABSTRACT, AstFactory.typeName(classB, []), withClause, null); 1275 Keyword.ABSTRACT, AstFactory.typeName(classB, []), withClause, null);
539 classCAst.accept(builder); 1276
1277 ElementHolder holder = buildElementsForAst(alias);
540 List<ClassElement> types = holder.types; 1278 List<ClassElement> types = holder.types;
541 expect(types, hasLength(1)); 1279 expect(types, hasLength(1));
542 ClassElement type = types[0]; 1280 ClassElement type = types[0];
543 expect(type.isAbstract, isTrue); 1281 expect(type.isAbstract, isTrue);
544 expect(type.isMixinApplication, isTrue); 1282 expect(type.isMixinApplication, isTrue);
545 } 1283 }
546 1284
547 void test_visitClassTypeAlias_typeParams() { 1285 void test_visitClassTypeAlias_typeParams() {
548 // class B {} 1286 // class B {}
549 // class M {} 1287 // class M {}
550 // class C<T> = B with M 1288 // class C<T> = B with M
551 ElementHolder holder = new ElementHolder();
552 ElementBuilder builder = _makeBuilder(holder);
553 ClassElementImpl classB = ElementFactory.classElement2('B', []); 1289 ClassElementImpl classB = ElementFactory.classElement2('B', []);
554 ConstructorElementImpl constructorB = 1290 ConstructorElementImpl constructorB =
555 ElementFactory.constructorElement2(classB, '', []); 1291 ElementFactory.constructorElement2(classB, '', []);
556 constructorB.setModifier(Modifier.SYNTHETIC, true); 1292 constructorB.setModifier(Modifier.SYNTHETIC, true);
557 classB.constructors = [constructorB]; 1293 classB.constructors = [constructorB];
558 ClassElementImpl classM = ElementFactory.classElement2('M', []); 1294 ClassElementImpl classM = ElementFactory.classElement2('M', []);
559 WithClause withClause = 1295 WithClause withClause =
560 AstFactory.withClause([AstFactory.typeName(classM, [])]); 1296 AstFactory.withClause([AstFactory.typeName(classM, [])]);
561 ClassTypeAlias classCAst = AstFactory.classTypeAlias( 1297 ClassTypeAlias alias = AstFactory.classTypeAlias(
562 'C', 1298 'C',
563 AstFactory.typeParameterList(['T']), 1299 AstFactory.typeParameterList(['T']),
564 null, 1300 null,
565 AstFactory.typeName(classB, []), 1301 AstFactory.typeName(classB, []),
566 withClause, 1302 withClause,
567 null); 1303 null);
568 classCAst.accept(builder); 1304
1305 ElementHolder holder = buildElementsForAst(alias);
569 List<ClassElement> types = holder.types; 1306 List<ClassElement> types = holder.types;
570 expect(types, hasLength(1)); 1307 expect(types, hasLength(1));
571 ClassElement type = types[0]; 1308 ClassElement type = types[0];
572 expect(type.typeParameters, hasLength(1)); 1309 expect(type.typeParameters, hasLength(1));
573 expect(type.typeParameters[0].name, equals('T')); 1310 expect(type.typeParameters[0].name, equals('T'));
574 } 1311 }
575 1312
576 void test_visitCompilationUnit_codeRange() {
577 TopLevelVariableDeclaration topLevelVariableDeclaration = AstFactory
578 .topLevelVariableDeclaration(null, AstFactory.typeName4('int'),
579 [AstFactory.variableDeclaration('V')]);
580 CompilationUnit unit = new CompilationUnit(
581 topLevelVariableDeclaration.beginToken,
582 null,
583 [],
584 [topLevelVariableDeclaration],
585 topLevelVariableDeclaration.endToken);
586 ElementHolder holder = new ElementHolder();
587 ElementBuilder builder = _makeBuilder(holder);
588 unit.beginToken.offset = 10;
589 unit.endToken.offset = 40;
590 unit.accept(builder);
591
592 CompilationUnitElement element = builder.compilationUnitElement;
593 _assertHasCodeRange(element, 0, 41);
594 }
595
596 void test_visitConstructorDeclaration_external() { 1313 void test_visitConstructorDeclaration_external() {
597 ElementHolder holder = new ElementHolder();
598 ElementBuilder builder = _makeBuilder(holder);
599 String className = "A"; 1314 String className = "A";
600 ConstructorDeclaration constructorDeclaration = 1315 ConstructorDeclaration constructorDeclaration =
601 AstFactory.constructorDeclaration2( 1316 AstFactory.constructorDeclaration2(
602 null, 1317 null,
603 null, 1318 null,
604 AstFactory.identifier3(className), 1319 AstFactory.identifier3(className),
605 null, 1320 null,
606 AstFactory.formalParameterList(), 1321 AstFactory.formalParameterList(),
607 null, 1322 null,
608 AstFactory.blockFunctionBody2()); 1323 AstFactory.blockFunctionBody2());
609 constructorDeclaration.externalKeyword = 1324 constructorDeclaration.externalKeyword =
610 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); 1325 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
611 constructorDeclaration.accept(builder); 1326
1327 ElementHolder holder = buildElementsForAst(constructorDeclaration);
612 List<ConstructorElement> constructors = holder.constructors; 1328 List<ConstructorElement> constructors = holder.constructors;
613 expect(constructors, hasLength(1)); 1329 expect(constructors, hasLength(1));
614 ConstructorElement constructor = constructors[0]; 1330 ConstructorElement constructor = constructors[0];
615 expect(constructor, isNotNull); 1331 expect(constructor, isNotNull);
616 expect(constructor.isExternal, isTrue); 1332 expect(constructor.isExternal, isTrue);
617 expect(constructor.isFactory, isFalse); 1333 expect(constructor.isFactory, isFalse);
618 expect(constructor.name, ""); 1334 expect(constructor.name, "");
619 expect(constructor.functions, hasLength(0)); 1335 expect(constructor.functions, hasLength(0));
620 expect(constructor.labels, hasLength(0)); 1336 expect(constructor.labels, hasLength(0));
621 expect(constructor.localVariables, hasLength(0)); 1337 expect(constructor.localVariables, hasLength(0));
622 expect(constructor.parameters, hasLength(0)); 1338 expect(constructor.parameters, hasLength(0));
623 } 1339 }
624 1340
625 void test_visitConstructorDeclaration_factory() { 1341 void test_visitConstructorDeclaration_factory() {
626 ElementHolder holder = new ElementHolder();
627 ElementBuilder builder = _makeBuilder(holder);
628 String className = "A"; 1342 String className = "A";
629 ConstructorDeclaration constructorDeclaration = 1343 ConstructorDeclaration constructorDeclaration =
630 AstFactory.constructorDeclaration2( 1344 AstFactory.constructorDeclaration2(
631 null, 1345 null,
632 Keyword.FACTORY, 1346 Keyword.FACTORY,
633 AstFactory.identifier3(className), 1347 AstFactory.identifier3(className),
634 null, 1348 null,
635 AstFactory.formalParameterList(), 1349 AstFactory.formalParameterList(),
636 null, 1350 null,
637 AstFactory.blockFunctionBody2()); 1351 AstFactory.blockFunctionBody2());
638 constructorDeclaration.accept(builder); 1352
1353 ElementHolder holder = buildElementsForAst(constructorDeclaration);
639 List<ConstructorElement> constructors = holder.constructors; 1354 List<ConstructorElement> constructors = holder.constructors;
640 expect(constructors, hasLength(1)); 1355 expect(constructors, hasLength(1));
641 ConstructorElement constructor = constructors[0]; 1356 ConstructorElement constructor = constructors[0];
642 expect(constructor, isNotNull); 1357 expect(constructor, isNotNull);
643 expect(constructor.isExternal, isFalse); 1358 expect(constructor.isExternal, isFalse);
644 expect(constructor.isFactory, isTrue); 1359 expect(constructor.isFactory, isTrue);
645 expect(constructor.name, ""); 1360 expect(constructor.name, "");
646 expect(constructor.functions, hasLength(0)); 1361 expect(constructor.functions, hasLength(0));
647 expect(constructor.labels, hasLength(0)); 1362 expect(constructor.labels, hasLength(0));
648 expect(constructor.localVariables, hasLength(0)); 1363 expect(constructor.localVariables, hasLength(0));
649 expect(constructor.parameters, hasLength(0)); 1364 expect(constructor.parameters, hasLength(0));
650 } 1365 }
651 1366
652 void test_visitConstructorDeclaration_minimal() { 1367 void test_visitConstructorDeclaration_minimal() {
653 ElementHolder holder = new ElementHolder();
654 ElementBuilder builder = _makeBuilder(holder);
655 String className = "A"; 1368 String className = "A";
656 ConstructorDeclaration constructorDeclaration = 1369 ConstructorDeclaration constructorDeclaration =
657 AstFactory.constructorDeclaration2( 1370 AstFactory.constructorDeclaration2(
658 null, 1371 null,
659 null, 1372 null,
660 AstFactory.identifier3(className), 1373 AstFactory.identifier3(className),
661 null, 1374 null,
662 AstFactory.formalParameterList(), 1375 AstFactory.formalParameterList(),
663 null, 1376 null,
664 AstFactory.blockFunctionBody2()); 1377 AstFactory.blockFunctionBody2());
665 constructorDeclaration.documentationComment = AstFactory 1378 constructorDeclaration.documentationComment = AstFactory
666 .documentationComment( 1379 .documentationComment(
667 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1380 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
668 constructorDeclaration.endToken.offset = 80; 1381 constructorDeclaration.endToken.offset = 80;
669 constructorDeclaration.accept(builder);
670 1382
1383 ElementHolder holder = buildElementsForAst(constructorDeclaration);
671 List<ConstructorElement> constructors = holder.constructors; 1384 List<ConstructorElement> constructors = holder.constructors;
672 expect(constructors, hasLength(1)); 1385 expect(constructors, hasLength(1));
673 ConstructorElement constructor = constructors[0]; 1386 ConstructorElement constructor = constructors[0];
674 expect(constructor, isNotNull); 1387 expect(constructor, isNotNull);
675 _assertHasCodeRange(constructor, 50, 31); 1388 assertHasCodeRange(constructor, 50, 31);
676 expect(constructor.documentationComment, '/// aaa'); 1389 expect(constructor.documentationComment, '/// aaa');
677 expect(constructor.isExternal, isFalse); 1390 expect(constructor.isExternal, isFalse);
678 expect(constructor.isFactory, isFalse); 1391 expect(constructor.isFactory, isFalse);
679 expect(constructor.name, ""); 1392 expect(constructor.name, "");
680 expect(constructor.functions, hasLength(0)); 1393 expect(constructor.functions, hasLength(0));
681 expect(constructor.labels, hasLength(0)); 1394 expect(constructor.labels, hasLength(0));
682 expect(constructor.localVariables, hasLength(0)); 1395 expect(constructor.localVariables, hasLength(0));
683 expect(constructor.parameters, hasLength(0)); 1396 expect(constructor.parameters, hasLength(0));
684 } 1397 }
685 1398
686 void test_visitConstructorDeclaration_named() { 1399 void test_visitConstructorDeclaration_named() {
687 ElementHolder holder = new ElementHolder();
688 ElementBuilder builder = _makeBuilder(holder);
689 String className = "A"; 1400 String className = "A";
690 String constructorName = "c"; 1401 String constructorName = "c";
691 ConstructorDeclaration constructorDeclaration = 1402 ConstructorDeclaration constructorDeclaration =
692 AstFactory.constructorDeclaration2( 1403 AstFactory.constructorDeclaration2(
693 null, 1404 null,
694 null, 1405 null,
695 AstFactory.identifier3(className), 1406 AstFactory.identifier3(className),
696 constructorName, 1407 constructorName,
697 AstFactory.formalParameterList(), 1408 AstFactory.formalParameterList(),
698 null, 1409 null,
699 AstFactory.blockFunctionBody2()); 1410 AstFactory.blockFunctionBody2());
700 constructorDeclaration.accept(builder); 1411
1412 ElementHolder holder = buildElementsForAst(constructorDeclaration);
701 List<ConstructorElement> constructors = holder.constructors; 1413 List<ConstructorElement> constructors = holder.constructors;
702 expect(constructors, hasLength(1)); 1414 expect(constructors, hasLength(1));
703 ConstructorElement constructor = constructors[0]; 1415 ConstructorElement constructor = constructors[0];
704 expect(constructor, isNotNull); 1416 expect(constructor, isNotNull);
705 expect(constructor.isExternal, isFalse); 1417 expect(constructor.isExternal, isFalse);
706 expect(constructor.isFactory, isFalse); 1418 expect(constructor.isFactory, isFalse);
707 expect(constructor.name, constructorName); 1419 expect(constructor.name, constructorName);
708 expect(constructor.functions, hasLength(0)); 1420 expect(constructor.functions, hasLength(0));
709 expect(constructor.labels, hasLength(0)); 1421 expect(constructor.labels, hasLength(0));
710 expect(constructor.localVariables, hasLength(0)); 1422 expect(constructor.localVariables, hasLength(0));
711 expect(constructor.parameters, hasLength(0)); 1423 expect(constructor.parameters, hasLength(0));
712 expect(constructorDeclaration.name.staticElement, same(constructor)); 1424 expect(constructorDeclaration.name.staticElement, same(constructor));
713 expect(constructorDeclaration.element, same(constructor)); 1425 expect(constructorDeclaration.element, same(constructor));
714 } 1426 }
715 1427
716 void test_visitConstructorDeclaration_unnamed() { 1428 void test_visitConstructorDeclaration_unnamed() {
717 ElementHolder holder = new ElementHolder();
718 ElementBuilder builder = _makeBuilder(holder);
719 String className = "A"; 1429 String className = "A";
720 ConstructorDeclaration constructorDeclaration = 1430 ConstructorDeclaration constructorDeclaration =
721 AstFactory.constructorDeclaration2( 1431 AstFactory.constructorDeclaration2(
722 null, 1432 null,
723 null, 1433 null,
724 AstFactory.identifier3(className), 1434 AstFactory.identifier3(className),
725 null, 1435 null,
726 AstFactory.formalParameterList(), 1436 AstFactory.formalParameterList(),
727 null, 1437 null,
728 AstFactory.blockFunctionBody2()); 1438 AstFactory.blockFunctionBody2());
729 constructorDeclaration.accept(builder); 1439
1440 ElementHolder holder = buildElementsForAst(constructorDeclaration);
730 List<ConstructorElement> constructors = holder.constructors; 1441 List<ConstructorElement> constructors = holder.constructors;
731 expect(constructors, hasLength(1)); 1442 expect(constructors, hasLength(1));
732 ConstructorElement constructor = constructors[0]; 1443 ConstructorElement constructor = constructors[0];
733 expect(constructor, isNotNull); 1444 expect(constructor, isNotNull);
734 expect(constructor.isExternal, isFalse); 1445 expect(constructor.isExternal, isFalse);
735 expect(constructor.isFactory, isFalse); 1446 expect(constructor.isFactory, isFalse);
736 expect(constructor.name, ""); 1447 expect(constructor.name, "");
737 expect(constructor.functions, hasLength(0)); 1448 expect(constructor.functions, hasLength(0));
738 expect(constructor.labels, hasLength(0)); 1449 expect(constructor.labels, hasLength(0));
739 expect(constructor.localVariables, hasLength(0)); 1450 expect(constructor.localVariables, hasLength(0));
740 expect(constructor.parameters, hasLength(0)); 1451 expect(constructor.parameters, hasLength(0));
741 expect(constructorDeclaration.element, same(constructor)); 1452 expect(constructorDeclaration.element, same(constructor));
742 } 1453 }
743 1454
744 void test_visitDeclaredIdentifier_noType() {
745 LocalVariableElement variable =
746 buildElementsForText('f() { for (var i in []) {} }')
747 .functions[0]
748 .localVariables[0];
749 _assertHasCodeRange(variable, 11, 5);
750 expect(variable, isNotNull);
751 expect(variable.hasImplicitType, isTrue);
752 expect(variable.isConst, isFalse);
753 expect(variable.isDeprecated, isFalse);
754 expect(variable.isFinal, isFalse);
755 expect(variable.isOverride, isFalse);
756 expect(variable.isPrivate, isFalse);
757 expect(variable.isPublic, isTrue);
758 expect(variable.isSynthetic, isFalse);
759 expect(variable.name, 'i');
760 }
761
762 void test_visitDeclaredIdentifier_type() {
763 LocalVariableElement variable =
764 buildElementsForText('f() { for (int i in []) {} }')
765 .functions[0]
766 .localVariables[0];
767 _assertHasCodeRange(variable, 11, 5);
768 expect(variable.hasImplicitType, isFalse);
769 expect(variable.isConst, isFalse);
770 expect(variable.isDeprecated, isFalse);
771 expect(variable.isFinal, isFalse);
772 expect(variable.isOverride, isFalse);
773 expect(variable.isPrivate, isFalse);
774 expect(variable.isPublic, isTrue);
775 expect(variable.isSynthetic, isFalse);
776 expect(variable.name, 'i');
777 }
778
779 void test_visitDefaultFormalParameter_noType() {
780 // p = 0
781 ElementHolder holder = new ElementHolder();
782 ElementBuilder builder = _makeBuilder(holder);
783 String parameterName = 'p';
784 DefaultFormalParameter formalParameter =
785 AstFactory.positionalFormalParameter(
786 AstFactory.simpleFormalParameter3(parameterName),
787 AstFactory.integer(0));
788 formalParameter.beginToken.offset = 50;
789 formalParameter.endToken.offset = 80;
790 formalParameter.accept(builder);
791
792 List<ParameterElement> parameters = holder.parameters;
793 expect(parameters, hasLength(1));
794 ParameterElement parameter = parameters[0];
795 _assertHasCodeRange(parameter, 50, 31);
796 expect(parameter.hasImplicitType, isTrue);
797 expect(parameter.initializer, isNotNull);
798 expect(parameter.initializer.type, isNotNull);
799 expect(parameter.initializer.hasImplicitReturnType, isTrue);
800 expect(parameter.isConst, isFalse);
801 expect(parameter.isDeprecated, isFalse);
802 expect(parameter.isFinal, isFalse);
803 expect(parameter.isInitializingFormal, isFalse);
804 expect(parameter.isOverride, isFalse);
805 expect(parameter.isPrivate, isFalse);
806 expect(parameter.isPublic, isTrue);
807 expect(parameter.isSynthetic, isFalse);
808 expect(parameter.name, parameterName);
809 }
810
811 void test_visitDefaultFormalParameter_type() {
812 // E p = 0
813 ElementHolder holder = new ElementHolder();
814 ElementBuilder builder = _makeBuilder(holder);
815 String parameterName = 'p';
816 DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter(
817 AstFactory.simpleFormalParameter4(
818 AstFactory.typeName4('E'), parameterName),
819 AstFactory.integer(0));
820 formalParameter.accept(builder);
821
822 List<ParameterElement> parameters = holder.parameters;
823 expect(parameters, hasLength(1));
824 ParameterElement parameter = parameters[0];
825 expect(parameter.hasImplicitType, isFalse);
826 expect(parameter.initializer, isNotNull);
827 expect(parameter.initializer.type, isNotNull);
828 expect(parameter.initializer.hasImplicitReturnType, isTrue);
829 expect(parameter.isConst, isFalse);
830 expect(parameter.isDeprecated, isFalse);
831 expect(parameter.isFinal, isFalse);
832 expect(parameter.isInitializingFormal, isFalse);
833 expect(parameter.isOverride, isFalse);
834 expect(parameter.isPrivate, isFalse);
835 expect(parameter.isPublic, isTrue);
836 expect(parameter.isSynthetic, isFalse);
837 expect(parameter.name, parameterName);
838 }
839
840 void test_visitEnumDeclaration() { 1455 void test_visitEnumDeclaration() {
841 ElementHolder holder = new ElementHolder();
842 ElementBuilder builder = _makeBuilder(holder);
843 String enumName = "E"; 1456 String enumName = "E";
844 EnumDeclaration enumDeclaration = 1457 EnumDeclaration enumDeclaration =
845 AstFactory.enumDeclaration2(enumName, ["ONE"]); 1458 AstFactory.enumDeclaration2(enumName, ["ONE"]);
846 enumDeclaration.documentationComment = AstFactory.documentationComment( 1459 enumDeclaration.documentationComment = AstFactory.documentationComment(
847 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1460 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
848 enumDeclaration.endToken.offset = 80; 1461 enumDeclaration.endToken.offset = 80;
849 enumDeclaration.accept(builder); 1462 ElementHolder holder = buildElementsForAst(enumDeclaration);
850 List<ClassElement> enums = holder.enums; 1463 List<ClassElement> enums = holder.enums;
851 expect(enums, hasLength(1)); 1464 expect(enums, hasLength(1));
852 ClassElement enumElement = enums[0]; 1465 ClassElement enumElement = enums[0];
853 expect(enumElement, isNotNull); 1466 expect(enumElement, isNotNull);
854 _assertHasCodeRange(enumElement, 50, 31); 1467 assertHasCodeRange(enumElement, 50, 31);
855 expect(enumElement.documentationComment, '/// aaa'); 1468 expect(enumElement.documentationComment, '/// aaa');
856 expect(enumElement.name, enumName); 1469 expect(enumElement.name, enumName);
857 } 1470 }
858 1471
859 void test_visitFieldDeclaration() { 1472 void test_visitFieldDeclaration() {
860 ElementHolder holder = new ElementHolder();
861 ElementBuilder builder = _makeBuilder(holder);
862 String firstFieldName = "x"; 1473 String firstFieldName = "x";
863 String secondFieldName = "y"; 1474 String secondFieldName = "y";
864 FieldDeclaration fieldDeclaration = 1475 FieldDeclaration fieldDeclaration =
865 AstFactory.fieldDeclaration2(false, null, [ 1476 AstFactory.fieldDeclaration2(false, null, [
866 AstFactory.variableDeclaration(firstFieldName), 1477 AstFactory.variableDeclaration(firstFieldName),
867 AstFactory.variableDeclaration(secondFieldName) 1478 AstFactory.variableDeclaration(secondFieldName)
868 ]); 1479 ]);
869 fieldDeclaration.documentationComment = AstFactory.documentationComment( 1480 fieldDeclaration.documentationComment = AstFactory.documentationComment(
870 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1481 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
871 fieldDeclaration.endToken.offset = 110; 1482 fieldDeclaration.endToken.offset = 110;
872 fieldDeclaration.accept(builder);
873 1483
1484 ElementHolder holder = buildElementsForAst(fieldDeclaration);
874 List<FieldElement> fields = holder.fields; 1485 List<FieldElement> fields = holder.fields;
875 expect(fields, hasLength(2)); 1486 expect(fields, hasLength(2));
876 1487
877 FieldElement firstField = fields[0]; 1488 FieldElement firstField = fields[0];
878 expect(firstField, isNotNull); 1489 expect(firstField, isNotNull);
879 _assertHasCodeRange(firstField, 50, 61); 1490 assertHasCodeRange(firstField, 50, 61);
880 expect(firstField.documentationComment, '/// aaa'); 1491 expect(firstField.documentationComment, '/// aaa');
881 expect(firstField.name, firstFieldName); 1492 expect(firstField.name, firstFieldName);
882 expect(firstField.initializer, isNull); 1493 expect(firstField.initializer, isNull);
883 expect(firstField.isConst, isFalse); 1494 expect(firstField.isConst, isFalse);
884 expect(firstField.isFinal, isFalse); 1495 expect(firstField.isFinal, isFalse);
885 expect(firstField.isSynthetic, isFalse); 1496 expect(firstField.isSynthetic, isFalse);
886 1497
887 FieldElement secondField = fields[1]; 1498 FieldElement secondField = fields[1];
888 expect(secondField, isNotNull); 1499 expect(secondField, isNotNull);
889 _assertHasCodeRange(secondField, 50, 61); 1500 assertHasCodeRange(secondField, 50, 61);
890 expect(secondField.documentationComment, '/// aaa'); 1501 expect(secondField.documentationComment, '/// aaa');
891 expect(secondField.name, secondFieldName); 1502 expect(secondField.name, secondFieldName);
892 expect(secondField.initializer, isNull); 1503 expect(secondField.initializer, isNull);
893 expect(secondField.isConst, isFalse); 1504 expect(secondField.isConst, isFalse);
894 expect(secondField.isFinal, isFalse); 1505 expect(secondField.isFinal, isFalse);
895 expect(secondField.isSynthetic, isFalse); 1506 expect(secondField.isSynthetic, isFalse);
896 } 1507 }
897 1508
898 void test_visitFieldFormalParameter() { 1509 void test_visitFieldFormalParameter() {
899 ElementHolder holder = new ElementHolder();
900 ElementBuilder builder = _makeBuilder(holder);
901 String parameterName = "p"; 1510 String parameterName = "p";
902 FieldFormalParameter formalParameter = 1511 FieldFormalParameter formalParameter =
903 AstFactory.fieldFormalParameter(null, null, parameterName); 1512 AstFactory.fieldFormalParameter(null, null, parameterName);
904 formalParameter.beginToken.offset = 50; 1513 formalParameter.beginToken.offset = 50;
905 formalParameter.endToken.offset = 80; 1514 formalParameter.endToken.offset = 80;
906 formalParameter.accept(builder); 1515 ElementHolder holder = buildElementsForAst(formalParameter);
907 List<ParameterElement> parameters = holder.parameters; 1516 List<ParameterElement> parameters = holder.parameters;
908 expect(parameters, hasLength(1)); 1517 expect(parameters, hasLength(1));
909 ParameterElement parameter = parameters[0]; 1518 ParameterElement parameter = parameters[0];
910 expect(parameter, isNotNull); 1519 expect(parameter, isNotNull);
911 _assertHasCodeRange(parameter, 50, 31); 1520 assertHasCodeRange(parameter, 50, 31);
912 expect(parameter.name, parameterName); 1521 expect(parameter.name, parameterName);
913 expect(parameter.initializer, isNull); 1522 expect(parameter.initializer, isNull);
914 expect(parameter.isConst, isFalse); 1523 expect(parameter.isConst, isFalse);
915 expect(parameter.isFinal, isFalse); 1524 expect(parameter.isFinal, isFalse);
916 expect(parameter.isSynthetic, isFalse); 1525 expect(parameter.isSynthetic, isFalse);
917 expect(parameter.parameterKind, ParameterKind.REQUIRED); 1526 expect(parameter.parameterKind, ParameterKind.REQUIRED);
918 expect(parameter.parameters, hasLength(0)); 1527 expect(parameter.parameters, hasLength(0));
919 } 1528 }
920 1529
921 void test_visitFieldFormalParameter_functionTyped() { 1530 void test_visitFieldFormalParameter_functionTyped() {
922 ElementHolder holder = new ElementHolder();
923 ElementBuilder builder = _makeBuilder(holder);
924 String parameterName = "p"; 1531 String parameterName = "p";
925 FieldFormalParameter formalParameter = AstFactory.fieldFormalParameter( 1532 FieldFormalParameter formalParameter = AstFactory.fieldFormalParameter(
926 null, 1533 null,
927 null, 1534 null,
928 parameterName, 1535 parameterName,
929 AstFactory 1536 AstFactory
930 .formalParameterList([AstFactory.simpleFormalParameter3("a")])); 1537 .formalParameterList([AstFactory.simpleFormalParameter3("a")]));
931 formalParameter.accept(builder); 1538 ElementHolder holder = buildElementsForAst(formalParameter);
932 List<ParameterElement> parameters = holder.parameters; 1539 List<ParameterElement> parameters = holder.parameters;
933 expect(parameters, hasLength(1)); 1540 expect(parameters, hasLength(1));
934 ParameterElement parameter = parameters[0]; 1541 ParameterElement parameter = parameters[0];
935 expect(parameter, isNotNull); 1542 expect(parameter, isNotNull);
936 expect(parameter.name, parameterName); 1543 expect(parameter.name, parameterName);
937 expect(parameter.initializer, isNull); 1544 expect(parameter.initializer, isNull);
938 expect(parameter.isConst, isFalse); 1545 expect(parameter.isConst, isFalse);
939 expect(parameter.isFinal, isFalse); 1546 expect(parameter.isFinal, isFalse);
940 expect(parameter.isSynthetic, isFalse); 1547 expect(parameter.isSynthetic, isFalse);
941 expect(parameter.parameterKind, ParameterKind.REQUIRED); 1548 expect(parameter.parameterKind, ParameterKind.REQUIRED);
942 expect(parameter.parameters, hasLength(1)); 1549 expect(parameter.parameters, hasLength(1));
943 } 1550 }
944 1551
945 void test_visitFormalParameterList() { 1552 void test_visitFormalParameterList() {
946 ElementHolder holder = new ElementHolder();
947 ElementBuilder builder = _makeBuilder(holder);
948 String firstParameterName = "a"; 1553 String firstParameterName = "a";
949 String secondParameterName = "b"; 1554 String secondParameterName = "b";
950 FormalParameterList parameterList = AstFactory.formalParameterList([ 1555 FormalParameterList parameterList = AstFactory.formalParameterList([
951 AstFactory.simpleFormalParameter3(firstParameterName), 1556 AstFactory.simpleFormalParameter3(firstParameterName),
952 AstFactory.simpleFormalParameter3(secondParameterName) 1557 AstFactory.simpleFormalParameter3(secondParameterName)
953 ]); 1558 ]);
954 parameterList.accept(builder); 1559 ElementHolder holder = buildElementsForAst(parameterList);
955 List<ParameterElement> parameters = holder.parameters; 1560 List<ParameterElement> parameters = holder.parameters;
956 expect(parameters, hasLength(2)); 1561 expect(parameters, hasLength(2));
957 expect(parameters[0].name, firstParameterName); 1562 expect(parameters[0].name, firstParameterName);
958 expect(parameters[1].name, secondParameterName); 1563 expect(parameters[1].name, secondParameterName);
959 } 1564 }
960 1565
961 void test_visitFunctionDeclaration_external() { 1566 void test_visitFunctionDeclaration_external() {
962 // external f(); 1567 // external f();
963 ElementHolder holder = new ElementHolder();
964 ElementBuilder builder = _makeBuilder(holder);
965 String functionName = "f"; 1568 String functionName = "f";
966 FunctionDeclaration declaration = AstFactory.functionDeclaration( 1569 FunctionDeclaration declaration = AstFactory.functionDeclaration(
967 null, 1570 null,
968 null, 1571 null,
969 functionName, 1572 functionName,
970 AstFactory.functionExpression2( 1573 AstFactory.functionExpression2(
971 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody())); 1574 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody()));
972 declaration.externalKeyword = 1575 declaration.externalKeyword =
973 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); 1576 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
974 declaration.accept(builder);
975 1577
1578 ElementHolder holder = buildElementsForAst(declaration);
976 List<FunctionElement> functions = holder.functions; 1579 List<FunctionElement> functions = holder.functions;
977 expect(functions, hasLength(1)); 1580 expect(functions, hasLength(1));
978 FunctionElement function = functions[0]; 1581 FunctionElement function = functions[0];
979 expect(function, isNotNull); 1582 expect(function, isNotNull);
980 expect(function.name, functionName); 1583 expect(function.name, functionName);
981 expect(declaration.element, same(function)); 1584 expect(declaration.element, same(function));
982 expect(declaration.functionExpression.element, same(function)); 1585 expect(declaration.functionExpression.element, same(function));
983 expect(function.hasImplicitReturnType, isTrue); 1586 expect(function.hasImplicitReturnType, isTrue);
984 expect(function.isExternal, isTrue); 1587 expect(function.isExternal, isTrue);
985 expect(function.isSynthetic, isFalse); 1588 expect(function.isSynthetic, isFalse);
986 expect(function.typeParameters, hasLength(0)); 1589 expect(function.typeParameters, hasLength(0));
987 } 1590 }
988 1591
989 void test_visitFunctionDeclaration_getter() { 1592 void test_visitFunctionDeclaration_getter() {
990 // get f() {} 1593 // get f() {}
991 ElementHolder holder = new ElementHolder();
992 ElementBuilder builder = _makeBuilder(holder);
993 String functionName = "f"; 1594 String functionName = "f";
994 FunctionDeclaration declaration = AstFactory.functionDeclaration( 1595 FunctionDeclaration declaration = AstFactory.functionDeclaration(
995 null, 1596 null,
996 Keyword.GET, 1597 Keyword.GET,
997 functionName, 1598 functionName,
998 AstFactory.functionExpression2( 1599 AstFactory.functionExpression2(
999 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2())); 1600 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()));
1000 declaration.documentationComment = AstFactory.documentationComment( 1601 declaration.documentationComment = AstFactory.documentationComment(
1001 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1602 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
1002 declaration.endToken.offset = 80; 1603 declaration.endToken.offset = 80;
1003 declaration.accept(builder);
1004 1604
1605 ElementHolder holder = buildElementsForAst(declaration);
1005 List<PropertyAccessorElement> accessors = holder.accessors; 1606 List<PropertyAccessorElement> accessors = holder.accessors;
1006 expect(accessors, hasLength(1)); 1607 expect(accessors, hasLength(1));
1007 PropertyAccessorElement accessor = accessors[0]; 1608 PropertyAccessorElement accessor = accessors[0];
1008 expect(accessor, isNotNull); 1609 expect(accessor, isNotNull);
1009 _assertHasCodeRange(accessor, 50, 31); 1610 assertHasCodeRange(accessor, 50, 31);
1010 expect(accessor.documentationComment, '/// aaa'); 1611 expect(accessor.documentationComment, '/// aaa');
1011 expect(accessor.name, functionName); 1612 expect(accessor.name, functionName);
1012 expect(declaration.element, same(accessor)); 1613 expect(declaration.element, same(accessor));
1013 expect(declaration.functionExpression.element, same(accessor)); 1614 expect(declaration.functionExpression.element, same(accessor));
1014 expect(accessor.hasImplicitReturnType, isTrue); 1615 expect(accessor.hasImplicitReturnType, isTrue);
1015 expect(accessor.isGetter, isTrue); 1616 expect(accessor.isGetter, isTrue);
1016 expect(accessor.isExternal, isFalse); 1617 expect(accessor.isExternal, isFalse);
1017 expect(accessor.isSetter, isFalse); 1618 expect(accessor.isSetter, isFalse);
1018 expect(accessor.isSynthetic, isFalse); 1619 expect(accessor.isSynthetic, isFalse);
1019 expect(accessor.typeParameters, hasLength(0)); 1620 expect(accessor.typeParameters, hasLength(0));
1020 PropertyInducingElement variable = accessor.variable; 1621 PropertyInducingElement variable = accessor.variable;
1021 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement, 1622 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement,
1022 TopLevelVariableElement, variable); 1623 TopLevelVariableElement, variable);
1023 expect(variable.isSynthetic, isTrue); 1624 expect(variable.isSynthetic, isTrue);
1024 } 1625 }
1025 1626
1026 void test_visitFunctionDeclaration_plain() { 1627 void test_visitFunctionDeclaration_plain() {
1027 // T f() {} 1628 // T f() {}
1028 ElementHolder holder = new ElementHolder();
1029 ElementBuilder builder = _makeBuilder(holder);
1030 String functionName = "f"; 1629 String functionName = "f";
1031 FunctionDeclaration declaration = AstFactory.functionDeclaration( 1630 FunctionDeclaration declaration = AstFactory.functionDeclaration(
1032 AstFactory.typeName4('T'), 1631 AstFactory.typeName4('T'),
1033 null, 1632 null,
1034 functionName, 1633 functionName,
1035 AstFactory.functionExpression2( 1634 AstFactory.functionExpression2(
1036 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2())); 1635 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()));
1037 declaration.documentationComment = AstFactory.documentationComment( 1636 declaration.documentationComment = AstFactory.documentationComment(
1038 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1637 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
1039 declaration.endToken.offset = 80; 1638 declaration.endToken.offset = 80;
1040 declaration.accept(builder);
1041 1639
1640 ElementHolder holder = buildElementsForAst(declaration);
1042 List<FunctionElement> functions = holder.functions; 1641 List<FunctionElement> functions = holder.functions;
1043 expect(functions, hasLength(1)); 1642 expect(functions, hasLength(1));
1044 FunctionElement function = functions[0]; 1643 FunctionElement function = functions[0];
1045 expect(function, isNotNull); 1644 expect(function, isNotNull);
1046 _assertHasCodeRange(function, 50, 31); 1645 assertHasCodeRange(function, 50, 31);
1047 expect(function.documentationComment, '/// aaa'); 1646 expect(function.documentationComment, '/// aaa');
1048 expect(function.hasImplicitReturnType, isFalse); 1647 expect(function.hasImplicitReturnType, isFalse);
1049 expect(function.name, functionName); 1648 expect(function.name, functionName);
1050 expect(declaration.element, same(function)); 1649 expect(declaration.element, same(function));
1051 expect(declaration.functionExpression.element, same(function)); 1650 expect(declaration.functionExpression.element, same(function));
1052 expect(function.isExternal, isFalse); 1651 expect(function.isExternal, isFalse);
1053 expect(function.isSynthetic, isFalse); 1652 expect(function.isSynthetic, isFalse);
1054 expect(function.typeParameters, hasLength(0)); 1653 expect(function.typeParameters, hasLength(0));
1055 } 1654 }
1056 1655
1057 void test_visitFunctionDeclaration_setter() { 1656 void test_visitFunctionDeclaration_setter() {
1058 // set f() {} 1657 // set f() {}
1059 ElementHolder holder = new ElementHolder();
1060 ElementBuilder builder = _makeBuilder(holder);
1061 String functionName = "f"; 1658 String functionName = "f";
1062 FunctionDeclaration declaration = AstFactory.functionDeclaration( 1659 FunctionDeclaration declaration = AstFactory.functionDeclaration(
1063 null, 1660 null,
1064 Keyword.SET, 1661 Keyword.SET,
1065 functionName, 1662 functionName,
1066 AstFactory.functionExpression2( 1663 AstFactory.functionExpression2(
1067 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2())); 1664 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()));
1068 declaration.documentationComment = AstFactory.documentationComment( 1665 declaration.documentationComment = AstFactory.documentationComment(
1069 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1666 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
1070 declaration.endToken.offset = 80; 1667 declaration.endToken.offset = 80;
1071 declaration.accept(builder);
1072 1668
1669 ElementHolder holder = buildElementsForAst(declaration);
1073 List<PropertyAccessorElement> accessors = holder.accessors; 1670 List<PropertyAccessorElement> accessors = holder.accessors;
1074 expect(accessors, hasLength(1)); 1671 expect(accessors, hasLength(1));
1075 PropertyAccessorElement accessor = accessors[0]; 1672 PropertyAccessorElement accessor = accessors[0];
1076 expect(accessor, isNotNull); 1673 expect(accessor, isNotNull);
1077 _assertHasCodeRange(accessor, 50, 31); 1674 assertHasCodeRange(accessor, 50, 31);
1078 expect(accessor.documentationComment, '/// aaa'); 1675 expect(accessor.documentationComment, '/// aaa');
1079 expect(accessor.hasImplicitReturnType, isTrue); 1676 expect(accessor.hasImplicitReturnType, isTrue);
1080 expect(accessor.name, "$functionName="); 1677 expect(accessor.name, "$functionName=");
1081 expect(declaration.element, same(accessor)); 1678 expect(declaration.element, same(accessor));
1082 expect(declaration.functionExpression.element, same(accessor)); 1679 expect(declaration.functionExpression.element, same(accessor));
1083 expect(accessor.isGetter, isFalse); 1680 expect(accessor.isGetter, isFalse);
1084 expect(accessor.isExternal, isFalse); 1681 expect(accessor.isExternal, isFalse);
1085 expect(accessor.isSetter, isTrue); 1682 expect(accessor.isSetter, isTrue);
1086 expect(accessor.isSynthetic, isFalse); 1683 expect(accessor.isSynthetic, isFalse);
1087 expect(accessor.typeParameters, hasLength(0)); 1684 expect(accessor.typeParameters, hasLength(0));
1088 PropertyInducingElement variable = accessor.variable; 1685 PropertyInducingElement variable = accessor.variable;
1089 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement, 1686 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement,
1090 TopLevelVariableElement, variable); 1687 TopLevelVariableElement, variable);
1091 expect(variable.isSynthetic, isTrue); 1688 expect(variable.isSynthetic, isTrue);
1092 } 1689 }
1093 1690
1094 void test_visitFunctionDeclaration_typeParameters() { 1691 void test_visitFunctionDeclaration_typeParameters() {
1095 // f<E>() {} 1692 // f<E>() {}
1096 ElementHolder holder = new ElementHolder();
1097 ElementBuilder builder = _makeBuilder(holder);
1098 String functionName = 'f'; 1693 String functionName = 'f';
1099 String typeParameterName = 'E'; 1694 String typeParameterName = 'E';
1100 FunctionExpression expression = AstFactory.functionExpression3( 1695 FunctionExpression expression = AstFactory.functionExpression3(
1101 AstFactory.typeParameterList([typeParameterName]), 1696 AstFactory.typeParameterList([typeParameterName]),
1102 AstFactory.formalParameterList(), 1697 AstFactory.formalParameterList(),
1103 AstFactory.blockFunctionBody2()); 1698 AstFactory.blockFunctionBody2());
1104 FunctionDeclaration declaration = 1699 FunctionDeclaration declaration =
1105 AstFactory.functionDeclaration(null, null, functionName, expression); 1700 AstFactory.functionDeclaration(null, null, functionName, expression);
1106 declaration.accept(builder);
1107 1701
1702 ElementHolder holder = buildElementsForAst(declaration);
1108 List<FunctionElement> functions = holder.functions; 1703 List<FunctionElement> functions = holder.functions;
1109 expect(functions, hasLength(1)); 1704 expect(functions, hasLength(1));
1110 FunctionElement function = functions[0]; 1705 FunctionElement function = functions[0];
1111 expect(function, isNotNull); 1706 expect(function, isNotNull);
1112 expect(function.hasImplicitReturnType, isTrue); 1707 expect(function.hasImplicitReturnType, isTrue);
1113 expect(function.name, functionName); 1708 expect(function.name, functionName);
1114 expect(function.isExternal, isFalse); 1709 expect(function.isExternal, isFalse);
1115 expect(function.isSynthetic, isFalse); 1710 expect(function.isSynthetic, isFalse);
1116 expect(declaration.element, same(function)); 1711 expect(declaration.element, same(function));
1117 expect(expression.element, same(function)); 1712 expect(expression.element, same(function));
1118 List<TypeParameterElement> typeParameters = function.typeParameters; 1713 List<TypeParameterElement> typeParameters = function.typeParameters;
1119 expect(typeParameters, hasLength(1)); 1714 expect(typeParameters, hasLength(1));
1120 TypeParameterElement typeParameter = typeParameters[0]; 1715 TypeParameterElement typeParameter = typeParameters[0];
1121 expect(typeParameter, isNotNull); 1716 expect(typeParameter, isNotNull);
1122 expect(typeParameter.name, typeParameterName); 1717 expect(typeParameter.name, typeParameterName);
1123 } 1718 }
1124 1719
1125 void test_visitFunctionExpression() {
1126 ElementHolder holder = new ElementHolder();
1127 ElementBuilder builder = _makeBuilder(holder);
1128 FunctionExpression expression = AstFactory.functionExpression2(
1129 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2());
1130 expression.accept(builder);
1131 List<FunctionElement> functions = holder.functions;
1132 expect(functions, hasLength(1));
1133 FunctionElement function = functions[0];
1134 expect(function, isNotNull);
1135 expect(expression.element, same(function));
1136 expect(function.hasImplicitReturnType, isTrue);
1137 expect(function.isSynthetic, isFalse);
1138 expect(function.typeParameters, hasLength(0));
1139 }
1140
1141 void test_visitFunctionExpression_inBlockBody() {
1142 List<FunctionElement> functions =
1143 buildElementsForText('f() { return () => 42; }').functions[0].functions;
1144 expect(functions, hasLength(1));
1145 FunctionElement function = functions[0];
1146 expect(function, isNotNull);
1147 expect(function.hasImplicitReturnType, isTrue);
1148 expect(function.isSynthetic, isFalse);
1149 expect(function.typeParameters, hasLength(0));
1150 }
1151
1152 void test_visitFunctionExpression_inExpressionBody() {
1153 List<FunctionElement> functions =
1154 buildElementsForText('f() => () => 42;').functions[0].functions;
1155 expect(functions, hasLength(1));
1156 FunctionElement function = functions[0];
1157 expect(function, isNotNull);
1158 expect(function.hasImplicitReturnType, isTrue);
1159 expect(function.isSynthetic, isFalse);
1160 expect(function.typeParameters, hasLength(0));
1161 }
1162
1163 void test_visitFunctionTypeAlias() {
1164 ElementHolder holder = new ElementHolder();
1165 ElementBuilder builder = _makeBuilder(holder);
1166 String aliasName = "F";
1167 String parameterName = "E";
1168 FunctionTypeAlias aliasNode = AstFactory.typeAlias(
1169 null, aliasName, AstFactory.typeParameterList([parameterName]), null);
1170 aliasNode.documentationComment = AstFactory.documentationComment(
1171 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
1172 aliasNode.endToken.offset = 80;
1173 aliasNode.accept(builder);
1174
1175 List<FunctionTypeAliasElement> aliases = holder.typeAliases;
1176 expect(aliases, hasLength(1));
1177 FunctionTypeAliasElement alias = aliases[0];
1178 expect(alias, isNotNull);
1179 _assertHasCodeRange(alias, 50, 31);
1180 expect(alias.documentationComment, '/// aaa');
1181 expect(alias.name, aliasName);
1182 expect(alias.parameters, hasLength(0));
1183 List<TypeParameterElement> typeParameters = alias.typeParameters;
1184 expect(typeParameters, hasLength(1));
1185 TypeParameterElement typeParameter = typeParameters[0];
1186 expect(typeParameter, isNotNull);
1187 expect(typeParameter.name, parameterName);
1188 }
1189
1190 void test_visitFunctionTypedFormalParameter() {
1191 ElementHolder holder = new ElementHolder();
1192 ElementBuilder builder = _makeBuilder(holder);
1193 String parameterName = "p";
1194 FunctionTypedFormalParameter formalParameter =
1195 AstFactory.functionTypedFormalParameter(null, parameterName);
1196 _useParameterInMethod(formalParameter, 100, 110);
1197 formalParameter.accept(builder);
1198 List<ParameterElement> parameters = holder.parameters;
1199 expect(parameters, hasLength(1));
1200 ParameterElement parameter = parameters[0];
1201 expect(parameter, isNotNull);
1202 expect(parameter.name, parameterName);
1203 expect(parameter.initializer, isNull);
1204 expect(parameter.isConst, isFalse);
1205 expect(parameter.isFinal, isFalse);
1206 expect(parameter.isSynthetic, isFalse);
1207 expect(parameter.parameterKind, ParameterKind.REQUIRED);
1208 _assertVisibleRange(parameter, 100, 110);
1209 }
1210
1211 void test_visitFunctionTypedFormalParameter_withTypeParameters() {
1212 ElementHolder holder = new ElementHolder();
1213 ElementBuilder builder = _makeBuilder(holder);
1214 String parameterName = "p";
1215 FunctionTypedFormalParameter formalParameter =
1216 AstFactory.functionTypedFormalParameter(null, parameterName);
1217 formalParameter.typeParameters = AstFactory.typeParameterList(['F']);
1218 _useParameterInMethod(formalParameter, 100, 110);
1219 formalParameter.accept(builder);
1220 List<ParameterElement> parameters = holder.parameters;
1221 expect(parameters, hasLength(1));
1222 ParameterElement parameter = parameters[0];
1223 expect(parameter, isNotNull);
1224 expect(parameter.name, parameterName);
1225 expect(parameter.initializer, isNull);
1226 expect(parameter.isConst, isFalse);
1227 expect(parameter.isFinal, isFalse);
1228 expect(parameter.isSynthetic, isFalse);
1229 expect(parameter.parameterKind, ParameterKind.REQUIRED);
1230 expect(parameter.typeParameters, hasLength(1));
1231 _assertVisibleRange(parameter, 100, 110);
1232 }
1233
1234 void test_visitLabeledStatement() {
1235 List<LabelElement> labels =
1236 buildElementsForText('f() { l: print(42); }').functions[0].labels;
1237 expect(labels, hasLength(1));
1238 LabelElement label = labels[0];
1239 expect(label, isNotNull);
1240 expect(label.name, 'l');
1241 expect(label.isSynthetic, isFalse);
1242 }
1243
1244 void test_visitMethodDeclaration_abstract() { 1720 void test_visitMethodDeclaration_abstract() {
1245 // m(); 1721 // m();
1246 ElementHolder holder = new ElementHolder();
1247 ElementBuilder builder = _makeBuilder(holder);
1248 String methodName = "m"; 1722 String methodName = "m";
1249 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 1723 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1250 null, 1724 null,
1251 null, 1725 null,
1252 null, 1726 null,
1253 null, 1727 null,
1254 AstFactory.identifier3(methodName), 1728 AstFactory.identifier3(methodName),
1255 AstFactory.formalParameterList(), 1729 AstFactory.formalParameterList(),
1256 AstFactory.emptyFunctionBody()); 1730 AstFactory.emptyFunctionBody());
1257 methodDeclaration.accept(builder);
1258 1731
1732 ElementHolder holder = buildElementsForAst(methodDeclaration);
1259 List<MethodElement> methods = holder.methods; 1733 List<MethodElement> methods = holder.methods;
1260 expect(methods, hasLength(1)); 1734 expect(methods, hasLength(1));
1261 MethodElement method = methods[0]; 1735 MethodElement method = methods[0];
1262 expect(method, isNotNull); 1736 expect(method, isNotNull);
1263 expect(method.hasImplicitReturnType, isTrue); 1737 expect(method.hasImplicitReturnType, isTrue);
1264 expect(method.name, methodName); 1738 expect(method.name, methodName);
1265 expect(method.functions, hasLength(0)); 1739 expect(method.functions, hasLength(0));
1266 expect(method.labels, hasLength(0)); 1740 expect(method.labels, hasLength(0));
1267 expect(method.localVariables, hasLength(0)); 1741 expect(method.localVariables, hasLength(0));
1268 expect(method.parameters, hasLength(0)); 1742 expect(method.parameters, hasLength(0));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1305 // class members nodes and their elements 1779 // class members nodes and their elements
1306 FieldDeclaration fieldDeclNode = classNode.members[0]; 1780 FieldDeclaration fieldDeclNode = classNode.members[0];
1307 VariableDeclaration fieldNode = fieldDeclNode.fields.variables.single; 1781 VariableDeclaration fieldNode = fieldDeclNode.fields.variables.single;
1308 MethodDeclaration getterNode = classNode.members[1]; 1782 MethodDeclaration getterNode = classNode.members[1];
1309 expect(fieldNode.element, notSyntheticFieldElement); 1783 expect(fieldNode.element, notSyntheticFieldElement);
1310 expect(getterNode.element, notSyntheticGetterElement); 1784 expect(getterNode.element, notSyntheticGetterElement);
1311 } 1785 }
1312 1786
1313 void test_visitMethodDeclaration_external() { 1787 void test_visitMethodDeclaration_external() {
1314 // external m(); 1788 // external m();
1315 ElementHolder holder = new ElementHolder();
1316 ElementBuilder builder = _makeBuilder(holder);
1317 String methodName = "m"; 1789 String methodName = "m";
1318 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 1790 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1319 null, 1791 null,
1320 null, 1792 null,
1321 null, 1793 null,
1322 null, 1794 null,
1323 AstFactory.identifier3(methodName), 1795 AstFactory.identifier3(methodName),
1324 AstFactory.formalParameterList(), 1796 AstFactory.formalParameterList(),
1325 AstFactory.emptyFunctionBody()); 1797 AstFactory.emptyFunctionBody());
1326 methodDeclaration.externalKeyword = 1798 methodDeclaration.externalKeyword =
1327 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); 1799 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
1328 methodDeclaration.accept(builder);
1329 1800
1801 ElementHolder holder = buildElementsForAst(methodDeclaration);
1330 List<MethodElement> methods = holder.methods; 1802 List<MethodElement> methods = holder.methods;
1331 expect(methods, hasLength(1)); 1803 expect(methods, hasLength(1));
1332 MethodElement method = methods[0]; 1804 MethodElement method = methods[0];
1333 expect(method, isNotNull); 1805 expect(method, isNotNull);
1334 expect(method.hasImplicitReturnType, isTrue); 1806 expect(method.hasImplicitReturnType, isTrue);
1335 expect(method.name, methodName); 1807 expect(method.name, methodName);
1336 expect(method.functions, hasLength(0)); 1808 expect(method.functions, hasLength(0));
1337 expect(method.labels, hasLength(0)); 1809 expect(method.labels, hasLength(0));
1338 expect(method.localVariables, hasLength(0)); 1810 expect(method.localVariables, hasLength(0));
1339 expect(method.parameters, hasLength(0)); 1811 expect(method.parameters, hasLength(0));
1340 expect(method.typeParameters, hasLength(0)); 1812 expect(method.typeParameters, hasLength(0));
1341 expect(method.isAbstract, isFalse); 1813 expect(method.isAbstract, isFalse);
1342 expect(method.isExternal, isTrue); 1814 expect(method.isExternal, isTrue);
1343 expect(method.isStatic, isFalse); 1815 expect(method.isStatic, isFalse);
1344 expect(method.isSynthetic, isFalse); 1816 expect(method.isSynthetic, isFalse);
1345 } 1817 }
1346 1818
1347 void test_visitMethodDeclaration_getter() { 1819 void test_visitMethodDeclaration_getter() {
1348 // get m() {} 1820 // get m() {}
1349 ElementHolder holder = new ElementHolder();
1350 ElementBuilder builder = _makeBuilder(holder);
1351 String methodName = "m"; 1821 String methodName = "m";
1352 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 1822 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1353 null, 1823 null,
1354 null, 1824 null,
1355 Keyword.GET, 1825 Keyword.GET,
1356 null, 1826 null,
1357 AstFactory.identifier3(methodName), 1827 AstFactory.identifier3(methodName),
1358 AstFactory.formalParameterList(), 1828 AstFactory.formalParameterList(),
1359 AstFactory.blockFunctionBody2()); 1829 AstFactory.blockFunctionBody2());
1360 methodDeclaration.documentationComment = AstFactory.documentationComment( 1830 methodDeclaration.documentationComment = AstFactory.documentationComment(
1361 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1831 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
1362 methodDeclaration.endToken.offset = 80; 1832 methodDeclaration.endToken.offset = 80;
1363 methodDeclaration.accept(builder);
1364 1833
1834 ElementHolder holder = buildElementsForAst(methodDeclaration);
1365 List<FieldElement> fields = holder.fields; 1835 List<FieldElement> fields = holder.fields;
1366 expect(fields, hasLength(1)); 1836 expect(fields, hasLength(1));
1367 FieldElement field = fields[0]; 1837 FieldElement field = fields[0];
1368 expect(field, isNotNull); 1838 expect(field, isNotNull);
1369 expect(field.name, methodName); 1839 expect(field.name, methodName);
1370 expect(field.isSynthetic, isTrue); 1840 expect(field.isSynthetic, isTrue);
1371 expect(field.setter, isNull); 1841 expect(field.setter, isNull);
1372 PropertyAccessorElement getter = field.getter; 1842 PropertyAccessorElement getter = field.getter;
1373 expect(getter, isNotNull); 1843 expect(getter, isNotNull);
1374 _assertHasCodeRange(getter, 50, 31); 1844 assertHasCodeRange(getter, 50, 31);
1375 expect(getter.documentationComment, '/// aaa'); 1845 expect(getter.documentationComment, '/// aaa');
1376 expect(getter.hasImplicitReturnType, isTrue); 1846 expect(getter.hasImplicitReturnType, isTrue);
1377 expect(getter.isAbstract, isFalse); 1847 expect(getter.isAbstract, isFalse);
1378 expect(getter.isExternal, isFalse); 1848 expect(getter.isExternal, isFalse);
1379 expect(getter.isGetter, isTrue); 1849 expect(getter.isGetter, isTrue);
1380 expect(getter.isSynthetic, isFalse); 1850 expect(getter.isSynthetic, isFalse);
1381 expect(getter.name, methodName); 1851 expect(getter.name, methodName);
1382 expect(getter.variable, field); 1852 expect(getter.variable, field);
1383 expect(getter.functions, hasLength(0)); 1853 expect(getter.functions, hasLength(0));
1384 expect(getter.labels, hasLength(0)); 1854 expect(getter.labels, hasLength(0));
1385 expect(getter.localVariables, hasLength(0)); 1855 expect(getter.localVariables, hasLength(0));
1386 expect(getter.parameters, hasLength(0)); 1856 expect(getter.parameters, hasLength(0));
1387 } 1857 }
1388 1858
1389 void test_visitMethodDeclaration_getter_abstract() { 1859 void test_visitMethodDeclaration_getter_abstract() {
1390 // get m(); 1860 // get m();
1391 ElementHolder holder = new ElementHolder();
1392 ElementBuilder builder = _makeBuilder(holder);
1393 String methodName = "m"; 1861 String methodName = "m";
1394 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 1862 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1395 null, 1863 null,
1396 null, 1864 null,
1397 Keyword.GET, 1865 Keyword.GET,
1398 null, 1866 null,
1399 AstFactory.identifier3(methodName), 1867 AstFactory.identifier3(methodName),
1400 AstFactory.formalParameterList(), 1868 AstFactory.formalParameterList(),
1401 AstFactory.emptyFunctionBody()); 1869 AstFactory.emptyFunctionBody());
1402 methodDeclaration.accept(builder);
1403 1870
1871 ElementHolder holder = buildElementsForAst(methodDeclaration);
1404 List<FieldElement> fields = holder.fields; 1872 List<FieldElement> fields = holder.fields;
1405 expect(fields, hasLength(1)); 1873 expect(fields, hasLength(1));
1406 FieldElement field = fields[0]; 1874 FieldElement field = fields[0];
1407 expect(field, isNotNull); 1875 expect(field, isNotNull);
1408 expect(field.name, methodName); 1876 expect(field.name, methodName);
1409 expect(field.isSynthetic, isTrue); 1877 expect(field.isSynthetic, isTrue);
1410 expect(field.setter, isNull); 1878 expect(field.setter, isNull);
1411 PropertyAccessorElement getter = field.getter; 1879 PropertyAccessorElement getter = field.getter;
1412 expect(getter, isNotNull); 1880 expect(getter, isNotNull);
1413 expect(getter.hasImplicitReturnType, isTrue); 1881 expect(getter.hasImplicitReturnType, isTrue);
1414 expect(getter.isAbstract, isTrue); 1882 expect(getter.isAbstract, isTrue);
1415 expect(getter.isExternal, isFalse); 1883 expect(getter.isExternal, isFalse);
1416 expect(getter.isGetter, isTrue); 1884 expect(getter.isGetter, isTrue);
1417 expect(getter.isSynthetic, isFalse); 1885 expect(getter.isSynthetic, isFalse);
1418 expect(getter.name, methodName); 1886 expect(getter.name, methodName);
1419 expect(getter.variable, field); 1887 expect(getter.variable, field);
1420 expect(getter.functions, hasLength(0)); 1888 expect(getter.functions, hasLength(0));
1421 expect(getter.labels, hasLength(0)); 1889 expect(getter.labels, hasLength(0));
1422 expect(getter.localVariables, hasLength(0)); 1890 expect(getter.localVariables, hasLength(0));
1423 expect(getter.parameters, hasLength(0)); 1891 expect(getter.parameters, hasLength(0));
1424 } 1892 }
1425 1893
1426 void test_visitMethodDeclaration_getter_external() { 1894 void test_visitMethodDeclaration_getter_external() {
1427 // external get m(); 1895 // external get m();
1428 ElementHolder holder = new ElementHolder();
1429 ElementBuilder builder = _makeBuilder(holder);
1430 String methodName = "m"; 1896 String methodName = "m";
1431 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration( 1897 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration(
1432 null, 1898 null,
1433 null, 1899 null,
1434 Keyword.GET, 1900 Keyword.GET,
1435 null, 1901 null,
1436 AstFactory.identifier3(methodName), 1902 AstFactory.identifier3(methodName),
1437 AstFactory.formalParameterList()); 1903 AstFactory.formalParameterList());
1438 methodDeclaration.externalKeyword = 1904 methodDeclaration.externalKeyword =
1439 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); 1905 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
1440 methodDeclaration.accept(builder);
1441 1906
1907 ElementHolder holder = buildElementsForAst(methodDeclaration);
1442 List<FieldElement> fields = holder.fields; 1908 List<FieldElement> fields = holder.fields;
1443 expect(fields, hasLength(1)); 1909 expect(fields, hasLength(1));
1444 FieldElement field = fields[0]; 1910 FieldElement field = fields[0];
1445 expect(field, isNotNull); 1911 expect(field, isNotNull);
1446 expect(field.name, methodName); 1912 expect(field.name, methodName);
1447 expect(field.isSynthetic, isTrue); 1913 expect(field.isSynthetic, isTrue);
1448 expect(field.setter, isNull); 1914 expect(field.setter, isNull);
1449 PropertyAccessorElement getter = field.getter; 1915 PropertyAccessorElement getter = field.getter;
1450 expect(getter, isNotNull); 1916 expect(getter, isNotNull);
1451 expect(getter.hasImplicitReturnType, isTrue); 1917 expect(getter.hasImplicitReturnType, isTrue);
1452 expect(getter.isAbstract, isFalse); 1918 expect(getter.isAbstract, isFalse);
1453 expect(getter.isExternal, isTrue); 1919 expect(getter.isExternal, isTrue);
1454 expect(getter.isGetter, isTrue); 1920 expect(getter.isGetter, isTrue);
1455 expect(getter.isSynthetic, isFalse); 1921 expect(getter.isSynthetic, isFalse);
1456 expect(getter.name, methodName); 1922 expect(getter.name, methodName);
1457 expect(getter.variable, field); 1923 expect(getter.variable, field);
1458 expect(getter.functions, hasLength(0)); 1924 expect(getter.functions, hasLength(0));
1459 expect(getter.labels, hasLength(0)); 1925 expect(getter.labels, hasLength(0));
1460 expect(getter.localVariables, hasLength(0)); 1926 expect(getter.localVariables, hasLength(0));
1461 expect(getter.parameters, hasLength(0)); 1927 expect(getter.parameters, hasLength(0));
1462 } 1928 }
1463 1929
1464 void test_visitMethodDeclaration_minimal() { 1930 void test_visitMethodDeclaration_minimal() {
1465 // T m() {} 1931 // T m() {}
1466 ElementHolder holder = new ElementHolder();
1467 ElementBuilder builder = _makeBuilder(holder);
1468 String methodName = "m"; 1932 String methodName = "m";
1469 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 1933 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1470 null, 1934 null,
1471 AstFactory.typeName4('T'), 1935 AstFactory.typeName4('T'),
1472 null, 1936 null,
1473 null, 1937 null,
1474 AstFactory.identifier3(methodName), 1938 AstFactory.identifier3(methodName),
1475 AstFactory.formalParameterList(), 1939 AstFactory.formalParameterList(),
1476 AstFactory.blockFunctionBody2()); 1940 AstFactory.blockFunctionBody2());
1477 methodDeclaration.documentationComment = AstFactory.documentationComment( 1941 methodDeclaration.documentationComment = AstFactory.documentationComment(
1478 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 1942 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
1479 methodDeclaration.endToken.offset = 80; 1943 methodDeclaration.endToken.offset = 80;
1480 methodDeclaration.accept(builder); 1944
1481 1945 ElementHolder holder = buildElementsForAst(methodDeclaration);
1482 List<MethodElement> methods = holder.methods; 1946 List<MethodElement> methods = holder.methods;
1483 expect(methods, hasLength(1)); 1947 expect(methods, hasLength(1));
1484 MethodElement method = methods[0]; 1948 MethodElement method = methods[0];
1485 expect(method, isNotNull); 1949 expect(method, isNotNull);
1486 _assertHasCodeRange(method, 50, 31); 1950 assertHasCodeRange(method, 50, 31);
1487 expect(method.documentationComment, '/// aaa'); 1951 expect(method.documentationComment, '/// aaa');
1488 expect(method.hasImplicitReturnType, isFalse); 1952 expect(method.hasImplicitReturnType, isFalse);
1489 expect(method.name, methodName); 1953 expect(method.name, methodName);
1490 expect(method.functions, hasLength(0)); 1954 expect(method.functions, hasLength(0));
1491 expect(method.labels, hasLength(0)); 1955 expect(method.labels, hasLength(0));
1492 expect(method.localVariables, hasLength(0)); 1956 expect(method.localVariables, hasLength(0));
1493 expect(method.parameters, hasLength(0)); 1957 expect(method.parameters, hasLength(0));
1494 expect(method.typeParameters, hasLength(0)); 1958 expect(method.typeParameters, hasLength(0));
1495 expect(method.isAbstract, isFalse); 1959 expect(method.isAbstract, isFalse);
1496 expect(method.isExternal, isFalse); 1960 expect(method.isExternal, isFalse);
1497 expect(method.isStatic, isFalse); 1961 expect(method.isStatic, isFalse);
1498 expect(method.isSynthetic, isFalse); 1962 expect(method.isSynthetic, isFalse);
1499 } 1963 }
1500 1964
1501 void test_visitMethodDeclaration_operator() { 1965 void test_visitMethodDeclaration_operator() {
1502 // operator +(addend) {} 1966 // operator +(addend) {}
1503 ElementHolder holder = new ElementHolder();
1504 ElementBuilder builder = _makeBuilder(holder);
1505 String methodName = "+"; 1967 String methodName = "+";
1506 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 1968 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1507 null, 1969 null,
1508 null, 1970 null,
1509 null, 1971 null,
1510 Keyword.OPERATOR, 1972 Keyword.OPERATOR,
1511 AstFactory.identifier3(methodName), 1973 AstFactory.identifier3(methodName),
1512 AstFactory 1974 AstFactory
1513 .formalParameterList([AstFactory.simpleFormalParameter3("addend")]), 1975 .formalParameterList([AstFactory.simpleFormalParameter3("addend")]),
1514 AstFactory.blockFunctionBody2()); 1976 AstFactory.blockFunctionBody2());
1515 methodDeclaration.accept(builder); 1977
1516 1978 ElementHolder holder = buildElementsForAst(methodDeclaration);
1517 List<MethodElement> methods = holder.methods; 1979 List<MethodElement> methods = holder.methods;
1518 expect(methods, hasLength(1)); 1980 expect(methods, hasLength(1));
1519 MethodElement method = methods[0]; 1981 MethodElement method = methods[0];
1520 expect(method, isNotNull); 1982 expect(method, isNotNull);
1521 expect(method.hasImplicitReturnType, isTrue); 1983 expect(method.hasImplicitReturnType, isTrue);
1522 expect(method.name, methodName); 1984 expect(method.name, methodName);
1523 expect(method.functions, hasLength(0)); 1985 expect(method.functions, hasLength(0));
1524 expect(method.labels, hasLength(0)); 1986 expect(method.labels, hasLength(0));
1525 expect(method.localVariables, hasLength(0)); 1987 expect(method.localVariables, hasLength(0));
1526 expect(method.parameters, hasLength(1)); 1988 expect(method.parameters, hasLength(1));
1527 expect(method.typeParameters, hasLength(0)); 1989 expect(method.typeParameters, hasLength(0));
1528 expect(method.isAbstract, isFalse); 1990 expect(method.isAbstract, isFalse);
1529 expect(method.isExternal, isFalse); 1991 expect(method.isExternal, isFalse);
1530 expect(method.isStatic, isFalse); 1992 expect(method.isStatic, isFalse);
1531 expect(method.isSynthetic, isFalse); 1993 expect(method.isSynthetic, isFalse);
1532 } 1994 }
1533 1995
1534 void test_visitMethodDeclaration_setter() { 1996 void test_visitMethodDeclaration_setter() {
1535 // set m() {} 1997 // set m() {}
1536 ElementHolder holder = new ElementHolder();
1537 ElementBuilder builder = _makeBuilder(holder);
1538 String methodName = "m"; 1998 String methodName = "m";
1539 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 1999 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1540 null, 2000 null,
1541 null, 2001 null,
1542 Keyword.SET, 2002 Keyword.SET,
1543 null, 2003 null,
1544 AstFactory.identifier3(methodName), 2004 AstFactory.identifier3(methodName),
1545 AstFactory.formalParameterList(), 2005 AstFactory.formalParameterList(),
1546 AstFactory.blockFunctionBody2()); 2006 AstFactory.blockFunctionBody2());
1547 methodDeclaration.documentationComment = AstFactory.documentationComment( 2007 methodDeclaration.documentationComment = AstFactory.documentationComment(
1548 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); 2008 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
1549 methodDeclaration.endToken.offset = 80; 2009 methodDeclaration.endToken.offset = 80;
1550 methodDeclaration.accept(builder); 2010
1551 2011 ElementHolder holder = buildElementsForAst(methodDeclaration);
1552 List<FieldElement> fields = holder.fields; 2012 List<FieldElement> fields = holder.fields;
1553 expect(fields, hasLength(1)); 2013 expect(fields, hasLength(1));
1554 FieldElement field = fields[0]; 2014 FieldElement field = fields[0];
1555 expect(field, isNotNull); 2015 expect(field, isNotNull);
1556 expect(field.name, methodName); 2016 expect(field.name, methodName);
1557 expect(field.isSynthetic, isTrue); 2017 expect(field.isSynthetic, isTrue);
1558 expect(field.getter, isNull); 2018 expect(field.getter, isNull);
1559 2019
1560 PropertyAccessorElement setter = field.setter; 2020 PropertyAccessorElement setter = field.setter;
1561 expect(setter, isNotNull); 2021 expect(setter, isNotNull);
1562 _assertHasCodeRange(setter, 50, 31); 2022 assertHasCodeRange(setter, 50, 31);
1563 expect(setter.documentationComment, '/// aaa'); 2023 expect(setter.documentationComment, '/// aaa');
1564 expect(setter.hasImplicitReturnType, isTrue); 2024 expect(setter.hasImplicitReturnType, isTrue);
1565 expect(setter.isAbstract, isFalse); 2025 expect(setter.isAbstract, isFalse);
1566 expect(setter.isExternal, isFalse); 2026 expect(setter.isExternal, isFalse);
1567 expect(setter.isSetter, isTrue); 2027 expect(setter.isSetter, isTrue);
1568 expect(setter.isSynthetic, isFalse); 2028 expect(setter.isSynthetic, isFalse);
1569 expect(setter.name, "$methodName="); 2029 expect(setter.name, "$methodName=");
1570 expect(setter.displayName, methodName); 2030 expect(setter.displayName, methodName);
1571 expect(setter.variable, field); 2031 expect(setter.variable, field);
1572 expect(setter.functions, hasLength(0)); 2032 expect(setter.functions, hasLength(0));
1573 expect(setter.labels, hasLength(0)); 2033 expect(setter.labels, hasLength(0));
1574 expect(setter.localVariables, hasLength(0)); 2034 expect(setter.localVariables, hasLength(0));
1575 expect(setter.parameters, hasLength(0)); 2035 expect(setter.parameters, hasLength(0));
1576 } 2036 }
1577 2037
1578 void test_visitMethodDeclaration_setter_abstract() { 2038 void test_visitMethodDeclaration_setter_abstract() {
1579 // set m(); 2039 // set m();
1580 ElementHolder holder = new ElementHolder();
1581 ElementBuilder builder = _makeBuilder(holder);
1582 String methodName = "m"; 2040 String methodName = "m";
1583 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 2041 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1584 null, 2042 null,
1585 null, 2043 null,
1586 Keyword.SET, 2044 Keyword.SET,
1587 null, 2045 null,
1588 AstFactory.identifier3(methodName), 2046 AstFactory.identifier3(methodName),
1589 AstFactory.formalParameterList(), 2047 AstFactory.formalParameterList(),
1590 AstFactory.emptyFunctionBody()); 2048 AstFactory.emptyFunctionBody());
1591 methodDeclaration.accept(builder); 2049
1592 2050 ElementHolder holder = buildElementsForAst(methodDeclaration);
1593 List<FieldElement> fields = holder.fields; 2051 List<FieldElement> fields = holder.fields;
1594 expect(fields, hasLength(1)); 2052 expect(fields, hasLength(1));
1595 FieldElement field = fields[0]; 2053 FieldElement field = fields[0];
1596 expect(field, isNotNull); 2054 expect(field, isNotNull);
1597 expect(field.name, methodName); 2055 expect(field.name, methodName);
1598 expect(field.isSynthetic, isTrue); 2056 expect(field.isSynthetic, isTrue);
1599 expect(field.getter, isNull); 2057 expect(field.getter, isNull);
1600 PropertyAccessorElement setter = field.setter; 2058 PropertyAccessorElement setter = field.setter;
1601 expect(setter, isNotNull); 2059 expect(setter, isNotNull);
1602 expect(setter.hasImplicitReturnType, isTrue); 2060 expect(setter.hasImplicitReturnType, isTrue);
1603 expect(setter.isAbstract, isTrue); 2061 expect(setter.isAbstract, isTrue);
1604 expect(setter.isExternal, isFalse); 2062 expect(setter.isExternal, isFalse);
1605 expect(setter.isSetter, isTrue); 2063 expect(setter.isSetter, isTrue);
1606 expect(setter.isSynthetic, isFalse); 2064 expect(setter.isSynthetic, isFalse);
1607 expect(setter.name, "$methodName="); 2065 expect(setter.name, "$methodName=");
1608 expect(setter.displayName, methodName); 2066 expect(setter.displayName, methodName);
1609 expect(setter.variable, field); 2067 expect(setter.variable, field);
1610 expect(setter.functions, hasLength(0)); 2068 expect(setter.functions, hasLength(0));
1611 expect(setter.labels, hasLength(0)); 2069 expect(setter.labels, hasLength(0));
1612 expect(setter.localVariables, hasLength(0)); 2070 expect(setter.localVariables, hasLength(0));
1613 expect(setter.parameters, hasLength(0)); 2071 expect(setter.parameters, hasLength(0));
1614 } 2072 }
1615 2073
1616 void test_visitMethodDeclaration_setter_external() { 2074 void test_visitMethodDeclaration_setter_external() {
1617 // external m(); 2075 // external m();
1618 ElementHolder holder = new ElementHolder();
1619 ElementBuilder builder = _makeBuilder(holder);
1620 String methodName = "m"; 2076 String methodName = "m";
1621 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration( 2077 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration(
1622 null, 2078 null,
1623 null, 2079 null,
1624 Keyword.SET, 2080 Keyword.SET,
1625 null, 2081 null,
1626 AstFactory.identifier3(methodName), 2082 AstFactory.identifier3(methodName),
1627 AstFactory.formalParameterList()); 2083 AstFactory.formalParameterList());
1628 methodDeclaration.externalKeyword = 2084 methodDeclaration.externalKeyword =
1629 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); 2085 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL);
1630 methodDeclaration.accept(builder); 2086
1631 2087 ElementHolder holder = buildElementsForAst(methodDeclaration);
1632 List<FieldElement> fields = holder.fields; 2088 List<FieldElement> fields = holder.fields;
1633 expect(fields, hasLength(1)); 2089 expect(fields, hasLength(1));
1634 FieldElement field = fields[0]; 2090 FieldElement field = fields[0];
1635 expect(field, isNotNull); 2091 expect(field, isNotNull);
1636 expect(field.name, methodName); 2092 expect(field.name, methodName);
1637 expect(field.isSynthetic, isTrue); 2093 expect(field.isSynthetic, isTrue);
1638 expect(field.getter, isNull); 2094 expect(field.getter, isNull);
1639 PropertyAccessorElement setter = field.setter; 2095 PropertyAccessorElement setter = field.setter;
1640 expect(setter, isNotNull); 2096 expect(setter, isNotNull);
1641 expect(setter.hasImplicitReturnType, isTrue); 2097 expect(setter.hasImplicitReturnType, isTrue);
1642 expect(setter.isAbstract, isFalse); 2098 expect(setter.isAbstract, isFalse);
1643 expect(setter.isExternal, isTrue); 2099 expect(setter.isExternal, isTrue);
1644 expect(setter.isSetter, isTrue); 2100 expect(setter.isSetter, isTrue);
1645 expect(setter.isSynthetic, isFalse); 2101 expect(setter.isSynthetic, isFalse);
1646 expect(setter.name, "$methodName="); 2102 expect(setter.name, "$methodName=");
1647 expect(setter.displayName, methodName); 2103 expect(setter.displayName, methodName);
1648 expect(setter.variable, field); 2104 expect(setter.variable, field);
1649 expect(setter.functions, hasLength(0)); 2105 expect(setter.functions, hasLength(0));
1650 expect(setter.labels, hasLength(0)); 2106 expect(setter.labels, hasLength(0));
1651 expect(setter.localVariables, hasLength(0)); 2107 expect(setter.localVariables, hasLength(0));
1652 expect(setter.parameters, hasLength(0)); 2108 expect(setter.parameters, hasLength(0));
1653 } 2109 }
1654 2110
1655 void test_visitMethodDeclaration_static() { 2111 void test_visitMethodDeclaration_static() {
1656 // static m() {} 2112 // static m() {}
1657 ElementHolder holder = new ElementHolder();
1658 ElementBuilder builder = _makeBuilder(holder);
1659 String methodName = "m"; 2113 String methodName = "m";
1660 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 2114 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1661 Keyword.STATIC, 2115 Keyword.STATIC,
1662 null, 2116 null,
1663 null, 2117 null,
1664 null, 2118 null,
1665 AstFactory.identifier3(methodName), 2119 AstFactory.identifier3(methodName),
1666 AstFactory.formalParameterList(), 2120 AstFactory.formalParameterList(),
1667 AstFactory.blockFunctionBody2()); 2121 AstFactory.blockFunctionBody2());
1668 methodDeclaration.accept(builder); 2122 ElementHolder holder = buildElementsForAst(methodDeclaration);
1669 List<MethodElement> methods = holder.methods; 2123 List<MethodElement> methods = holder.methods;
1670 expect(methods, hasLength(1)); 2124 expect(methods, hasLength(1));
1671 MethodElement method = methods[0]; 2125 MethodElement method = methods[0];
1672 expect(method, isNotNull); 2126 expect(method, isNotNull);
1673 expect(method.hasImplicitReturnType, isTrue); 2127 expect(method.hasImplicitReturnType, isTrue);
1674 expect(method.name, methodName); 2128 expect(method.name, methodName);
1675 expect(method.functions, hasLength(0)); 2129 expect(method.functions, hasLength(0));
1676 expect(method.labels, hasLength(0)); 2130 expect(method.labels, hasLength(0));
1677 expect(method.localVariables, hasLength(0)); 2131 expect(method.localVariables, hasLength(0));
1678 expect(method.parameters, hasLength(0)); 2132 expect(method.parameters, hasLength(0));
1679 expect(method.typeParameters, hasLength(0)); 2133 expect(method.typeParameters, hasLength(0));
1680 expect(method.isAbstract, isFalse); 2134 expect(method.isAbstract, isFalse);
1681 expect(method.isExternal, isFalse); 2135 expect(method.isExternal, isFalse);
1682 expect(method.isStatic, isTrue); 2136 expect(method.isStatic, isTrue);
1683 expect(method.isSynthetic, isFalse); 2137 expect(method.isSynthetic, isFalse);
1684 } 2138 }
1685 2139
1686 void test_visitMethodDeclaration_typeParameters() { 2140 void test_visitMethodDeclaration_typeParameters() {
1687 // m<E>() {} 2141 // m<E>() {}
1688 ElementHolder holder = new ElementHolder();
1689 ElementBuilder builder = _makeBuilder(holder);
1690 String methodName = "m"; 2142 String methodName = "m";
1691 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( 2143 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2(
1692 null, 2144 null,
1693 null, 2145 null,
1694 null, 2146 null,
1695 null, 2147 null,
1696 AstFactory.identifier3(methodName), 2148 AstFactory.identifier3(methodName),
1697 AstFactory.formalParameterList(), 2149 AstFactory.formalParameterList(),
1698 AstFactory.blockFunctionBody2()); 2150 AstFactory.blockFunctionBody2());
1699 methodDeclaration.typeParameters = AstFactory.typeParameterList(['E']); 2151 methodDeclaration.typeParameters = AstFactory.typeParameterList(['E']);
1700 methodDeclaration.accept(builder); 2152
1701 2153 ElementHolder holder = buildElementsForAst(methodDeclaration);
1702 List<MethodElement> methods = holder.methods; 2154 List<MethodElement> methods = holder.methods;
1703 expect(methods, hasLength(1)); 2155 expect(methods, hasLength(1));
1704 MethodElement method = methods[0]; 2156 MethodElement method = methods[0];
1705 expect(method, isNotNull); 2157 expect(method, isNotNull);
1706 expect(method.hasImplicitReturnType, isTrue); 2158 expect(method.hasImplicitReturnType, isTrue);
1707 expect(method.name, methodName); 2159 expect(method.name, methodName);
1708 expect(method.functions, hasLength(0)); 2160 expect(method.functions, hasLength(0));
1709 expect(method.labels, hasLength(0)); 2161 expect(method.labels, hasLength(0));
1710 expect(method.localVariables, hasLength(0)); 2162 expect(method.localVariables, hasLength(0));
1711 expect(method.parameters, hasLength(0)); 2163 expect(method.parameters, hasLength(0));
1712 expect(method.typeParameters, hasLength(1)); 2164 expect(method.typeParameters, hasLength(1));
1713 expect(method.isAbstract, isFalse); 2165 expect(method.isAbstract, isFalse);
1714 expect(method.isExternal, isFalse); 2166 expect(method.isExternal, isFalse);
1715 expect(method.isStatic, isFalse); 2167 expect(method.isStatic, isFalse);
1716 expect(method.isSynthetic, isFalse); 2168 expect(method.isSynthetic, isFalse);
1717 } 2169 }
1718 2170
1719 void test_visitMethodDeclaration_withMembers() {
1720 MethodElement method = buildElementsForText(
1721 'class C { m(p) { var v; try { l: return; } catch (e) {} } }')
1722 .types[0]
1723 .methods[0];
1724 String methodName = "m";
1725 String parameterName = "p";
1726 String localVariableName = "v";
1727 String labelName = "l";
1728 String exceptionParameterName = "e";
1729 expect(method, isNotNull);
1730 expect(method.hasImplicitReturnType, isTrue);
1731 expect(method.name, methodName);
1732 expect(method.typeParameters, hasLength(0));
1733 expect(method.isAbstract, isFalse);
1734 expect(method.isExternal, isFalse);
1735 expect(method.isStatic, isFalse);
1736 expect(method.isSynthetic, isFalse);
1737 List<VariableElement> parameters = method.parameters;
1738 expect(parameters, hasLength(1));
1739 VariableElement parameter = parameters[0];
1740 expect(parameter, isNotNull);
1741 expect(parameter.name, parameterName);
1742 List<VariableElement> localVariables = method.localVariables;
1743 expect(localVariables, hasLength(2));
1744 VariableElement firstVariable = localVariables[0];
1745 VariableElement secondVariable = localVariables[1];
1746 expect(firstVariable, isNotNull);
1747 expect(secondVariable, isNotNull);
1748 expect(
1749 (firstVariable.name == localVariableName &&
1750 secondVariable.name == exceptionParameterName) ||
1751 (firstVariable.name == exceptionParameterName &&
1752 secondVariable.name == localVariableName),
1753 isTrue);
1754 List<LabelElement> labels = method.labels;
1755 expect(labels, hasLength(1));
1756 LabelElement label = labels[0];
1757 expect(label, isNotNull);
1758 expect(label.name, labelName);
1759 }
1760
1761 void test_visitNamedFormalParameter() {
1762 ElementHolder holder = new ElementHolder();
1763 ElementBuilder builder = _makeBuilder(holder);
1764 String parameterName = "p";
1765 DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter(
1766 AstFactory.simpleFormalParameter3(parameterName),
1767 AstFactory.identifier3("42"));
1768 _useParameterInMethod(formalParameter, 100, 110);
1769 formalParameter.beginToken.offset = 50;
1770 formalParameter.endToken.offset = 80;
1771 formalParameter.accept(builder);
1772 List<ParameterElement> parameters = holder.parameters;
1773 expect(parameters, hasLength(1));
1774 ParameterElement parameter = parameters[0];
1775 expect(parameter, isNotNull);
1776 _assertHasCodeRange(parameter, 50, 32);
1777 expect(parameter.name, parameterName);
1778 expect(parameter.isConst, isFalse);
1779 expect(parameter.isFinal, isFalse);
1780 expect(parameter.isSynthetic, isFalse);
1781 expect(parameter.parameterKind, ParameterKind.NAMED);
1782 _assertVisibleRange(parameter, 100, 110);
1783 expect(parameter.defaultValueCode, "42");
1784 FunctionElement initializer = parameter.initializer;
1785 expect(initializer, isNotNull);
1786 expect(initializer.isSynthetic, isTrue);
1787 expect(initializer.hasImplicitReturnType, isTrue);
1788 }
1789
1790 void test_visitSimpleFormalParameter_noType() {
1791 // p
1792 ElementHolder holder = new ElementHolder();
1793 ElementBuilder builder = _makeBuilder(holder);
1794 String parameterName = "p";
1795 SimpleFormalParameter formalParameter =
1796 AstFactory.simpleFormalParameter3(parameterName);
1797 _useParameterInMethod(formalParameter, 100, 110);
1798 formalParameter.accept(builder);
1799 List<ParameterElement> parameters = holder.parameters;
1800 expect(parameters, hasLength(1));
1801 ParameterElement parameter = parameters[0];
1802 expect(parameter, isNotNull);
1803 expect(parameter.hasImplicitType, isTrue);
1804 expect(parameter.initializer, isNull);
1805 expect(parameter.isConst, isFalse);
1806 expect(parameter.isFinal, isFalse);
1807 expect(parameter.isSynthetic, isFalse);
1808 expect(parameter.name, parameterName);
1809 expect(parameter.parameterKind, ParameterKind.REQUIRED);
1810 _assertVisibleRange(parameter, 100, 110);
1811 }
1812
1813 void test_visitSimpleFormalParameter_type() {
1814 // T p
1815 ElementHolder holder = new ElementHolder();
1816 ElementBuilder builder = _makeBuilder(holder);
1817 String parameterName = "p";
1818 SimpleFormalParameter formalParameter = AstFactory.simpleFormalParameter4(
1819 AstFactory.typeName4('T'), parameterName);
1820 _useParameterInMethod(formalParameter, 100, 110);
1821 formalParameter.accept(builder);
1822 List<ParameterElement> parameters = holder.parameters;
1823 expect(parameters, hasLength(1));
1824 ParameterElement parameter = parameters[0];
1825 expect(parameter, isNotNull);
1826 expect(parameter.hasImplicitType, isFalse);
1827 expect(parameter.initializer, isNull);
1828 expect(parameter.isConst, isFalse);
1829 expect(parameter.isFinal, isFalse);
1830 expect(parameter.isSynthetic, isFalse);
1831 expect(parameter.name, parameterName);
1832 expect(parameter.parameterKind, ParameterKind.REQUIRED);
1833 _assertVisibleRange(parameter, 100, 110);
1834 }
1835
1836 void test_visitTypeAlias_minimal() { 2171 void test_visitTypeAlias_minimal() {
1837 ElementHolder holder = new ElementHolder();
1838 ElementBuilder builder = _makeBuilder(holder);
1839 String aliasName = "F"; 2172 String aliasName = "F";
1840 TypeAlias typeAlias = AstFactory.typeAlias(null, aliasName, null, null); 2173 TypeAlias typeAlias = AstFactory.typeAlias(null, aliasName, null, null);
1841 typeAlias.accept(builder); 2174 ElementHolder holder = buildElementsForAst(typeAlias);
1842 List<FunctionTypeAliasElement> aliases = holder.typeAliases; 2175 List<FunctionTypeAliasElement> aliases = holder.typeAliases;
1843 expect(aliases, hasLength(1)); 2176 expect(aliases, hasLength(1));
1844 FunctionTypeAliasElement alias = aliases[0]; 2177 FunctionTypeAliasElement alias = aliases[0];
1845 expect(alias, isNotNull); 2178 expect(alias, isNotNull);
1846 expect(alias.name, aliasName); 2179 expect(alias.name, aliasName);
1847 expect(alias.type, isNotNull); 2180 expect(alias.type, isNotNull);
1848 expect(alias.isSynthetic, isFalse); 2181 expect(alias.isSynthetic, isFalse);
1849 } 2182 }
1850 2183
1851 void test_visitTypeAlias_withFormalParameters() { 2184 void test_visitTypeAlias_withFormalParameters() {
1852 ElementHolder holder = new ElementHolder();
1853 ElementBuilder builder = _makeBuilder(holder);
1854 String aliasName = "F"; 2185 String aliasName = "F";
1855 String firstParameterName = "x"; 2186 String firstParameterName = "x";
1856 String secondParameterName = "y"; 2187 String secondParameterName = "y";
1857 TypeAlias typeAlias = AstFactory.typeAlias( 2188 TypeAlias typeAlias = AstFactory.typeAlias(
1858 null, 2189 null,
1859 aliasName, 2190 aliasName,
1860 AstFactory.typeParameterList(), 2191 AstFactory.typeParameterList(),
1861 AstFactory.formalParameterList([ 2192 AstFactory.formalParameterList([
1862 AstFactory.simpleFormalParameter3(firstParameterName), 2193 AstFactory.simpleFormalParameter3(firstParameterName),
1863 AstFactory.simpleFormalParameter3(secondParameterName) 2194 AstFactory.simpleFormalParameter3(secondParameterName)
1864 ])); 2195 ]));
1865 typeAlias.beginToken.offset = 50; 2196 typeAlias.beginToken.offset = 50;
1866 typeAlias.endToken.offset = 80; 2197 typeAlias.endToken.offset = 80;
1867 typeAlias.accept(builder); 2198 ElementHolder holder = buildElementsForAst(typeAlias);
1868 List<FunctionTypeAliasElement> aliases = holder.typeAliases; 2199 List<FunctionTypeAliasElement> aliases = holder.typeAliases;
1869 expect(aliases, hasLength(1)); 2200 expect(aliases, hasLength(1));
1870 FunctionTypeAliasElement alias = aliases[0]; 2201 FunctionTypeAliasElement alias = aliases[0];
1871 expect(alias, isNotNull); 2202 expect(alias, isNotNull);
1872 _assertHasCodeRange(alias, 50, 31); 2203 assertHasCodeRange(alias, 50, 31);
1873 expect(alias.name, aliasName); 2204 expect(alias.name, aliasName);
1874 expect(alias.type, isNotNull); 2205 expect(alias.type, isNotNull);
1875 expect(alias.isSynthetic, isFalse); 2206 expect(alias.isSynthetic, isFalse);
1876 List<VariableElement> parameters = alias.parameters; 2207 List<VariableElement> parameters = alias.parameters;
1877 expect(parameters, hasLength(2)); 2208 expect(parameters, hasLength(2));
1878 expect(parameters[0].name, firstParameterName); 2209 expect(parameters[0].name, firstParameterName);
1879 expect(parameters[1].name, secondParameterName); 2210 expect(parameters[1].name, secondParameterName);
1880 List<TypeParameterElement> typeParameters = alias.typeParameters; 2211 List<TypeParameterElement> typeParameters = alias.typeParameters;
1881 expect(typeParameters, isNotNull); 2212 expect(typeParameters, isNotNull);
1882 expect(typeParameters, hasLength(0)); 2213 expect(typeParameters, hasLength(0));
1883 } 2214 }
1884 2215
1885 void test_visitTypeAlias_withTypeParameters() { 2216 void test_visitTypeAlias_withTypeParameters() {
1886 ElementHolder holder = new ElementHolder();
1887 ElementBuilder builder = _makeBuilder(holder);
1888 String aliasName = "F"; 2217 String aliasName = "F";
1889 String firstTypeParameterName = "A"; 2218 String firstTypeParameterName = "A";
1890 String secondTypeParameterName = "B"; 2219 String secondTypeParameterName = "B";
1891 TypeAlias typeAlias = AstFactory.typeAlias( 2220 TypeAlias typeAlias = AstFactory.typeAlias(
1892 null, 2221 null,
1893 aliasName, 2222 aliasName,
1894 AstFactory.typeParameterList( 2223 AstFactory.typeParameterList(
1895 [firstTypeParameterName, secondTypeParameterName]), 2224 [firstTypeParameterName, secondTypeParameterName]),
1896 AstFactory.formalParameterList()); 2225 AstFactory.formalParameterList());
1897 typeAlias.accept(builder); 2226 ElementHolder holder = buildElementsForAst(typeAlias);
1898 List<FunctionTypeAliasElement> aliases = holder.typeAliases; 2227 List<FunctionTypeAliasElement> aliases = holder.typeAliases;
1899 expect(aliases, hasLength(1)); 2228 expect(aliases, hasLength(1));
1900 FunctionTypeAliasElement alias = aliases[0]; 2229 FunctionTypeAliasElement alias = aliases[0];
1901 expect(alias, isNotNull); 2230 expect(alias, isNotNull);
1902 expect(alias.name, aliasName); 2231 expect(alias.name, aliasName);
1903 expect(alias.type, isNotNull); 2232 expect(alias.type, isNotNull);
1904 expect(alias.isSynthetic, isFalse); 2233 expect(alias.isSynthetic, isFalse);
1905 List<VariableElement> parameters = alias.parameters; 2234 List<VariableElement> parameters = alias.parameters;
1906 expect(parameters, isNotNull); 2235 expect(parameters, isNotNull);
1907 expect(parameters, hasLength(0)); 2236 expect(parameters, hasLength(0));
1908 List<TypeParameterElement> typeParameters = alias.typeParameters; 2237 List<TypeParameterElement> typeParameters = alias.typeParameters;
1909 expect(typeParameters, hasLength(2)); 2238 expect(typeParameters, hasLength(2));
1910 expect(typeParameters[0].name, firstTypeParameterName); 2239 expect(typeParameters[0].name, firstTypeParameterName);
1911 expect(typeParameters[1].name, secondTypeParameterName); 2240 expect(typeParameters[1].name, secondTypeParameterName);
1912 } 2241 }
1913 2242
1914 void test_visitTypeParameter() { 2243 void test_visitTypeParameter() {
1915 ElementHolder holder = new ElementHolder();
1916 ElementBuilder builder = _makeBuilder(holder);
1917 String parameterName = "E"; 2244 String parameterName = "E";
1918 TypeParameter typeParameter = AstFactory.typeParameter(parameterName); 2245 TypeParameter typeParameter = AstFactory.typeParameter(parameterName);
1919 typeParameter.beginToken.offset = 50; 2246 typeParameter.beginToken.offset = 50;
1920 typeParameter.accept(builder); 2247 ElementHolder holder = buildElementsForAst(typeParameter);
1921 List<TypeParameterElement> typeParameters = holder.typeParameters; 2248 List<TypeParameterElement> typeParameters = holder.typeParameters;
1922 expect(typeParameters, hasLength(1)); 2249 expect(typeParameters, hasLength(1));
1923 TypeParameterElement typeParameterElement = typeParameters[0]; 2250 TypeParameterElement typeParameterElement = typeParameters[0];
1924 expect(typeParameterElement, isNotNull); 2251 expect(typeParameterElement, isNotNull);
1925 _assertHasCodeRange(typeParameterElement, 50, 1); 2252 assertHasCodeRange(typeParameterElement, 50, 1);
1926 expect(typeParameterElement.name, parameterName); 2253 expect(typeParameterElement.name, parameterName);
1927 expect(typeParameterElement.bound, isNull); 2254 expect(typeParameterElement.bound, isNull);
1928 expect(typeParameterElement.isSynthetic, isFalse); 2255 expect(typeParameterElement.isSynthetic, isFalse);
1929 } 2256 }
1930 2257 }
1931 void test_visitVariableDeclaration_inConstructor() { 2258
1932 List<ConstructorElement> constructors = 2259 abstract class _BaseTest {
1933 buildElementsForText('class C { C() { var v = 1; } }') 2260 CompilationUnitElement compilationUnitElement;
1934 .types[0] 2261 CompilationUnit _compilationUnit;
1935 .constructors; 2262
1936 expect(constructors, hasLength(1)); 2263 CompilationUnit get compilationUnit => _compilationUnit;
1937 List<LocalVariableElement> variableElements = 2264
1938 constructors[0].localVariables; 2265 void assertHasCodeRange(Element element, int offset, int length) {
1939 expect(variableElements, hasLength(1));
1940 LocalVariableElement variableElement = variableElements[0];
1941 _assertHasCodeRange(variableElement, 16, 10);
1942 expect(variableElement.hasImplicitType, isTrue);
1943 expect(variableElement.name, 'v');
1944 _assertVisibleRange(variableElement, 14, 28);
1945 }
1946
1947 void test_visitVariableDeclaration_inForEachStatement() {
1948 ElementHolder holder = new ElementHolder();
1949 ElementBuilder builder = _makeBuilder(holder);
1950 //
1951 // m() { for (var v in []) }
1952 //
1953 String variableName = "v";
1954 Statement statement = AstFactory.forEachStatement(
1955 AstFactory.declaredIdentifier3('v'),
1956 AstFactory.listLiteral(),
1957 AstFactory.block());
1958 _setNodeSourceRange(statement, 100, 110);
1959 MethodDeclaration method = AstFactory.methodDeclaration2(
1960 null,
1961 null,
1962 null,
1963 null,
1964 AstFactory.identifier3("m"),
1965 AstFactory.formalParameterList(),
1966 AstFactory.blockFunctionBody2([statement]));
1967 _setBlockBodySourceRange(method.body, 200, 220);
1968 method.accept(builder);
1969
1970 List<MethodElement> methods = holder.methods;
1971 expect(methods, hasLength(1));
1972 List<LocalVariableElement> variableElements = methods[0].localVariables;
1973 expect(variableElements, hasLength(1));
1974 LocalVariableElement variableElement = variableElements[0];
1975 expect(variableElement.name, variableName);
1976 _assertVisibleRange(variableElement, 100, 110);
1977 }
1978
1979 void test_visitVariableDeclaration_inForStatement() {
1980 ElementHolder holder = new ElementHolder();
1981 ElementBuilder builder = _makeBuilder(holder);
1982 //
1983 // m() { for (T v;;) }
1984 //
1985 String variableName = "v";
1986 ForStatement statement = AstFactory.forStatement2(
1987 AstFactory.variableDeclarationList(null, AstFactory.typeName4('T'),
1988 [AstFactory.variableDeclaration('v')]),
1989 null,
1990 null,
1991 AstFactory.block());
1992 _setNodeSourceRange(statement, 100, 110);
1993 MethodDeclaration method = AstFactory.methodDeclaration2(
1994 null,
1995 null,
1996 null,
1997 null,
1998 AstFactory.identifier3("m"),
1999 AstFactory.formalParameterList(),
2000 AstFactory.blockFunctionBody2([statement]));
2001 _setBlockBodySourceRange(method.body, 200, 220);
2002 method.accept(builder);
2003
2004 List<MethodElement> methods = holder.methods;
2005 expect(methods, hasLength(1));
2006 List<LocalVariableElement> variableElements = methods[0].localVariables;
2007 expect(variableElements, hasLength(1));
2008 LocalVariableElement variableElement = variableElements[0];
2009 expect(variableElement.name, variableName);
2010 _assertVisibleRange(variableElement, 100, 110);
2011 }
2012
2013 void test_visitVariableDeclaration_inMethod() {
2014 ElementHolder holder = new ElementHolder();
2015 ElementBuilder builder = _makeBuilder(holder);
2016 //
2017 // m() {T v;}
2018 //
2019 String variableName = "v";
2020 VariableDeclaration variable =
2021 AstFactory.variableDeclaration2(variableName, null);
2022 Statement statement = AstFactory.variableDeclarationStatement(
2023 null, AstFactory.typeName4('T'), [variable]);
2024 MethodDeclaration method = AstFactory.methodDeclaration2(
2025 null,
2026 null,
2027 null,
2028 null,
2029 AstFactory.identifier3("m"),
2030 AstFactory.formalParameterList(),
2031 AstFactory.blockFunctionBody2([statement]));
2032 _setBlockBodySourceRange(method.body, 100, 110);
2033 method.accept(builder);
2034
2035 List<MethodElement> methods = holder.methods;
2036 expect(methods, hasLength(1));
2037 List<LocalVariableElement> variableElements = methods[0].localVariables;
2038 expect(variableElements, hasLength(1));
2039 LocalVariableElement variableElement = variableElements[0];
2040 expect(variableElement.hasImplicitType, isFalse);
2041 expect(variableElement.name, variableName);
2042 _assertVisibleRange(variableElement, 100, 110);
2043 }
2044
2045 void test_visitVariableDeclaration_localNestedInFunction() {
2046 ElementHolder holder = new ElementHolder();
2047 ElementBuilder builder = _makeBuilder(holder);
2048 //
2049 // var f = () {var v;};
2050 //
2051 String variableName = "v";
2052 VariableDeclaration variable =
2053 AstFactory.variableDeclaration2(variableName, null);
2054 Statement statement =
2055 AstFactory.variableDeclarationStatement2(null, [variable]);
2056 Expression initializer = AstFactory.functionExpression2(
2057 AstFactory.formalParameterList(),
2058 AstFactory.blockFunctionBody2([statement]));
2059 String fieldName = "f";
2060 VariableDeclaration field =
2061 AstFactory.variableDeclaration2(fieldName, initializer);
2062 FieldDeclaration fieldDeclaration =
2063 AstFactory.fieldDeclaration2(false, null, [field]);
2064 fieldDeclaration.accept(builder);
2065
2066 List<FieldElement> variables = holder.fields;
2067 expect(variables, hasLength(1));
2068 FieldElement fieldElement = variables[0];
2069 expect(fieldElement, isNotNull);
2070 FunctionElement initializerElement = fieldElement.initializer;
2071 expect(initializerElement, isNotNull);
2072 expect(initializerElement.hasImplicitReturnType, isTrue);
2073 List<FunctionElement> functionElements = initializerElement.functions;
2074 expect(functionElements, hasLength(1));
2075 List<LocalVariableElement> variableElements =
2076 functionElements[0].localVariables;
2077 expect(variableElements, hasLength(1));
2078 LocalVariableElement variableElement = variableElements[0];
2079 expect(variableElement.hasImplicitType, isTrue);
2080 expect(variableElement.isConst, isFalse);
2081 expect(variableElement.isFinal, isFalse);
2082 expect(variableElement.isSynthetic, isFalse);
2083 expect(variableElement.name, variableName);
2084 }
2085
2086 void test_visitVariableDeclaration_noInitializer() {
2087 // var v;
2088 ElementHolder holder = new ElementHolder();
2089 ElementBuilder builder = _makeBuilder(holder);
2090 String variableName = "v";
2091 VariableDeclaration variableDeclaration =
2092 AstFactory.variableDeclaration2(variableName, null);
2093 AstFactory.variableDeclarationList2(null, [variableDeclaration]);
2094 variableDeclaration.accept(builder);
2095
2096 List<TopLevelVariableElement> variables = holder.topLevelVariables;
2097 expect(variables, hasLength(1));
2098 TopLevelVariableElement variable = variables[0];
2099 expect(variable, isNotNull);
2100 expect(variable.hasImplicitType, isTrue);
2101 expect(variable.initializer, isNull);
2102 expect(variable.name, variableName);
2103 expect(variable.isConst, isFalse);
2104 expect(variable.isFinal, isFalse);
2105 expect(variable.isSynthetic, isFalse);
2106 expect(variable.getter, isNotNull);
2107 expect(variable.setter, isNotNull);
2108 }
2109
2110 void test_visitVariableDeclaration_top() {
2111 // final a, b;
2112 ElementHolder holder = new ElementHolder();
2113 ElementBuilder builder = _makeBuilder(holder);
2114 VariableDeclaration variableDeclaration1 =
2115 AstFactory.variableDeclaration('a');
2116 VariableDeclaration variableDeclaration2 =
2117 AstFactory.variableDeclaration('b');
2118 TopLevelVariableDeclaration topLevelVariableDeclaration = AstFactory
2119 .topLevelVariableDeclaration(
2120 Keyword.FINAL, null, [variableDeclaration1, variableDeclaration2]);
2121 topLevelVariableDeclaration.documentationComment = AstFactory
2122 .documentationComment(
2123 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []);
2124
2125 topLevelVariableDeclaration.accept(builder);
2126 List<TopLevelVariableElement> variables = holder.topLevelVariables;
2127 expect(variables, hasLength(2));
2128
2129 TopLevelVariableElement variable1 = variables[0];
2130 expect(variable1, isNotNull);
2131 expect(variable1.documentationComment, '/// aaa');
2132
2133 TopLevelVariableElement variable2 = variables[1];
2134 expect(variable2, isNotNull);
2135 expect(variable2.documentationComment, '/// aaa');
2136 }
2137
2138 void test_visitVariableDeclaration_top_const_hasInitializer() {
2139 // const v = 42;
2140 ElementHolder holder = new ElementHolder();
2141 ElementBuilder builder = _makeBuilder(holder);
2142 String variableName = "v";
2143 VariableDeclaration variableDeclaration =
2144 AstFactory.variableDeclaration2(variableName, AstFactory.integer(42));
2145 AstFactory.variableDeclarationList2(Keyword.CONST, [variableDeclaration]);
2146 variableDeclaration.accept(builder);
2147
2148 List<TopLevelVariableElement> variables = holder.topLevelVariables;
2149 expect(variables, hasLength(1));
2150 TopLevelVariableElement variable = variables[0];
2151 expect(variable, new isInstanceOf<ConstTopLevelVariableElementImpl>());
2152 expect(variable.initializer, isNotNull);
2153 expect(variable.initializer.type, isNotNull);
2154 expect(variable.initializer.hasImplicitReturnType, isTrue);
2155 expect(variable.name, variableName);
2156 expect(variable.hasImplicitType, isTrue);
2157 expect(variable.isConst, isTrue);
2158 expect(variable.isFinal, isFalse);
2159 expect(variable.isSynthetic, isFalse);
2160 expect(variable.getter, isNotNull);
2161 expect(variable.setter, isNull);
2162 }
2163
2164 void test_visitVariableDeclaration_top_final() {
2165 // final v;
2166 ElementHolder holder = new ElementHolder();
2167 ElementBuilder builder = _makeBuilder(holder);
2168 String variableName = "v";
2169 VariableDeclaration variableDeclaration =
2170 AstFactory.variableDeclaration2(variableName, null);
2171 AstFactory.variableDeclarationList2(Keyword.FINAL, [variableDeclaration]);
2172 variableDeclaration.accept(builder);
2173 List<TopLevelVariableElement> variables = holder.topLevelVariables;
2174 expect(variables, hasLength(1));
2175 TopLevelVariableElement variable = variables[0];
2176 expect(variable, isNotNull);
2177 expect(variable.hasImplicitType, isTrue);
2178 expect(variable.initializer, isNull);
2179 expect(variable.name, variableName);
2180 expect(variable.isConst, isFalse);
2181 expect(variable.isFinal, isTrue);
2182 expect(variable.isSynthetic, isFalse);
2183 expect(variable.getter, isNotNull);
2184 expect(variable.setter, isNull);
2185 }
2186
2187 void _assertHasCodeRange(Element element, int offset, int length) {
2188 ElementImpl elementImpl = element; 2266 ElementImpl elementImpl = element;
2189 expect(elementImpl.codeOffset, offset); 2267 expect(elementImpl.codeOffset, offset);
2190 expect(elementImpl.codeLength, length); 2268 expect(elementImpl.codeLength, length);
2191 } 2269 }
2192 2270
2271 /**
2272 * Build elements using [ApiElementBuilder].
2273 */
2274 ElementHolder buildElementsForAst(AstNode node) {
2275 ElementHolder holder = new ElementHolder();
2276 AstVisitor builder = createElementBuilder(holder);
2277 node.accept(builder);
2278 return holder;
2279 }
2280
2281 /**
2282 * Parse the given [code], and build elements using [ApiElementBuilder].
2283 */
2284 ElementHolder buildElementsForText(String code) {
2285 ElementHolder holder = new ElementHolder();
2286 AstVisitor builder = createElementBuilder(holder);
2287 _visitAstOfCode(code, builder);
2288 return holder;
2289 }
2290
2291 /**
2292 * Verify that the given [metadata] has exactly one annotation, and that its
2293 * [ElementAnnotationImpl] is unresolved.
2294 */
2295 void checkAnnotation(NodeList<Annotation> metadata) {
2296 expect(metadata, hasLength(1));
2297 expect(metadata[0], new isInstanceOf<AnnotationImpl>());
2298 AnnotationImpl annotation = metadata[0];
2299 expect(annotation.elementAnnotation,
2300 new isInstanceOf<ElementAnnotationImpl>());
2301 ElementAnnotationImpl elementAnnotation = annotation.elementAnnotation;
2302 expect(elementAnnotation.element, isNull); // Not yet resolved
2303 expect(elementAnnotation.compilationUnit, isNotNull);
2304 expect(elementAnnotation.compilationUnit, compilationUnitElement);
2305 }
2306
2307 /**
2308 * Verify that the given [element] has exactly one annotation, and that its
2309 * [ElementAnnotationImpl] is unresolved.
2310 */
2311 void checkMetadata(Element element) {
2312 expect(element.metadata, hasLength(1));
2313 expect(element.metadata[0], new isInstanceOf<ElementAnnotationImpl>());
2314 ElementAnnotationImpl elementAnnotation = element.metadata[0];
2315 expect(elementAnnotation.element, isNull); // Not yet resolved
2316 expect(elementAnnotation.compilationUnit, isNotNull);
2317 expect(elementAnnotation.compilationUnit, compilationUnitElement);
2318 }
2319
2320 AstVisitor createElementBuilder(ElementHolder holder);
2321
2322 void setUp() {
2323 compilationUnitElement = new CompilationUnitElementImpl('test.dart');
2324 }
2325
2193 void _assertVisibleRange(LocalElement element, int offset, int end) { 2326 void _assertVisibleRange(LocalElement element, int offset, int end) {
2194 SourceRange visibleRange = element.visibleRange; 2327 SourceRange visibleRange = element.visibleRange;
2195 expect(visibleRange.offset, offset); 2328 expect(visibleRange.offset, offset);
2196 expect(visibleRange.end, end); 2329 expect(visibleRange.end, end);
2197 } 2330 }
2198 2331
2199 ElementBuilder _makeBuilder(ElementHolder holder) => 2332 /**
2200 new ElementBuilder(holder, new CompilationUnitElementImpl('test.dart')); 2333 * Parse the given [code], and visit it with the given [visitor].
2201 2334 * Fail if any error is logged.
2202 void _setBlockBodySourceRange(BlockFunctionBody body, int offset, int end) { 2335 */
2203 _setNodeSourceRange(body.block, offset, end); 2336 void _visitAstOfCode(String code, AstVisitor visitor) {
2204 } 2337 TestLogger logger = new TestLogger();
2205 2338 AnalysisEngine.instance.logger = logger;
2206 void _setNodeSourceRange(AstNode node, int offset, int end) { 2339 try {
2207 node.beginToken.offset = offset; 2340 _compilationUnit = ParserTestCase.parseCompilationUnit(code);
2208 Token endToken = node.endToken; 2341 compilationUnit.accept(visitor);
2209 endToken.offset = end - endToken.length; 2342 } finally {
2210 } 2343 expect(logger.log, hasLength(0));
2211 2344 AnalysisEngine.instance.logger = Logger.NULL;
2212 void _useParameterInMethod( 2345 }
2213 FormalParameter formalParameter, int blockOffset, int blockEnd) {
2214 Block block = AstFactory.block();
2215 block.leftBracket.offset = blockOffset;
2216 block.rightBracket.offset = blockEnd - 1;
2217 BlockFunctionBody body = AstFactory.blockFunctionBody(block);
2218 AstFactory.methodDeclaration2(
2219 null,
2220 null,
2221 null,
2222 null,
2223 AstFactory.identifier3("main"),
2224 AstFactory.formalParameterList([formalParameter]),
2225 body);
2226 } 2346 }
2227 } 2347 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/builder.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698