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

Side by Side Diff: tests/lib/mirrors/invoke_test.dart

Issue 19839002: Improve test coverage of reflective invocation. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 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
« tests/lib/lib.status ('K') | « tests/lib/lib.status ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5 library test.invoke_test;
6
7 import 'dart:mirrors';
8
9 import 'package:expect/expect.dart';
10 import '../../async_helper.dart';
11
12 class C {
13 var field;
ahe 2013/07/20 16:34:59 Weird indentation.
rmacnak 2013/07/22 18:40:48 Oops, Sublime normally converts my tabs to spaces.
14 C() : this.field = 'default';
15 C.named(this.field);
16
17 get getter => 'get $field';
18 set setter(v) => field = 'set $v';
19 method(x, y, z) => '$x+$y+$z';
20 toString() => 'a C';
21
22 noSuchMethod(invocation) => 'DNU';
23
24 static var staticField = 'initial';
25 static get staticGetter => 'sget $staticField';
26 static set staticSetter(v) => staticField = 'sset $v';
27 static staticFunction(x, y) => "($x,$y)";
28 }
29
30 var libraryField = 'a priori';
31 get libraryGetter => 'lget $libraryField';
32 set librarySetter(v) => libraryField = 'lset $v';
33 libraryFunction(x,y) => '$x$y';
34
35 testSync() {
36 var result;
37
38 // InstanceMirror invoke
39 C c = new C();
40 InstanceMirror im = reflect(c);
41 result = im.invoke(const Symbol('method'), [2,4,8]);
42 Expect.equals('2+4+8', result.reflectee);
43 result = im.invoke(const Symbol('doesntExist'), [2,4,8]);
44 Expect.equals('DNU', result.reflectee);
45 result = im.invoke(const Symbol('method'), [2,4]); // Wrong arity.
46 Expect.equals('DNU', result.reflectee);
47
48 // InstanceMirror invokeGetter
49 result = im.getField(const Symbol('getter'));
50 Expect.equals('get default', result.reflectee);
51 result = im.getField(const Symbol('field'));
52 Expect.equals('default', result.reflectee);
53 result = im.getField(const Symbol('doesntExist'));
54 Expect.equals('DNU', result.reflectee);
55
56 // InstanceMirror invokeSetter
57 im.setField(const Symbol('setter'), 'foo');
58 Expect.equals('set foo', c.field);
59 im.setField(const Symbol('field'), 'bar');
60 Expect.equals('bar', c.field);
61 im.setField(const Symbol('doesntExist'), 'bar');
62 // Expect does not throw.
ahe 2013/07/20 16:34:59 What does this mean?
rmacnak 2013/07/22 18:40:48 We expect setField(#doesntExist) to complete witho
ahe 2013/07/22 19:29:49 Please add the comment *above* the line to which i
rmacnak 2013/07/22 20:16:56 Done.
63
64 // ClassMirror invoke
65 ClassMirror cm = reflectClass(C);
66 result = cm.invoke(const Symbol('staticFunction'),[3,4]);
67 Expect.equals('(3,4)', result.reflectee);
68 try {
69 result = cm.invoke(const Symbol('doesntExist'),[3,4]);
ahe 2013/07/20 16:34:59 Weird indentation.
70 Expect.fail('Should not reach');
71 } catch(exception) {}
ahe 2013/07/20 16:34:59 This doesn't work. Also, NEVER have an empty catc
rmacnak 2013/07/22 18:40:48 This works on the VM. In any case, converted to E
ahe 2013/07/22 19:29:49 What I meant is: You could do absolutely anything
rmacnak 2013/07/22 20:16:56 Ah, you mean it doesn't properly assert both contr
72 try {
73 result = cm.invoke(const Symbol('staticFunction'),[3]); // Wrong arity.
ahe 2013/07/20 16:34:59 Weird indention.
74 Expect.fail('Should not reach');
75 } catch(exception) {}
ahe 2013/07/20 16:34:59 This doesn't work.
76
77 // ClassMirror invokeGetter
ahe 2013/07/20 16:34:59 Seem to be a systemic problem below: Weird indent
78 result = cm.getField(const Symbol('staticGetter'));
79 Expect.equals('sget initial', result.reflectee);
80 result = cm.getField(const Symbol('staticField'));
81 Expect.equals('initial', result.reflectee);
82 try {
83 result = cm.getField(const Symbol('doesntExist'));
84 Expect.fail('Should not reach');
85 } catch(exception) {}
86
87 // ClassMirror invokeSetter
88 cm.setField(const Symbol('staticSetter'), 'sfoo');
89 Expect.equals('sset sfoo', C.staticField);
90 cm.setField(const Symbol('staticField'), 'sbar');
91 Expect.equals('sbar', C.staticField);
92 try {
93 result = cm.setField(const Symbol('doesntExist'), 'sbar');
94 Expect.fail('Should not reach');
95 } catch(exception) {}
96
97 // ClassMirror invokeConstructor
98 result = cm.newInstance(const Symbol(''), []);
99 Expect.isTrue(result.reflectee is C);
100 Expect.equals('default', result.reflectee.field);
101 result = cm.newInstance(const Symbol('named'), ['my value']);
102 Expect.isTrue(result.reflectee is C);
103 Expect.equals('my value', result.reflectee.field);
104 try {
105 result = cm.newInstance(const Symbol('doesntExist'), ['my value']);
106 Expect.fail('Should not reach');
107 } catch(exception) {}
108 try {
109 result = cm.newInstance(const Symbol('named'), []); // Wrong arity.
110 Expect.fail('Should not reach');
111 } catch(exception) {}
112
113 // LibraryMirror invoke
114 LibraryMirror lm = cm.owner;
115 result = lm.invoke(const Symbol('libraryFunction'),[':',')']);
116 Expect.equals(':)', result.reflectee);
117 try {
118 result = lm.invoke(const Symbol('doesntExist'), [':',')']);
119 Expect.fail('Should not reach');
120 } catch(exception) {}
121 try {
122 result = lm.invoke(const Symbol('libraryFunction'), [':']); // Wrong ar ity.
123 Expect.fail('Should not reach');
124 } catch(exception) {}
125
126 // LibraryMirror invokeGetter
127 result = lm.getField(const Symbol('libraryGetter'));
128 Expect.equals('lget a priori', result.reflectee);
129 result = lm.getField(const Symbol('libraryField'));
130 Expect.equals('a priori', result.reflectee);
131 try {
132 result = lm.getField(const Symbol('doesntExist'));
133 Expect.fail('Should not reach');
134 } catch(exception) {}
135
136 // LibraryMirror invokeSetter
137 lm.setField(const Symbol('librarySetter'), 'lfoo');
138 Expect.equals('lset lfoo', libraryField);
139 lm.setField(const Symbol('libraryField'), 'lbar');
140 Expect.equals('lbar', libraryField);
141 try {
142 result = lm.setField(const Symbol('doesntExist'), 'lbar');
143 Expect.fail('Should not reach');
144 } catch(exception) {}
145 }
146
147 testAsync() {
148 var future;
149
150 // InstanceMirror invoke
151 C c = new C();
152 InstanceMirror im = reflect(c);
153 future = im.invokeAsync(const Symbol('method'), [2,4,8]);
154 asyncStart();
155 future.then((result) {
156 Expect.equals('2+4+8', result.reflectee);
157 asyncEnd();
158 });
159 future = im.invokeAsync(const Symbol('method'), [im,im,im]);
160 asyncStart();
161 future.then((result) {
162 Expect.equals('a C+a C+a C', result.reflectee);
163 asyncEnd();
164 });
165 future = im.invokeAsync(const Symbol('doesntExist'), [2,4,8]);
166 asyncStart();
167 future.then((result) {
168 Expect.equals('DNU', result.reflectee);
169 asyncEnd();
170 });
171 future = im.invokeAsync(const Symbol('method'), [2, 4]); // Wrong arity.
172 asyncStart();
173 future.then((result) {
174 Expect.equals('DNU', result.reflectee);
175 asyncEnd();
176 });
177
178 // InstanceMirror invokeGetter
179 future = im.getFieldAsync(const Symbol('getter'));
180 asyncStart();
181 future.then((result) {
182 Expect.equals('get default', result.reflectee);
183 asyncEnd();
184 });
185 future = im.getFieldAsync(const Symbol('field'));
186 asyncStart();
187 future.then((result) {
188 Expect.equals('default', result.reflectee);
189 asyncEnd();
190 });
191 future = im.getFieldAsync(const Symbol('doesntExist'));
192 asyncStart();
193 future.then((result) {
194 Expect.equals('DNU', result.reflectee);
195 asyncEnd();
196 });
197
198 // InstanceMirror invokeSetter
199 future = im.setFieldAsync(const Symbol('setter'), 'foo');
200 asyncStart();
201 future.then((result) {
202 Expect.equals('set foo', c.field);
203 asyncEnd();
204 var future2 = im.setFieldAsync(const Symbol('field'), 'bar');
205 asyncStart();
206 future2.then((result) {
207 Expect.equals('bar', c.field);
208 asyncEnd();
209 var future3 = im.setFieldAsync(const Symbol('field'), im);
210 asyncStart();
211 future3.then((result) {
212 Expect.equals(c, c.field);
213 asyncEnd();
214 });
215 });
216 });
217 future = im.setFieldAsync(const Symbol('doesntExist'), 'bar');
218 asyncStart();
219 future.then((result) {
220 asyncEnd();
221 }, onError:(exception) {
222 Expect.fail('Should not throw');
223 });
224
225
226 // ClassMirror invoke
227 ClassMirror cm = reflectClass(C);
228 future = cm.invokeAsync(const Symbol('staticFunction'),[3,4]);
229 asyncStart();
230 future.then((result) {
231 Expect.equals('(3,4)', result.reflectee);
232 asyncEnd();
233 });
234 future = cm.invokeAsync(const Symbol('staticFunction'),[im,im]);
235 asyncStart();
236 future.then((result) {
237 Expect.equals('(a C,a C)', result.reflectee);
238 asyncEnd();
239 });
240 future = cm.invokeAsync(const Symbol('doesntExist'),[im,im]);
241 asyncStart();
242 future.then((result) {
243 Expect.fail('Should not reach');
244 }, onError:(exception){
245 asyncEnd();
246 });
247 future = cm.invokeAsync(const Symbol('staticFunction'),[3]); //Wrong arity.
248 asyncStart();
249 future.then((result) {
250 Expect.fail('Should not reach');
251 }, onError:(exception){
252 asyncEnd();
253 });
254
255 // ClassMirror invokeGetter
256 C.staticField = 'initial'; // Reset from synchronous test.
257 future = cm.getFieldAsync(const Symbol('staticGetter'));
258 asyncStart();
259 future.then((result) {
260 Expect.equals('sget initial', result.reflectee);
261 asyncEnd();
262 });
263 future = cm.getFieldAsync(const Symbol('staticField'));
264 asyncStart();
265 future.then((result) {
266 Expect.equals('initial', result.reflectee);
267 asyncEnd();
268 });
269 future = cm.getFieldAsync(const Symbol('doesntExist'));
270 asyncStart();
271 future.then((result) {
272 Expect.fail('Should not reach');
273 }, onError:(exception){
274 asyncEnd();
275 });
276
277 // ClassMirror invokeSetter
278 future = cm.setFieldAsync(const Symbol('staticSetter'), 'sfoo');
279 asyncStart();
280 future.then((result) {
281 Expect.equals('sset sfoo', C.staticField);
282 asyncEnd();
283 var future2 = cm.setFieldAsync(const Symbol('staticField'), 'sbar');
284 asyncStart();
285 future2.then((result) {
286 Expect.equals('sbar', C.staticField);
287 asyncEnd();
288 var future3 = cm.setFieldAsync(const Symbol('staticField'), im);
289 asyncStart();
290 future3.then((result) {
291 Expect.equals(c, C.staticField);
292 asyncEnd();
293 });
294 });
295 });
296 future = cm.setFieldAsync(const Symbol('doesntExist'), 'sbar');
297 asyncStart();
298 future.then((result) {
299 Expect.fail('Should not reach');
300 }, onError:(exception){
301 asyncEnd();
302 });
303
304 // ClassMirror invokeConstructor
305 future = cm.newInstanceAsync(const Symbol(''), []);
306 asyncStart();
307 future.then((result) {
308 Expect.isTrue(result.reflectee is C);
309 Expect.equals('default', result.reflectee.field);
310 asyncEnd();
311 });
312 future = cm.newInstanceAsync(const Symbol('named'), ['my value']);
313 asyncStart();
314 future.then((result) {
315 Expect.isTrue(result.reflectee is C);
316 Expect.equals('my value', result.reflectee.field);
317 asyncEnd();
318 });
319 future = cm.newInstanceAsync(const Symbol('named'), [im]);
320 asyncStart();
321 future.then((result) {
322 Expect.isTrue(result.reflectee is C);
323 Expect.equals(c, result.reflectee.field);
324 asyncEnd();
325 });
326 future = cm.newInstanceAsync(const Symbol('doesntExist'), ['my value']);
327 asyncStart();
328 future.then((result) {
329 Expect.fail('Should not reach');
330 }, onError:(exception){
331 asyncEnd();
332 });
333 future = cm.newInstanceAsync(const Symbol('named'), []); // Wrong arity.
334 asyncStart();
335 future.then((result) {
336 Expect.fail('Should not reach');
337 }, onError:(exception){
338 asyncEnd();
339 });
340
341 // LibraryMirror invoke
342 LibraryMirror lm = cm.owner;
343 future = lm.invokeAsync(const Symbol('libraryFunction'),[':',')']);
344 asyncStart();
345 future.then((result) {
346 Expect.equals(':)', result.reflectee);
347 asyncEnd();
348 });
349 future = lm.invokeAsync(const Symbol('libraryFunction'),[im,im]);
350 asyncStart();
351 future.then((result) {
352 Expect.equals('a Ca C', result.reflectee);
353 asyncEnd();
354 });
355 future = lm.invokeAsync(const Symbol('doesntExist'),[im,im]);
356 asyncStart();
357 future.then((result) {
358 Expect.fail('Should not reach');
359 }, onError:(exception){
360 asyncEnd();
361 });
362 future = lm.invokeAsync(const Symbol('libraryFunction'),[':']); // Wrong arit y.
363 asyncStart();
364 future.then((result) {
365 Expect.fail('Should not reach');
366 }, onError:(exception){
367 asyncEnd();
368 });
369
370 // LibraryMirror invokeGetter
371 libraryField = 'a priori'; // Reset from synchronous test.
372 future = lm.getFieldAsync(const Symbol('libraryGetter'));
373 asyncStart();
374 future.then((result) {
375 Expect.equals('lget a priori', result.reflectee);
376 asyncEnd();
377 });
378 future = lm.getFieldAsync(const Symbol('libraryField'));
379 asyncStart();
380 future.then((result) {
381 Expect.equals('a priori', result.reflectee);
382 asyncEnd();
383 });
384 future = lm.getFieldAsync(const Symbol('doesntExist'));
385 asyncStart();
386 future.then((result) {
387 Expect.fail('Should not reach');
388 }, onError:(exception){
389 asyncEnd();
390 });
391
392 // LibraryMirror invokeSetter
393 future = lm.setFieldAsync(const Symbol('librarySetter'), 'lfoo');
394 asyncStart();
395 future.then((result) {
396 Expect.equals('lset lfoo', libraryField);
397 asyncEnd();
398 var future2 = lm.setFieldAsync(const Symbol('libraryField'), 'lbar');
399 asyncStart();
400 future2.then((result) {
401 Expect.equals('lbar', libraryField);
402 asyncEnd();
403 var future3 = lm.setFieldAsync(const Symbol('libraryField'), im);
404 asyncStart();
405 future3.then((result) {
406 Expect.equals(c, libraryField);
407 asyncEnd();
408 });
409 });
410 });
411 future = lm.setFieldAsync(const Symbol('doesntExist'), 'lbar');
412 asyncStart();
413 future.then((result) {
414 Expect.fail('Should not reach');
415 }, onError:(exception){
416 asyncEnd();
417 });
418 }
419
420 main() {
421 testSync();
422 asyncStart();
423 testAsync();
424 asyncEnd();
425 }
OLDNEW
« tests/lib/lib.status ('K') | « tests/lib/lib.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698