OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // TODO(rmacnak): Move the existing mirror tests here (a place for | 5 // TODO(rmacnak): Move the existing mirror tests here (a place for |
6 // cross-implementation tests). | 6 // cross-implementation tests). |
7 | 7 |
8 library MirrorsTest; | 8 library MirrorsTest; |
9 import "dart:mirrors"; | 9 import "dart:mirrors"; |
10 import "../../../pkg/unittest/lib/unittest.dart"; | 10 import "../../../pkg/unittest/lib/unittest.dart"; |
11 | 11 |
12 var topLevelField; | 12 var topLevelField; |
13 | 13 |
14 class Class { | 14 class Class<T> { |
15 Class() { this.field = "default value"; } | 15 Class() { this.field = "default value"; } |
16 Class.withInitialValue(this.field); | 16 Class.withInitialValue(this.field); |
17 var field; | 17 var field; |
18 static var staticField; | 18 static var staticField; |
19 } | 19 } |
20 | 20 |
| 21 typedef Typedef(); |
| 22 |
21 testFieldAccess(mirrors) { | 23 testFieldAccess(mirrors) { |
22 var instance = new Class(); | 24 var instance = new Class(); |
23 | 25 |
24 var libMirror = mirrors.libraries["MirrorsTest"]; | 26 var libMirror = mirrors.libraries[const Symbol("MirrorsTest")]; |
25 var classMirror = libMirror.classes["Class"]; | 27 var classMirror = libMirror.classes[const Symbol("Class")]; |
26 var instMirror = reflect(instance); | 28 var instMirror = reflect(instance); |
27 | 29 |
28 libMirror.setFieldAsync('topLevelField', 42); | 30 libMirror.setFieldAsync(new Symbol('topLevelField'), 42); |
29 var future = libMirror.getFieldAsync('topLevelField'); | 31 var future = libMirror.getFieldAsync(new Symbol('topLevelField')); |
30 future.then(expectAsync1((resultMirror) { | 32 future.then(expectAsync1((resultMirror) { |
31 expect(resultMirror.reflectee, equals(42)); | 33 expect(resultMirror.reflectee, equals(42)); |
32 expect(topLevelField, equals(42)); | 34 expect(topLevelField, equals(42)); |
33 })); | 35 })); |
34 | 36 |
35 classMirror.setFieldAsync('staticField', 43); | 37 classMirror.setFieldAsync(new Symbol('staticField'), 43); |
36 future = classMirror.getFieldAsync('staticField'); | 38 future = classMirror.getFieldAsync(new Symbol('staticField')); |
37 future.then(expectAsync1((resultMirror) { | 39 future.then(expectAsync1((resultMirror) { |
38 expect(resultMirror.reflectee, equals(43)); | 40 expect(resultMirror.reflectee, equals(43)); |
39 expect(Class.staticField, equals(43)); | 41 expect(Class.staticField, equals(43)); |
40 })); | 42 })); |
41 | 43 |
42 instMirror.setFieldAsync('field', 44); | 44 instMirror.setFieldAsync(new Symbol('field'), 44); |
43 future = instMirror.getFieldAsync('field'); | 45 future = instMirror.getFieldAsync(new Symbol('field')); |
44 future.then(expectAsync1((resultMirror) { | 46 future.then(expectAsync1((resultMirror) { |
45 expect(resultMirror.reflectee, equals(44)); | 47 expect(resultMirror.reflectee, equals(44)); |
46 expect(instance.field, equals(44)); | 48 expect(instance.field, equals(44)); |
47 })); | 49 })); |
48 } | 50 } |
49 | 51 |
50 testClosureMirrors(mirrors) { | 52 testClosureMirrors(mirrors) { |
51 var closure = (x, y, z) { return x + y + z; }; | 53 var closure = (x, y, z) { return x + y + z; }; |
52 | 54 |
53 var mirror = reflect(closure); | 55 var mirror = reflect(closure); |
54 expect(mirror is ClosureMirror, equals(true)); | 56 expect(mirror is ClosureMirror, equals(true)); |
55 | 57 |
56 var funcMirror = mirror.function; | 58 var funcMirror = mirror.function; |
57 expect(funcMirror is MethodMirror, equals(true)); | 59 expect(funcMirror is MethodMirror, equals(true)); |
58 expect(funcMirror.parameters.length, equals(3)); | 60 expect(funcMirror.parameters.length, equals(3)); |
59 | 61 |
60 var future = mirror.applyAsync([2, 4, 8]); | 62 var future = mirror.applyAsync([2, 4, 8]); |
61 future.then(expectAsync1((resultMirror) { | 63 future.then(expectAsync1((resultMirror) { |
62 expect(resultMirror.reflectee, equals(14)); | 64 expect(resultMirror.reflectee, equals(14)); |
63 })); | 65 })); |
64 } | 66 } |
65 | 67 |
66 testInvokeConstructor(mirrors) { | 68 testInvokeConstructor(mirrors) { |
67 var libMirror = mirrors.libraries["MirrorsTest"]; | 69 var libMirror = mirrors.libraries[const Symbol("MirrorsTest")]; |
68 var classMirror = libMirror.classes["Class"]; | 70 var classMirror = libMirror.classes[const Symbol("Class")]; |
69 | 71 |
70 var future = classMirror.newInstanceAsync('', []); | 72 var future = classMirror.newInstanceAsync(new Symbol(''), []); |
71 future.then(expectAsync1((resultMirror) { | 73 future.then(expectAsync1((resultMirror) { |
72 var instance = resultMirror.reflectee; | 74 var instance = resultMirror.reflectee; |
73 expect(instance is Class, equals(true)); | 75 expect(instance is Class, equals(true)); |
74 expect(instance.field, equals("default value")); | 76 expect(instance.field, equals("default value")); |
75 })); | 77 })); |
76 | 78 |
77 future = classMirror.newInstanceAsync('withInitialValue', [45]); | 79 future = classMirror.newInstanceAsync(new Symbol('withInitialValue'), [45]); |
78 future.then(expectAsync1((resultMirror) { | 80 future.then(expectAsync1((resultMirror) { |
79 var instance = resultMirror.reflectee; | 81 var instance = resultMirror.reflectee; |
80 expect(instance is Class, equals(true)); | 82 expect(instance is Class, equals(true)); |
81 expect(instance.field, equals(45)); | 83 expect(instance.field, equals(45)); |
82 })); | 84 })); |
83 } | 85 } |
84 | 86 |
| 87 testNames(mirrors) { |
| 88 var libMirror = mirrors.libraries[const Symbol('MirrorsTest')]; |
| 89 var classMirror = libMirror.classes[const Symbol('Class')]; |
| 90 var typedefMirror = libMirror.members[const Symbol('Typedef')]; |
| 91 var methodMirror = libMirror.functions[const Symbol('testNames')]; |
| 92 var variableMirror = classMirror.variables[const Symbol('field')]; |
| 93 |
| 94 expect(libMirror.simpleName, equals(const Symbol('MirrorsTest'))); |
| 95 expect(libMirror.qualifiedName, equals(const Symbol('MirrorsTest'))); |
| 96 |
| 97 expect(classMirror.simpleName, equals(const Symbol('Class'))); |
| 98 expect(classMirror.qualifiedName, equals(const Symbol('MirrorsTest.Class'))); |
| 99 |
| 100 TypeVariableMirror typeVariable = classMirror.typeVariables.values.single; |
| 101 expect(typeVariable.simpleName, equals(const Symbol('T'))); |
| 102 expect(typeVariable.qualifiedName, |
| 103 equals(const Symbol('MirrorsTest.Class.T'))); |
| 104 |
| 105 expect(typedefMirror.simpleName, equals(const Symbol('Typedef'))); |
| 106 expect(typedefMirror.qualifiedName, |
| 107 equals(const Symbol('MirrorsTest.Typedef'))); |
| 108 |
| 109 expect(methodMirror.simpleName, equals(const Symbol('testNames'))); |
| 110 expect(methodMirror.qualifiedName, |
| 111 equals(const Symbol('MirrorsTest.testNames'))); |
| 112 |
| 113 expect(variableMirror.simpleName, equals(const Symbol('field'))); |
| 114 expect(variableMirror.qualifiedName, |
| 115 equals(const Symbol('MirrorsTest.Class.field'))); |
| 116 } |
| 117 |
85 main() { | 118 main() { |
86 var mirrors = currentMirrorSystem(); | 119 var mirrors = currentMirrorSystem(); |
87 | 120 |
88 test("Test field access", () { testFieldAccess(mirrors); }); | 121 test("Test field access", () { testFieldAccess(mirrors); }); |
89 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); | 122 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); |
90 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); | 123 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); |
| 124 test("Test simple and qualifiedName", () { testNames(mirrors); }); |
91 } | 125 } |
92 | |
OLD | NEW |