OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.utils; | 5 library analyzer.test.utils; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
9 import 'package:analyzer/dart/ast/visitor.dart'; | |
10 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
11 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
12 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 11 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
| 12 import 'package:analyzer/src/generated/testing/element_search.dart'; |
13 import 'package:front_end/src/base/source.dart'; | 13 import 'package:front_end/src/base/source.dart'; |
14 import 'package:test/test.dart'; | 14 import 'package:test/test.dart'; |
15 | 15 |
16 /** | 16 /** |
17 * Search the [unit] for the [Element]s with the given [name]. | |
18 */ | |
19 List<Element> findElementsByName(CompilationUnit unit, String name) { | |
20 var finder = new _ElementsByNameFinder(name); | |
21 unit.accept(finder); | |
22 return finder.elements; | |
23 } | |
24 | |
25 /** | |
26 * Search the [unit] for the [LocalVariableElement] with the given [name]. | 17 * Search the [unit] for the [LocalVariableElement] with the given [name]. |
27 * Fail if there is not exactly one such variable. | 18 * Fail if there is not exactly one such variable. |
28 */ | 19 */ |
29 LocalVariableElement findLocalVariable(CompilationUnit unit, String name) { | 20 LocalVariableElement findLocalVariable(CompilationUnit unit, String name) { |
30 var finder = new _ElementsByNameFinder(name); | 21 List<Element> elements = findElementsByName(unit, name); |
31 unit.accept(finder); | |
32 List<Element> localVariables = | 22 List<Element> localVariables = |
33 finder.elements.where((e) => e is LocalVariableElement).toList(); | 23 elements.where((e) => e is LocalVariableElement).toList(); |
34 expect(localVariables, hasLength(1)); | 24 expect(localVariables, hasLength(1)); |
35 return localVariables[0]; | 25 return localVariables[0]; |
36 } | 26 } |
37 | 27 |
38 /** | 28 /** |
39 * The type of an assertion which asserts properties of [T]s. | 29 * The type of an assertion which asserts properties of [T]s. |
40 */ | 30 */ |
41 typedef void Asserter<T>(T type); | 31 typedef void Asserter<T>(T type); |
42 | 32 |
43 /** | 33 /** |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 Asserter<DartType> argAssert0, Asserter<DartType> argAssert1) => | 316 Asserter<DartType> argAssert0, Asserter<DartType> argAssert1) => |
327 isInstantiationOf(isMap)([argAssert0, argAssert1]); | 317 isInstantiationOf(isMap)([argAssert0, argAssert1]); |
328 | 318 |
329 /** | 319 /** |
330 * Assert that a type is equal to the [expected]. | 320 * Assert that a type is equal to the [expected]. |
331 */ | 321 */ |
332 Asserter<DartType> isType(DartType expected) => (DartType t) { | 322 Asserter<DartType> isType(DartType expected) => (DartType t) { |
333 expect(t, expected); | 323 expect(t, expected); |
334 }; | 324 }; |
335 } | 325 } |
336 | |
337 class _ElementsByNameFinder extends RecursiveAstVisitor<Null> { | |
338 final String name; | |
339 final List<Element> elements = []; | |
340 | |
341 _ElementsByNameFinder(this.name); | |
342 | |
343 @override | |
344 visitSimpleIdentifier(SimpleIdentifier node) { | |
345 if (node.name == name && node.inDeclarationContext()) { | |
346 elements.add(node.staticElement); | |
347 } | |
348 } | |
349 } | |
OLD | NEW |