Index: tests/compiler/dart2js_extra/mirror_libraries_test.dart |
diff --git a/tests/compiler/dart2js_extra/mirror_libraries_test.dart b/tests/compiler/dart2js_extra/mirror_libraries_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2fd18e5c6db2d45a1286027efeba9fb2c4dbc966 |
--- /dev/null |
+++ b/tests/compiler/dart2js_extra/mirror_libraries_test.dart |
@@ -0,0 +1,254 @@ |
+// Copyright (c) 2012, 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. |
+// |
+// Test basic mirrors functionality. |
+ |
+import 'dart:mirrors'; |
+import 'dart:isolate'; |
+ |
+import 'async_helper.dart'; |
+ |
+var simpleVar = 123; |
+final finalVar = 456; |
+ |
+dynamic functionDynamic() { |
+ return 123; |
+} |
+int functionInt() { |
+ return 456; |
+} |
+void functionVoid() { |
+} |
+ |
+class C_Used { |
+ int xx; |
+ set x(int _xx) { xx = _xx; } |
+ |
+ int yy; |
+ int ss; |
+ int vv; |
+ C_Used(this.xx, [this.ss = 128, this.vv = 256]) { |
+ yy = 111; |
+ } |
+ C_Used.withOpts(this.xx, {var s : 1024, var v : 2048}) { |
+ yy = 222; ss = s; vv = v; |
+ } |
+ |
+ String toString() => 'C_Used(xx=$xx, yy=$yy, ss=$ss, vv=$vv)'; |
+} |
+class C1_NotUsed {} |
+ |
+void test() { |
+ MirrorSystem mirrorSystem = currentMirrorSystem(); |
+ |
+ for (LibraryMirror lm in mirrorSystem.libraries.values) { |
+ print("${lm.simpleName}"); |
+ if (lm.classes != null) { |
+ lm.classes.forEach( |
+ (key, value) { |
+ print("\tClass: $key\t${value == null?'':value}"); |
+ if (value.methods != null) { |
+ value.methods.forEach( |
+ (key, value) => print("\t\tMethod: $key\t$value")); |
+ } |
+ if (value.getters != null) { |
+ value.getters.forEach( |
+ (key, value) => print("\t\tGetter: $key\t$value")); |
+ } |
+ if (value.setters != null) { |
+ value.setters.forEach( |
+ (key, value) => print("\t\tSetter: $key\t$value")); |
+ } |
+ if (value.variables != null) { |
+ value.variables.forEach( |
+ (key, value) => print("\t\tVariable: $key\t$value")); |
+ } |
+ if (value.constructors != null) { |
+ value.constructors.forEach( |
+ (key, value) => print("\t\tConstuctor: $key\t$value")); |
+ } |
+ if (value.typeVariables != null) { |
+ value.typeVariables.forEach( |
+ (key, value) => print("\t\tTypeVariable: $key\t$value")); |
+ } |
+ if (value.typeArguments != null) { |
+ value.typeArguments.forEach( |
+ (key, value) => print("\t\tTypeArgument: $key\t$value")); |
+ } |
+ }); |
+ } |
+ if (lm.functions != null) { |
+ lm.functions.forEach( |
+ (key, value) => print("\tFunction: $key\t${value == null?'':value}")); |
+ } |
+ if (lm.variables != null) { |
+ lm.variables.forEach( |
+ (key, value) => print("\tVariable: $key\t${value == null?'':value}")); |
+ } |
+ } |
+ |
+ Collection<LibraryMirror> libraries = mirrorSystem.libraries.values; |
+ Expect.equals(1, libraries.filter( |
+ (library) => library.simpleName == "dart.mirrors").length); |
+ Collection<LibraryMirror> thisLibraryMirrors = |
+ libraries.filter( |
+ (library) => library.simpleName.endsWith('mirror_libraries_test.dart')); |
+ Expect.equals(1, thisLibraryMirrors.length); |
+ LibraryMirror thisLibraryMirror = thisLibraryMirrors[0]; |
+ Expect.isTrue(thisLibraryMirror.classes.containsKey("C_Used")); |
+ ClassMirror classMirror = thisLibraryMirror.classes["C_Used"]; |
+ Expect.equals(2, classMirror.constructors.length); |
+ classMirror.constructors.forEach((name, constructorMirror) { |
+ Expect.isNotNull(name); |
+ Expect.isNotNull(constructorMirror.constructorName); |
+ Expect.isTrue(constructorMirror.isConstructor); |
+ |
+ asyncTest((onDone) { |
+ classMirror.newInstance( |
+ constructorMirror.constructorName, |
+ [218], |
+ null).then((value) { |
+ Expect.equals(218, value.reflectee.xx); |
+ }); |
+ onDone(true); |
+ }); |
+ }); |
+ |
+ Expect.equals(4, classMirror.variables.length); |
+ Expect.equals(1, |
+ classMirror.variables.values.filter((v) => v.simpleName == "xx").length); |
+ Expect.equals(1, |
+ classMirror.variables.values.filter((v) => v.simpleName == "yy").length); |
+ Expect.equals(1, |
+ classMirror.variables.values.filter((v) => v.simpleName == "ss").length); |
+ Expect.equals(1, |
+ classMirror.variables.values.filter((v) => v.simpleName == "vv").length); |
+ |
+ asyncTest((onDone) { |
+ classMirror.newInstance("", [], null).then((value) { |
+ Expect.isTrue(value.hasReflectee); |
+ }); |
+ onDone(true); |
+ }); |
+ asyncTest((onDone) { |
+ classMirror.newInstance("", [], null).then((value) { |
+ Expect.isNull(value.reflectee.xx); |
+ }); |
+ onDone(true); |
+ }); |
+ asyncTest((onDone) { |
+ classMirror.newInstance("", [314], null).then((value) { |
+ Expect.equals(314, value.reflectee.xx); |
+ }); |
+ onDone(true); |
+ }); |
+ asyncTest((onDone) { |
+ classMirror.newInstance("", [314], null).then((value) { |
+ Expect.equals(111, value.reflectee.yy); |
+ }); |
+ onDone(true); |
+ }); |
+ asyncTest((onDone) { |
+ classMirror.newInstance("withOpts", [159], null).then((value) { |
+ Expect.equals(159, value.reflectee.xx); |
+ }); |
+ onDone(true); |
+ }); |
+ asyncTest((onDone) { |
+ classMirror.newInstance("withOpts", [26], null).then((value) { |
+ Expect.equals(222, value.reflectee.yy); |
+ }); |
+ onDone(true); |
+ }); |
+ |
+ asyncTest((onDone) { |
+ classMirror.newInstance("", [314, 159, 26], null).then((instance) { |
+ asyncTest((onDone) { |
+ instance.invoke("toString", []).then((value) { |
+ Expect.equals( |
+ "C_Used(xx=314, yy=111, ss=159, vv=26)", |
+ value.reflectee); |
+ onDone(true); |
+ }); |
+ }); |
+ }); |
+ onDone(true); |
+ }); |
+ |
+ Expect.equals(1, |
+ thisLibraryMirror.variables.values.filter( |
+ (v) => v.simpleName == "simpleVar").length); |
+ VariableMirror variableMirror = |
+ thisLibraryMirror.variables.values.filter( |
+ (v) => v.simpleName == "simpleVar")[0]; |
+ Expect.isFalse(variableMirror.isStatic); |
+ Expect.isFalse(variableMirror.isFinal); |
+ Expect.equals(1, |
+ thisLibraryMirror.variables.values.filter( |
+ (v) => v.simpleName == "finalVar").length); |
+ variableMirror = |
+ thisLibraryMirror.variables.values.filter( |
+ (v) => v.simpleName == "finalVar")[0]; |
+ Expect.isFalse(variableMirror.isStatic); |
+ Expect.isTrue(variableMirror.isFinal); |
+ |
+ Expect.isTrue(thisLibraryMirror.classes.containsKey("C1_NotUsed")); |
+ classMirror = thisLibraryMirror.classes["C1_NotUsed"]; |
+ asyncTest((onDone) { |
+ classMirror.newInstance("", [], null).then((value) { |
+ Expect.isFalse(value.hasReflectee); |
+ }); |
+ onDone(true); |
+ }); |
+ asyncTest((onDone) { |
+ classMirror.newInstance("", [], null).then((value) { |
+ Expect.isNull(value.reflectee); |
+ }); |
+ onDone(true); |
+ }); |
+ |
+ Expect.isTrue(thisLibraryMirror.functions.containsKey("functionVoid")); |
+ MethodMirror functionMirror = thisLibraryMirror.functions["functionVoid"]; |
+ Expect.equals(functionMirror.returnType, mirrorSystem.voidType); |
+ |
+ Expect.isTrue(thisLibraryMirror.functions.containsKey("functionDynamic")); |
+ functionMirror = thisLibraryMirror.functions["functionDynamic"]; |
+ Expect.equals(functionMirror.returnType, mirrorSystem.dynamicType); |
+ |
+ Expect.isTrue(thisLibraryMirror.functions.containsKey("functionInt")); |
+ functionMirror = thisLibraryMirror.functions["functionInt"]; |
+ Expect.isNotNull(functionMirror.returnType); |
+} |
+ |
+void main() { |
+ // Dummy use to get class constructor generated. |
+ new C_Used(123); |
+ new C_Used.withOpts(456); |
+ |
+ var result; |
+ for (var i=0; i<123; i++) { |
+ functionVoid(); |
+ result = functionDynamic(); |
+ } |
+ print("$result"); |
+ for (var i=0; i<123; i++) { |
+ result = functionInt(); |
+ } |
+ |
+ print(""" |
+ |
+This program is using an experimental feature called \"mirrors\". As |
+currently implemented, mirrors do not work with minification, and will |
+cause spurious errors depending on how code was optimized. |
+ |
+The authors of this program are aware of these problems and have |
+decided the thrill of using an experimental feature is outweighing the |
+risks. Furthermore, the authors of this program understand that |
+long-term, to fix the problems mentioned above, mirrors may have |
+negative impact on size and performance of Dart programs compiled to |
+JavaScript. |
+"""); |
+ |
+ test(); |
+} |