| 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 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 10 import "package:async_helper/async_helper.dart"; | |
| 11 | 10 |
| 12 class C { | 11 class C { |
| 13 var field; | 12 var field; |
| 14 C() : this.field = 'default'; | 13 C() : this.field = 'default'; |
| 15 C.named(this.field); | 14 C.named(this.field); |
| 16 | 15 |
| 17 get getter => 'get $field'; | 16 get getter => 'get $field'; |
| 18 set setter(v) => field = 'set $v'; | 17 set setter(v) => field = 'set $v'; |
| 19 method(x, y, z) => '$x+$y+$z'; | 18 method(x, y, z) => '$x+$y+$z'; |
| 20 toString() => 'a C'; | 19 toString() => 'a C'; |
| 21 | 20 |
| 22 noSuchMethod(invocation) => 'DNU'; | 21 noSuchMethod(invocation) => 'DNU'; |
| 23 | 22 |
| 24 static var staticField = 'initial'; | 23 static var staticField = 'initial'; |
| 25 static get staticGetter => 'sget $staticField'; | 24 static get staticGetter => 'sget $staticField'; |
| 26 static set staticSetter(v) => staticField = 'sset $v'; | 25 static set staticSetter(v) => staticField = 'sset $v'; |
| 27 static staticFunction(x, y) => "($x,$y)"; | 26 static staticFunction(x, y) => "($x,$y)"; |
| 28 } | 27 } |
| 29 | 28 |
| 30 var libraryField = 'a priori'; | 29 var libraryField = 'a priori'; |
| 31 get libraryGetter => 'lget $libraryField'; | 30 get libraryGetter => 'lget $libraryField'; |
| 32 set librarySetter(v) => libraryField = 'lset $v'; | 31 set librarySetter(v) => libraryField = 'lset $v'; |
| 33 libraryFunction(x,y) => '$x$y'; | 32 libraryFunction(x,y) => '$x$y'; |
| 34 | 33 |
| 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) { | 34 bool isNoSuchMethodError(e) { |
| 63 return e is NoSuchMethodError; | 35 return e is NoSuchMethodError; |
| 64 } | 36 } |
| 65 | 37 |
| 66 testSync() { | 38 testSync() { |
| 67 var result; | 39 var result; |
| 68 | 40 |
| 69 // InstanceMirror invoke | 41 // InstanceMirror invoke |
| 70 C c = new C(); | 42 C c = new C(); |
| 71 InstanceMirror im = reflect(c); | 43 InstanceMirror im = reflect(c); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 lm.getField(const Symbol('libraryField')).reflectee); | 144 lm.getField(const Symbol('libraryField')).reflectee); |
| 173 result = lm.setField(const Symbol('libraryField'), 'lbar'); | 145 result = lm.setField(const Symbol('libraryField'), 'lbar'); |
| 174 Expect.equals('lbar', result.reflectee); | 146 Expect.equals('lbar', result.reflectee); |
| 175 Expect.equals('lbar', libraryField); | 147 Expect.equals('lbar', libraryField); |
| 176 Expect.equals('lbar', lm.getField(const Symbol('libraryField')).reflectee); | 148 Expect.equals('lbar', lm.getField(const Symbol('libraryField')).reflectee); |
| 177 Expect.throws(() => lm.setField(const Symbol('doesntExist'), 'lbar'), | 149 Expect.throws(() => lm.setField(const Symbol('doesntExist'), 'lbar'), |
| 178 isNoSuchMethodError, | 150 isNoSuchMethodError, |
| 179 'Not defined'); | 151 'Not defined'); |
| 180 } | 152 } |
| 181 | 153 |
| 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() { | 154 main() { |
| 353 testSync(); | 155 testSync(); |
| 354 testAsync(); | |
| 355 } | 156 } |
| OLD | NEW |