Index: pkg/analyzer/test/utils.dart |
diff --git a/pkg/analyzer/test/utils.dart b/pkg/analyzer/test/utils.dart |
index 154a266ba20c7a9dbb2e3755ad1a6596af9be815..61697c59d9e4bd0fb3b7024e956be9775ef1a316 100644 |
--- a/pkg/analyzer/test/utils.dart |
+++ b/pkg/analyzer/test/utils.dart |
@@ -6,6 +6,7 @@ library analyzer.test.utils; |
import 'package:analyzer/dart/ast/ast.dart'; |
import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
+import 'package:analyzer/dart/ast/visitor.dart'; |
import 'package:analyzer/dart/element/element.dart'; |
import 'package:analyzer/dart/element/type.dart'; |
import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
@@ -13,6 +14,19 @@ import 'package:front_end/src/base/source.dart'; |
import 'package:test/test.dart'; |
/** |
+ * Search the [unit] for the [LocalVariableElement] with the given [name]. |
+ * Fail if there is not exactly one such variable. |
+ */ |
+LocalVariableElement findLocalVariable(CompilationUnit unit, String name) { |
+ var finder = new _ElementsByNameFinder(name); |
+ unit.accept(finder); |
+ List<Element> localVariables = |
+ finder.elements.where((e) => e is LocalVariableElement).toList(); |
+ expect(localVariables, hasLength(1)); |
+ return localVariables[0]; |
+} |
+ |
+/** |
* The type of an assertion which asserts properties of [T]s. |
*/ |
typedef void Asserter<T>(T type); |
@@ -310,3 +324,17 @@ class TypeAssertions { |
expect(t, expected); |
}; |
} |
+ |
+class _ElementsByNameFinder extends RecursiveAstVisitor<Null> { |
+ final String name; |
+ final List<Element> elements = []; |
+ |
+ _ElementsByNameFinder(this.name); |
+ |
+ @override |
+ visitSimpleIdentifier(SimpleIdentifier node) { |
+ if (node.name == name && node.inDeclarationContext()) { |
+ elements.add(node.staticElement); |
+ } |
+ } |
+} |