Index: tests/compiler/dart2js/mirror_system_helper.dart |
diff --git a/tests/compiler/dart2js/mirror_system_helper.dart b/tests/compiler/dart2js/mirror_system_helper.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..40d61883739bc3afe1928244dc1b4f924bba2aca |
--- /dev/null |
+++ b/tests/compiler/dart2js/mirror_system_helper.dart |
@@ -0,0 +1,144 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library mirror_system_helper; |
+ |
+import 'dart:async'; |
+import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; |
+import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart'; |
+import 'mock_compiler.dart'; |
+ |
+export '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; |
+export '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.dart'; |
+ |
+const String SOURCE = 'source'; |
+final Uri SOURCE_URI = new Uri(scheme: SOURCE, path: SOURCE); |
+ |
+// TODO(johnniwinther): Move this to a mirrors helper library. |
+Future<MirrorSystem> createMirrorSystem(String source) { |
+ MockCompiler compiler = new MockCompiler( |
+ analyzeOnly: true, |
+ analyzeAll: true, |
+ preserveComments: true); |
+ compiler.registerSource(SOURCE_URI, source); |
+ compiler.librariesToAnalyzeWhenRun = <Uri>[SOURCE_URI]; |
+ return compiler.runCompiler(null).then((_) { |
+ return new Dart2JsMirrorSystem(compiler); |
+ }); |
+} |
+ |
+/** |
+ * Returns [:true:] if [type] is an instance of [:decl:] with type arguments |
+ * equal to [typeArgument]. |
+ */ |
+bool isType(ClassMirror decl, List<TypeMirror> typeArguments, |
karlklose
2013/11/05 10:34:18
'isInstance'?
Johnni Winther
2013/11/05 11:20:47
Done.
|
+ ClassMirror type) { |
+ if (type.isOriginalDeclaration) return false; |
+ if (!sameDecl(decl, type)) return false; |
+ return equalsTypes(typeArguments, type.typeArguments); |
+} |
+ |
+/** |
+ * Returns [:true:] if [type] is the same type as [expected]. This method |
+ * equates a non-generic declaration with its instantiation. |
+ */ |
+bool equalsType(TypeMirror expected, TypeMirror type) { |
karlklose
2013/11/05 10:34:18
'isEqualType'?
Johnni Winther
2013/11/05 11:20:47
Done.
|
+ if (expected == type) return true; |
+ if (expected is ClassMirror && type is ClassMirror) { |
+ if (!sameDecl(expected, type)) return false; |
+ if (expected.isOriginalDeclaration || expected.typeArguments.isEmpty) { |
+ return type.isOriginalDeclaration || type.typeArguments.isEmpty; |
+ } |
+ return equalsTypes(expected.typeArguments, type.typeArguments); |
+ } |
+ return true; |
+} |
+ |
+/** |
+ * Returns [:true:] if [types] are equals to [expected] using the equalitry |
+ * defined by [equalsType]. |
+ */ |
+bool equalsTypes(List<TypeMirror> expected, List<TypeMirror> types) { |
karlklose
2013/11/05 10:34:18
Ditto.
Johnni Winther
2013/11/05 11:20:47
Done.
|
+ return checkSameList(expected, types, equalsType); |
+} |
+ |
+/** |
+ * Returns [:true:] if an instance of [type] with type arguments equal to |
+ * [typeArguments] is found in [types]. |
+ */ |
+bool containsType(ClassMirror decl, List<TypeMirror> typeArguments, |
+ Iterable<TypeMirror> types) { |
+ for (var type in types) { |
karlklose
2013/11/05 10:34:18
types.any((type) => isType(decl, typeArguments, ty
Johnni Winther
2013/11/05 11:20:47
Done.
|
+ if (isType(decl, typeArguments, type)) { |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+/** |
+ * Returns the declaration of [type]. |
+ */ |
+TypeMirror toDecl(TypeMirror type) { |
karlklose
2013/11/05 10:34:18
'Decl' -> 'Declaration'? (also multiple times belo
Johnni Winther
2013/11/05 11:20:47
Done.
|
+ return type is ClassMirror ? type.originalDeclaration : type; |
+} |
+ |
+/** |
+ * Returns [:true:] if [type] is of the same declaration as [expected]. |
+ */ |
+bool sameDecl(TypeMirror expected, TypeMirror type) { |
+ return toDecl(expected) == toDecl(type); |
+} |
+ |
+/** |
+ * Returns [:true:] if a type of the declaration of [expected] is in [types]. |
+ */ |
+bool containsDecl(TypeMirror expected, Iterable<TypeMirror> types) { |
+ for (var type in types) { |
+ if (sameDecl(expected, type)) { |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+/** |
+ * Returns [:true:] if declarations of [expected] are the same as those of |
+ * [types], taking order into account. |
+ */ |
+bool sameDeclList(Iterable<TypeMirror> expected, |
+ Iterable<TypeMirror> types) { |
+ return checkSameList(expected, types, sameDecl); |
+} |
+ |
+/** |
+ * Returns [:true:] if declarations of [expected] are the same as those of |
+ * [iterable], not taking order into account. |
+ */ |
+bool sameDeclSet(Iterable<TypeMirror> expected, |
+ Iterable<TypeMirror> types) { |
+ Set<TypeMirror> expectedSet = expected.map(toDecl).toSet(); |
+ Set<TypeMirror> typesSet = types.map(toDecl).toSet(); |
+ return expectedSet.length == typesSet.length && |
+ expectedSet.containsAll(typesSet); |
+} |
+ |
+/** |
+ * Utility method for checking whether [expected] and [iterable] contains the |
+ * same elements with respect to the checking function [check], takin order |
+ * into account. |
+ */ |
+bool checkSameList(Iterable<TypeMirror> expected, |
+ Iterable<TypeMirror> types, |
+ bool check(TypeMirror a, TypeMirror b)) { |
+ if (expected.length != types.length) return false; |
+ Iterator<TypeMirror> expectedIterator = expected.iterator; |
+ Iterator<TypeMirror> typesIterator = types.iterator; |
+ while (expectedIterator.moveNext() && typesIterator.moveNext()) { |
+ if (!check(expectedIterator.current, typesIterator.current)) { |
+ return false; |
+ } |
+ } |
+ return true; |
+} |