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

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, 4 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
« no previous file with comments | « 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;
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 Future expectValueThen(Future future, Function onValue) {
36 asyncStart();
37 wrappedOnValue(resultIn) {
38 var resultOut = onValue(resultIn);
39 asyncEnd();
40 return resultOut;
41 }
42 onError(e) {
43 Expect.fail("Value expected. ($e)");
44 }
45 return future.then(wrappedOnValue, onError: onError);
46 }
47
48 Future expectError(Future future, Function errorPredicate, String reason) {
49 asyncStart();
50 onValue(result) {
51 Expect.fail("Error expected ($reason)");
52 }
53 onError(e) {
54 asyncEnd();
55 if (!errorPredicate(e)) {
56 Expect.fail("Unexpected error ($reason)");
57 }
58 }
59 return future.then(onValue, onError: onError);
60 }
61
62 bool isNoSuchMethodError(e) {
63 return e is NoSuchMethodError;
64 }
65
66 testSync() {
67 var result;
68
69 // InstanceMirror invoke
70 C c = new C();
71 InstanceMirror im = reflect(c);
72 result = im.invoke(const Symbol('method'), [2,4,8]);
73 Expect.equals('2+4+8', result.reflectee);
74 result = im.invoke(const Symbol('doesntExist'), [2,4,8]);
75 Expect.equals('DNU', result.reflectee);
76 result = im.invoke(const Symbol('method'), [2,4]); // Wrong arity.
77 Expect.equals('DNU', result.reflectee);
78
79 // InstanceMirror invokeGetter
80 result = im.getField(const Symbol('getter'));
81 Expect.equals('get default', result.reflectee);
82 result = im.getField(const Symbol('field'));
83 Expect.equals('default', result.reflectee);
84 result = im.getField(const Symbol('doesntExist'));
85 Expect.equals('DNU', result.reflectee);
86
87 // InstanceMirror invokeSetter
88 result = im.setField(const Symbol('setter'), 'foo');
89 Expect.equals('foo', result.reflectee);
90 Expect.equals('set foo', c.field);
91 Expect.equals('set foo', im.getField(const Symbol('field')).reflectee);
92 result = im.setField(const Symbol('field'), 'bar');
93 Expect.equals('bar', result.reflectee);
94 Expect.equals('bar', c.field);
95 Expect.equals('bar', im.getField(const Symbol('field')).reflectee);
96 result = im.setField(const Symbol('doesntExist'), 'bar');
97 Expect.equals('bar', result.reflectee);
98
99 // ClassMirror invoke
100 ClassMirror cm = reflectClass(C);
101 result = cm.invoke(const Symbol('staticFunction'),[3,4]);
102 Expect.equals('(3,4)', result.reflectee);
103 Expect.throws(() => cm.invoke(const Symbol('doesntExist'),[3,4]),
104 isNoSuchMethodError,
105 'Not defined');
106 Expect.throws(() => cm.invoke(const Symbol('staticFunction'),[3]),
107 isNoSuchMethodError,
108 'Wrong arity');
109
110 // ClassMirror invokeGetter
111 result = cm.getField(const Symbol('staticGetter'));
112 Expect.equals('sget initial', result.reflectee);
113 result = cm.getField(const Symbol('staticField'));
114 Expect.equals('initial', result.reflectee);
115 Expect.throws(() => cm.getField(const Symbol('doesntExist')),
116 isNoSuchMethodError,
117 'Not defined');
118
119 // ClassMirror invokeSetter
120 result = cm.setField(const Symbol('staticSetter'), 'sfoo');
121 Expect.equals('sfoo', result.reflectee);
122 Expect.equals('sset sfoo', C.staticField);
123 Expect.equals('sset sfoo',
124 cm.getField(const Symbol('staticField')).reflectee);
125 result = cm.setField(const Symbol('staticField'), 'sbar');
126 Expect.equals('sbar', result.reflectee);
127 Expect.equals('sbar', C.staticField);
128 Expect.equals('sbar', cm.getField(const Symbol('staticField')).reflectee);
129 Expect.throws(() => cm.setField(const Symbol('doesntExist'), 'sbar'),
130 isNoSuchMethodError,
131 'Not defined');
132
133 // ClassMirror invokeConstructor
134 result = cm.newInstance(const Symbol(''), []);
135 Expect.isTrue(result.reflectee is C);
136 Expect.equals('default', result.reflectee.field);
137 result = cm.newInstance(const Symbol('named'), ['my value']);
138 Expect.isTrue(result.reflectee is C);
139 Expect.equals('my value', result.reflectee.field);
140 Expect.throws(() => cm.newInstance(const Symbol('doesntExist'), ['my value']),
141 isNoSuchMethodError,
142 'Not defined');
143 Expect.throws(() => cm.newInstance(const Symbol('named'), []),
144 isNoSuchMethodError,
145 'Wrong arity');
146
147 // LibraryMirror invoke
148 LibraryMirror lm = cm.owner;
149 result = lm.invoke(const Symbol('libraryFunction'),[':',')']);
150 Expect.equals(':)', result.reflectee);
151 Expect.throws(() => lm.invoke(const Symbol('doesntExist'), [':',')']),
152 isNoSuchMethodError,
153 'Not defined');
154 Expect.throws(() => lm.invoke(const Symbol('libraryFunction'), [':']),
155 isNoSuchMethodError,
156 'Wrong arity');
157
158 // LibraryMirror invokeGetter
159 result = lm.getField(const Symbol('libraryGetter'));
160 Expect.equals('lget a priori', result.reflectee);
161 result = lm.getField(const Symbol('libraryField'));
162 Expect.equals('a priori', result.reflectee);
163 Expect.throws(() => lm.getField(const Symbol('doesntExist')),
164 isNoSuchMethodError,
165 'Not defined');
166
167 // LibraryMirror invokeSetter
168 result = lm.setField(const Symbol('librarySetter'), 'lfoo');
169 Expect.equals('lfoo', result.reflectee);
170 Expect.equals('lset lfoo', libraryField);
171 Expect.equals('lset lfoo',
172 lm.getField(const Symbol('libraryField')).reflectee);
173 result = lm.setField(const Symbol('libraryField'), 'lbar');
174 Expect.equals('lbar', result.reflectee);
175 Expect.equals('lbar', libraryField);
176 Expect.equals('lbar', lm.getField(const Symbol('libraryField')).reflectee);
177 Expect.throws(() => lm.setField(const Symbol('doesntExist'), 'lbar'),
178 isNoSuchMethodError,
179 'Not defined');
180 }
181
182 testAsync() {
183 var future;
184
185 // InstanceMirror invoke
186 C c = new C();
187 InstanceMirror im = reflect(c);
188 future = im.invokeAsync(const Symbol('method'), [2,4,8]);
189 expectValueThen(future, (result) {
190 Expect.equals('2+4+8', result.reflectee);
191 });
192 future = im.invokeAsync(const Symbol('method'), [im,im,im]);
193 expectValueThen(future, (result) {
194 Expect.equals('a C+a C+a C', result.reflectee);
195 });
196 future = im.invokeAsync(const Symbol('doesntExist'), [2,4,8]);
197 expectValueThen(future, (result) {
198 Expect.equals('DNU', result.reflectee);
199 });
200 future = im.invokeAsync(const Symbol('method'), [2, 4]); // Wrong arity.
201 expectValueThen(future, (result) {
202 Expect.equals('DNU', result.reflectee);
203 });
204
205 // InstanceMirror invokeGetter
206 future = im.getFieldAsync(const Symbol('getter'));
207 expectValueThen(future, (result) {
208 Expect.equals('get default', result.reflectee);
209 });
210 future = im.getFieldAsync(const Symbol('field'));
211 expectValueThen(future, (result) {
212 Expect.equals('default', result.reflectee);
213 });
214 future = im.getFieldAsync(const Symbol('doesntExist'));
215 expectValueThen(future, (result) {
216 Expect.equals('DNU', result.reflectee);
217 });
218
219 // InstanceMirror invokeSetter
220 future = im.setFieldAsync(const Symbol('setter'), 'foo');
221 expectValueThen(future, (result) {
222 Expect.equals('foo', result.reflectee);
223 Expect.equals('set foo', c.field);
224 return im.setFieldAsync(const Symbol('field'), 'bar');
225 }).then((result) {
226 Expect.equals('bar', result.reflectee);
227 Expect.equals('bar', c.field);
228 return im.setFieldAsync(const Symbol('field'), im);
229 }).then((result) {
230 Expect.equals(im.reflectee, result.reflectee);
231 Expect.equals(c, c.field);
232 });
233 future = im.setFieldAsync(const Symbol('doesntExist'), 'bar');
234 expectValueThen(future, (result) {
235 Expect.equals('bar', result.reflectee);
236 });
237
238
239 // ClassMirror invoke
240 ClassMirror cm = reflectClass(C);
241 future = cm.invokeAsync(const Symbol('staticFunction'),[3,4]);
242 expectValueThen(future, (result) {
243 Expect.equals('(3,4)', result.reflectee);
244 });
245 future = cm.invokeAsync(const Symbol('staticFunction'),[im,im]);
246 expectValueThen(future, (result) {
247 Expect.equals('(a C,a C)', result.reflectee);
248 });
249 future = cm.invokeAsync(const Symbol('doesntExist'),[im,im]);
250 expectError(future, isNoSuchMethodError, 'Not defined');
251 future = cm.invokeAsync(const Symbol('staticFunction'),[3]);
252 expectError(future, isNoSuchMethodError, 'Wrong arity');
253
254 // ClassMirror invokeGetter
255 C.staticField = 'initial'; // Reset from synchronous test.
256 future = cm.getFieldAsync(const Symbol('staticGetter'));
257 expectValueThen(future, (result) {
258 Expect.equals('sget initial', result.reflectee);
259 });
260 future = cm.getFieldAsync(const Symbol('staticField'));
261 expectValueThen(future, (result) {
262 Expect.equals('initial', result.reflectee);
263 });
264 future = cm.getFieldAsync(const Symbol('doesntExist'));
265 expectError(future, isNoSuchMethodError, 'Not defined');
266
267 // ClassMirror invokeSetter
268 future = cm.setFieldAsync(const Symbol('staticSetter'), 'sfoo');
269 expectValueThen(future, (result) {
270 Expect.equals('sfoo', result.reflectee);
271 Expect.equals('sset sfoo', C.staticField);
272 return cm.setFieldAsync(const Symbol('staticField'), 'sbar');
273 }).then((result) {
274 Expect.equals('sbar', result.reflectee);
275 Expect.equals('sbar', C.staticField);
276 return cm.setFieldAsync(const Symbol('staticField'), im);;
277 }).then((result) {
278 Expect.equals(im.reflectee, result.reflectee);
279 Expect.equals(c, C.staticField);
280 });
281 future = cm.setFieldAsync(const Symbol('doesntExist'), 'sbar');
282 expectError(future, isNoSuchMethodError, 'Not defined');
283
284 // ClassMirror invokeConstructor
285 future = cm.newInstanceAsync(const Symbol(''), []);
286 expectValueThen(future, (result) {
287 Expect.isTrue(result.reflectee is C);
288 Expect.equals('default', result.reflectee.field);
289 });
290 future = cm.newInstanceAsync(const Symbol('named'), ['my value']);
291 expectValueThen(future, (result) {
292 Expect.isTrue(result.reflectee is C);
293 Expect.equals('my value', result.reflectee.field);
294 });
295 future = cm.newInstanceAsync(const Symbol('named'), [im]);
296 expectValueThen(future, (result) {
297 Expect.isTrue(result.reflectee is C);
298 Expect.equals(c, result.reflectee.field);
299 });
300 future = cm.newInstanceAsync(const Symbol('doesntExist'), ['my value']);
301 expectError(future, isNoSuchMethodError, 'Not defined');
302 future = cm.newInstanceAsync(const Symbol('named'), []);
303 expectError(future, isNoSuchMethodError, 'Wrong arity');
304
305
306 // LibraryMirror invoke
307 LibraryMirror lm = cm.owner;
308 future = lm.invokeAsync(const Symbol('libraryFunction'),[':',')']);
309 expectValueThen(future, (result) {
310 Expect.equals(':)', result.reflectee);
311 });
312 future = lm.invokeAsync(const Symbol('libraryFunction'),[im,im]);
313 expectValueThen(future, (result) {
314 Expect.equals('a Ca C', result.reflectee);
315 });
316 future = lm.invokeAsync(const Symbol('doesntExist'),[im,im]);
317 expectError(future, isNoSuchMethodError, 'Not defined');
318 future = lm.invokeAsync(const Symbol('libraryFunction'),[':']);
319 expectError(future, isNoSuchMethodError, 'Wrong arity');
320
321 // LibraryMirror invokeGetter
322 libraryField = 'a priori'; // Reset from synchronous test.
323 future = lm.getFieldAsync(const Symbol('libraryGetter'));
324 expectValueThen(future, (result) {
325 Expect.equals('lget a priori', result.reflectee);
326 });
327 future = lm.getFieldAsync(const Symbol('libraryField'));
328 expectValueThen(future, (result) {
329 Expect.equals('a priori', result.reflectee);
330 });
331 future = lm.getFieldAsync(const Symbol('doesntExist'));
332 expectError(future, isNoSuchMethodError, 'Not defined');
333
334 // LibraryMirror invokeSetter
335 future = lm.setFieldAsync(const Symbol('librarySetter'), 'lfoo');
336 expectValueThen(future, (result) {
337 Expect.equals('lfoo', result.reflectee);
338 Expect.equals('lset lfoo', libraryField);
339 return lm.setFieldAsync(const Symbol('libraryField'), 'lbar');
340 }).then((result) {
341 Expect.equals('lbar', result.reflectee);
342 Expect.equals('lbar', libraryField);
343 return lm.setFieldAsync(const Symbol('libraryField'), im);
344 }).then((result) {
345 Expect.equals(im.reflectee, result.reflectee);
346 Expect.equals(c, libraryField);
347 });
348 future = lm.setFieldAsync(const Symbol('doesntExist'), 'lbar');
349 expectError(future, isNoSuchMethodError, 'Not defined');
350 }
351
352 main() {
353 testSync();
354 testAsync();
355 }
OLDNEW
« no previous file with comments | « tests/lib/lib.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698