OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library analyzer.test.src.dart.constant.utilities_test; | 5 library analyzer.test.src.dart.constant.utilities_test; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/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/token.dart'; | 10 import 'package:analyzer/src/dart/ast/token.dart'; |
11 import 'package:analyzer/src/dart/element/element.dart'; | 11 import 'package:analyzer/src/dart/element/element.dart'; |
12 import 'package:analyzer/src/generated/constant.dart'; | 12 import 'package:analyzer/src/generated/constant.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/source_io.dart'; | 16 import 'package:analyzer/src/generated/source_io.dart'; |
17 import 'package:analyzer/src/generated/testing/ast_factory.dart'; | 17 import 'package:analyzer/src/generated/testing/ast_test_factory.dart'; |
18 import 'package:analyzer/src/generated/testing/element_factory.dart'; | 18 import 'package:analyzer/src/generated/testing/element_factory.dart'; |
19 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; | 19 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; |
20 import 'package:analyzer/src/generated/utilities_collection.dart'; | 20 import 'package:analyzer/src/generated/utilities_collection.dart'; |
21 import 'package:analyzer/src/task/dart.dart'; | 21 import 'package:analyzer/src/task/dart.dart'; |
22 import 'package:test/test.dart'; | 22 import 'package:test/test.dart'; |
23 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 23 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
24 | 24 |
25 import '../../../generated/engine_test.dart'; | 25 import '../../../generated/engine_test.dart'; |
26 import '../../../generated/test_support.dart'; | 26 import '../../../generated/test_support.dart'; |
27 | 27 |
(...skipping 21 matching lines...) Expand all Loading... |
49 * Test an annotation that consists solely of an identifier (and hence | 49 * Test an annotation that consists solely of an identifier (and hence |
50 * represents a reference to a compile-time constant variable). | 50 * represents a reference to a compile-time constant variable). |
51 */ | 51 */ |
52 void test_visitAnnotation_constantVariable() { | 52 void test_visitAnnotation_constantVariable() { |
53 CompilationUnitElement compilationUnitElement = | 53 CompilationUnitElement compilationUnitElement = |
54 ElementFactory.compilationUnit('/test.dart', _source)..source = _source; | 54 ElementFactory.compilationUnit('/test.dart', _source)..source = _source; |
55 ElementFactory.library(_context, 'L').definingCompilationUnit = | 55 ElementFactory.library(_context, 'L').definingCompilationUnit = |
56 compilationUnitElement; | 56 compilationUnitElement; |
57 ElementAnnotationImpl elementAnnotation = | 57 ElementAnnotationImpl elementAnnotation = |
58 new ElementAnnotationImpl(compilationUnitElement); | 58 new ElementAnnotationImpl(compilationUnitElement); |
59 _node = elementAnnotation.annotationAst = AstFactory.annotation( | 59 _node = elementAnnotation.annotationAst = AstTestFactory.annotation( |
60 AstFactory.identifier3('x'))..elementAnnotation = elementAnnotation; | 60 AstTestFactory.identifier3('x'))..elementAnnotation = elementAnnotation; |
61 expect(_findAnnotations(), contains(_node)); | 61 expect(_findAnnotations(), contains(_node)); |
62 } | 62 } |
63 | 63 |
64 void test_visitAnnotation_enumConstant() { | 64 void test_visitAnnotation_enumConstant() { |
65 // Analyzer ignores annotations on enum constant declarations. | 65 // Analyzer ignores annotations on enum constant declarations. |
66 Annotation annotation = AstFactory.annotation2( | 66 Annotation annotation = AstTestFactory.annotation2( |
67 AstFactory.identifier3('A'), null, AstFactory.argumentList()); | 67 AstTestFactory.identifier3('A'), null, AstTestFactory.argumentList()); |
68 _node = new EnumConstantDeclaration( | 68 _node = new EnumConstantDeclaration( |
69 null, <Annotation>[annotation], AstFactory.identifier3('C')); | 69 null, <Annotation>[annotation], AstTestFactory.identifier3('C')); |
70 expect(_findConstants(), isEmpty); | 70 expect(_findConstants(), isEmpty); |
71 } | 71 } |
72 | 72 |
73 /** | 73 /** |
74 * Test an annotation that represents the invocation of a constant | 74 * Test an annotation that represents the invocation of a constant |
75 * constructor. | 75 * constructor. |
76 */ | 76 */ |
77 void test_visitAnnotation_invocation() { | 77 void test_visitAnnotation_invocation() { |
78 CompilationUnitElement compilationUnitElement = | 78 CompilationUnitElement compilationUnitElement = |
79 ElementFactory.compilationUnit('/test.dart', _source)..source = _source; | 79 ElementFactory.compilationUnit('/test.dart', _source)..source = _source; |
80 ElementFactory.library(_context, 'L').definingCompilationUnit = | 80 ElementFactory.library(_context, 'L').definingCompilationUnit = |
81 compilationUnitElement; | 81 compilationUnitElement; |
82 ElementAnnotationImpl elementAnnotation = | 82 ElementAnnotationImpl elementAnnotation = |
83 new ElementAnnotationImpl(compilationUnitElement); | 83 new ElementAnnotationImpl(compilationUnitElement); |
84 _node = elementAnnotation.annotationAst = AstFactory.annotation2( | 84 _node = elementAnnotation.annotationAst = AstTestFactory.annotation2( |
85 AstFactory.identifier3('A'), null, AstFactory.argumentList()) | 85 AstTestFactory.identifier3('A'), null, AstTestFactory.argumentList()) |
86 ..elementAnnotation = elementAnnotation; | 86 ..elementAnnotation = elementAnnotation; |
87 expect(_findAnnotations(), contains(_node)); | 87 expect(_findAnnotations(), contains(_node)); |
88 } | 88 } |
89 | 89 |
90 void test_visitAnnotation_partOf() { | 90 void test_visitAnnotation_partOf() { |
91 // Analyzer ignores annotations on "part of" directives. | 91 // Analyzer ignores annotations on "part of" directives. |
92 Annotation annotation = AstFactory.annotation2( | 92 Annotation annotation = AstTestFactory.annotation2( |
93 AstFactory.identifier3('A'), null, AstFactory.argumentList()); | 93 AstTestFactory.identifier3('A'), null, AstTestFactory.argumentList()); |
94 _node = AstFactory.partOfDirective2( | 94 _node = AstTestFactory.partOfDirective2(<Annotation>[annotation], |
95 <Annotation>[annotation], AstFactory.libraryIdentifier2(<String>['L'])); | 95 AstTestFactory.libraryIdentifier2(<String>['L'])); |
96 expect(_findConstants(), isEmpty); | 96 expect(_findConstants(), isEmpty); |
97 } | 97 } |
98 | 98 |
99 void test_visitConstructorDeclaration_const() { | 99 void test_visitConstructorDeclaration_const() { |
100 ConstructorElement element = _setupConstructorDeclaration("A", true); | 100 ConstructorElement element = _setupConstructorDeclaration("A", true); |
101 expect(_findConstants(), contains(element)); | 101 expect(_findConstants(), contains(element)); |
102 } | 102 } |
103 | 103 |
104 void test_visitConstructorDeclaration_nonConst() { | 104 void test_visitConstructorDeclaration_nonConst() { |
105 _setupConstructorDeclaration("A", false); | 105 _setupConstructorDeclaration("A", false); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 ConstantFinder finder = new ConstantFinder(); | 186 ConstantFinder finder = new ConstantFinder(); |
187 _node.accept(finder); | 187 _node.accept(finder); |
188 List<ConstantEvaluationTarget> constants = finder.constantsToCompute; | 188 List<ConstantEvaluationTarget> constants = finder.constantsToCompute; |
189 expect(constants, isNotNull); | 189 expect(constants, isNotNull); |
190 return constants; | 190 return constants; |
191 } | 191 } |
192 | 192 |
193 ConstructorElement _setupConstructorDeclaration(String name, bool isConst) { | 193 ConstructorElement _setupConstructorDeclaration(String name, bool isConst) { |
194 Keyword constKeyword = isConst ? Keyword.CONST : null; | 194 Keyword constKeyword = isConst ? Keyword.CONST : null; |
195 ConstructorDeclaration constructorDeclaration = | 195 ConstructorDeclaration constructorDeclaration = |
196 AstFactory.constructorDeclaration2( | 196 AstTestFactory.constructorDeclaration2( |
197 constKeyword, | 197 constKeyword, |
198 null, | 198 null, |
199 null, | 199 null, |
200 name, | 200 name, |
201 AstFactory.formalParameterList(), | 201 AstTestFactory.formalParameterList(), |
202 null, | 202 null, |
203 AstFactory.blockFunctionBody2()); | 203 AstTestFactory.blockFunctionBody2()); |
204 ClassElement classElement = ElementFactory.classElement2(name); | 204 ClassElement classElement = ElementFactory.classElement2(name); |
205 ConstructorElement element = | 205 ConstructorElement element = |
206 ElementFactory.constructorElement(classElement, name, isConst); | 206 ElementFactory.constructorElement(classElement, name, isConst); |
207 constructorDeclaration.element = element; | 207 constructorDeclaration.element = element; |
208 _node = constructorDeclaration; | 208 _node = constructorDeclaration; |
209 return element; | 209 return element; |
210 } | 210 } |
211 | 211 |
212 VariableDeclaration _setupFieldDeclaration( | 212 VariableDeclaration _setupFieldDeclaration( |
213 String className, String fieldName, Keyword keyword, | 213 String className, String fieldName, Keyword keyword, |
214 {bool isInitialized: true, | 214 {bool isInitialized: true, |
215 bool isStatic: false, | 215 bool isStatic: false, |
216 bool hasConstConstructor: false}) { | 216 bool hasConstConstructor: false}) { |
217 VariableDeclaration variableDeclaration = isInitialized | 217 VariableDeclaration variableDeclaration = isInitialized |
218 ? AstFactory.variableDeclaration2(fieldName, AstFactory.integer(0)) | 218 ? AstTestFactory.variableDeclaration2( |
219 : AstFactory.variableDeclaration(fieldName); | 219 fieldName, AstTestFactory.integer(0)) |
| 220 : AstTestFactory.variableDeclaration(fieldName); |
220 VariableElement fieldElement = ElementFactory.fieldElement( | 221 VariableElement fieldElement = ElementFactory.fieldElement( |
221 fieldName, | 222 fieldName, |
222 isStatic, | 223 isStatic, |
223 keyword == Keyword.FINAL, | 224 keyword == Keyword.FINAL, |
224 keyword == Keyword.CONST, | 225 keyword == Keyword.CONST, |
225 _typeProvider.intType); | 226 _typeProvider.intType); |
226 variableDeclaration.name.staticElement = fieldElement; | 227 variableDeclaration.name.staticElement = fieldElement; |
227 FieldDeclaration fieldDeclaration = AstFactory.fieldDeclaration2( | 228 FieldDeclaration fieldDeclaration = AstTestFactory.fieldDeclaration2( |
228 isStatic, keyword, <VariableDeclaration>[variableDeclaration]); | 229 isStatic, keyword, <VariableDeclaration>[variableDeclaration]); |
229 ClassDeclaration classDeclaration = | 230 ClassDeclaration classDeclaration = AstTestFactory.classDeclaration( |
230 AstFactory.classDeclaration(null, className, null, null, null, null); | 231 null, className, null, null, null, null); |
231 classDeclaration.members.add(fieldDeclaration); | 232 classDeclaration.members.add(fieldDeclaration); |
232 _node = classDeclaration; | 233 _node = classDeclaration; |
233 ClassElementImpl classElement = ElementFactory.classElement2(className); | 234 ClassElementImpl classElement = ElementFactory.classElement2(className); |
234 classElement.fields = <FieldElement>[fieldElement]; | 235 classElement.fields = <FieldElement>[fieldElement]; |
235 classDeclaration.name.staticElement = classElement; | 236 classDeclaration.name.staticElement = classElement; |
236 if (hasConstConstructor) { | 237 if (hasConstConstructor) { |
237 ConstructorDeclaration constructorDeclaration = | 238 ConstructorDeclaration constructorDeclaration = |
238 AstFactory.constructorDeclaration2( | 239 AstTestFactory.constructorDeclaration2( |
239 Keyword.CONST, | 240 Keyword.CONST, |
240 null, | 241 null, |
241 AstFactory.identifier3(className), | 242 AstTestFactory.identifier3(className), |
242 null, | 243 null, |
243 AstFactory.formalParameterList(), | 244 AstTestFactory.formalParameterList(), |
244 null, | 245 null, |
245 AstFactory.blockFunctionBody2()); | 246 AstTestFactory.blockFunctionBody2()); |
246 classDeclaration.members.add(constructorDeclaration); | 247 classDeclaration.members.add(constructorDeclaration); |
247 ConstructorElement constructorElement = | 248 ConstructorElement constructorElement = |
248 ElementFactory.constructorElement(classElement, '', true); | 249 ElementFactory.constructorElement(classElement, '', true); |
249 constructorDeclaration.element = constructorElement; | 250 constructorDeclaration.element = constructorElement; |
250 classElement.constructors = <ConstructorElement>[constructorElement]; | 251 classElement.constructors = <ConstructorElement>[constructorElement]; |
251 } else { | 252 } else { |
252 classElement.constructors = ConstructorElement.EMPTY_LIST; | 253 classElement.constructors = ConstructorElement.EMPTY_LIST; |
253 } | 254 } |
254 return variableDeclaration; | 255 return variableDeclaration; |
255 } | 256 } |
256 | 257 |
257 VariableElement _setupVariableDeclaration( | 258 VariableElement _setupVariableDeclaration( |
258 String name, bool isConst, bool isInitialized, | 259 String name, bool isConst, bool isInitialized, |
259 {isFinal: false}) { | 260 {isFinal: false}) { |
260 VariableDeclaration variableDeclaration = isInitialized | 261 VariableDeclaration variableDeclaration = isInitialized |
261 ? AstFactory.variableDeclaration2(name, AstFactory.integer(0)) | 262 ? AstTestFactory.variableDeclaration2(name, AstTestFactory.integer(0)) |
262 : AstFactory.variableDeclaration(name); | 263 : AstTestFactory.variableDeclaration(name); |
263 SimpleIdentifier identifier = variableDeclaration.name; | 264 SimpleIdentifier identifier = variableDeclaration.name; |
264 VariableElement element = ElementFactory.localVariableElement(identifier); | 265 VariableElement element = ElementFactory.localVariableElement(identifier); |
265 identifier.staticElement = element; | 266 identifier.staticElement = element; |
266 Keyword keyword = isConst ? Keyword.CONST : isFinal ? Keyword.FINAL : null; | 267 Keyword keyword = isConst ? Keyword.CONST : isFinal ? Keyword.FINAL : null; |
267 AstFactory.variableDeclarationList2(keyword, [variableDeclaration]); | 268 AstTestFactory.variableDeclarationList2(keyword, [variableDeclaration]); |
268 _node = variableDeclaration; | 269 _node = variableDeclaration; |
269 return element; | 270 return element; |
270 } | 271 } |
271 } | 272 } |
272 | 273 |
273 @reflectiveTest | 274 @reflectiveTest |
274 class ReferenceFinderTest { | 275 class ReferenceFinderTest { |
275 DirectedGraph<ConstantEvaluationTarget> _referenceGraph; | 276 DirectedGraph<ConstantEvaluationTarget> _referenceGraph; |
276 VariableElement _head; | 277 VariableElement _head; |
277 Element _tail; | 278 Element _tail; |
(...skipping 13 matching lines...) Expand all Loading... |
291 _assertOneArc(_tail); | 292 _assertOneArc(_tail); |
292 } | 293 } |
293 | 294 |
294 void test_visitSuperConstructorInvocation_nonConst() { | 295 void test_visitSuperConstructorInvocation_nonConst() { |
295 _visitNode(_makeTailSuperConstructorInvocation("A", false)); | 296 _visitNode(_makeTailSuperConstructorInvocation("A", false)); |
296 _assertOneArc(_tail); | 297 _assertOneArc(_tail); |
297 } | 298 } |
298 | 299 |
299 void test_visitSuperConstructorInvocation_unresolved() { | 300 void test_visitSuperConstructorInvocation_unresolved() { |
300 SuperConstructorInvocation superConstructorInvocation = | 301 SuperConstructorInvocation superConstructorInvocation = |
301 AstFactory.superConstructorInvocation(); | 302 AstTestFactory.superConstructorInvocation(); |
302 _visitNode(superConstructorInvocation); | 303 _visitNode(superConstructorInvocation); |
303 _assertNoArcs(); | 304 _assertNoArcs(); |
304 } | 305 } |
305 | 306 |
306 void _assertNoArcs() { | 307 void _assertNoArcs() { |
307 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head); | 308 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head); |
308 expect(tails, hasLength(0)); | 309 expect(tails, hasLength(0)); |
309 } | 310 } |
310 | 311 |
311 void _assertOneArc(Element tail) { | 312 void _assertOneArc(Element tail) { |
312 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head); | 313 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head); |
313 expect(tails, hasLength(1)); | 314 expect(tails, hasLength(1)); |
314 expect(tails.first, same(tail)); | 315 expect(tails.first, same(tail)); |
315 } | 316 } |
316 | 317 |
317 ReferenceFinder _createReferenceFinder(ConstantEvaluationTarget source) => | 318 ReferenceFinder _createReferenceFinder(ConstantEvaluationTarget source) => |
318 new ReferenceFinder((ConstantEvaluationTarget dependency) { | 319 new ReferenceFinder((ConstantEvaluationTarget dependency) { |
319 _referenceGraph.addEdge(source, dependency); | 320 _referenceGraph.addEdge(source, dependency); |
320 }); | 321 }); |
321 SuperConstructorInvocation _makeTailSuperConstructorInvocation( | 322 SuperConstructorInvocation _makeTailSuperConstructorInvocation( |
322 String name, bool isConst) { | 323 String name, bool isConst) { |
323 List<ConstructorInitializer> initializers = | 324 List<ConstructorInitializer> initializers = |
324 new List<ConstructorInitializer>(); | 325 new List<ConstructorInitializer>(); |
325 ConstructorDeclaration constructorDeclaration = | 326 ConstructorDeclaration constructorDeclaration = |
326 AstFactory.constructorDeclaration(AstFactory.identifier3(name), null, | 327 AstTestFactory.constructorDeclaration(AstTestFactory.identifier3(name), |
327 AstFactory.formalParameterList(), initializers); | 328 null, AstTestFactory.formalParameterList(), initializers); |
328 if (isConst) { | 329 if (isConst) { |
329 constructorDeclaration.constKeyword = new KeywordToken(Keyword.CONST, 0); | 330 constructorDeclaration.constKeyword = new KeywordToken(Keyword.CONST, 0); |
330 } | 331 } |
331 ClassElementImpl classElement = ElementFactory.classElement2(name); | 332 ClassElementImpl classElement = ElementFactory.classElement2(name); |
332 SuperConstructorInvocation superConstructorInvocation = | 333 SuperConstructorInvocation superConstructorInvocation = |
333 AstFactory.superConstructorInvocation(); | 334 AstTestFactory.superConstructorInvocation(); |
334 ConstructorElementImpl constructorElement = | 335 ConstructorElementImpl constructorElement = |
335 ElementFactory.constructorElement(classElement, name, isConst); | 336 ElementFactory.constructorElement(classElement, name, isConst); |
336 _tail = constructorElement; | 337 _tail = constructorElement; |
337 superConstructorInvocation.staticElement = constructorElement; | 338 superConstructorInvocation.staticElement = constructorElement; |
338 return superConstructorInvocation; | 339 return superConstructorInvocation; |
339 } | 340 } |
340 | 341 |
341 SimpleIdentifier _makeTailVariable(String name, bool isConst) { | 342 SimpleIdentifier _makeTailVariable(String name, bool isConst) { |
342 VariableDeclaration variableDeclaration = | 343 VariableDeclaration variableDeclaration = |
343 AstFactory.variableDeclaration(name); | 344 AstTestFactory.variableDeclaration(name); |
344 ConstLocalVariableElementImpl variableElement = | 345 ConstLocalVariableElementImpl variableElement = |
345 ElementFactory.constLocalVariableElement(name); | 346 ElementFactory.constLocalVariableElement(name); |
346 _tail = variableElement; | 347 _tail = variableElement; |
347 variableElement.isConst = isConst; | 348 variableElement.isConst = isConst; |
348 AstFactory.variableDeclarationList2( | 349 AstTestFactory.variableDeclarationList2( |
349 isConst ? Keyword.CONST : Keyword.VAR, [variableDeclaration]); | 350 isConst ? Keyword.CONST : Keyword.VAR, [variableDeclaration]); |
350 SimpleIdentifier identifier = AstFactory.identifier3(name); | 351 SimpleIdentifier identifier = AstTestFactory.identifier3(name); |
351 identifier.staticElement = variableElement; | 352 identifier.staticElement = variableElement; |
352 return identifier; | 353 return identifier; |
353 } | 354 } |
354 | 355 |
355 void _visitNode(AstNode node) { | 356 void _visitNode(AstNode node) { |
356 node.accept(_createReferenceFinder(_head)); | 357 node.accept(_createReferenceFinder(_head)); |
357 } | 358 } |
358 } | 359 } |
359 | 360 |
360 class _TestAnalysisContext extends TestAnalysisContext { | 361 class _TestAnalysisContext extends TestAnalysisContext { |
361 @override | 362 @override |
362 InternalAnalysisContext getContextFor(Source source) => this; | 363 InternalAnalysisContext getContextFor(Source source) => this; |
363 } | 364 } |
OLD | NEW |