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