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