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

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