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.dart"); | 8 #library("MirrorsTest.dart"); |
9 #import("dart:mirrors"); | 9 #import("dart:mirrors"); |
10 #import("../../../pkg/unittest/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 } | 19 } |
20 | 20 |
21 testFieldAccess(mirrors) { | 21 testFieldAccess(mirrors) { |
22 var instance = new Class(); | 22 var instance = new Class(); |
23 | 23 |
24 var libMirror = mirrors.libraries["MirrorsTest.dart"]; | 24 var libMirror = mirrors.libraries["MirrorsTest.dart"]; |
25 var classMirror = libMirror.classes["Class"]; | 25 var classMirror = libMirror.classes["Class"]; |
26 var instMirror = reflect(instance); | 26 var instMirror = reflect(instance); |
27 | 27 |
28 libMirror.setField('topLevelField', 42); | 28 libMirror.setField('topLevelField', 42); |
29 var future = libMirror.getField('topLevelField'); | 29 var future = libMirror.getField('topLevelField'); |
30 future.then(expectAsync1((resultMirror) { | 30 future.then(expectAsync1((resultMirror) { |
31 expect(resultMirror.reflectee, equals(42)); | 31 expect(resultMirror.reflectee, equals(42)); |
32 expect(topLevelField, equals(42)); | 32 expect(topLevelField, equals(42)); |
33 })); | 33 })); |
34 | 34 |
35 classMirror.setField('staticField', 43); | 35 classMirror.setField('staticField', 43); |
36 future = classMirror.getField('staticField'); | 36 future = classMirror.getField('staticField'); |
37 future.then(expectAsync1((resultMirror) { | 37 future.then(expectAsync1((resultMirror) { |
38 expect(resultMirror.reflectee, equals(43)); | 38 expect(resultMirror.reflectee, equals(43)); |
39 expect(Class.staticField, equals(43)); | 39 expect(Class.staticField, equals(43)); |
40 })); | 40 })); |
41 | 41 |
42 instMirror.setField('field', 44); | 42 instMirror.setField('field', 44); |
43 future = instMirror.getField('field'); | 43 future = instMirror.getField('field'); |
44 future.then(expectAsync1((resultMirror) { | 44 future.then(expectAsync1((resultMirror) { |
45 expect(resultMirror.reflectee, equals(44)); | 45 expect(resultMirror.reflectee, equals(44)); |
46 expect(instance.field, equals(44)); | 46 expect(instance.field, equals(44)); |
47 })); | 47 })); |
48 } | 48 } |
49 | 49 |
50 testClosureMirrors(mirrors) { | 50 testClosureMirrors(mirrors) { |
51 var closure = (x, y, z) { return x + y + z; }; | 51 var closure = (x, y, z) { return x + y + z; }; |
52 | 52 |
53 var mirror = reflect(closure); | 53 var mirror = reflect(closure); |
54 expect(mirror is ClosureMirror, equals(true)); | 54 expect(mirror is ClosureMirror, equals(true)); |
55 | 55 |
56 var funcMirror = mirror.function; | 56 var funcMirror = mirror.function; |
57 expect(funcMirror is MethodMirror, equals(true)); | 57 expect(funcMirror is MethodMirror, equals(true)); |
58 expect(funcMirror.parameters.length, equals(3)); | 58 expect(funcMirror.parameters.length, equals(3)); |
59 | 59 |
60 var future = mirror.apply([2, 4, 8]); | 60 var future = mirror.apply([2, 4, 8]); |
61 future.then(expectAsync1((resultMirror) { | 61 future.then(expectAsync1((resultMirror) { |
62 expect(resultMirror.reflectee, equals(14)); | 62 expect(resultMirror.reflectee, equals(14)); |
63 })); | 63 })); |
64 } | 64 } |
65 | 65 |
66 testInvokeConstructor(mirrors) { | 66 testInvokeConstructor(mirrors) { |
67 var libMirror = mirrors.libraries["MirrorsTest.dart"]; | 67 var libMirror = mirrors.libraries["MirrorsTest.dart"]; |
68 var classMirror = libMirror.classes["Class"]; | 68 var classMirror = libMirror.classes["Class"]; |
69 | 69 |
70 var future = classMirror.newInstance('', []); | 70 var future = classMirror.newInstance('', []); |
71 future.then(expectAsync1((resultMirror) { | 71 future.then(expectAsync1((resultMirror) { |
72 var instance = resultMirror.reflectee; | 72 var instance = resultMirror.reflectee; |
73 expect(instance is Class, equals(true)); | 73 expect(instance is Class, equals(true)); |
74 expect(instance.field, equals("default value")); | 74 expect(instance.field, equals("default value")); |
75 })); | 75 })); |
76 | 76 |
77 future = classMirror.newInstance('withInitialValue', [45]); | 77 future = classMirror.newInstance('withInitialValue', [45]); |
78 future.then(expectAsync1((resultMirror) { | 78 future.then(expectAsync1((resultMirror) { |
79 var instance = resultMirror.reflectee; | 79 var instance = resultMirror.reflectee; |
80 expect(instance is Class, equals(true)); | 80 expect(instance is Class, equals(true)); |
81 expect(instance.field, equals(45)); | 81 expect(instance.field, equals(45)); |
82 })); | 82 })); |
83 } | 83 } |
84 | 84 |
85 main() { | 85 main() { |
86 var mirrors = currentMirrorSystem(); | 86 var mirrors = currentMirrorSystem(); |
87 | 87 |
88 test("Test field access", () { testFieldAccess(mirrors); }); | 88 test("Test field access", () { testFieldAccess(mirrors); }); |
89 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); | 89 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); |
90 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); | 90 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); |
91 } | 91 } |
92 | 92 |
OLD | NEW |