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 |
| 11 import '../../compiler/dart2js_extra/async_helper.dart'; |
11 | 12 |
12 var topLevelField; | 13 var topLevelField; |
13 | 14 |
14 class Class { | 15 class Class { |
15 Class() { this.field = "default value"; } | 16 Class() { this.field = "default value"; } |
16 Class.withInitialValue(this.field); | 17 Class.withInitialValue(this.field); |
17 var field; | 18 var field; |
18 static var staticField; | 19 static var staticField; |
19 } | 20 } |
20 | 21 |
21 testFieldAccess(mirrors) { | 22 void testFieldAccess(mirrors) { |
| 23 var dummyInstance = new Class.withInitialValue(123); |
| 24 |
22 var instance = new Class(); | 25 var instance = new Class(); |
23 | 26 |
24 var libMirror = mirrors.libraries["MirrorsTest"]; | 27 var libMirror = mirrors.libraries["MirrorsTest"]; |
25 var classMirror = libMirror.classes["Class"]; | 28 var classMirror = libMirror.classes["Class"]; |
26 var instMirror = reflect(instance); | 29 var instMirror = reflect(instance); |
27 | 30 |
28 libMirror.setField('topLevelField', 42); | 31 asyncTest((onDone) { |
29 var future = libMirror.getField('topLevelField'); | 32 libMirror.setField('topLevelField', 42).then( |
30 future.then(expectAsync1((resultMirror) { | 33 (result) { |
31 expect(resultMirror.reflectee, equals(42)); | 34 libMirror.getField('topLevelField').then( |
32 expect(topLevelField, equals(42)); | 35 (resultMirror) { |
33 })); | 36 Expect.equals(42, resultMirror.reflectee); |
| 37 Expect.equals(42, topLevelField); |
| 38 onDone(true); |
| 39 }); |
| 40 }); |
| 41 }); |
34 | 42 |
35 classMirror.setField('staticField', 43); | 43 asyncTest((onDone) { |
36 future = classMirror.getField('staticField'); | 44 classMirror.setField('staticField', 43).then( |
37 future.then(expectAsync1((resultMirror) { | 45 (result) { |
38 expect(resultMirror.reflectee, equals(43)); | 46 classMirror.getField('staticField').then( |
39 expect(Class.staticField, equals(43)); | 47 (resultMirror) { |
40 })); | 48 Expect.equals(43, resultMirror.reflectee); |
| 49 Expect.equals(43, Class.staticField); |
| 50 onDone(true); |
| 51 }); |
| 52 }); |
| 53 }); |
41 | 54 |
42 instMirror.setField('field', 44); | 55 asyncTest((onDone) { |
43 future = instMirror.getField('field'); | 56 instMirror.setField('field', 44).then( |
44 future.then(expectAsync1((resultMirror) { | 57 (result) { |
45 expect(resultMirror.reflectee, equals(44)); | 58 instMirror.getField('field').then( |
46 expect(instance.field, equals(44)); | 59 (resultMirror) { |
47 })); | 60 Expect.equals(44, resultMirror.reflectee); |
| 61 Expect.equals(44, instance.field); |
| 62 onDone(true); |
| 63 }); |
| 64 }); |
| 65 }); |
48 } | 66 } |
49 | 67 |
50 testClosureMirrors(mirrors) { | 68 testClosureMirrors(mirrors) { |
51 var closure = (x, y, z) { return x + y + z; }; | 69 var closure = (x, y, z) { return x + y + z; }; |
52 | 70 |
53 var mirror = reflect(closure); | 71 var mirror = reflect(closure); |
54 expect(mirror is ClosureMirror, equals(true)); | 72 Expect.isTrue(mirror is ClosureMirror); |
55 | 73 |
56 var funcMirror = mirror.function; | 74 var funcMirror = mirror.function; |
57 expect(funcMirror is MethodMirror, equals(true)); | 75 Expect.isTrue(funcMirror is MethodMirror); |
58 expect(funcMirror.parameters.length, equals(3)); | 76 Expect.equals(3, funcMirror.parameters.length); |
59 | 77 |
60 var future = mirror.apply([2, 4, 8]); | 78 asyncTest((onDone) { |
61 future.then(expectAsync1((resultMirror) { | 79 mirror.apply([2, 4, 8]).then( |
62 expect(resultMirror.reflectee, equals(14)); | 80 (resultMirror) { |
63 })); | 81 Expect.equals(14, resultMirror.reflectee); |
| 82 onDone(true); |
| 83 }); |
| 84 }); |
64 } | 85 } |
65 | 86 |
66 testInvokeConstructor(mirrors) { | 87 testInvokeConstructor(mirrors) { |
67 var libMirror = mirrors.libraries["MirrorsTest"]; | 88 var libMirror = mirrors.libraries["MirrorsTest"]; |
68 var classMirror = libMirror.classes["Class"]; | 89 var classMirror = libMirror.classes["Class"]; |
69 | 90 |
70 var future = classMirror.newInstance('', []); | 91 asyncTest((onDone) { |
71 future.then(expectAsync1((resultMirror) { | 92 classMirror.newInstance('', []).then( |
72 var instance = resultMirror.reflectee; | 93 (resultMirror) { |
73 expect(instance is Class, equals(true)); | 94 var instance = resultMirror.reflectee; |
74 expect(instance.field, equals("default value")); | 95 Expect.isTrue(instance is Class); |
75 })); | 96 Expect.equals("default value", instance.field); |
| 97 onDone(true); |
| 98 }); |
| 99 }); |
76 | 100 |
77 future = classMirror.newInstance('withInitialValue', [45]); | 101 asyncTest((onDone) { |
78 future.then(expectAsync1((resultMirror) { | 102 classMirror.newInstance('withInitialValue', [45]).then( |
79 var instance = resultMirror.reflectee; | 103 (resultMirror) { |
80 expect(instance is Class, equals(true)); | 104 var instance = resultMirror.reflectee; |
81 expect(instance.field, equals(45)); | 105 Expect.isTrue(instance is Class); |
82 })); | 106 Expect.equals(45, instance.field); |
| 107 onDone(true); |
| 108 }); |
| 109 }); |
83 } | 110 } |
84 | 111 |
85 main() { | 112 main() { |
| 113 print(""" |
| 114 |
| 115 This program is using an experimental feature called \"mirrors\". As |
| 116 currently implemented, mirrors do not work with minification, and will |
| 117 cause spurious errors depending on how code was optimized. |
| 118 |
| 119 The authors of this program are aware of these problems and have |
| 120 decided the thrill of using an experimental feature is outweighing the |
| 121 risks. Furthermore, the authors of this program understand that |
| 122 long-term, to fix the problems mentioned above, mirrors may have |
| 123 negative impact on size and performance of Dart programs compiled to |
| 124 JavaScript. |
| 125 """); |
| 126 |
86 var mirrors = currentMirrorSystem(); | 127 var mirrors = currentMirrorSystem(); |
87 | 128 |
88 test("Test field access", () { testFieldAccess(mirrors); }); | 129 testFieldAccess(mirrors); |
89 test("Test closure mirrors", () { testClosureMirrors(mirrors); }); | 130 testClosureMirrors(mirrors); |
90 test("Test invoke constructor", () { testInvokeConstructor(mirrors); }); | 131 testInvokeConstructor(mirrors); |
91 } | 132 } |
92 | 133 |
OLD | NEW |