Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, 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 async_await_test; | |
| 6 | |
| 7 import "package:unittest/unittest.dart"; | |
| 8 import "dart:async"; | |
| 9 | |
| 10 main() { | |
| 11 group("basic", () { | |
| 12 test("async w/o await", () { | |
| 13 f() async { return id(42); } | |
| 14 return expect42(f()); | |
| 15 }); | |
| 16 | |
| 17 test("async waits", () { | |
| 18 // Calling an "async" function won't do anything immediately. | |
| 19 var result = []; | |
| 20 f() async { | |
| 21 result.add(1); | |
| 22 return id(42); | |
| 23 }; | |
| 24 var future = f(); | |
| 25 result.add(0); | |
| 26 return future.whenComplete(() { | |
| 27 expect(result, equals([0, 1])); | |
| 28 }); | |
| 29 }); | |
| 30 | |
| 31 test("async throws", () { | |
| 32 f() async { | |
| 33 throw "err"; | |
| 34 return id(42); | |
| 35 } | |
| 36 return throwsErr(f()); | |
| 37 }); | |
| 38 | |
| 39 test("await future", () { | |
| 40 f() async { | |
| 41 var v = await new Future.value(42); | |
| 42 return v; | |
| 43 }; | |
| 44 return expect42(f()); | |
| 45 }); | |
| 46 | |
| 47 test("await value", () { | |
| 48 f() async { | |
| 49 var v = await id(42); | |
| 50 return v; | |
| 51 }; | |
| 52 return expect42(f()); | |
| 53 }); | |
| 54 | |
| 55 test("await null", () { | |
| 56 f() async { | |
| 57 var v = await null; | |
| 58 expect(v, equals(null)); | |
| 59 }; | |
| 60 return f(); | |
| 61 }); | |
| 62 | |
| 63 test("await await", () { | |
| 64 f() async { | |
| 65 return await await new Future.value(42); | |
| 66 } | |
| 67 return expect42(f()); | |
| 68 }); | |
| 69 | |
| 70 test("await fake value future", () { | |
| 71 f() async { | |
| 72 return await new FakeValueFuture(42); | |
| 73 } | |
| 74 return expect42(f()); | |
| 75 }); | |
| 76 | |
| 77 test("await fake error future", () { | |
| 78 f() async { | |
| 79 return await new FakeErrorFuture("err"); | |
| 80 } | |
| 81 return throwsErr(f()); | |
| 82 }); | |
| 83 | |
| 84 test("await value is delayed", () { | |
| 85 f() async { | |
| 86 bool x = false; | |
| 87 scheduleMicrotask(() { x = true; }); | |
| 88 var y = await true; | |
| 89 expect(x, equals(y)); | |
| 90 } | |
| 91 return f(); | |
| 92 }); | |
| 93 | |
| 94 test("await throw", () { | |
| 95 f() async { | |
| 96 await (throw "err"); // Check grammar: Are parentheses necessary? | |
| 97 return id(42); | |
| 98 } | |
| 99 return throwsErr(f()); | |
| 100 }); | |
| 101 | |
| 102 test("throw before await", () { | |
| 103 f() async { | |
| 104 var x = throw "err"; | |
| 105 await x; // Check grammar: Are parentheses necessary? | |
| 106 return id(42); | |
| 107 } | |
| 108 return throwsErr(f()); | |
| 109 }); | |
| 110 | |
| 111 test("async await error", () { | |
| 112 f() async { | |
| 113 await new Future.error("err"); | |
| 114 return id(42); | |
| 115 } | |
| 116 return throwsErr(f()); | |
| 117 }); | |
| 118 | |
| 119 test("async flattens futures", () { | |
| 120 f() async { | |
| 121 return new Future.value(42); // Not awaited. | |
| 122 }; | |
| 123 return f().then((v) { | |
| 124 expect(v, equals(42)); // And not a Future with value 42. | |
| 125 }); | |
| 126 }); | |
| 127 | |
| 128 test("async flattens futures, error", () { | |
| 129 f() async { | |
| 130 return new Future.error("err"); // Not awaited. | |
| 131 }; | |
| 132 return throwsErr(f()); | |
| 133 }); | |
| 134 | |
| 135 test("await for", () { | |
| 136 f(s) async { | |
| 137 int i = 0; | |
| 138 await for (int v in s) { | |
| 139 i += v; | |
| 140 } | |
| 141 return i; | |
| 142 } | |
| 143 return f(mkStream()).then((v) { | |
| 144 expect(v, equals(45)); // 0 + 1 + ... + 9 | |
| 145 }); | |
| 146 }); | |
| 147 | |
| 148 test("await for w/ await", () { | |
| 149 f(s) async { | |
| 150 int i = 0; | |
| 151 await for (int v in s) { | |
| 152 i += await new Future.value(v); | |
| 153 } | |
| 154 return i; | |
| 155 } | |
| 156 return f(mkStream()).then((v) { | |
| 157 expect(v, equals(45)); // 0 + 1 + ... + 9 | |
| 158 }); | |
| 159 }); | |
| 160 | |
| 161 test("await for empty", () { | |
| 162 f(s) async { | |
| 163 int v = 0; | |
| 164 await for (int i in s) { | |
| 165 v += i; | |
| 166 } | |
| 167 return v; | |
| 168 } | |
| 169 var s = (new StreamController()..close()).stream; | |
| 170 return f(s).then((v) { | |
| 171 expect(v, equals(0)); | |
| 172 }); | |
| 173 }); | |
| 174 }); | |
| 175 | |
| 176 group("for", () { | |
| 177 test("await in for-loop", () { | |
| 178 f() async { | |
| 179 int v = 0; | |
| 180 for (int i = 0; i < 10; i++) { | |
| 181 v += await new Future.value(42); | |
| 182 } | |
| 183 return v; | |
| 184 } | |
| 185 return f().then((v) { | |
| 186 expect(v, equals(10 * id(42))); | |
| 187 }); | |
| 188 }); | |
| 189 | |
| 190 test("await in for-init", () { | |
| 191 f() async { | |
| 192 int v = 0; | |
| 193 for (int i = await new Future.value(42); i >= 0; i -= 10) { | |
| 194 v += 10; | |
| 195 } | |
| 196 return v; | |
| 197 } | |
| 198 return f().then((v) { | |
| 199 expect(v, equals(10 * 5)); | |
| 200 }); | |
| 201 }); | |
| 202 | |
| 203 test("await in for-test", () { | |
| 204 f() async { | |
| 205 int v = 0; | |
| 206 for (int i = 0; i < await new Future.value(42); i += 10) { | |
| 207 v += 10; | |
| 208 } | |
| 209 return v; | |
| 210 } | |
| 211 return f().then((v) { | |
| 212 expect(v, equals(10 * 5)); | |
| 213 }); | |
| 214 }); | |
| 215 | |
| 216 test("await in for-incr", () { | |
| 217 f() async { | |
| 218 int v = 0; | |
| 219 for (int i = 0; i < 100; i += await new Future.value(42)) { | |
| 220 v += 10; | |
| 221 } | |
| 222 return v; | |
| 223 } | |
| 224 return f().then((v) { | |
| 225 expect(v, equals(10 * 3)); | |
| 226 }); | |
| 227 }); | |
| 228 | |
| 229 test("await err in for-loop", () { | |
| 230 f() async { | |
| 231 int v = 0; | |
| 232 for (int i = 0; i < 10; i++) { | |
| 233 v += await new Future.error("err"); | |
| 234 } | |
| 235 return v; | |
| 236 } | |
| 237 return throwsErr(f()); | |
| 238 }); | |
| 239 | |
| 240 test("await err in for-init", () { | |
| 241 f() async { | |
| 242 int v = 0; | |
| 243 for (int i = await new Future.error("err"); i >= 0; i -= 10) { | |
| 244 v += 10; | |
| 245 } | |
| 246 return v; | |
| 247 } | |
| 248 return throwsErr(f()); | |
| 249 }); | |
| 250 | |
| 251 test("await err in for-test", () { | |
| 252 f() async { | |
| 253 int v = 0; | |
| 254 for (int i = 0; i < await new Future.error("err"); i += 10) { | |
| 255 v += 10; | |
| 256 } | |
| 257 return v; | |
| 258 } | |
| 259 return throwsErr(f()); | |
| 260 }); | |
| 261 | |
| 262 test("await err in for-incr", () { | |
| 263 f() async { | |
| 264 int v = 0; | |
| 265 for (int i = 0; i < 100; i += await new Future.error("err")) { | |
| 266 v += 10; | |
| 267 } | |
| 268 return v; | |
| 269 } | |
| 270 return throwsErr(f()); | |
| 271 }); | |
| 272 | |
| 273 test("await in empty for-loop", () { | |
| 274 f() async { | |
| 275 int v = 0; | |
| 276 for (int i = 0; i > 0; i += 1) { | |
| 277 v += await new Future.value(42); | |
| 278 } | |
| 279 return v; | |
| 280 } | |
| 281 return f().then((v) { | |
| 282 expect(v, equals(0)); | |
| 283 }); | |
| 284 }); | |
| 285 | |
| 286 test("await in empty for-loop 2", () { | |
| 287 f() async { | |
| 288 int v = 0; | |
| 289 for (int i = 0; i > 0; i += await new Future.value(1)) { | |
| 290 v += 1; | |
| 291 } | |
| 292 return v; | |
| 293 } | |
| 294 return f().then((v) { | |
| 295 expect(v, equals(0)); | |
| 296 }); | |
| 297 }); | |
| 298 | |
| 299 test("break before await in for-loop", () { | |
| 300 f() async { | |
| 301 int v = 0; | |
| 302 for (int i = 0; i < 10; i += 1) { | |
| 303 if (i == 2) break; | |
| 304 v += await new Future.value(42); | |
| 305 } | |
| 306 return v; | |
| 307 } | |
| 308 return f().then((v) { | |
| 309 expect(v, equals(42 * 2)); | |
| 310 }); | |
| 311 }); | |
| 312 | |
| 313 test("break before await in for-loop 2", () { | |
| 314 f() async { | |
| 315 int v = 0; | |
| 316 for (int i = 0; i < 10; i += await new Future.value(1)) { | |
| 317 if (i == 2) break; | |
| 318 v += id(42); | |
| 319 } | |
| 320 return v; | |
| 321 } | |
| 322 return f().then((v) { | |
| 323 expect(v, equals(42 * 2)); | |
| 324 }); | |
| 325 }); | |
| 326 | |
| 327 test("continue before await", () { | |
| 328 f() async { | |
| 329 int v = 0; | |
| 330 for (int i = 0; i < 10; i += 1) { | |
| 331 if (i == 2) continue; | |
| 332 v += await new Future.value(42); | |
| 333 } | |
| 334 return v; | |
| 335 } | |
| 336 return f().then((v) { | |
| 337 expect(v, equals(42 * 9)); | |
| 338 }); | |
| 339 }); | |
| 340 | |
| 341 test("continue after await", () { | |
| 342 f() async { | |
| 343 int v = 0; | |
| 344 for (int i = 0; i < 10; i += 1) { | |
| 345 var j = await new Future.value(42); | |
| 346 if (i == 2) continue; | |
| 347 v += j; | |
| 348 } | |
| 349 return v; | |
| 350 } | |
| 351 return f().then((v) { | |
| 352 expect(v, equals(42 * 9)); | |
| 353 }); | |
| 354 }); | |
| 355 }); | |
| 356 | |
| 357 group("while", () { | |
| 358 test("await in while-loop", () { | |
| 359 f() async { | |
| 360 int v = 0; | |
| 361 int i = 0; | |
| 362 while (i < 10) { | |
| 363 v += await new Future.value(42); | |
| 364 i++; | |
| 365 } | |
| 366 return v; | |
| 367 } | |
| 368 return f().then((v) { | |
| 369 expect(v, equals(10 * id(42))); | |
| 370 }); | |
| 371 }); | |
| 372 | |
| 373 test("await in while-test", () { | |
| 374 f() async { | |
| 375 int v = 0; | |
| 376 int i = 0; | |
| 377 while (i < await new Future.value(42)) { | |
| 378 v += 10; | |
| 379 i += 10; | |
| 380 } | |
| 381 return v; | |
| 382 } | |
| 383 return f().then((v) { | |
| 384 expect(v, equals(10 * 5)); | |
| 385 }); | |
| 386 }); | |
| 387 | |
| 388 test("await err in loop", () { | |
| 389 f() async { | |
| 390 int v = 0; | |
| 391 int i = 0; | |
| 392 while (i < 10) { | |
| 393 v += await new Future.error("err"); | |
| 394 i++; | |
| 395 } | |
| 396 return v; | |
| 397 } | |
| 398 return throwsErr(f()); | |
| 399 }); | |
| 400 | |
| 401 test("await err in test", () { | |
| 402 f() async { | |
| 403 int v = 0; | |
| 404 int i = 0; | |
| 405 while (i < await new Future.error("err")) { | |
| 406 v += 10; | |
| 407 i += 10; | |
| 408 } | |
| 409 return v; | |
| 410 } | |
| 411 return throwsErr(f()); | |
| 412 }); | |
| 413 | |
| 414 test("break before await", () { | |
| 415 f() async { | |
| 416 int v = 0; | |
| 417 int i = 0; | |
| 418 while (i < 10) { | |
| 419 if (i == 2) break; | |
| 420 v += await new Future.value(42); | |
| 421 i += 1; | |
| 422 } | |
| 423 return v; | |
| 424 } | |
| 425 return f().then((v) { | |
| 426 expect(v, equals(42 * 2)); | |
| 427 }); | |
| 428 }); | |
| 429 | |
| 430 test("break after await", () { | |
| 431 f() async { | |
| 432 int v = 0; | |
| 433 int i = 0; | |
| 434 while (i < 10) { | |
| 435 v += await new Future.value(42); | |
| 436 if (i == 2) break; | |
| 437 i += 1; | |
| 438 } | |
| 439 return v; | |
| 440 } | |
| 441 return f().then((v) { | |
| 442 expect(v, equals(42 * 3)); | |
| 443 }); | |
| 444 }); | |
| 445 | |
| 446 test("continue before await", () { | |
| 447 f() async { | |
| 448 int v = 0; | |
| 449 int i = 0; | |
| 450 while (i < 10) { | |
| 451 i += 1; | |
| 452 if (i == 2) continue; | |
| 453 v += await new Future.value(42); | |
| 454 } | |
| 455 return v; | |
| 456 } | |
| 457 return f().then((v) { | |
| 458 expect(v, equals(42 * 9)); | |
| 459 }); | |
| 460 }); | |
| 461 | |
| 462 test("continue after await", () { | |
| 463 f() async { | |
| 464 int v = 0; | |
| 465 int i = 0; | |
| 466 while (i < 10) { | |
| 467 i += 1; | |
| 468 int j = await new Future.value(42); | |
| 469 if (i == 2) continue; | |
| 470 v += j; | |
| 471 } | |
| 472 return v; | |
| 473 } | |
| 474 return f().then((v) { | |
| 475 expect(v, equals(42 * 9)); | |
| 476 }); | |
| 477 }); | |
| 478 }); | |
| 479 | |
| 480 group("do-while", () { | |
| 481 test("await in loop", () { | |
| 482 f() async { | |
| 483 int v = 0; | |
| 484 int i = 0; | |
| 485 do { | |
| 486 v += await new Future.value(42); | |
| 487 i++; | |
| 488 } while (i < 10); | |
| 489 return v; | |
| 490 } | |
| 491 return f().then((v) { | |
| 492 expect(v, equals(10 * id(42))); | |
| 493 }); | |
| 494 }); | |
| 495 | |
| 496 test("await in test", () { | |
| 497 f() async { | |
| 498 int v = 0; | |
| 499 int i = 0; | |
| 500 do { | |
| 501 v += 10; | |
| 502 i += 10; | |
| 503 } while (i < await new Future.value(42)); | |
| 504 return v; | |
| 505 } | |
| 506 return f().then((v) { | |
| 507 expect(v, equals(10 * 5)); | |
| 508 }); | |
| 509 }); | |
| 510 | |
| 511 test("await err in loop", () { | |
| 512 f() async { | |
| 513 int v = 0; | |
| 514 int i = 0; | |
| 515 do { | |
| 516 v += await new Future.error("err"); | |
| 517 i++; | |
| 518 } while (i < 10); | |
| 519 return v; | |
| 520 } | |
| 521 return f().then((v) { fail("didn't throw"); }, | |
| 522 onError: (e) { expect(e, equals("err")); }); | |
| 523 }); | |
| 524 | |
| 525 test("await err in test", () { | |
| 526 f() async { | |
| 527 int v = 0; | |
| 528 int i = 0; | |
| 529 do { | |
| 530 v += 10; | |
| 531 i += 10; | |
| 532 } while (i < await new Future.error("err")); | |
| 533 return v; | |
| 534 } | |
| 535 return f().then((v) { fail("didn't throw"); }, | |
| 536 onError: (e) { expect(e, equals("err")); }); | |
| 537 }); | |
| 538 | |
| 539 test("break before await", () { | |
| 540 f() async { | |
| 541 int v = 0; | |
| 542 int i = 0; | |
| 543 do { | |
| 544 if (i == 2) break; | |
| 545 v += await new Future.value(42); | |
| 546 i += 1; | |
| 547 } while (i < 10); | |
| 548 return v; | |
| 549 } | |
| 550 return f().then((v) { | |
| 551 expect(v, equals(42 * 2)); | |
| 552 }); | |
| 553 }); | |
| 554 | |
| 555 test("break after await", () { | |
| 556 f() async { | |
| 557 int v = 0; | |
| 558 int i = 0; | |
| 559 do { | |
| 560 v += await new Future.value(42); | |
| 561 if (i == 2) break; | |
| 562 i += 1; | |
| 563 } while (i < 10); | |
| 564 return v; | |
| 565 } | |
| 566 return f().then((v) { | |
| 567 expect(v, equals(42 * 3)); | |
| 568 }); | |
| 569 }); | |
| 570 | |
| 571 test("continue before await", () { | |
| 572 f() async { | |
| 573 int v = 0; | |
| 574 int i = 0; | |
| 575 do { | |
| 576 i += 1; | |
| 577 if (i == 2) continue; | |
| 578 v += await new Future.value(42); | |
| 579 } while (i < 10); | |
| 580 return v; | |
| 581 } | |
| 582 return f().then((v) { | |
| 583 expect(v, equals(42 * 9)); | |
| 584 }); | |
| 585 }); | |
| 586 | |
| 587 test("continue after await", () { | |
| 588 f() async { | |
| 589 int v = 0; | |
| 590 int i = 0; | |
| 591 do { | |
| 592 i += 1; | |
| 593 int j = await new Future.value(42); | |
| 594 if (i == 2) continue; | |
| 595 v += j; | |
| 596 } while (i < 10); | |
| 597 return v; | |
| 598 } | |
| 599 return f().then((v) { | |
| 600 expect(v, equals(42 * 9)); | |
| 601 }); | |
| 602 }); | |
| 603 }); | |
| 604 | |
| 605 group("for-in", () { | |
| 606 test("await in for-in", () { | |
| 607 f() async { | |
| 608 var v = 0; | |
| 609 for (var fut in [1, 2, 3].map((v) => new Future.value(v))) { | |
| 610 v += await fut; | |
| 611 } | |
| 612 return v; | |
| 613 } | |
| 614 return f().then((v) { | |
| 615 expect(v, equals(6)); | |
| 616 }); | |
| 617 }); | |
| 618 | |
| 619 test("await in for-in iterable", () { | |
| 620 f() async { | |
| 621 var v = 0; | |
| 622 for (var i in await new Future.value([1, 2, 3])) { | |
| 623 v += i; | |
| 624 } | |
| 625 return v; | |
| 626 } | |
| 627 return f().then((v) { | |
| 628 expect(v, equals(6)); | |
| 629 }); | |
| 630 }); | |
| 631 | |
| 632 test("await err in for-in", () { | |
| 633 f() async { | |
| 634 var v = 0; | |
| 635 for (var fut in [1, 2, 3].map((v) => (v != 1) | |
| 636 ? new Future.value(v) | |
| 637 : new Future.error("err"))) { | |
| 638 v += await fut; | |
| 639 } | |
| 640 return v; | |
| 641 } | |
| 642 return f().then((v) { fail("didn't throw"); }, | |
| 643 onError: (e) { expect(e, equals("err")); }); | |
| 644 }); | |
| 645 | |
| 646 test("await err in for-in iterable", () { | |
| 647 f() async { | |
| 648 var v = 0; | |
| 649 for (var i in await new Future.error("err")) { | |
| 650 v += i; | |
| 651 } | |
| 652 return v; | |
| 653 } | |
| 654 return f().then((v) { fail("didn't throw"); }, | |
| 655 onError: (e) { expect(e, equals("err")); }); | |
| 656 }); | |
| 657 | |
| 658 test("break before await in for-in", () { | |
| 659 f() async { | |
| 660 var v = 0; | |
| 661 for (var fut in [1, 2, 3].map((v) => new Future.value(v))) { | |
| 662 if (v == 3) break; | |
| 663 v += await fut; | |
| 664 } | |
| 665 return v; | |
| 666 } | |
| 667 return f().then((v) { | |
| 668 expect(v, equals(3)); | |
| 669 }); | |
| 670 }); | |
| 671 }); | |
| 672 | |
| 673 group("try-catch", () { | |
| 674 test("try-no-catch", () { | |
| 675 f() async { | |
| 676 try { | |
| 677 return await id(42); | |
| 678 } catch(e) { | |
| 679 return 37; | |
| 680 } | |
| 681 } | |
| 682 return expect42(f()); | |
| 683 }); | |
| 684 | |
| 685 test("await in body", () { | |
| 686 f() async { | |
| 687 try { | |
| 688 await new Future.error(42); | |
| 689 } catch(e) { | |
| 690 return e; | |
| 691 } | |
| 692 } | |
| 693 return expect42(f()); | |
| 694 }); | |
| 695 | |
| 696 test("throw before await in body", () { | |
| 697 int i = id(0); | |
| 698 f() async { | |
| 699 try { | |
| 700 if (i >= 0) throw id(42); | |
| 701 return await new Future.value(10); | |
| 702 } catch(e) { | |
| 703 return e; | |
| 704 } | |
| 705 } | |
| 706 return expect42(f()); | |
| 707 }); | |
| 708 | |
| 709 test("try-catch await in catch", () { | |
| 710 f() async { | |
| 711 try { | |
| 712 throw id(42); | |
| 713 } catch(e) { | |
| 714 return await new Future.value(e); | |
| 715 } | |
| 716 } | |
| 717 return expect42(f()); | |
| 718 }); | |
| 719 | |
| 720 test("try-catch await error in catch", () { | |
| 721 f() async { | |
| 722 try { | |
| 723 throw id(42); | |
| 724 } catch(e) { | |
| 725 await new Future.error("err"); | |
| 726 } | |
| 727 } | |
| 728 return f().then((v) { fail("didn't throw"); }, | |
| 729 onError: (e) { expect(e, equals("err")); }); | |
| 730 }); | |
| 731 | |
| 732 test("try-catch-rethrow", () { | |
| 733 f() async { | |
| 734 try { | |
| 735 await new Future.error("err"); | |
| 736 } catch(e) { | |
| 737 if (e == id(42)) return; | |
| 738 rethrow; | |
| 739 } | |
| 740 } | |
| 741 return f().then((v) { fail("didn't throw"); }, | |
| 742 onError: (e) { expect(e, equals("err")); }); | |
| 743 }); | |
| 744 }); | |
| 745 | |
| 746 group("try-finally", () { | |
| 747 test("await in body", () { | |
| 748 f() async { | |
| 749 try { | |
| 750 return await new Future.value(42); | |
| 751 } finally { | |
| 752 // Don't do anything. | |
| 753 } | |
| 754 } | |
| 755 return expect42(f()); | |
| 756 }); | |
| 757 | |
| 758 test("await in finally", () { | |
| 759 var x = 0; | |
| 760 f() async { | |
| 761 try { | |
| 762 return id(42); | |
| 763 } finally { | |
| 764 x = await new Future.value(37); | |
| 765 } | |
| 766 } | |
| 767 return f().then((v) { | |
| 768 expect(v, equals(42)); | |
| 769 expect(x, equals(37)); | |
| 770 }); | |
| 771 }); | |
| 772 | |
| 773 test("await err in body", () { | |
| 774 f() async { | |
| 775 try { | |
| 776 return await new Future.error("err"); | |
| 777 } finally { | |
| 778 // Don't do anything. | |
| 779 } | |
| 780 } | |
| 781 return f().then((v) { fail("didn't throw"); }, | |
| 782 onError: (e) { expect(e, equals("err")); }); | |
| 783 }); | |
| 784 | |
| 785 test("await err in finally", () { | |
| 786 f() async { | |
| 787 try { | |
| 788 return id(42); | |
| 789 } finally { | |
| 790 await new Future.error("err"); | |
| 791 } | |
| 792 } | |
| 793 return f().then((v) { fail("didn't throw"); }, | |
| 794 onError: (e) { expect(e, equals("err")); }); | |
| 795 }); | |
| 796 | |
| 797 test("await err in both", () { | |
| 798 f() async { | |
| 799 try { | |
| 800 await new Future.error("not err"); | |
| 801 } finally { | |
| 802 await new Future.error("err"); | |
| 803 } | |
| 804 } | |
| 805 return f().then((v) { fail("didn't throw"); }, | |
| 806 onError: (e) { expect(e, equals("err")); }); | |
| 807 }); | |
| 808 | |
| 809 test("await err in body, override in finally", () { | |
| 810 f() async { | |
| 811 try { | |
| 812 return await new Future.error("err"); | |
| 813 } finally { | |
| 814 return id(42); | |
| 815 } | |
| 816 } | |
| 817 return expect42(f()); | |
| 818 }); | |
| 819 | |
| 820 // test("await in body, override in finally", () { | |
| 821 // f() async { | |
| 822 // label: try { | |
| 823 // return await new Future.value(37); | |
| 824 // } finally { | |
| 825 // break label; | |
| 826 // } | |
| 827 // return id(42); | |
| 828 // } | |
| 829 // return expect42(f()); | |
| 830 // }); | |
| 831 | |
| 832 test("await, override in finally", () { | |
| 833 var x = 0; | |
| 834 f() async { | |
| 835 label: try { | |
| 836 return 87; | |
| 837 } finally { | |
| 838 x = await new Future.value(37); | |
| 839 break label; | |
| 840 } | |
| 841 return id(42); | |
| 842 } | |
| 843 return f().then((v) { | |
| 844 expect(v, equals(42)); | |
| 845 expect(x, equals(37)); | |
| 846 }); | |
| 847 }); | |
| 848 | |
| 849 test("throw in body, await, override in finally 3", () { | |
| 850 var x = 0; | |
| 851 f() async { | |
| 852 label: try { | |
| 853 throw "err"; | |
| 854 } finally { | |
| 855 x = await new Future.value(37); | |
| 856 break label; | |
| 857 } | |
| 858 return id(42); | |
| 859 } | |
| 860 return f().then((v) { | |
| 861 expect(v, equals(42)); | |
| 862 expect(x, equals(37)); | |
| 863 }); | |
| 864 }); | |
| 865 | |
| 866 // test("await err in body, override in finally 2", () { | |
| 867 // f() async { | |
| 868 // label: try { | |
| 869 // return await new Future.error("err"); | |
| 870 // } finally { | |
| 871 // break label; | |
| 872 // } | |
| 873 // return id(42); | |
| 874 // } | |
| 875 // return expect42(f()); | |
| 876 // }); | |
| 877 | |
| 878 test("await in body, no-exit in finally", () { | |
| 879 f() async { | |
| 880 for (int i = 0; i < 10; i++) { | |
| 881 try { | |
| 882 return await i; | |
| 883 } finally { | |
| 884 continue; | |
| 885 } | |
| 886 } | |
| 887 return id(42); | |
| 888 } | |
| 889 return expect42(f()); | |
| 890 }); | |
| 891 | |
| 892 test("no-exit after await in finally", () { | |
| 893 f() async { | |
| 894 int i = 0; | |
| 895 for (; i < 10; i++) { | |
| 896 try { | |
| 897 break; | |
| 898 } finally { | |
| 899 await new Future.value(42); | |
| 900 continue; | |
| 901 } | |
| 902 } | |
| 903 return id(i); | |
| 904 } | |
| 905 return f().then((v) { | |
| 906 expect(v, equals(10)); | |
| 907 }); | |
| 908 }); | |
| 909 | |
| 910 test("exit after continue, await in finally", () { | |
| 911 f() async { | |
| 912 int i = 0; | |
| 913 for (; i < 10; i++) { | |
| 914 try { | |
| 915 continue; | |
| 916 } finally { | |
| 917 await new Future.value(42); | |
| 918 break; | |
| 919 } | |
| 920 } | |
| 921 return id(i); | |
| 922 } | |
| 923 return f().then((v) { | |
| 924 expect(v, equals(0)); | |
| 925 }); | |
| 926 }); | |
| 927 | |
| 928 test("no-exit before await in finally 2", () { | |
| 929 f() async { | |
| 930 for (int i = 0; i < 10; i++) { | |
| 931 try { | |
| 932 return i; | |
| 933 } finally { | |
| 934 if (i >= 0) continue; | |
| 935 await new Future.value(42); | |
| 936 } | |
| 937 } | |
| 938 return id(42); | |
| 939 } | |
| 940 return expect42(f()); | |
| 941 }); | |
| 942 | |
| 943 test("no-exit after await in finally", () { | |
| 944 f() async { | |
| 945 for (int i = 0; i < 10; i++) { | |
| 946 try { | |
| 947 return i; | |
| 948 } finally { | |
| 949 await new Future.value(42); | |
| 950 continue; | |
| 951 } | |
| 952 } | |
| 953 return id(42); | |
| 954 } | |
| 955 return expect42(f()); | |
| 956 }); | |
| 957 | |
| 958 test("nested finallies", () { | |
| 959 var x = 0; | |
| 960 f() async { | |
| 961 try { | |
| 962 try { | |
| 963 return 42; | |
| 964 } finally { | |
| 965 x = await new Future.value(37); | |
| 966 } | |
| 967 } finally { | |
| 968 x += await new Future.value(37); | |
| 969 } | |
| 970 } | |
| 971 return f().then((v) { | |
| 972 expect(v, equals(42)); | |
| 973 expect(x, equals(74)); | |
| 974 }); | |
| 975 }); | |
| 976 | |
| 977 // test("nested finallies 2", () { | |
| 978 // var x = 0; | |
| 979 // f() async { | |
| 980 // label: try { | |
| 981 // try { | |
| 982 // break label; | |
| 983 // } finally { | |
| 984 // x = await new Future.value(37); | |
| 985 // } | |
| 986 // } finally { | |
| 987 // x += await new Future.value(37); | |
| 988 // } | |
| 989 // return 42; | |
| 990 // } | |
| 991 // return f().then((v) { | |
| 992 // expect(v, equals(42)); | |
| 993 // expect(x, equals(74)); | |
| 994 // }); | |
| 995 // }); | |
| 996 | |
| 997 // test("nested finallies 3", () { | |
| 998 // var x = 0; | |
| 999 // f() async { | |
| 1000 // label: try { | |
| 1001 // try { | |
| 1002 // break label; | |
| 1003 // } finally { | |
| 1004 // return await new Future.value(42); | |
| 1005 // } | |
| 1006 // } finally { | |
| 1007 // break label; | |
| 1008 // } | |
| 1009 // return 42; | |
| 1010 // } | |
| 1011 // return expect42(f()); | |
| 1012 // }); | |
| 1013 | |
| 1014 test("nested finallies, throw", () { | |
| 1015 var x = 0; | |
| 1016 f() async { | |
| 1017 try { | |
| 1018 try { | |
| 1019 throw "err"; | |
| 1020 } finally { | |
| 1021 x = await new Future.value(37); | |
| 1022 } | |
| 1023 } finally { | |
| 1024 x += await new Future.value(37); | |
| 1025 } | |
| 1026 } | |
| 1027 return f().then((v) { fail("didn't throw"); }, | |
| 1028 onError: (e) { | |
| 1029 expect(e, equals("err")); | |
| 1030 expect(x, equals(2 * 37)); | |
| 1031 }); | |
| 1032 }); | |
| 1033 }); | |
| 1034 | |
| 1035 group("try-catch-finally", () { | |
| 1036 test("await in body", () { | |
| 1037 f() async { | |
| 1038 try { | |
| 1039 return await new Future.value(42); | |
| 1040 } catch (e) { | |
| 1041 throw null; | |
| 1042 } finally { | |
| 1043 if (id(42) == id(10)) return 10; | |
| 1044 } | |
| 1045 } | |
| 1046 return expect42(f()); | |
| 1047 }); | |
| 1048 | |
| 1049 test("await in catch, not hit", () { | |
| 1050 f() async { | |
| 1051 try { | |
| 1052 return id(42); | |
| 1053 } catch (e) { | |
| 1054 await new Future.error("err"); | |
| 1055 } finally { | |
| 1056 if (id(42) == id(10)) return 10; | |
| 1057 } | |
| 1058 } | |
| 1059 return expect42(f()); | |
| 1060 }); | |
| 1061 | |
| 1062 test("await in catch, hit", () { | |
| 1063 f() async { | |
| 1064 try { | |
| 1065 return throw id(42); | |
| 1066 } catch (e) { | |
| 1067 return await new Future.value(e); | |
| 1068 } finally { | |
| 1069 if (id(42) == id(10)) return 10; | |
| 1070 } | |
| 1071 } | |
| 1072 return expect42(f()); | |
| 1073 }); | |
| 1074 | |
| 1075 test("await in finally", () { | |
| 1076 var x = 0; | |
| 1077 f() async { | |
| 1078 try { | |
| 1079 return id(42); | |
| 1080 } catch (e) { | |
| 1081 throw null; | |
| 1082 } finally { | |
| 1083 x = await new Future.value(37); | |
| 1084 if (id(42) == id(10)) return 10; | |
| 1085 } | |
| 1086 } | |
| 1087 return f().then((v) { | |
| 1088 expect(v, equals(42)); | |
| 1089 expect(x, equals(37)); | |
| 1090 }); | |
| 1091 }); | |
| 1092 }); | |
| 1093 | |
| 1094 group("switch", () { | |
| 1095 test("await in expression", () { | |
| 1096 f(v) async { | |
| 1097 switch (await new Future.value(v)) { | |
| 1098 case 1: return 1; | |
| 1099 case 2: return 42; | |
| 1100 default: return 3; | |
| 1101 } | |
| 1102 return null; | |
| 1103 } | |
| 1104 return expect42(f(2)); | |
| 1105 }); | |
| 1106 | |
| 1107 test("await err in expression", () { | |
| 1108 f(v) async { | |
| 1109 switch (await new Future.error("err")) { | |
| 1110 case 1: return 1; | |
| 1111 case 2: return 42; | |
| 1112 default: return 3; | |
| 1113 } | |
| 1114 return null; | |
| 1115 } | |
| 1116 return throwsErr(f(2)); | |
| 1117 }); | |
| 1118 | |
| 1119 test("await in case", () { | |
| 1120 f(v) async { | |
| 1121 switch (v) { | |
| 1122 case 1: return 1; | |
| 1123 case 2: return await new Future.value(42); | |
| 1124 default: return 3; | |
| 1125 } | |
| 1126 return null; | |
| 1127 } | |
| 1128 return expect42(f(2)); | |
| 1129 }); | |
| 1130 | |
| 1131 test("await err in case", () { | |
| 1132 f(v) async { | |
| 1133 switch (v) { | |
| 1134 case 1: return 1; | |
| 1135 case 2: return await new Future.error("err"); | |
| 1136 default: return 3; | |
| 1137 } | |
| 1138 return null; | |
| 1139 } | |
| 1140 return throwsErr(f(2)); | |
| 1141 }); | |
| 1142 | |
| 1143 test("continue before await in case", () { | |
| 1144 f(v) async { | |
| 1145 switch (v) { | |
| 1146 label: | |
| 1147 case 1: return 42; | |
| 1148 case 2: | |
| 1149 if (v <= 2) continue label; | |
| 1150 return await new Future.value(10); | |
| 1151 default: return 3; | |
| 1152 } | |
| 1153 return null; | |
| 1154 } | |
| 1155 return expect42(f(2)); | |
| 1156 }); | |
| 1157 | |
| 1158 test("continue after await in case", () { | |
| 1159 f(v) async { | |
| 1160 switch (v) { | |
| 1161 label: | |
| 1162 case 1: return 42; | |
| 1163 case 2: | |
| 1164 await new Future.value(10); | |
| 1165 continue label; | |
| 1166 default: return 3; | |
| 1167 } | |
| 1168 return null; | |
| 1169 } | |
| 1170 return expect42(f(2)); | |
| 1171 }); | |
| 1172 }); | |
| 1173 | |
| 1174 group("if", () { | |
| 1175 test("await in test", () { | |
| 1176 f(v) async { | |
| 1177 if (await new Future.value(v)) { | |
| 1178 return 42; | |
| 1179 } else { | |
| 1180 return 37; | |
| 1181 } | |
| 1182 } | |
| 1183 return expect42(f(true)); | |
| 1184 }); | |
| 1185 | |
| 1186 test("await err in test", () { | |
| 1187 f(v) async { | |
| 1188 if (await new Future.error("err")) { | |
| 1189 return 42; | |
| 1190 } else { | |
| 1191 return 37; | |
| 1192 } | |
| 1193 } | |
| 1194 return throwsErr(f(true)); | |
| 1195 }); | |
| 1196 | |
| 1197 test("await in then", () { | |
| 1198 f(v) async { | |
| 1199 if (v) { | |
| 1200 return await new Future.value(42); | |
| 1201 } | |
| 1202 return 37; | |
| 1203 } | |
| 1204 return expect42(f(true)); | |
| 1205 }); | |
| 1206 | |
| 1207 test("await err in then", () { | |
| 1208 f(v) async { | |
| 1209 if (v) { | |
| 1210 return await new Future.error("err"); | |
| 1211 } | |
| 1212 return 37; | |
| 1213 } | |
| 1214 return throwsErr(f(true)); | |
| 1215 }); | |
| 1216 | |
| 1217 test("await in then with else", () { | |
| 1218 f(v) async { | |
| 1219 if (v) { | |
| 1220 return await new Future.value(42); | |
| 1221 } else { | |
| 1222 return 87; | |
| 1223 } | |
| 1224 return 37; | |
| 1225 } | |
| 1226 return expect42(f(true)); | |
| 1227 }); | |
| 1228 | |
| 1229 test("await err in then with else", () { | |
| 1230 f(v) async { | |
| 1231 if (v) { | |
| 1232 return await new Future.error("err"); | |
| 1233 } else { | |
| 1234 return 87; | |
| 1235 } | |
| 1236 return 37; | |
| 1237 } | |
| 1238 return throwsErr(f(true)); | |
| 1239 }); | |
| 1240 | |
| 1241 test("await in else", () { | |
| 1242 f(v) async { | |
| 1243 if (v) { | |
| 1244 return 37; | |
| 1245 } else { | |
| 1246 return await new Future.value(42); | |
| 1247 } | |
| 1248 return 87; | |
| 1249 } | |
| 1250 return expect42(f(false)); | |
| 1251 }); | |
| 1252 | |
| 1253 test("await err in else", () { | |
| 1254 f(v) async { | |
| 1255 if (v) { | |
| 1256 return 37; | |
| 1257 } else { | |
| 1258 return await new Future.error("err"); | |
| 1259 } | |
| 1260 return 87; | |
| 1261 } | |
| 1262 return throwsErr(f(false)); | |
| 1263 }); | |
| 1264 | |
| 1265 test("await in else-if test", () { | |
| 1266 f(v) async { | |
| 1267 if (v) { | |
| 1268 return 37; | |
| 1269 } else if (!await new Future.value(v)) { | |
| 1270 return 42; | |
| 1271 } else { | |
| 1272 return 37; | |
| 1273 } | |
| 1274 return 87; | |
| 1275 } | |
| 1276 return expect42(f(false)); | |
| 1277 }); | |
| 1278 | |
| 1279 test("await in else-if then", () { | |
| 1280 f(v) async { | |
| 1281 if (v) { | |
| 1282 return 37; | |
| 1283 } else if (!v) { | |
| 1284 return await new Future.value(42); | |
| 1285 } else { | |
| 1286 return 37; | |
| 1287 } | |
| 1288 return 87; | |
| 1289 } | |
| 1290 return expect42(f(false)); | |
| 1291 }); | |
| 1292 }); | |
| 1293 | |
| 1294 group("conditional operator", () { | |
| 1295 test("await in test", () { | |
| 1296 f(v) async { | |
| 1297 return (await new Future.value(v)) ? 42 : 37; | |
| 1298 } | |
| 1299 return expect42(f(true)); | |
| 1300 }); | |
| 1301 | |
| 1302 test("await err in test", () { | |
| 1303 f(v) async { | |
| 1304 return (await new Future.error("err")) ? 42 : 37; | |
| 1305 } | |
| 1306 return throwsErr(f(true)); | |
| 1307 }); | |
| 1308 | |
| 1309 test("await in then", () { | |
| 1310 f(v) async { | |
| 1311 return v ? (await new Future.value(42)) : 37; | |
| 1312 } | |
| 1313 return expect42(f(true)); | |
| 1314 }); | |
| 1315 | |
| 1316 test("await err in then", () { | |
| 1317 f(v) async { | |
| 1318 return v ? (await new Future.error("err")) : 37; | |
| 1319 } | |
| 1320 return throwsErr(f(true)); | |
| 1321 }); | |
| 1322 | |
| 1323 test("await in else", () { | |
| 1324 f(v) async { | |
| 1325 return v ? 37 : (await new Future.value(42)); | |
| 1326 } | |
| 1327 return expect42(f(false)); | |
| 1328 }); | |
| 1329 | |
| 1330 test("await err in else", () { | |
| 1331 f(v) async { | |
| 1332 return v ? 37 : (await new Future.error("err")); | |
| 1333 } | |
| 1334 return throwsErr(f(false)); | |
| 1335 }); | |
| 1336 }); | |
| 1337 | |
| 1338 group("async declarations", () { | |
| 1339 var f42 = new Future.value(42); | |
| 1340 | |
| 1341 // Top-level declarations or local declarations in top-level functions. | |
| 1342 test("topMethod", () { | |
| 1343 return expect42(topMethod(f42)); | |
| 1344 }); | |
| 1345 | |
| 1346 test("topArrowMethod", () { | |
| 1347 return expect42(topArrowMethod(f42)); | |
| 1348 }); | |
| 1349 | |
| 1350 test("topGetter", () { | |
| 1351 return expect42(topGetter); | |
| 1352 }); | |
| 1353 | |
| 1354 test("topArrowGetter", () { | |
| 1355 return expect42(topArrowGetter); | |
| 1356 }); | |
| 1357 | |
| 1358 test("topLocal", () { | |
| 1359 return expect42(topLocal(f42)); | |
| 1360 }); | |
| 1361 | |
| 1362 test("topArrowLocal", () { | |
| 1363 return expect42(topArrowLocal(f42)); | |
| 1364 }); | |
| 1365 | |
| 1366 test("topExpression", () { | |
| 1367 return expect42(topExpression(f42)); | |
| 1368 }); | |
| 1369 | |
| 1370 test("topArrowExpression", () { | |
| 1371 return expect42(topArrowExpression(f42)); | |
| 1372 }); | |
| 1373 | |
| 1374 test("topVarExpression", () { | |
| 1375 return expect42(topVarExpression(f42)); | |
| 1376 }); | |
| 1377 | |
| 1378 test("topVarArrowExpression", () { | |
| 1379 return expect42(topVarArrowExpression(f42)); | |
| 1380 }); | |
| 1381 | |
| 1382 // Static declarations or local declarations in static functions. | |
| 1383 test("staticMethod", () { | |
| 1384 return expect42(Async.staticMethod(f42)); | |
| 1385 }); | |
| 1386 | |
| 1387 test("staticArrowMethod", () { | |
| 1388 return expect42(Async.staticArrowMethod(f42)); | |
| 1389 }); | |
| 1390 | |
| 1391 test("staticGetter", () { | |
| 1392 return expect42(Async.staticGetter); | |
| 1393 }); | |
| 1394 | |
| 1395 test("staticArrowGetter", () { | |
| 1396 return expect42(Async.staticArrowGetter); | |
| 1397 }); | |
| 1398 | |
| 1399 test("staticLocal", () { | |
| 1400 return expect42(Async.staticLocal(f42)); | |
| 1401 }); | |
| 1402 | |
| 1403 test("staticArrowLocal", () { | |
| 1404 return expect42(Async.staticArrowLocal(f42)); | |
| 1405 }); | |
| 1406 | |
| 1407 test("staticExpression", () { | |
| 1408 return expect42(Async.staticExpression(f42)); | |
| 1409 }); | |
| 1410 | |
| 1411 test("staticArrowExpression", () { | |
| 1412 return expect42(Async.staticArrowExpression(f42)); | |
| 1413 }); | |
| 1414 | |
| 1415 test("staticVarExpression", () { | |
| 1416 return expect42(Async.staticVarExpression(f42)); | |
| 1417 }); | |
| 1418 | |
| 1419 test("staticVarArrowExpression", () { | |
| 1420 return expect42(Async.staticVarArrowExpression(f42)); | |
| 1421 }); | |
| 1422 | |
| 1423 // Instance declarations or local declarations in instance functions. | |
| 1424 var async = new Async(); | |
| 1425 | |
| 1426 test("instanceMethod", () { | |
| 1427 return expect42(async.instanceMethod(f42)); | |
| 1428 }); | |
| 1429 | |
| 1430 test("instanceArrowMethod", () { | |
| 1431 return expect42(async.instanceArrowMethod(f42)); | |
| 1432 }); | |
| 1433 | |
| 1434 test("instanceGetter", () { | |
| 1435 return expect42(async.instanceGetter); | |
| 1436 }); | |
| 1437 | |
| 1438 test("instanceArrowGetter", () { | |
| 1439 return expect42(async.instanceArrowGetter); | |
| 1440 }); | |
| 1441 | |
| 1442 test("instanceLocal", () { | |
| 1443 return expect42(async.instanceLocal(f42)); | |
| 1444 }); | |
| 1445 | |
| 1446 test("instanceArrowLocal", () { | |
| 1447 return expect42(async.instanceArrowLocal(f42)); | |
| 1448 }); | |
| 1449 | |
| 1450 test("instanceExpression", () { | |
| 1451 return expect42(async.instanceExpression(f42)); | |
| 1452 }); | |
| 1453 | |
| 1454 test("instanceArrowExpression", () { | |
| 1455 return expect42(async.instanceArrowExpression(f42)); | |
| 1456 }); | |
| 1457 | |
| 1458 test("instanceVarExpression", () { | |
| 1459 return expect42(async.instanceVarExpression(f42)); | |
| 1460 }); | |
| 1461 | |
| 1462 test("instanceVarArrowExpression", () { | |
| 1463 return expect42(async.instanceVarArrowExpression(f42)); | |
| 1464 }); | |
| 1465 | |
| 1466 // Local functions in constructor initializer list. | |
| 1467 test("initializerExpression", () { | |
| 1468 var async = new Async.initializer(f42); | |
| 1469 return expect42(async.initValue); | |
| 1470 }); | |
| 1471 | |
| 1472 test("initializerArrowExpression", () { | |
| 1473 var async = new Async.initializerArrow(f42); | |
| 1474 return expect42(async.initValue); | |
| 1475 }); | |
| 1476 | |
| 1477 test("async in async", () { | |
| 1478 return expect42(asyncInAsync(f42)); | |
| 1479 }); | |
| 1480 | |
| 1481 test("sync in async", () { | |
| 1482 return expect42(syncInAsync(f42)); | |
| 1483 }); | |
| 1484 | |
| 1485 test("async in sync", () { | |
| 1486 return expect42(asyncInSync(f42)); | |
| 1487 }); | |
| 1488 | |
| 1489 // Equality and identity. | |
| 1490 test("Identical and equals", () { | |
| 1491 expect(async.instanceMethod, equals(async.instanceMethod)); | |
| 1492 expect(Async.staticMethod, same(Async.staticMethod)); | |
| 1493 expect(topMethod, same(topMethod)); | |
| 1494 }); | |
| 1495 }); | |
| 1496 | |
| 1497 group("await expression", () { | |
| 1498 const c42 = 42; | |
| 1499 final v42 = 42; | |
| 1500 | |
| 1501 test("local variable", () { | |
| 1502 var l42 = 42; | |
| 1503 f() async { | |
| 1504 return await l42; | |
| 1505 } | |
| 1506 return expect42(f()); | |
| 1507 }); | |
| 1508 | |
| 1509 test("parameter", () { | |
| 1510 f(p) async { | |
| 1511 return await p; | |
| 1512 } | |
| 1513 return expect42(f(42)); | |
| 1514 }); | |
| 1515 | |
| 1516 test("final local variable", () { | |
| 1517 f() async { | |
| 1518 return await v42; | |
| 1519 } | |
| 1520 return expect42(f()); | |
| 1521 }); | |
| 1522 | |
| 1523 test("const local variable", () { | |
| 1524 f() async { | |
| 1525 return await c42; | |
| 1526 } | |
| 1527 return expect42(f()); | |
| 1528 }); | |
| 1529 | |
| 1530 test("unary prefix operator", () { | |
| 1531 f() async { | |
| 1532 return -await -42; | |
| 1533 } | |
| 1534 return expect42(f()); | |
| 1535 }); | |
| 1536 | |
| 1537 test("suffix operator", () { | |
| 1538 f() async { | |
| 1539 var v = [42]; | |
| 1540 return await v[0]; | |
| 1541 } | |
| 1542 return expect42(f()); | |
| 1543 }); | |
| 1544 | |
| 1545 // http://dartbug.com/22634 | |
| 1546 // test("unary postfix operator", () { | |
| 1547 // f() async { | |
| 1548 // var x = 42; | |
| 1549 // return await x++; | |
| 1550 // } | |
| 1551 // return expect42(f()); | |
| 1552 // }); | |
| 1553 | |
| 1554 // test("suffix operator + increment", () { | |
| 1555 // f() async { | |
| 1556 // var v = [42]; | |
| 1557 // return await v[0]++; | |
| 1558 // } | |
| 1559 // return expect42(f()); | |
| 1560 // }); | |
| 1561 | |
| 1562 test("unary pre-increment operator", () { | |
| 1563 f() async { | |
| 1564 var x = 41; | |
| 1565 return await ++x; | |
| 1566 } | |
| 1567 return expect42(f()); | |
| 1568 }); | |
| 1569 | |
| 1570 test("suffix operator + pre-increment", () { | |
| 1571 f() async { | |
| 1572 var v = [41]; | |
| 1573 return await ++v[0]; | |
| 1574 } | |
| 1575 return expect42(f()); | |
| 1576 }); | |
| 1577 | |
| 1578 test("assignment operator", () { | |
| 1579 f() async { | |
| 1580 var x = 37; | |
| 1581 return await (x = 42); | |
| 1582 } | |
| 1583 return expect42(f()); | |
| 1584 }); | |
| 1585 | |
| 1586 test("assignment-op operator", () { | |
| 1587 f() async { | |
| 1588 var x = 37; | |
| 1589 return await (x += 5); | |
| 1590 } | |
| 1591 return expect42(f()); | |
| 1592 }); | |
| 1593 | |
| 1594 test("binary operator", () { | |
| 1595 f() async { | |
| 1596 return await (10 + 11) + await (10 + 11); | |
| 1597 } | |
| 1598 return expect42(f()); | |
| 1599 }); | |
| 1600 | |
| 1601 test("ternary operator", () { | |
| 1602 f(v) async { | |
| 1603 return await ((v == 10) ? new Future.value(42) : 37); | |
| 1604 } | |
| 1605 return expect42(f(10)); | |
| 1606 }); | |
| 1607 | |
| 1608 test("top-level function call", () { | |
| 1609 f() async { | |
| 1610 return await topMethod(42); | |
| 1611 } | |
| 1612 return expect42(f()); | |
| 1613 }); | |
| 1614 | |
| 1615 test("static function call", () { | |
| 1616 f() async { | |
| 1617 return await Async.staticMethod(42); | |
| 1618 } | |
| 1619 return expect42(f()); | |
| 1620 }); | |
| 1621 | |
| 1622 test("instance function call", () { | |
| 1623 f() async { | |
| 1624 var a = new Async(); | |
| 1625 return await a.instanceMethod(42); | |
| 1626 } | |
| 1627 return expect42(f()); | |
| 1628 }); | |
| 1629 | |
| 1630 test("top-level function call w/ await", () { | |
| 1631 f() async { | |
| 1632 return await topMethod(await 42); | |
| 1633 } | |
| 1634 return expect42(f()); | |
| 1635 }); | |
| 1636 | |
| 1637 test("static function call w/ await", () { | |
| 1638 f() async { | |
| 1639 return await Async.staticMethod(await 42); | |
| 1640 } | |
| 1641 return expect42(f()); | |
| 1642 }); | |
| 1643 | |
| 1644 test("instance function call w/ await", () { | |
| 1645 f() async { | |
| 1646 var a = new Async(); | |
| 1647 return await a.instanceMethod(await 42); | |
| 1648 } | |
| 1649 return expect42(f()); | |
| 1650 }); | |
| 1651 | |
| 1652 test("top-level getter call", () { | |
| 1653 f() async { | |
| 1654 return await topGetter; | |
| 1655 } | |
| 1656 return expect42(f()); | |
| 1657 }); | |
| 1658 | |
| 1659 test("static getter call", () { | |
| 1660 f() async { | |
| 1661 return await Async.staticGetter; | |
| 1662 } | |
| 1663 return expect42(f()); | |
| 1664 }); | |
| 1665 | |
| 1666 test("top-level getter call", () { | |
| 1667 f() async { | |
| 1668 var a = new Async(); | |
| 1669 return await a.instanceGetter; | |
| 1670 } | |
| 1671 return expect42(f()); | |
| 1672 }); | |
| 1673 }); | |
| 1674 | |
| 1675 group("syntax", () { | |
| 1676 test("async as variable", () { | |
| 1677 // Valid identifiers outside of async function. | |
| 1678 var async = 42; | |
| 1679 expect(async, equals(42)); | |
| 1680 }); | |
| 1681 | |
| 1682 test("await as variable", () { | |
| 1683 // Valid identifiers outside of async function. | |
| 1684 var await = 42; | |
| 1685 expect(await, equals(42)); | |
| 1686 }); | |
| 1687 | |
| 1688 test("yield as variable", () { | |
| 1689 // Valid identifiers outside of async function. | |
| 1690 var yield = 42; | |
| 1691 expect(yield, equals(42)); | |
| 1692 }); | |
| 1693 }); | |
| 1694 } | |
| 1695 | |
| 1696 | |
| 1697 // Attempt to obfuscates value to avoid too much constant folding. | |
| 1698 id(v) { | |
| 1699 try { | |
| 1700 if (v != null) throw v; | |
| 1701 } catch (e) { | |
| 1702 return e; | |
| 1703 } | |
| 1704 return null; | |
| 1705 } | |
| 1706 | |
| 1707 // Create a stream for testing "async for-in". | |
| 1708 Stream mkStream() { | |
| 1709 var c; | |
| 1710 int i = 0; | |
| 1711 next() { | |
| 1712 c.add(i++); | |
| 1713 if (i == 10) { | |
| 1714 c.close(); | |
| 1715 } else { | |
| 1716 scheduleMicrotask(next); | |
| 1717 } | |
| 1718 } | |
| 1719 c = new StreamController(onListen: () { | |
| 1720 scheduleMicrotask(next); | |
| 1721 }); | |
| 1722 return c.stream; | |
| 1723 } | |
| 1724 | |
| 1725 // Check that future contains the error "err". | |
| 1726 Future throwsErr(Future future) { | |
| 1727 return future.then((v) { fail("didn't throw"); }, | |
| 1728 onError: (e) { expect(e, equals("err")); }); | |
| 1729 } | |
| 1730 | |
| 1731 // Check that future contains the value 42. | |
| 1732 Future expect42(Future future) { | |
| 1733 return future.then((v) { | |
| 1734 expect(v, equals(42)); | |
| 1735 }); | |
| 1736 } | |
| 1737 | |
| 1738 | |
| 1739 // Various async declarations. | |
| 1740 | |
| 1741 Future topMethod(f) async { return await f; } | |
| 1742 | |
| 1743 Future topArrowMethod(f) async => await f; | |
| 1744 | |
| 1745 Future get topGetter async { | |
| 1746 return await new Future.value(42); | |
| 1747 } | |
| 1748 | |
| 1749 // Future get topArrowGetter async => await new Future.value(42); | |
| 1750 | |
| 1751 Future topLocal(f) { | |
| 1752 local() async { return await f; } | |
| 1753 return local(); | |
| 1754 } | |
| 1755 | |
| 1756 Future topArrowLocal(f) { | |
| 1757 local() async => await f; | |
| 1758 return local(); | |
| 1759 } | |
| 1760 | |
| 1761 Future topExpression(f) { | |
| 1762 return () async { return await f; } (); | |
| 1763 } | |
| 1764 | |
| 1765 Future topArrowExpression(f) { | |
| 1766 return (() async => await f) (); | |
| 1767 } | |
| 1768 | |
| 1769 var topVarExpression = (f) async { return await f; }; | |
| 1770 | |
| 1771 var topVarArrowExpression = (f) async => await f; | |
| 1772 | |
| 1773 class Async { | |
| 1774 var initValue; | |
| 1775 Async(); | |
| 1776 | |
| 1777 Async.initializer(f) : initValue = (() async { return await f; } ()); | |
| 1778 | |
| 1779 Async.initializerArrow(f) : initValue = ((() async => await f) ()); | |
| 1780 | |
| 1781 /* static */ | |
| 1782 static Future staticMethod(f) async { return await f; } | |
| 1783 | |
| 1784 static Future staticArrowMethod(f) async => await f; | |
| 1785 | |
| 1786 static Future get staticGetter async { | |
| 1787 return await new Future.value(42); | |
| 1788 } | |
| 1789 | |
| 1790 // static Future get staticArrowGetter async => await new Future.value(42); | |
| 1791 | |
| 1792 static Future staticLocal(f) { | |
| 1793 local() async { return await f; } | |
| 1794 return local(); | |
| 1795 } | |
| 1796 | |
| 1797 static Future staticArrowLocal(f) { | |
| 1798 local() async => await f; | |
| 1799 return local(); | |
| 1800 } | |
| 1801 | |
| 1802 static Future staticExpression(f) { | |
| 1803 return () async { return await f; } (); | |
| 1804 } | |
| 1805 | |
| 1806 static Future staticArrowExpression(f) { | |
| 1807 return (() async => await f) (); | |
| 1808 } | |
| 1809 | |
| 1810 static var staticVarExpression = (f) async { return await f; }; | |
| 1811 | |
| 1812 static var staticVarArrowExpression = (f) async => await f; | |
| 1813 | |
| 1814 /* instance */ | |
| 1815 Future instanceMethod(f) async { return await f; } | |
| 1816 | |
| 1817 Future instanceArrowMethod(f) async => await f; | |
| 1818 | |
| 1819 Future get instanceGetter async { | |
| 1820 return await new Future.value(42); | |
| 1821 } | |
| 1822 | |
| 1823 // Future get instanceArrowGetter async => await new Future.value(42); | |
| 1824 | |
| 1825 Future instanceLocal(f) { | |
| 1826 local() async { return await f; } | |
| 1827 return local(); | |
| 1828 } | |
| 1829 | |
| 1830 Future instanceArrowLocal(f) { | |
| 1831 local() async => await f; | |
| 1832 return local(); | |
| 1833 } | |
| 1834 | |
| 1835 Future instanceExpression(f) { | |
| 1836 return () async { return await f; } (); | |
| 1837 } | |
| 1838 | |
| 1839 Future instanceArrowExpression(f) { | |
| 1840 return (() async => await f) (); | |
| 1841 } | |
| 1842 | |
| 1843 var instanceVarExpression = (f) async { return await f; }; | |
| 1844 | |
| 1845 var instanceVarArrowExpression = (f) async => await f; | |
| 1846 } | |
| 1847 | |
| 1848 Future asyncInAsync(f) async { | |
| 1849 inner(f) async { | |
| 1850 return await f; | |
| 1851 } | |
| 1852 return await inner(f); | |
| 1853 } | |
| 1854 | |
| 1855 Future asyncInSync(f) { | |
| 1856 inner(f) async { | |
| 1857 return await f; | |
| 1858 } | |
| 1859 return inner(f); | |
| 1860 } | |
| 1861 | |
| 1862 Future syncInAsync(f) async { | |
| 1863 inner(f) { | |
| 1864 return f; | |
| 1865 } | |
| 1866 return await inner(f); | |
| 1867 } | |
| 1868 | |
| 1869 /** | |
| 1870 * A non-standard implementation of Future with a value. | |
| 1871 */ | |
| 1872 class FakeValueFuture implements Future { | |
| 1873 final _value; | |
| 1874 FakeValueFuture(this._value); | |
| 1875 Future then(callback(value), {Function onError}) { | |
| 1876 return new Future.microtask(() => callback(_value)); | |
| 1877 } | |
| 1878 Future whenComplete(callback()) { | |
| 1879 return new Future.microtask(() { callback(); }); | |
| 1880 } | |
| 1881 Future catchError(Function onError, [bool test(error)]) => this; | |
|
sigurdm
2015/03/03 14:13:10
Also test needs to be named parameter
| |
| 1882 Stream asStream() => (new StreamController()..add(_value)..close()).stream; | |
| 1883 Future timeout(Duration duration, {onTimeout}) => this; | |
| 1884 } | |
| 1885 | |
| 1886 typedef BinaryFunction(a, b); | |
| 1887 | |
| 1888 /** | |
| 1889 * A non-standard implementation of Future with an error. | |
| 1890 */ | |
| 1891 class FakeErrorFuture implements Future { | |
| 1892 final _error; | |
| 1893 FakeErrorFuture(this._error); | |
| 1894 Future then(callback(value), {Function onError}) { | |
| 1895 if (onError != null) { | |
| 1896 if (onError is BinaryFunction) { | |
| 1897 return new Future.microtask(() => onError(_error, null)); | |
| 1898 } | |
| 1899 return new Future.microtask(() => onError(_error)); | |
| 1900 } | |
| 1901 return this; | |
| 1902 } | |
| 1903 Future whenComplete(callback()) { | |
| 1904 return new Future.microtask(() { callback(); }).then((_) => this); | |
| 1905 } | |
| 1906 Future catchError(Function onError, [bool test(error)]) { | |
| 1907 return new Future.microtask(() { | |
| 1908 if (test != null && !test(_error)) return this; | |
| 1909 if (onError is BinaryFunction) { | |
| 1910 return onError(_error, null); | |
| 1911 } | |
| 1912 return onError(_error); | |
| 1913 }); | |
| 1914 } | |
| 1915 Stream asStream() => | |
| 1916 (new StreamController()..addError(_error)..close()).stream; | |
| 1917 Future timeout(Duration duration, {onTimeout}) => this; | |
| 1918 } | |
| OLD | NEW |