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<T> { | 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 m(a, b, c) => {"a": a, "b": b, "c": c}; |
19 } | 20 } |
20 | 21 |
21 typedef Typedef(); | 22 typedef Typedef(); |
22 | 23 |
| 24 testInvoke(mirrors) { |
| 25 var instance = new Class(); |
| 26 var instMirror = reflect(instance); |
| 27 |
| 28 expect(instMirror.invoke(const Symbol("m"),['A', 'B', instance]).reflectee, |
| 29 equals({"a": 'A', "b":'B', "c": instance})); |
| 30 } |
| 31 |
23 testFieldAccess(mirrors) { | 32 testFieldAccess(mirrors) { |
24 var instance = new Class(); | 33 var instance = new Class(); |
25 | 34 |
26 var libMirror = mirrors.libraries[const Symbol("MirrorsTest")]; | 35 var libMirror = mirrors.libraries[const Symbol("MirrorsTest")]; |
27 var classMirror = libMirror.classes[const Symbol("Class")]; | 36 var classMirror = libMirror.classes[const Symbol("Class")]; |
28 var instMirror = reflect(instance); | 37 var instMirror = reflect(instance); |
29 | 38 |
| 39 libMirror.setField(const Symbol('topLevelField'), [91]); |
| 40 expect(libMirror.getField(const Symbol('topLevelField')).reflectee, |
| 41 equals([91])); |
| 42 expect(topLevelField, equals([91])); |
| 43 |
30 libMirror.setFieldAsync(new Symbol('topLevelField'), 42); | 44 libMirror.setFieldAsync(new Symbol('topLevelField'), 42); |
31 var future = libMirror.getFieldAsync(new Symbol('topLevelField')); | 45 var future = libMirror.getFieldAsync(new Symbol('topLevelField')); |
32 future.then(expectAsync1((resultMirror) { | 46 future.then(expectAsync1((resultMirror) { |
33 expect(resultMirror.reflectee, equals(42)); | 47 expect(resultMirror.reflectee, equals(42)); |
34 expect(topLevelField, equals(42)); | 48 expect(topLevelField, equals(42)); |
35 })); | 49 })); |
36 | 50 |
37 classMirror.setFieldAsync(new Symbol('staticField'), 43); | 51 classMirror.setFieldAsync(new Symbol('staticField'), 43); |
38 future = classMirror.getFieldAsync(new Symbol('staticField')); | 52 future = classMirror.getFieldAsync(new Symbol('staticField')); |
39 future.then(expectAsync1((resultMirror) { | 53 future.then(expectAsync1((resultMirror) { |
(...skipping 12 matching lines...) Expand all Loading... |
52 testClosureMirrors(mirrors) { | 66 testClosureMirrors(mirrors) { |
53 var closure = (x, y, z) { return x + y + z; }; | 67 var closure = (x, y, z) { return x + y + z; }; |
54 | 68 |
55 var mirror = reflect(closure); | 69 var mirror = reflect(closure); |
56 expect(mirror is ClosureMirror, equals(true)); | 70 expect(mirror is ClosureMirror, equals(true)); |
57 | 71 |
58 var funcMirror = mirror.function; | 72 var funcMirror = mirror.function; |
59 expect(funcMirror is MethodMirror, equals(true)); | 73 expect(funcMirror is MethodMirror, equals(true)); |
60 expect(funcMirror.parameters.length, equals(3)); | 74 expect(funcMirror.parameters.length, equals(3)); |
61 | 75 |
| 76 expect(mirror.apply([7, 8, 9]).reflectee, equals(24)); |
| 77 |
62 var future = mirror.applyAsync([2, 4, 8]); | 78 var future = mirror.applyAsync([2, 4, 8]); |
63 future.then(expectAsync1((resultMirror) { | 79 future.then(expectAsync1((resultMirror) { |
64 expect(resultMirror.reflectee, equals(14)); | 80 expect(resultMirror.reflectee, equals(14)); |
65 })); | 81 })); |
66 } | 82 } |
67 | 83 |
68 testInvokeConstructor(mirrors) { | 84 testInvokeConstructor(mirrors) { |
69 var libMirror = mirrors.libraries[const Symbol("MirrorsTest")]; | 85 var libMirror = mirrors.libraries[const Symbol("MirrorsTest")]; |
70 var classMirror = libMirror.classes[const Symbol("Class")]; | 86 var classMirror = libMirror.classes[const Symbol("Class")]; |
71 | 87 |
| 88 var instanceMirror = classMirror.newInstance(const Symbol(''),[]); |
| 89 expect(instanceMirror.reflectee is Class, equals(true)); |
| 90 expect(instanceMirror.reflectee.field, equals("default value")); |
| 91 |
| 92 instanceMirror = classMirror.newInstance(const Symbol('withInitialValue'), |
| 93 [45]); |
| 94 expect(instanceMirror.reflectee is Class, equals(true)); |
| 95 expect(instanceMirror.reflectee.field, equals(45)); |
| 96 |
72 var future = classMirror.newInstanceAsync(new Symbol(''), []); | 97 var future = classMirror.newInstanceAsync(new Symbol(''), []); |
73 future.then(expectAsync1((resultMirror) { | 98 future.then(expectAsync1((resultMirror) { |
74 var instance = resultMirror.reflectee; | 99 var instance = resultMirror.reflectee; |
75 expect(instance is Class, equals(true)); | 100 expect(instance is Class, equals(true)); |
76 expect(instance.field, equals("default value")); | 101 expect(instance.field, equals("default value")); |
77 })); | 102 })); |
78 | 103 |
79 future = classMirror.newInstanceAsync(new Symbol('withInitialValue'), [45]); | 104 future = classMirror.newInstanceAsync(new Symbol('withInitialValue'), [45]); |
80 future.then(expectAsync1((resultMirror) { | 105 future.then(expectAsync1((resultMirror) { |
81 var instance = resultMirror.reflectee; | 106 var instance = resultMirror.reflectee; |
(...skipping 28 matching lines...) Expand all Loading... |
110 expect(methodMirror.qualifiedName, | 135 expect(methodMirror.qualifiedName, |
111 equals(const Symbol('MirrorsTest.testNames'))); | 136 equals(const Symbol('MirrorsTest.testNames'))); |
112 | 137 |
113 expect(variableMirror.simpleName, equals(const Symbol('field'))); | 138 expect(variableMirror.simpleName, equals(const Symbol('field'))); |
114 expect(variableMirror.qualifiedName, | 139 expect(variableMirror.qualifiedName, |
115 equals(const Symbol('MirrorsTest.Class.field'))); | 140 equals(const Symbol('MirrorsTest.Class.field'))); |
116 } | 141 } |
117 | 142 |
118 main() { | 143 main() { |
119 var mirrors = currentMirrorSystem(); | 144 var mirrors = currentMirrorSystem(); |
120 | 145 test("Test reflective method invocation", () { testInvoke(mirrors); }); |
121 test("Test field access", () { testFieldAccess(mirrors); }); | 146 test("Test field access", () { testFieldAccess(mirrors); }); |
122 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); | 147 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); |
123 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); | 148 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); |
124 test("Test simple and qualifiedName", () { testNames(mirrors); }); | 149 test("Test simple and qualifiedName", () { testNames(mirrors); }); |
125 } | 150 } |
OLD | NEW |