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 | |
6 // cross-implementation tests). | |
7 | |
8 library MirrorsTest; | 5 library MirrorsTest; |
9 import "dart:mirrors"; | 6 import "dart:mirrors"; |
10 import "../../../pkg/unittest/lib/unittest.dart"; | 7 import "../../../pkg/unittest/lib/unittest.dart"; |
11 | 8 |
12 var topLevelField; | 9 var topLevelField; |
10 u(a, b, c) => {"a": a, "b": b, "c": c}; | |
11 _v(a, b) => a + b; | |
13 | 12 |
14 class Class<T> { | 13 class Class<T> { |
15 Class() { this.field = "default value"; } | 14 Class() { this.field = "default value"; } |
16 Class.withInitialValue(this.field); | 15 Class.withInitialValue(this.field); |
17 var field; | 16 var field; |
17 | |
18 Class.generative(this.field); | |
19 Class.redirecting(y) : this.generative(y*2); | |
20 factory Class.faktory(y) => "FakeClass $y"; | |
21 factory Class.redirectingFactory(y) = Class.faktory; | |
22 | |
23 m(a, b, c) => {"a": a, "b": b, "c": c}; | |
24 _n(a, b) => a + b; | |
25 noSuchMethod(invocation) => "DNU"; | |
26 | |
18 static var staticField; | 27 static var staticField; |
19 m(a, b, c) => {"a": a, "b": b, "c": c}; | 28 static s(a, b, c) => {"a": a, "b": b, "c": c}; |
29 static _t(a, b) => a + b; | |
20 } | 30 } |
21 | 31 |
22 typedef Typedef(); | 32 typedef Typedef(); |
23 | 33 |
24 testInvoke(mirrors) { | 34 testInvoke(mirrors, isDart2js) { |
25 var instance = new Class(); | 35 var instance = new Class(); |
26 var instMirror = reflect(instance); | 36 var instMirror = reflect(instance); |
27 | 37 |
28 expect(instMirror.invoke(const Symbol("m"),['A', 'B', instance]).reflectee, | 38 expect(instMirror.invoke(const Symbol("m"),['A', 'B', instance]).reflectee, |
29 equals({"a": 'A', "b":'B', "c": instance})); | 39 equals({"a": 'A', "b":'B', "c": instance})); |
40 expect(instMirror.invoke(const Symbol("notDefined"), []).reflectee, | |
ahe
2013/07/16 10:40:47
It would be great if you could avoid making furthe
| |
41 equals("DNU")); | |
42 expect(instMirror.invoke(const Symbol("m"), []).reflectee, | |
43 equals("DNU")); // Wrong arity. | |
44 // TODO(rmacnak): Implement access to private members. | |
45 // expect(instMirror.invoke(const Symbol("_n"), [3, 4]).reflectee, | |
46 // equals(7)); | |
47 | |
48 if (isDart2js) return; | |
49 | |
50 var classMirror = instMirror.type; | |
51 expect(classMirror.invoke(const Symbol("s"),['A', 'B', instance]).reflectee, | |
52 equals({"a": 'A', "b":'B', "c": instance})); | |
53 expect(() => classMirror.invoke(const Symbol("notDefined"), []).reflectee, | |
54 throws); | |
55 expect(() => classMirror.invoke(const Symbol("s"), []).reflectee, | |
56 throws); // Wrong arity. | |
57 // TODO(rmacnak): Implement access to private members. | |
58 // expect(classMirror.invoke(const Symbol("_t"), [3, 4]).reflectee, | |
59 // equals(7)); | |
60 | |
61 var libMirror = classMirror.owner; | |
62 expect(libMirror.invoke(const Symbol("u"),['A', 'B', instance]).reflectee, | |
63 equals({"a": 'A', "b":'B', "c": instance})); | |
64 expect(() => libMirror.invoke(const Symbol("notDefined"), []).reflectee, | |
65 throws); | |
66 expect(() => libMirror.invoke(const Symbol("u"), []).reflectee, | |
67 throws); // Wrong arity. | |
68 // NB: This works on the VM but fails at compile-time on dart2js. | |
69 // expect(libMirror.invoke(const Symbol("_v"), [3, 4]).reflectee, | |
70 // equals(7)); | |
30 } | 71 } |
31 | 72 |
32 testInstanceFieldAccess(mirrors) { | 73 testInstanceFieldAccess(mirrors) { |
33 var instance = new Class(); | 74 var instance = new Class(); |
34 var instMirror = reflect(instance); | 75 var instMirror = reflect(instance); |
35 | 76 |
36 instMirror.setFieldAsync(const Symbol('field'), 44); | 77 instMirror.setFieldAsync(const Symbol('field'), 44); |
37 instMirror.getFieldAsync(const Symbol('field')).then( | 78 instMirror.getFieldAsync(const Symbol('field')).then( |
38 expectAsync1((resultMirror) { | 79 expectAsync1((resultMirror) { |
39 expect(resultMirror.reflectee, equals(44)); | 80 expect(resultMirror.reflectee, equals(44)); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 expect(funcMirror.parameters.length, equals(3)); | 156 expect(funcMirror.parameters.length, equals(3)); |
116 | 157 |
117 expect(mirror.apply([7, 8, 9]).reflectee, equals(24)); | 158 expect(mirror.apply([7, 8, 9]).reflectee, equals(24)); |
118 | 159 |
119 var future = mirror.applyAsync([2, 4, 8]); | 160 var future = mirror.applyAsync([2, 4, 8]); |
120 future.then(expectAsync1((resultMirror) { | 161 future.then(expectAsync1((resultMirror) { |
121 expect(resultMirror.reflectee, equals(14)); | 162 expect(resultMirror.reflectee, equals(14)); |
122 })); | 163 })); |
123 } | 164 } |
124 | 165 |
125 testInvokeConstructor(mirrors) { | 166 testInvokeConstructor(mirrors, isDart2js) { |
126 var classMirror = reflectClass(Class); | 167 var classMirror = reflectClass(Class); |
127 | 168 |
128 var instanceMirror = classMirror.newInstance(const Symbol(''),[]); | 169 var instanceMirror = classMirror.newInstance(const Symbol(''),[]); |
129 expect(instanceMirror.reflectee is Class, equals(true)); | 170 expect(instanceMirror.reflectee is Class, equals(true)); |
130 expect(instanceMirror.reflectee.field, equals("default value")); | 171 expect(instanceMirror.reflectee.field, equals("default value")); |
131 | 172 |
132 instanceMirror = classMirror.newInstance(const Symbol('withInitialValue'), | 173 instanceMirror = classMirror.newInstance(const Symbol('withInitialValue'), |
133 [45]); | 174 [45]); |
134 expect(instanceMirror.reflectee is Class, equals(true)); | 175 expect(instanceMirror.reflectee is Class, equals(true)); |
135 expect(instanceMirror.reflectee.field, equals(45)); | 176 expect(instanceMirror.reflectee.field, equals(45)); |
136 | 177 |
178 | |
179 instanceMirror = classMirror.newInstance(const Symbol('generative'), | |
180 [7]); | |
181 expect(instanceMirror.reflectee is Class, equals(true)); | |
182 expect(instanceMirror.reflectee.field, equals(7)); | |
183 | |
184 instanceMirror = classMirror.newInstance(const Symbol('redirecting'), | |
185 [8]); | |
186 expect(instanceMirror.reflectee is Class, equals(true)); | |
187 expect(instanceMirror.reflectee.field, equals(16)); | |
188 | |
189 | |
190 if (!isDart2js) { | |
191 instanceMirror = classMirror.newInstance(const Symbol('faktory'), | |
192 [9]); | |
193 expect(instanceMirror.reflectee, equals('FakeClass 9')); | |
194 | |
195 instanceMirror = classMirror.newInstance(const Symbol('redirectingFactory'), | |
196 [10]); | |
197 expect(instanceMirror.reflectee, equals('FakeClass 10')); | |
198 } | |
199 | |
200 | |
137 var future = classMirror.newInstanceAsync(const Symbol(''), []); | 201 var future = classMirror.newInstanceAsync(const Symbol(''), []); |
138 future.then(expectAsync1((resultMirror) { | 202 future.then(expectAsync1((resultMirror) { |
139 var instance = resultMirror.reflectee; | 203 var instance = resultMirror.reflectee; |
140 expect(instance is Class, equals(true)); | 204 expect(instance is Class, equals(true)); |
141 expect(instance.field, equals("default value")); | 205 expect(instance.field, equals("default value")); |
142 })); | 206 })); |
143 | 207 |
144 future = classMirror.newInstanceAsync(const Symbol('withInitialValue'), [45]); | 208 future = classMirror.newInstanceAsync(const Symbol('withInitialValue'), [45]); |
145 future.then(expectAsync1((resultMirror) { | 209 future.then(expectAsync1((resultMirror) { |
146 var instance = resultMirror.reflectee; | 210 var instance = resultMirror.reflectee; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 | 259 |
196 testLibraryUri(var value, bool check(Uri)) { | 260 testLibraryUri(var value, bool check(Uri)) { |
197 var valueMirror = reflect(value); | 261 var valueMirror = reflect(value); |
198 ClassMirror valueClass = valueMirror.type; | 262 ClassMirror valueClass = valueMirror.type; |
199 LibraryMirror valueLibrary = valueClass.owner; | 263 LibraryMirror valueLibrary = valueClass.owner; |
200 expect(check(valueLibrary.uri), isTrue); | 264 expect(check(valueLibrary.uri), isTrue); |
201 } | 265 } |
202 | 266 |
203 mainWithArgument({bool isDart2js: false, bool isMinified: false}) { | 267 mainWithArgument({bool isDart2js: false, bool isMinified: false}) { |
204 var mirrors = currentMirrorSystem(); | 268 var mirrors = currentMirrorSystem(); |
205 test("Test reflective method invocation", () { testInvoke(mirrors); }); | 269 test("Test reflective method invocation", () { testInvoke(mirrors, |
270 isDart2js); }); | |
206 test("Test instance field access", () { testInstanceFieldAccess(mirrors); }); | 271 test("Test instance field access", () { testInstanceFieldAccess(mirrors); }); |
207 test('Test intercepted objects', () { testIntercepted(mirrors); }); | 272 test('Test intercepted objects', () { testIntercepted(mirrors); }); |
208 if (!isMinified) // TODO(ahe): Remove this line. | 273 if (!isMinified) // TODO(ahe): Remove this line. |
209 test("Test field access", () { testFieldAccess(mirrors); }); | 274 test("Test field access", () { testFieldAccess(mirrors); }); |
210 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); | 275 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); |
211 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); | 276 test("Test invoke constructor", () { testInvokeConstructor(mirrors, |
277 isDart2js); }); | |
212 test("Test current library uri", () { | 278 test("Test current library uri", () { |
213 testLibraryUri(new Class(), | 279 testLibraryUri(new Class(), |
214 (Uri uri) => uri.path.endsWith('/mirrors_test.dart')); | 280 (Uri uri) => uri.path.endsWith('/mirrors_test.dart')); |
215 }); | 281 }); |
216 test("Test dart library uri", () { | 282 test("Test dart library uri", () { |
217 testLibraryUri("test", (Uri uri) => uri == Uri.parse('dart:core')); | 283 testLibraryUri("test", (Uri uri) => uri == Uri.parse('dart:core')); |
218 }); | 284 }); |
219 if (!isMinified) // TODO(ahe): Remove this line. | 285 if (!isMinified) // TODO(ahe): Remove this line. |
220 test("Test simple and qualifiedName", () { testNames(mirrors, isDart2js); }); | 286 test("Test simple and qualifiedName", () { testNames(mirrors, isDart2js); }); |
221 if (isDart2js) return; // TODO(ahe): Remove this line. | 287 if (isDart2js) return; // TODO(ahe): Remove this line. |
222 test("Test reflect type", () { testReflectClass(mirrors); }); | 288 test("Test reflect type", () { testReflectClass(mirrors); }); |
223 } | 289 } |
224 | 290 |
225 main() { | 291 main() { |
226 mainWithArgument(); | 292 mainWithArgument(); |
227 } | 293 } |
OLD | NEW |