Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: runtime/tests/vm/dart/isolate_mirror_local_test.dart

Issue 10538043: Second local mirrors CL. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // Dart test program for checking implemention of IsolateMirror when 5 // Dart test program for checking implemention of IsolateMirror when
6 // inspecting the current isolate. 6 // inspecting the current isolate.
7 7
8 #library('isolate_mirror_local_test'); 8 #library('isolate_mirror_local_test');
9 9
10 #import('dart:isolate'); 10 #import('dart:isolate');
11 #import('dart:mirrors'); 11 #import('dart:mirrors');
12 12
13 ReceivePort rp; 13 ReceivePort exit_port;
14 Set expectedTests;
15
16 void testDone(String test) {
17 if (!expectedTests.contains(test)) {
18 throw "Unexpected test name '$test'";
19 }
20 expectedTests.remove(test);
21 if (expectedTests.isEmpty()) {
22 // All tests are done.
23 exit_port.close();
24 }
25 }
26
14 int global_var = 0; 27 int global_var = 0;
15 28
16 // This function will be invoked reflectively. 29 // This function will be invoked reflectively.
17 int function(int x) { 30 int function(int x) {
18 global_var = x; 31 global_var = x;
19 return x + 1; 32 return x + 1;
20 } 33 }
21 34
22 void testRootLibraryMirror(LibraryMirror lib_mirror) { 35 void testRootLibraryMirror(LibraryMirror lib_mirror) {
23 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName); 36 Expect.equals('isolate_mirror_local_test', lib_mirror.simpleName);
24 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart')); 37 Expect.isTrue(lib_mirror.url.contains('isolate_mirror_local_test.dart'));
38 Expect.equals("LibraryMirror on 'isolate_mirror_local_test'",
39 lib_mirror.toString());
25 40
26 // Test library invocation. 41 // Test library invocation by calling function(123).
27 Expect.equals(0, global_var); 42 Expect.equals(0, global_var);
28 lib_mirror.invoke('function', [ 123 ]).then( 43 lib_mirror.invoke('function', [ 123 ]).then(
29 (InstanceMirror retval) { 44 (InstanceMirror retval) {
30 Expect.equals(123, global_var); 45 Expect.equals(123, global_var);
46 Expect.equals('Smi', retval.getClass().simpleName);
47 Expect.isTrue(retval.hasSimpleValue);
31 Expect.equals(124, retval.simpleValue); 48 Expect.equals(124, retval.simpleValue);
32 rp.close(); 49 testDone('testRootLibraryMirror');
33 }); 50 });
34 } 51 }
35 52
36 void testLibrariesMap(Map libraries) { 53 void testLibrariesMap(Map libraries) {
37 // Just look for a couple of well-known libs. 54 // Just look for a couple of well-known libs.
38 LibraryMirror core_lib = libraries['dart:core']; 55 LibraryMirror core_lib = libraries['dart:core'];
39 Expect.isTrue(core_lib is LibraryMirror); 56 Expect.isTrue(core_lib is LibraryMirror);
40 57
41 LibraryMirror mirror_lib = libraries['dart:mirrors']; 58 LibraryMirror mirror_lib = libraries['dart:mirrors'];
42 Expect.isTrue(mirror_lib is LibraryMirror); 59 Expect.isTrue(mirror_lib is LibraryMirror);
60
61 // Lookup an interface from a library and make sure it is sane.
62 InterfaceMirror list_intf = core_lib.members()['List'];
63 Expect.isTrue(list_intf is InterfaceMirror);
64 Expect.equals('List', list_intf.simpleName);
65 Expect.equals('Object', list_intf.superclass().simpleName);
66 Expect.equals('ListFactory', list_intf.defaultFactory().simpleName);
67 Expect.equals('dart:core', list_intf.library.simpleName);
68 Expect.isFalse(list_intf.isClass);
69 Expect.equals('Collection', list_intf.superinterfaces()[0].simpleName);
70 Expect.equals("InterfaceMirror on 'List'", list_intf.toString());
71
72 // Lookup a class from a library and make sure it is sane.
73 InterfaceMirror oom_cls = core_lib.members()['OutOfMemoryException'];
74 Expect.isTrue(oom_cls is InterfaceMirror);
75 Expect.equals('OutOfMemoryException', oom_cls.simpleName);
76 Expect.equals('Object', oom_cls.superclass().simpleName);
77 Expect.isTrue(oom_cls.defaultFactory() === null);
78 Expect.equals('dart:core', oom_cls.library.simpleName);
79 Expect.isTrue(oom_cls.isClass);
80 Expect.equals('Exception', oom_cls.superinterfaces()[0].simpleName);
81 Expect.equals("InterfaceMirror on 'OutOfMemoryException'",
82 oom_cls.toString());
83 testDone('testLibrariesMap');
43 } 84 }
44 85
45 void testIsolateMirror(IsolateMirror mirror) { 86 void testIsolateMirror(IsolateMirror mirror) {
46 Expect.isTrue(mirror.debugName.contains('main')); 87 Expect.isTrue(mirror.debugName.contains('main'));
47 testRootLibraryMirror(mirror.rootLibrary); 88 testRootLibraryMirror(mirror.rootLibrary);
48 testLibrariesMap(mirror.libraries); 89 testLibrariesMap(mirror.libraries());
90 testDone('testIsolateMirror');
91 }
92
93 void testIntegerInstanceMirror(InstanceMirror mirror) {
94 // TODO(turnidge): The mirrors api exposes internal vm
95 // implementation class names. Is this okay?
96 Expect.equals('Smi', mirror.getClass().simpleName);
97 Expect.isTrue(mirror.hasSimpleValue);
98 Expect.equals(1001, mirror.simpleValue);
99 Expect.equals("InstanceMirror on <1001>", mirror.toString());
100
101 // Invoke (mirror + mirror).
102 mirror.invoke('+', [ mirror ]).then(
103 (InstanceMirror retval) {
104 Expect.equals('Smi', retval.getClass().simpleName);
105 Expect.isTrue(retval.hasSimpleValue);
106 Expect.equals(2002, retval.simpleValue);
107 testDone('testIntegerInstanceMirror');
108 });
109 }
110
111 void testStringInstanceMirror(InstanceMirror mirror) {
112 // TODO(turnidge): The mirrors api exposes internal vm
113 // implementation class names. Is this okay?
114 Expect.equals('OneByteString', mirror.getClass().simpleName);
115 Expect.isTrue(mirror.hasSimpleValue);
116 Expect.equals('This\nis\na\nString', mirror.simpleValue);
117 Expect.equals("InstanceMirror on <'This\\nis\\na\\nString'>",
118 mirror.toString());
119
120 // Invoke mirror[0].
121 mirror.invoke('[]', [ 0 ]).then(
122 (InstanceMirror retval) {
123 Expect.equals('OneByteString', retval.getClass().simpleName);
124 Expect.isTrue(retval.hasSimpleValue);
125 Expect.equals('T', retval.simpleValue);
126 testDone('testStringInstanceMirror');
127 });
128 }
129
130 void testBoolInstanceMirror(InstanceMirror mirror) {
131 Expect.equals('Bool', mirror.getClass().simpleName);
132 Expect.isTrue(mirror.hasSimpleValue);
133 Expect.equals(true, mirror.simpleValue);
134 Expect.equals("InstanceMirror on <true>", mirror.toString());
135 testDone('testBoolInstanceMirror');
136 }
137
138 void testNullInstanceMirror(InstanceMirror mirror) {
139 // TODO(turnidge): This is returning the wrong class. Fix it.
140 Expect.equals('Object', mirror.getClass().simpleName);
141 Expect.isTrue(mirror.hasSimpleValue);
142 Expect.equals(null, mirror.simpleValue);
143 Expect.equals("InstanceMirror on <null>", mirror.toString());
144 testDone('testNullInstanceMirror');
145 }
146
147 class MySuperClass {
148 }
149
150 class MyInterface {
151 }
152
153 class MyClass extends MySuperClass implements MyInterface {
154 MyClass(this.value) {}
155
156 final value;
157
158 int method(int arg) {
159 return arg + value;
160 }
161 }
162
163 void testCustomInstanceMirror(InstanceMirror mirror) {
164 Expect.isFalse(mirror.hasSimpleValue);
165 bool saw_exception = false;
166 try {
167 // mirror.simpleValue;
168 } catch (MirrorException me) {
169 saw_exception = true;
170 }
171 //Expect.isTrue(saw_exception);
172 Expect.equals("InstanceMirror on instance of 'MyClass'", mirror.toString());
173
174 InterfaceMirror cls = mirror.getClass();
175 Expect.isTrue(cls is InterfaceMirror);
176 Expect.equals('MyClass', cls.simpleName);
177 Expect.equals('MySuperClass', cls.superclass().simpleName);
178 Expect.isTrue(cls.defaultFactory() === null);
179 Expect.equals('isolate_mirror_local_test', cls.library.simpleName);
180 Expect.isTrue(cls.isClass);
181 Expect.equals('MyInterface', cls.superinterfaces()[0].simpleName);
182 Expect.equals("InterfaceMirror on 'MyClass'",
183 cls.toString());
184
185 // Invoke mirror.method(1000).
186 mirror.invoke('method', [ 1000 ]).then(
187 (InstanceMirror retval) {
188 Expect.equals('Smi', retval.getClass().simpleName);
189 Expect.isTrue(retval.hasSimpleValue);
190 Expect.equals(1017, retval.simpleValue);
191 testDone('testCustomInstanceMirror');
192 });
193
194 }
195
196 class MyException implements Exception {
197 MyException(this._message);
198 final String _message;
199 String toString() { return 'MyException: $_message'; }
200 }
201
202 void methodWithException() {
203 throw new MyException("from methodWithException");
204 }
205
206 void methodWithError() {
207 // We get a parse error when we try to run this function.
208 +++;
209 }
210
211 void testMirrorErrors(IsolateMirror mirror) {
212 LibraryMirror lib_mirror = mirror.rootLibrary;
213
214 Future<InstanceMirror> future =
215 lib_mirror.invoke('methodWithException', []);
216 future.handleException(
217 (MirroredError exc) {
218 Expect.isTrue(exc is MirroredUncaughtExceptionError);
219 Expect.equals('MyException',
220 exc.exception_mirror.getClass().simpleName);
221 Expect.equals('MyException: from methodWithException',
222 exc.exception_string);
223 Expect.isTrue(exc.stacktrace.toString().contains(
224 'isolate_mirror_local_test.dart'));
225 testDone('testMirrorErrors1');
226 return true;
227 });
228 future.then(
229 (InstanceMirror retval) {
230 // Should not reach here.
231 Expect.isTrue(false);
232 });
233
234 Future<InstanceMirror> future2 =
235 lib_mirror.invoke('methodWithError', []);
236 future2.handleException(
237 (MirroredError exc) {
238 Expect.isTrue(exc is MirroredCompilationError);
239 Expect.isTrue(exc.message.contains('unexpected token'));
240 testDone('testMirrorErrors2');
241 return true;
242 });
243 future2.then(
244 (InstanceMirror retval) {
245 // Should not reach here.
246 Expect.isTrue(false);
247 });
248
249 // TODO(turnidge): When we call a method that doesn't exist, we
250 // should probably call noSuchMethod(). I'm adding this test to
251 // document the current behavior in the meantime.
252 Future<InstanceMirror> future3 =
253 lib_mirror.invoke('methodNotFound', []);
254 future3.handleException(
255 (MirroredError exc) {
256 Expect.isTrue(exc is MirroredCompilationError);
257 Expect.isTrue(exc.message.contains(
258 "did not find top-level function 'methodNotFound'"));
259 testDone('testMirrorErrors3');
260 return true;
261 });
262 future3.then(
263 (InstanceMirror retval) {
264 // Should not reach here.
265 Expect.isTrue(false);
266 });
49 } 267 }
50 268
51 void main() { 269 void main() {
270 // When all of the expected tests complete, the exit_port is closed,
271 // allowing the program to terminate.
272 exit_port = new ReceivePort();
273 expectedTests = new Set<String>.from(['testRootLibraryMirror',
274 'testLibrariesMap',
275 'testIsolateMirror',
276 'testIntegerInstanceMirror',
277 'testStringInstanceMirror',
278 'testBoolInstanceMirror',
279 'testNullInstanceMirror',
280 'testCustomInstanceMirror',
281 'testMirrorErrors1',
282 'testMirrorErrors2',
283 'testMirrorErrors3']);
284
52 // Test that an isolate can reflect on itself. 285 // Test that an isolate can reflect on itself.
53 rp = new ReceivePort(); 286 isolateMirrorOf(exit_port.toSendPort()).then(testIsolateMirror);
54 isolateMirrorOf(rp.toSendPort()).then(testIsolateMirror); 287
55 } 288 testIntegerInstanceMirror(mirrorOf(1001));
289 testStringInstanceMirror(mirrorOf('This\nis\na\nString'));
290 testBoolInstanceMirror(mirrorOf(true));
291 testNullInstanceMirror(mirrorOf(null));
292 testCustomInstanceMirror(mirrorOf(new MyClass(17)));
293 testMirrorErrors(currentIsolateMirror());
294 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698