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