| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library test.invoke_test; | 5 library test.invoke_test; |
| 6 | 6 |
| 7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
| 8 | 8 |
| 9 import 'dart:async' show Future; | 9 import 'dart:async' show Future; |
| 10 | 10 |
| 11 import 'package:expect/expect.dart'; | 11 import 'package:expect/expect.dart'; |
| 12 import "package:async_helper/async_helper.dart"; | |
| 13 | 12 |
| 14 class C { | 13 class C { |
| 15 var field; | 14 var field; |
| 16 C() : this.field = 'default'; | 15 C() : this.field = 'default'; |
| 17 C.named(this.field); | 16 C.named(this.field); |
| 18 | 17 |
| 19 get getter => 'get $field'; | 18 get getter => 'get $field'; |
| 20 set setter(v) => field = 'set $v'; | 19 set setter(v) => field = 'set $v'; |
| 21 method(x, y, z) => '$x+$y+$z'; | 20 method(x, y, z) => '$x+$y+$z'; |
| 22 toString() => 'a C'; | 21 toString() => 'a C'; |
| 23 | 22 |
| 24 noSuchMethod(invocation) => 'DNU'; | 23 noSuchMethod(invocation) => 'DNU'; |
| 25 | 24 |
| 26 static var staticField = 'initial'; | 25 static var staticField = 'initial'; |
| 27 static get staticGetter => 'sget $staticField'; | 26 static get staticGetter => 'sget $staticField'; |
| 28 static set staticSetter(v) => staticField = 'sset $v'; | 27 static set staticSetter(v) => staticField = 'sset $v'; |
| 29 static staticFunction(x, y) => "($x,$y)"; | 28 static staticFunction(x, y) => "($x,$y)"; |
| 30 } | 29 } |
| 31 | 30 |
| 32 var libraryField = 'a priori'; | 31 var libraryField = 'a priori'; |
| 33 get libraryGetter => 'lget $libraryField'; | 32 get libraryGetter => 'lget $libraryField'; |
| 34 set librarySetter(v) => libraryField = 'lset $v'; | 33 set librarySetter(v) => libraryField = 'lset $v'; |
| 35 libraryFunction(x,y) => '$x$y'; | 34 libraryFunction(x,y) => '$x$y'; |
| 36 | 35 |
| 37 Future expectValueThen(Future future, Function onValue) { | |
| 38 asyncStart(); | |
| 39 wrappedOnValue(resultIn) { | |
| 40 var resultOut = onValue(resultIn); | |
| 41 asyncEnd(); | |
| 42 return resultOut; | |
| 43 } | |
| 44 onError(e) { | |
| 45 Expect.fail("Value expected. ($e)"); | |
| 46 } | |
| 47 return future.then(wrappedOnValue, onError: onError); | |
| 48 } | |
| 49 | |
| 50 Future expectError(Future future, Function errorPredicate, String reason) { | |
| 51 asyncStart(); | |
| 52 onValue(result) { | |
| 53 Expect.fail("Error expected ($reason)"); | |
| 54 } | |
| 55 onError(e) { | |
| 56 asyncEnd(); | |
| 57 if (!errorPredicate(e)) { | |
| 58 Expect.fail("Unexpected error ($reason)"); | |
| 59 } | |
| 60 } | |
| 61 return future.then(onValue, onError: onError); | |
| 62 } | |
| 63 | |
| 64 bool isNoSuchMethodError(e) { | 36 bool isNoSuchMethodError(e) { |
| 65 return e is NoSuchMethodError; | 37 return e is NoSuchMethodError; |
| 66 } | 38 } |
| 67 | 39 |
| 68 testSync() { | 40 testSync() { |
| 69 var result; | 41 var result; |
| 70 | 42 |
| 71 // InstanceMirror invoke | 43 // InstanceMirror invoke |
| 72 C c = new C(); | 44 C c = new C(); |
| 73 InstanceMirror im = reflect(c); | 45 InstanceMirror im = reflect(c); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 lm.getField(const Symbol('libraryField')).reflectee); | 146 lm.getField(const Symbol('libraryField')).reflectee); |
| 175 result = lm.setField(const Symbol('libraryField'), 'lbar'); | 147 result = lm.setField(const Symbol('libraryField'), 'lbar'); |
| 176 Expect.equals('lbar', result.reflectee); | 148 Expect.equals('lbar', result.reflectee); |
| 177 Expect.equals('lbar', libraryField); | 149 Expect.equals('lbar', libraryField); |
| 178 Expect.equals('lbar', lm.getField(const Symbol('libraryField')).reflectee); | 150 Expect.equals('lbar', lm.getField(const Symbol('libraryField')).reflectee); |
| 179 Expect.throws(() => lm.setField(const Symbol('doesntExist'), 'lbar'), | 151 Expect.throws(() => lm.setField(const Symbol('doesntExist'), 'lbar'), |
| 180 isNoSuchMethodError, | 152 isNoSuchMethodError, |
| 181 'Not defined'); | 153 'Not defined'); |
| 182 } | 154 } |
| 183 | 155 |
| 184 testAsync() { | |
| 185 var future; | |
| 186 | |
| 187 // InstanceMirror invoke | |
| 188 C c = new C(); | |
| 189 InstanceMirror im = reflect(c); | |
| 190 future = im.invokeAsync(const Symbol('method'), [2,4,8]); | |
| 191 expectValueThen(future, (result) { | |
| 192 Expect.equals('2+4+8', result.reflectee); | |
| 193 }); | |
| 194 future = im.invokeAsync(const Symbol('method'), [im,im,im]); | |
| 195 expectValueThen(future, (result) { | |
| 196 Expect.equals('a C+a C+a C', result.reflectee); | |
| 197 }); | |
| 198 future = im.invokeAsync(const Symbol('doesntExist'), [2,4,8]); | |
| 199 expectValueThen(future, (result) { | |
| 200 Expect.equals('DNU', result.reflectee); | |
| 201 }); | |
| 202 future = im.invokeAsync(const Symbol('method'), [2, 4]); // Wrong arity. | |
| 203 expectValueThen(future, (result) { | |
| 204 Expect.equals('DNU', result.reflectee); | |
| 205 }); | |
| 206 | |
| 207 // InstanceMirror invokeGetter | |
| 208 future = im.getFieldAsync(const Symbol('getter')); | |
| 209 expectValueThen(future, (result) { | |
| 210 Expect.equals('get default', result.reflectee); | |
| 211 }); | |
| 212 future = im.getFieldAsync(const Symbol('field')); | |
| 213 expectValueThen(future, (result) { | |
| 214 Expect.equals('default', result.reflectee); | |
| 215 }); | |
| 216 future = im.getFieldAsync(const Symbol('doesntExist')); | |
| 217 expectValueThen(future, (result) { | |
| 218 Expect.equals('DNU', result.reflectee); | |
| 219 }); | |
| 220 | |
| 221 // InstanceMirror invokeSetter | |
| 222 future = im.setFieldAsync(const Symbol('setter'), 'foo'); | |
| 223 expectValueThen(future, (result) { | |
| 224 Expect.equals('foo', result.reflectee); | |
| 225 Expect.equals('set foo', c.field); | |
| 226 return im.setFieldAsync(const Symbol('field'), 'bar'); | |
| 227 }).then((result) { | |
| 228 Expect.equals('bar', result.reflectee); | |
| 229 Expect.equals('bar', c.field); | |
| 230 return im.setFieldAsync(const Symbol('field'), im); | |
| 231 }).then((result) { | |
| 232 Expect.equals(im.reflectee, result.reflectee); | |
| 233 Expect.equals(c, c.field); | |
| 234 }); | |
| 235 future = im.setFieldAsync(const Symbol('doesntExist'), 'bar'); | |
| 236 expectValueThen(future, (result) { | |
| 237 Expect.equals('bar', result.reflectee); | |
| 238 }); | |
| 239 | |
| 240 | |
| 241 // ClassMirror invoke | |
| 242 ClassMirror cm = reflectClass(C); | |
| 243 future = cm.invokeAsync(const Symbol('staticFunction'),[3,4]); | |
| 244 expectValueThen(future, (result) { | |
| 245 Expect.equals('(3,4)', result.reflectee); | |
| 246 }); | |
| 247 future = cm.invokeAsync(const Symbol('staticFunction'),[im,im]); | |
| 248 expectValueThen(future, (result) { | |
| 249 Expect.equals('(a C,a C)', result.reflectee); | |
| 250 }); | |
| 251 future = cm.invokeAsync(const Symbol('doesntExist'),[im,im]); | |
| 252 expectError(future, isNoSuchMethodError, 'Not defined'); | |
| 253 future = cm.invokeAsync(const Symbol('staticFunction'),[3]); | |
| 254 expectError(future, isNoSuchMethodError, 'Wrong arity'); | |
| 255 | |
| 256 // ClassMirror invokeGetter | |
| 257 C.staticField = 'initial'; // Reset from synchronous test. | |
| 258 future = cm.getFieldAsync(const Symbol('staticGetter')); | |
| 259 expectValueThen(future, (result) { | |
| 260 Expect.equals('sget initial', result.reflectee); | |
| 261 }); | |
| 262 future = cm.getFieldAsync(const Symbol('staticField')); | |
| 263 expectValueThen(future, (result) { | |
| 264 Expect.equals('initial', result.reflectee); | |
| 265 }); | |
| 266 future = cm.getFieldAsync(const Symbol('doesntExist')); | |
| 267 expectError(future, isNoSuchMethodError, 'Not defined'); | |
| 268 | |
| 269 // ClassMirror invokeSetter | |
| 270 future = cm.setFieldAsync(const Symbol('staticSetter'), 'sfoo'); | |
| 271 expectValueThen(future, (result) { | |
| 272 Expect.equals('sfoo', result.reflectee); | |
| 273 Expect.equals('sset sfoo', C.staticField); | |
| 274 return cm.setFieldAsync(const Symbol('staticField'), 'sbar'); | |
| 275 }).then((result) { | |
| 276 Expect.equals('sbar', result.reflectee); | |
| 277 Expect.equals('sbar', C.staticField); | |
| 278 return cm.setFieldAsync(const Symbol('staticField'), im); | |
| 279 }).then((result) { | |
| 280 Expect.equals(im.reflectee, result.reflectee); | |
| 281 Expect.equals(c, C.staticField); | |
| 282 }); | |
| 283 future = cm.setFieldAsync(const Symbol('doesntExist'), 'sbar'); | |
| 284 expectError(future, isNoSuchMethodError, 'Not defined'); | |
| 285 | |
| 286 // ClassMirror invokeConstructor | |
| 287 future = cm.newInstanceAsync(const Symbol(''), []); | |
| 288 expectValueThen(future, (result) { | |
| 289 Expect.isTrue(result.reflectee is C); | |
| 290 Expect.equals('default', result.reflectee.field); | |
| 291 }); | |
| 292 future = cm.newInstanceAsync(const Symbol('named'), ['my value']); | |
| 293 expectValueThen(future, (result) { | |
| 294 Expect.isTrue(result.reflectee is C); | |
| 295 Expect.equals('my value', result.reflectee.field); | |
| 296 }); | |
| 297 future = cm.newInstanceAsync(const Symbol('named'), [im]); | |
| 298 expectValueThen(future, (result) { | |
| 299 Expect.isTrue(result.reflectee is C); | |
| 300 Expect.equals(c, result.reflectee.field); | |
| 301 }); | |
| 302 future = cm.newInstanceAsync(const Symbol('doesntExist'), ['my value']); | |
| 303 expectError(future, isNoSuchMethodError, 'Not defined'); | |
| 304 future = cm.newInstanceAsync(const Symbol('named'), []); | |
| 305 expectError(future, isNoSuchMethodError, 'Wrong arity'); | |
| 306 | |
| 307 | |
| 308 // LibraryMirror invoke | |
| 309 LibraryMirror lm = cm.owner; | |
| 310 future = lm.invokeAsync(const Symbol('libraryFunction'),[':',')']); | |
| 311 expectValueThen(future, (result) { | |
| 312 Expect.equals(':)', result.reflectee); | |
| 313 }); | |
| 314 future = lm.invokeAsync(const Symbol('libraryFunction'),[im,im]); | |
| 315 expectValueThen(future, (result) { | |
| 316 Expect.equals('a Ca C', result.reflectee); | |
| 317 }); | |
| 318 future = lm.invokeAsync(const Symbol('doesntExist'),[im,im]); | |
| 319 expectError(future, isNoSuchMethodError, 'Not defined'); | |
| 320 future = lm.invokeAsync(const Symbol('libraryFunction'),[':']); | |
| 321 expectError(future, isNoSuchMethodError, 'Wrong arity'); | |
| 322 | |
| 323 // LibraryMirror invokeGetter | |
| 324 libraryField = 'a priori'; // Reset from synchronous test. | |
| 325 future = lm.getFieldAsync(const Symbol('libraryGetter')); | |
| 326 expectValueThen(future, (result) { | |
| 327 Expect.equals('lget a priori', result.reflectee); | |
| 328 }); | |
| 329 future = lm.getFieldAsync(const Symbol('libraryField')); | |
| 330 expectValueThen(future, (result) { | |
| 331 Expect.equals('a priori', result.reflectee); | |
| 332 }); | |
| 333 future = lm.getFieldAsync(const Symbol('doesntExist')); | |
| 334 expectError(future, isNoSuchMethodError, 'Not defined'); | |
| 335 | |
| 336 // LibraryMirror invokeSetter | |
| 337 future = lm.setFieldAsync(const Symbol('librarySetter'), 'lfoo'); | |
| 338 expectValueThen(future, (result) { | |
| 339 Expect.equals('lfoo', result.reflectee); | |
| 340 Expect.equals('lset lfoo', libraryField); | |
| 341 return lm.setFieldAsync(const Symbol('libraryField'), 'lbar'); | |
| 342 }).then((result) { | |
| 343 Expect.equals('lbar', result.reflectee); | |
| 344 Expect.equals('lbar', libraryField); | |
| 345 return lm.setFieldAsync(const Symbol('libraryField'), im); | |
| 346 }).then((result) { | |
| 347 Expect.equals(im.reflectee, result.reflectee); | |
| 348 Expect.equals(c, libraryField); | |
| 349 }); | |
| 350 future = lm.setFieldAsync(const Symbol('doesntExist'), 'lbar'); | |
| 351 expectError(future, isNoSuchMethodError, 'Not defined'); | |
| 352 } | |
| 353 | |
| 354 main() { | 156 main() { |
| 355 testSync(); | 157 testSync(); |
| 356 testAsync(); | |
| 357 } | 158 } |
| OLD | NEW |