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"); | |
|
ahe
2013/07/24 09:43:35
How about:
Expect.fail("Value expected. ($e)");
| |
| 43 } | |
| 44 return future.then(onValue, onError: onError); | |
| 45 } | |
| 46 | |
| 47 shouldYieldError(future, errorPredicate, reason){ | |
|
ahe
2013/07/24 09:43:35
Missing space between ) and {.
| |
| 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 // TODO(11957): Remove MirroredCompilationError. | |
| 62 bool isInvokeError(e) { | |
| 63 return e is MirroredCompilationError || 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 im.setField(const Symbol('setter'), 'foo'); | |
| 89 Expect.equals('set foo', c.field); | |
| 90 im.setField(const Symbol('field'), 'bar'); | |
| 91 Expect.equals('bar', c.field); | |
| 92 // The following invocation we merely expect not to throw an expection | |
| 93 // because noSuchMethod is implemented. There is no side-effect to test for. | |
| 94 im.setField(const Symbol('doesntExist'), 'bar'); | |
| 95 | |
| 96 // ClassMirror invoke | |
| 97 ClassMirror cm = reflectClass(C); | |
| 98 result = cm.invoke(const Symbol('staticFunction'),[3,4]); | |
| 99 Expect.equals('(3,4)', result.reflectee); | |
| 100 Expect.throws(() => cm.invoke(const Symbol('doesntExist'),[3,4]), | |
| 101 isInvokeError, | |
| 102 'Not defined'); | |
| 103 Expect.throws(() => cm.invoke(const Symbol('staticFunction'),[3]), | |
| 104 isInvokeError, | |
| 105 'Wrong arity'); | |
| 106 | |
| 107 // ClassMirror invokeGetter | |
| 108 result = cm.getField(const Symbol('staticGetter')); | |
| 109 Expect.equals('sget initial', result.reflectee); | |
| 110 result = cm.getField(const Symbol('staticField')); | |
| 111 Expect.equals('initial', result.reflectee); | |
| 112 Expect.throws(() => cm.getField(const Symbol('doesntExist')), | |
| 113 isInvokeError, | |
| 114 'Not defined'); | |
| 115 | |
| 116 // ClassMirror invokeSetter | |
| 117 cm.setField(const Symbol('staticSetter'), 'sfoo'); | |
| 118 Expect.equals('sset sfoo', C.staticField); | |
| 119 cm.setField(const Symbol('staticField'), 'sbar'); | |
| 120 Expect.equals('sbar', C.staticField); | |
| 121 Expect.throws(() => cm.setField(const Symbol('doesntExist'), 'sbar'), | |
| 122 isInvokeError, | |
| 123 'Not defined'); | |
| 124 | |
| 125 // ClassMirror invokeConstructor | |
| 126 result = cm.newInstance(const Symbol(''), []); | |
| 127 Expect.isTrue(result.reflectee is C); | |
| 128 Expect.equals('default', result.reflectee.field); | |
| 129 result = cm.newInstance(const Symbol('named'), ['my value']); | |
| 130 Expect.isTrue(result.reflectee is C); | |
| 131 Expect.equals('my value', result.reflectee.field); | |
| 132 Expect.throws(() => cm.newInstance(const Symbol('doesntExist'), ['my value']), | |
| 133 isInvokeError, | |
| 134 'Not defined'); | |
| 135 Expect.throws(() => cm.newInstance(const Symbol('named'), []), | |
| 136 isInvokeError, | |
| 137 'Wrong arity'); | |
| 138 | |
| 139 // LibraryMirror invoke | |
| 140 LibraryMirror lm = cm.owner; | |
| 141 result = lm.invoke(const Symbol('libraryFunction'),[':',')']); | |
| 142 Expect.equals(':)', result.reflectee); | |
| 143 Expect.throws(() => lm.invoke(const Symbol('doesntExist'), [':',')']), | |
| 144 isInvokeError, | |
| 145 'Not defined'); | |
| 146 Expect.throws(() => lm.invoke(const Symbol('libraryFunction'), [':']), | |
| 147 isInvokeError, | |
| 148 'Wrong arity'); | |
| 149 | |
| 150 // LibraryMirror invokeGetter | |
| 151 result = lm.getField(const Symbol('libraryGetter')); | |
| 152 Expect.equals('lget a priori', result.reflectee); | |
| 153 result = lm.getField(const Symbol('libraryField')); | |
| 154 Expect.equals('a priori', result.reflectee); | |
| 155 Expect.throws(() => lm.getField(const Symbol('doesntExist')), | |
| 156 isInvokeError, | |
| 157 'Not defined'); | |
| 158 | |
| 159 // LibraryMirror invokeSetter | |
| 160 lm.setField(const Symbol('librarySetter'), 'lfoo'); | |
| 161 Expect.equals('lset lfoo', libraryField); | |
| 162 lm.setField(const Symbol('libraryField'), 'lbar'); | |
| 163 Expect.equals('lbar', libraryField); | |
| 164 Expect.throws(() => lm.setField(const Symbol('doesntExist'), 'lbar'), | |
| 165 isInvokeError, | |
| 166 'Not defined'); | |
| 167 } | |
| 168 | |
| 169 testAsync() { | |
| 170 var future; | |
| 171 | |
| 172 // InstanceMirror invoke | |
| 173 C c = new C(); | |
| 174 InstanceMirror im = reflect(c); | |
| 175 future = im.invokeAsync(const Symbol('method'), [2,4,8]); | |
| 176 shouldYieldValue(future).then((result) { | |
| 177 Expect.equals('2+4+8', result.reflectee); | |
| 178 }); | |
| 179 future = im.invokeAsync(const Symbol('method'), [im,im,im]); | |
| 180 shouldYieldValue(future).then((result) { | |
| 181 Expect.equals('a C+a C+a C', result.reflectee); | |
| 182 }); | |
| 183 future = im.invokeAsync(const Symbol('doesntExist'), [2,4,8]); | |
| 184 shouldYieldValue(future).then((result) { | |
| 185 Expect.equals('DNU', result.reflectee); | |
| 186 }); | |
| 187 future = im.invokeAsync(const Symbol('method'), [2, 4]); // Wrong arity. | |
| 188 shouldYieldValue(future).then((result) { | |
| 189 Expect.equals('DNU', result.reflectee); | |
| 190 }); | |
| 191 | |
| 192 // InstanceMirror invokeGetter | |
| 193 future = im.getFieldAsync(const Symbol('getter')); | |
| 194 shouldYieldValue(future).then((result) { | |
| 195 Expect.equals('get default', result.reflectee); | |
| 196 }); | |
| 197 future = im.getFieldAsync(const Symbol('field')); | |
| 198 shouldYieldValue(future).then((result) { | |
| 199 Expect.equals('default', result.reflectee); | |
| 200 }); | |
| 201 future = im.getFieldAsync(const Symbol('doesntExist')); | |
| 202 shouldYieldValue(future).then((result) { | |
| 203 Expect.equals('DNU', result.reflectee); | |
| 204 }); | |
| 205 | |
| 206 // InstanceMirror invokeSetter | |
| 207 future = im.setFieldAsync(const Symbol('setter'), 'foo'); | |
| 208 shouldYieldValue(future).then((result) { | |
| 209 Expect.equals('set foo', c.field); | |
| 210 var future2 = im.setFieldAsync(const Symbol('field'), 'bar'); | |
| 211 shouldYieldValue(future2).then((result) { | |
| 212 Expect.equals('bar', c.field); | |
| 213 var future3 = im.setFieldAsync(const Symbol('field'), im); | |
| 214 shouldYieldValue(future3).then((result) { | |
| 215 Expect.equals(c, c.field); | |
| 216 }); | |
| 217 }); | |
| 218 }); | |
| 219 future = im.setFieldAsync(const Symbol('doesntExist'), 'bar'); | |
| 220 shouldYieldValue(future); | |
| 221 | |
| 222 | |
| 223 // ClassMirror invoke | |
| 224 ClassMirror cm = reflectClass(C); | |
| 225 future = cm.invokeAsync(const Symbol('staticFunction'),[3,4]); | |
| 226 shouldYieldValue(future).then((result) { | |
| 227 Expect.equals('(3,4)', result.reflectee); | |
| 228 }); | |
| 229 future = cm.invokeAsync(const Symbol('staticFunction'),[im,im]); | |
| 230 shouldYieldValue(future).then((result) { | |
| 231 Expect.equals('(a C,a C)', result.reflectee); | |
| 232 }); | |
| 233 future = cm.invokeAsync(const Symbol('doesntExist'),[im,im]); | |
| 234 shouldYieldError(future, isInvokeError, 'Not defined'); | |
| 235 future = cm.invokeAsync(const Symbol('staticFunction'),[3]); | |
| 236 shouldYieldError(future, isInvokeError, 'Wrong arity'); | |
| 237 | |
| 238 // ClassMirror invokeGetter | |
| 239 C.staticField = 'initial'; // Reset from synchronous test. | |
| 240 future = cm.getFieldAsync(const Symbol('staticGetter')); | |
| 241 shouldYieldValue(future).then((result) { | |
| 242 Expect.equals('sget initial', result.reflectee); | |
| 243 }); | |
| 244 future = cm.getFieldAsync(const Symbol('staticField')); | |
| 245 shouldYieldValue(future).then((result) { | |
| 246 Expect.equals('initial', result.reflectee); | |
| 247 }); | |
| 248 future = cm.getFieldAsync(const Symbol('doesntExist')); | |
| 249 shouldYieldError(future, isInvokeError, 'Not defined'); | |
| 250 | |
| 251 // ClassMirror invokeSetter | |
| 252 future = cm.setFieldAsync(const Symbol('staticSetter'), 'sfoo'); | |
| 253 shouldYieldValue(future).then((result) { | |
| 254 Expect.equals('sset sfoo', C.staticField); | |
| 255 var future2 = cm.setFieldAsync(const Symbol('staticField'), 'sbar'); | |
| 256 shouldYieldValue(future2).then((result) { | |
| 257 Expect.equals('sbar', C.staticField); | |
| 258 var future3 = cm.setFieldAsync(const Symbol('staticField'), im);; | |
| 259 shouldYieldValue(future3).then((result) { | |
| 260 Expect.equals(c, C.staticField); | |
| 261 }); | |
| 262 }); | |
| 263 }); | |
| 264 future = cm.setFieldAsync(const Symbol('doesntExist'), 'sbar'); | |
| 265 shouldYieldError(future, isInvokeError, 'Not defined'); | |
| 266 | |
| 267 // ClassMirror invokeConstructor | |
| 268 future = cm.newInstanceAsync(const Symbol(''), []); | |
| 269 shouldYieldValue(future).then((result) { | |
| 270 Expect.isTrue(result.reflectee is C); | |
| 271 Expect.equals('default', result.reflectee.field); | |
| 272 }); | |
| 273 future = cm.newInstanceAsync(const Symbol('named'), ['my value']); | |
| 274 shouldYieldValue(future).then((result) { | |
| 275 Expect.isTrue(result.reflectee is C); | |
| 276 Expect.equals('my value', result.reflectee.field); | |
| 277 }); | |
| 278 future = cm.newInstanceAsync(const Symbol('named'), [im]); | |
| 279 shouldYieldValue(future).then((result) { | |
| 280 Expect.isTrue(result.reflectee is C); | |
| 281 Expect.equals(c, result.reflectee.field); | |
| 282 }); | |
| 283 future = cm.newInstanceAsync(const Symbol('doesntExist'), ['my value']); | |
| 284 shouldYieldError(future, isInvokeError, 'Not defined'); | |
| 285 future = cm.newInstanceAsync(const Symbol('named'), []); | |
| 286 shouldYieldError(future, isInvokeError, 'Wrong arity'); | |
| 287 | |
| 288 | |
| 289 // LibraryMirror invoke | |
| 290 LibraryMirror lm = cm.owner; | |
| 291 future = lm.invokeAsync(const Symbol('libraryFunction'),[':',')']); | |
| 292 shouldYieldValue(future).then((result) { | |
| 293 Expect.equals(':)', result.reflectee); | |
| 294 }); | |
| 295 future = lm.invokeAsync(const Symbol('libraryFunction'),[im,im]); | |
| 296 shouldYieldValue(future).then((result) { | |
| 297 Expect.equals('a Ca C', result.reflectee); | |
| 298 }); | |
| 299 future = lm.invokeAsync(const Symbol('doesntExist'),[im,im]); | |
| 300 shouldYieldError(future, isInvokeError, 'Not defined'); | |
| 301 future = lm.invokeAsync(const Symbol('libraryFunction'),[':']); // Wrong arit y. | |
| 302 shouldYieldError(future, isInvokeError, 'Wrong arity'); | |
| 303 | |
| 304 // LibraryMirror invokeGetter | |
| 305 libraryField = 'a priori'; // Reset from synchronous test. | |
| 306 future = lm.getFieldAsync(const Symbol('libraryGetter')); | |
| 307 shouldYieldValue(future).then((result) { | |
| 308 Expect.equals('lget a priori', result.reflectee); | |
| 309 }); | |
| 310 future = lm.getFieldAsync(const Symbol('libraryField')); | |
| 311 shouldYieldValue(future).then((result) { | |
| 312 Expect.equals('a priori', result.reflectee); | |
| 313 }); | |
| 314 future = lm.getFieldAsync(const Symbol('doesntExist')); | |
| 315 shouldYieldError(future, isInvokeError, 'Not defined'); | |
| 316 | |
| 317 // LibraryMirror invokeSetter | |
| 318 future = lm.setFieldAsync(const Symbol('librarySetter'), 'lfoo'); | |
| 319 shouldYieldValue(future).then((result) { | |
| 320 Expect.equals('lset lfoo', libraryField); | |
| 321 var future2 = lm.setFieldAsync(const Symbol('libraryField'), 'lbar'); | |
| 322 shouldYieldValue(future2).then((result) { | |
| 323 Expect.equals('lbar', libraryField); | |
| 324 var future3 = lm.setFieldAsync(const Symbol('libraryField'), im); | |
| 325 shouldYieldValue(future3).then((result) { | |
| 326 Expect.equals(c, libraryField); | |
| 327 }); | |
| 328 }); | |
| 329 }); | |
| 330 future = lm.setFieldAsync(const Symbol('doesntExist'), 'lbar'); | |
| 331 shouldYieldError(future, isInvokeError, 'Not defined'); | |
| 332 } | |
| 333 | |
| 334 main() { | |
| 335 testSync(); | |
| 336 testAsync(); | |
| 337 } | |
| OLD | NEW |