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 mkStream() { | |
| 9 var c; | |
| 10 int i = 0; | |
| 11 next() { | |
| 12 c.add(i++); | |
| 13 if (i == 10) { | |
| 14 c.close(); | |
| 15 } else { | |
| 16 scheduleMicrotask(next); | |
| 17 } | |
| 18 } | |
| 19 c = new StreamController(onListen: () { | |
| 20 scheduleMicrotask(next); | |
| 21 }); | |
| 22 return c.stream; | |
| 23 } | |
| 24 | |
| 25 main() { | |
| 26 group("basic", () { | |
| 27 test("async works", () { | |
| 28 f() async { return id(42); } | |
| 29 return f().then((v) { | |
| 30 expect(v, equals(42)); | |
| 31 }); | |
| 32 }); | |
| 33 | |
| 34 test("async waits", () { | |
| 35 // Calling an "async" function won't do anything immediately. | |
| 36 var result = []; | |
| 37 f() async { | |
| 38 result.add(1); | |
| 39 return id(42); | |
| 40 }; | |
| 41 var future = f(); | |
| 42 result.add(0); | |
| 43 return future.whenComplete(() { | |
| 44 expect(result, equals([0, 1])); | |
| 45 }); | |
| 46 }); | |
| 47 | |
| 48 test("async throws", () { | |
| 49 f() async { | |
| 50 throw "err"; | |
| 51 return id(42); | |
| 52 } | |
| 53 return f().then((_) { fail("Didn't throw"); }, | |
| 54 onError: (e, s) { expect(e, equals("err")); }); | |
|
Søren Gjesse
2015/03/03 08:08:41
Indentation.
Lasse Reichstein Nielsen
2015/03/03 11:22:42
I made an abstraction for "throwsErr".
| |
| 55 }); | |
| 56 | |
| 57 test("await future", () { | |
| 58 f() async { | |
| 59 var v = await new Future.value(42); | |
| 60 expect(v, equals(42)); | |
| 61 }; | |
| 62 return f(); | |
| 63 }); | |
| 64 | |
| 65 test("await value", () { | |
| 66 f() async { | |
| 67 var v = await id(42); | |
| 68 expect(v, equals(42)); | |
| 69 }; | |
| 70 return f(); | |
| 71 }); | |
| 72 | |
| 73 test("await null", () { | |
| 74 f() async { | |
| 75 var v = await null; | |
| 76 expect(v, equals(null)); | |
| 77 }; | |
| 78 return f(); | |
| 79 }); | |
| 80 | |
| 81 // test("await throw", () { | |
| 82 // f() async { | |
| 83 // await (throw "err"); // Check grammar: Are parentheses necessary? | |
| 84 // return id(42); | |
| 85 // } | |
| 86 // return f().then((_) { fail("Didn't throw"); }, | |
| 87 // onError: (e, s) { expect(e, equals("err")); }); | |
| 88 // }); | |
| 89 | |
| 90 test("throw before await", () { | |
| 91 f() async { | |
| 92 var x = throw "err"; | |
| 93 await x; // Check grammar: Are parentheses necessary? | |
|
Søren Gjesse
2015/03/03 08:08:41
Add test for await local without a throw.
Add tes
Lasse Reichstein Nielsen
2015/03/03 11:22:42
Ah, await of different expression types! Good idea
| |
| 94 return id(42); | |
| 95 } | |
| 96 return f().then((_) { fail("Didn't throw"); }, | |
| 97 onError: (e, s) { expect(e, equals("err")); }); | |
| 98 }); | |
| 99 | |
| 100 test("async await error", () { | |
| 101 f() async { | |
| 102 await new Future.error("err"); | |
| 103 return id(42); | |
| 104 } | |
| 105 return f().then((_) { fail("Didn't throw"); }, | |
| 106 onError: (e, s) { expect(e, equals("err")); }); | |
| 107 }); | |
| 108 | |
| 109 test("async flattens futures", () { | |
| 110 f() async { | |
| 111 return new Future.value(42); // Not awaited. | |
| 112 }; | |
| 113 return f().then((v) { | |
| 114 expect(v, equals(42)); // And not a Future with value id(42). | |
|
Søren Gjesse
2015/03/03 08:08:41
id(42) -> 42.
Did you intend to use id(42) above?
Lasse Reichstein Nielsen
2015/03/03 11:22:42
Nope.
Søren Gjesse
2015/03/03 11:39:56
What I ment was that the comment should not have i
| |
| 115 }); | |
| 116 }); | |
| 117 | |
| 118 test("async flattens futures, error", () { | |
| 119 f() async { | |
| 120 return new Future.error("err"); // Not awaited. | |
| 121 }; | |
| 122 return f().then((_) { fail("Didn't throw"); }, | |
| 123 onError: (e, s) { expect(e, equals("err")); }); | |
| 124 }); | |
| 125 | |
| 126 test("await for", () { | |
| 127 f(s) async { | |
| 128 int i = 0; | |
| 129 await for (int v in s) { | |
| 130 i += v; | |
| 131 } | |
| 132 return i; | |
| 133 } | |
| 134 return f(mkStream()).then((v) { | |
| 135 expect(v, equals(45)); // 0 + 1 + ... + 9 | |
| 136 }); | |
| 137 }); | |
| 138 | |
| 139 test("await for empty", () { | |
| 140 f(s) async { | |
| 141 int v = 0; | |
| 142 await for (int i in s) { | |
| 143 v += i; | |
| 144 } | |
| 145 return v; | |
| 146 } | |
| 147 var s = (new StreamController()..close()).stream; | |
| 148 return f(s).then((v) { | |
| 149 expect(v, equals(0)); // 0 + 1 + ... + 9 | |
|
Søren Gjesse
2015/03/03 08:08:41
Remove comment.
Lasse Reichstein Nielsen
2015/03/03 11:22:42
Done.
| |
| 150 }); | |
| 151 }); | |
| 152 }); | |
| 153 | |
| 154 group("for", () { | |
| 155 test("await in for-loop", () { | |
| 156 f() async { | |
| 157 int v = 0; | |
| 158 for (int i = 0; i < 10; i++) { | |
| 159 v += await new Future.value(42); | |
| 160 } | |
| 161 return v; | |
| 162 } | |
| 163 return f().then((v) { | |
| 164 expect(v, equals(10 * id(42))); | |
| 165 }); | |
| 166 }); | |
| 167 | |
| 168 test("await in for-init", () { | |
| 169 f() async { | |
| 170 int v = 0; | |
| 171 for (int i = await new Future.value(42); i >= 0; i -= 10) { | |
| 172 v += 10; | |
| 173 } | |
| 174 return v; | |
| 175 } | |
| 176 return f().then((v) { | |
| 177 expect(v, equals(10 * 5)); | |
| 178 }); | |
| 179 }); | |
| 180 | |
| 181 test("await in for-test", () { | |
| 182 f() async { | |
| 183 int v = 0; | |
| 184 for (int i = 0; i < await new Future.value(42); i += 10) { | |
| 185 v += 10; | |
| 186 } | |
| 187 return v; | |
| 188 } | |
| 189 return f().then((v) { | |
| 190 expect(v, equals(10 * 5)); | |
| 191 }); | |
| 192 }); | |
| 193 | |
| 194 test("await in for-incr", () { | |
| 195 f() async { | |
| 196 int v = 0; | |
| 197 for (int i = 0; i < 100; i += await new Future.value(42)) { | |
| 198 v += 10; | |
| 199 } | |
| 200 return v; | |
| 201 } | |
| 202 return f().then((v) { | |
| 203 expect(v, equals(10 * 3)); | |
| 204 }); | |
| 205 }); | |
| 206 | |
| 207 test("await err in for-loop", () { | |
| 208 f() async { | |
| 209 int v = 0; | |
| 210 for (int i = 0; i < 10; i++) { | |
| 211 v += await new Future.error("err"); | |
| 212 } | |
| 213 return v; | |
| 214 } | |
| 215 return f().then((v) { fail("didn't throw"); }, | |
| 216 onError: (e) { expect(e, equals("err")); }); | |
| 217 }); | |
| 218 | |
| 219 test("await err in for-init", () { | |
| 220 f() async { | |
| 221 int v = 0; | |
| 222 for (int i = await new Future.error("err"); i >= 0; i -= 10) { | |
| 223 v += 10; | |
| 224 } | |
| 225 return v; | |
| 226 } | |
| 227 return f().then((v) { fail("didn't throw"); }, | |
| 228 onError: (e) { expect(e, equals("err")); }); | |
| 229 }); | |
| 230 | |
| 231 test("await err in for-test", () { | |
| 232 f() async { | |
| 233 int v = 0; | |
| 234 for (int i = 0; i < await new Future.error("err"); i += 10) { | |
| 235 v += 10; | |
| 236 } | |
| 237 return v; | |
| 238 } | |
| 239 return f().then((v) { fail("didn't throw"); }, | |
| 240 onError: (e) { expect(e, equals("err")); }); | |
| 241 }); | |
| 242 | |
| 243 test("await err in for-incr", () { | |
| 244 f() async { | |
| 245 int v = 0; | |
| 246 for (int i = 0; i < 100; i += await new Future.error("err")) { | |
| 247 v += 10; | |
| 248 } | |
| 249 return v; | |
| 250 } | |
| 251 return f().then((v) { fail("didn't throw"); }, | |
| 252 onError: (e) { expect(e, equals("err")); }); | |
| 253 }); | |
| 254 | |
| 255 test("await in empty for-loop", () { | |
| 256 f() async { | |
| 257 int v = 0; | |
| 258 for (int i = 0; i > 0; i += 1) { | |
| 259 v += await new Future.value(42); | |
| 260 } | |
| 261 return v; | |
| 262 } | |
| 263 return f().then((v) { | |
| 264 expect(v, equals(0)); | |
| 265 }); | |
| 266 }); | |
| 267 | |
| 268 test("await in empty for-loop 2", () { | |
| 269 f() async { | |
| 270 int v = 0; | |
| 271 for (int i = 0; i > 0; i += await new Future.value(1)) { | |
| 272 v += 1; | |
| 273 } | |
| 274 return v; | |
| 275 } | |
| 276 return f().then((v) { | |
| 277 expect(v, equals(0)); | |
| 278 }); | |
| 279 }); | |
| 280 | |
| 281 test("break before await in for-loop", () { | |
| 282 f() async { | |
| 283 int v = 0; | |
| 284 for (int i = 0; i < 10; i += 1) { | |
| 285 if (i == 2) break; | |
| 286 v += await new Future.value(42); | |
| 287 } | |
| 288 return v; | |
| 289 } | |
| 290 return f().then((v) { | |
| 291 expect(v, equals(42 * 2)); | |
| 292 }); | |
| 293 }); | |
| 294 | |
| 295 test("break before await in for-loop 2", () { | |
| 296 f() async { | |
| 297 int v = 0; | |
| 298 for (int i = 0; i < 10; i += await new Future.value(1)) { | |
| 299 if (i == 2) break; | |
| 300 v += id(42); | |
| 301 } | |
| 302 return v; | |
| 303 } | |
| 304 return f().then((v) { | |
| 305 expect(v, equals(42 * 2)); | |
| 306 }); | |
| 307 }); | |
|
Søren Gjesse
2015/03/03 08:08:41
How about for tests with continue (same for while)
Lasse Reichstein Nielsen
2015/03/03 11:22:42
Sure, why not!
| |
| 308 }); | |
| 309 | |
| 310 group("while", () { | |
| 311 test("await in while-loop", () { | |
| 312 f() async { | |
| 313 int v = 0; | |
| 314 int i = 0; | |
| 315 while (i < 10) { | |
| 316 v += await new Future.value(42); | |
| 317 i++; | |
| 318 } | |
| 319 return v; | |
| 320 } | |
| 321 return f().then((v) { | |
| 322 expect(v, equals(10 * id(42))); | |
| 323 }); | |
| 324 }); | |
| 325 | |
| 326 test("await in while-test", () { | |
| 327 f() async { | |
| 328 int v = 0; | |
| 329 int i = 0; | |
| 330 while (i < await new Future.value(42)) { | |
| 331 v += 10; | |
| 332 i += 10; | |
| 333 } | |
| 334 return v; | |
| 335 } | |
| 336 return f().then((v) { | |
| 337 expect(v, equals(10 * 5)); | |
| 338 }); | |
| 339 }); | |
| 340 | |
| 341 test("await err in while-loop", () { | |
| 342 f() async { | |
| 343 int v = 0; | |
| 344 int i = 0; | |
| 345 while (i < 10) { | |
| 346 v += await new Future.error("err"); | |
| 347 i++; | |
| 348 } | |
| 349 return v; | |
| 350 } | |
| 351 return f().then((v) { fail("didn't throw"); }, | |
| 352 onError: (e) { expect(e, equals("err")); }); | |
| 353 }); | |
| 354 | |
| 355 test("await err in while-test", () { | |
| 356 f() async { | |
| 357 int v = 0; | |
| 358 int i = 0; | |
| 359 while (i < await new Future.error("err")) { | |
| 360 v += 10; | |
| 361 i += 10; | |
| 362 } | |
| 363 return v; | |
| 364 } | |
| 365 return f().then((v) { fail("didn't throw"); }, | |
| 366 onError: (e) { expect(e, equals("err")); }); | |
| 367 }); | |
| 368 | |
| 369 test("break before await in while", () { | |
| 370 f() async { | |
| 371 int v = 0; | |
| 372 int i = 0; | |
| 373 while (i < 10) { | |
| 374 if (i == 2) break; | |
| 375 v += await new Future.value(42); | |
| 376 i += 1; | |
| 377 } | |
| 378 return v; | |
| 379 } | |
| 380 return f().then((v) { | |
| 381 expect(v, equals(42 * 2)); | |
| 382 }); | |
| 383 }); | |
| 384 }); | |
| 385 | |
| 386 group("do-while", () { | |
| 387 test("await in do-while-loop", () { | |
| 388 f() async { | |
| 389 int v = 0; | |
| 390 int i = 0; | |
| 391 do { | |
| 392 v += await new Future.value(42); | |
| 393 i++; | |
| 394 } while (i < 10); | |
| 395 return v; | |
| 396 } | |
| 397 return f().then((v) { | |
| 398 expect(v, equals(10 * id(42))); | |
| 399 }); | |
| 400 }); | |
| 401 | |
| 402 test("await in do-while-test", () { | |
| 403 f() async { | |
| 404 int v = 0; | |
| 405 int i = 0; | |
| 406 do { | |
| 407 v += 10; | |
| 408 i += 10; | |
| 409 } while (i < await new Future.value(42)); | |
| 410 return v; | |
| 411 } | |
| 412 return f().then((v) { | |
| 413 expect(v, equals(10 * 5)); | |
| 414 }); | |
| 415 }); | |
| 416 | |
| 417 test("await err in do-while-loop", () { | |
| 418 f() async { | |
| 419 int v = 0; | |
| 420 int i = 0; | |
| 421 do { | |
| 422 v += await new Future.error("err"); | |
| 423 i++; | |
| 424 } while (i < 10); | |
| 425 return v; | |
| 426 } | |
| 427 return f().then((v) { fail("didn't throw"); }, | |
| 428 onError: (e) { expect(e, equals("err")); }); | |
| 429 }); | |
| 430 | |
| 431 test("await err in do-while-test", () { | |
| 432 f() async { | |
| 433 int v = 0; | |
| 434 int i = 0; | |
| 435 do { | |
| 436 v += 10; | |
| 437 i += 10; | |
| 438 } while (i < await new Future.error("err")); | |
| 439 return v; | |
| 440 } | |
| 441 return f().then((v) { fail("didn't throw"); }, | |
| 442 onError: (e) { expect(e, equals("err")); }); | |
| 443 }); | |
| 444 | |
| 445 test("break before await in do-while", () { | |
| 446 f() async { | |
| 447 int v = 0; | |
| 448 int i = 0; | |
| 449 do { | |
| 450 if (i == 2) break; | |
| 451 v += await new Future.value(42); | |
| 452 i += 1; | |
| 453 } while (i < 10); | |
| 454 return v; | |
| 455 } | |
| 456 return f().then((v) { | |
| 457 expect(v, equals(42 * 2)); | |
| 458 }); | |
| 459 }); | |
| 460 }); | |
| 461 | |
| 462 group("for-in", () { | |
| 463 test("await in for-in", () { | |
| 464 f() async { | |
| 465 var v = 0; | |
| 466 for (var fut in [1, 2, 3].map((v) => new Future.value(v))) { | |
| 467 v += await fut; | |
| 468 } | |
| 469 return v; | |
| 470 } | |
| 471 return f().then((v) { | |
| 472 expect(v, equals(6)); | |
| 473 }); | |
| 474 }); | |
| 475 | |
| 476 test("await in for-in iterable", () { | |
| 477 f() async { | |
| 478 var v = 0; | |
| 479 for (var i in await new Future.value([1, 2, 3])) { | |
| 480 v += i; | |
| 481 } | |
| 482 return v; | |
| 483 } | |
| 484 return f().then((v) { | |
| 485 expect(v, equals(6)); | |
| 486 }); | |
| 487 }); | |
| 488 | |
| 489 test("await err in for-in", () { | |
| 490 f() async { | |
| 491 var v = 0; | |
| 492 for (var fut in [1, 2, 3].map((v) => (v != 1) | |
| 493 ? new Future.value(v) | |
| 494 : new Future.error("err"))) { | |
| 495 v += await fut; | |
| 496 } | |
| 497 return v; | |
| 498 } | |
| 499 return f().then((v) { fail("didn't throw"); }, | |
| 500 onError: (e) { expect(e, equals("err")); }); | |
| 501 }); | |
| 502 | |
| 503 test("await err in for-in iterable", () { | |
| 504 f() async { | |
| 505 var v = 0; | |
| 506 for (var i in await new Future.error("err")) { | |
| 507 v += i; | |
| 508 } | |
| 509 return v; | |
| 510 } | |
| 511 return f().then((v) { fail("didn't throw"); }, | |
| 512 onError: (e) { expect(e, equals("err")); }); | |
| 513 }); | |
| 514 | |
| 515 test("break before await in for-in", () { | |
| 516 f() async { | |
| 517 var v = 0; | |
| 518 for (var fut in [1, 2, 3].map((v) => new Future.value(v))) { | |
| 519 if (v == 3) break; | |
| 520 v += await fut; | |
| 521 } | |
| 522 return v; | |
| 523 } | |
| 524 return f().then((v) { | |
| 525 expect(v, equals(3)); | |
| 526 }); | |
| 527 }); | |
| 528 }); | |
| 529 | |
| 530 group("try-catch", () { | |
| 531 test("try-no-catch", () { | |
| 532 f() async { | |
| 533 try { | |
| 534 return await id(42); | |
| 535 } catch(e) { | |
| 536 return 37; | |
| 537 } | |
| 538 } | |
| 539 return f().then((v) { | |
| 540 expect(v, equals(42)); | |
| 541 }); | |
| 542 }); | |
| 543 | |
| 544 test("await in body", () { | |
| 545 f() async { | |
| 546 try { | |
| 547 await new Future.error(42); | |
| 548 } catch(e) { | |
| 549 return e; | |
| 550 } | |
| 551 } | |
| 552 return f().then((v) { | |
| 553 expect(v, equals(42)); | |
| 554 }); | |
| 555 }); | |
| 556 | |
| 557 test("throw before await in body", () { | |
| 558 int i = id(0); | |
| 559 f() async { | |
| 560 try { | |
| 561 if (i >= 0) throw id(42); | |
| 562 return await new Future.value(10); | |
| 563 } catch(e) { | |
| 564 return e; | |
| 565 } | |
| 566 } | |
| 567 return f().then((v) { | |
| 568 expect(v, equals(42)); | |
| 569 }); | |
| 570 }); | |
| 571 | |
| 572 test("try-catch await in catch", () { | |
| 573 f() async { | |
| 574 try { | |
| 575 throw id(42); | |
| 576 } catch(e) { | |
| 577 return await new Future.value(e); | |
| 578 } | |
| 579 } | |
| 580 return f().then((v) { | |
| 581 expect(v, equals(42)); | |
| 582 }); | |
| 583 }); | |
| 584 | |
| 585 test("try-catch await error in catch", () { | |
| 586 f() async { | |
| 587 try { | |
| 588 throw id(42); | |
| 589 } catch(e) { | |
| 590 await new Future.error("err"); | |
| 591 } | |
| 592 } | |
| 593 return f().then((v) { fail("didn't throw"); }, | |
| 594 onError: (e) { expect(e, equals("err")); }); | |
| 595 }); | |
| 596 | |
| 597 test("try-catch-rethrow", () { | |
| 598 f() async { | |
| 599 try { | |
| 600 await new Future.error("err"); | |
| 601 } catch(e) { | |
| 602 if (e == id(42)) return; | |
| 603 rethrow; | |
| 604 } | |
| 605 } | |
| 606 return f().then((v) { fail("didn't throw"); }, | |
| 607 onError: (e) { expect(e, equals("err")); }); | |
| 608 }); | |
| 609 }); | |
| 610 | |
| 611 group("try-finally", () { | |
| 612 test("await in body", () { | |
| 613 f() async { | |
| 614 try { | |
| 615 return await new Future.value(42); | |
| 616 } finally { | |
| 617 // Don't do anything. | |
| 618 } | |
| 619 } | |
| 620 return f().then((v) { | |
| 621 expect(v, equals(42)); | |
| 622 }); | |
| 623 }); | |
| 624 | |
| 625 test("await in finally", () { | |
| 626 var x = 0; | |
| 627 f() async { | |
| 628 try { | |
| 629 return id(42); | |
| 630 } finally { | |
| 631 x = await new Future.value(37); | |
| 632 } | |
| 633 } | |
| 634 return f().then((v) { | |
| 635 expect(v, equals(42)); | |
| 636 expect(x, equals(37)); | |
| 637 }); | |
| 638 }); | |
| 639 | |
| 640 test("await err in body", () { | |
| 641 f() async { | |
| 642 try { | |
| 643 return await new Future.error("err"); | |
| 644 } finally { | |
| 645 // Don't do anything. | |
| 646 } | |
| 647 } | |
| 648 return f().then((v) { fail("didn't throw"); }, | |
| 649 onError: (e) { expect(e, equals("err")); }); | |
| 650 }); | |
| 651 | |
| 652 test("await err in finally", () { | |
| 653 f() async { | |
| 654 try { | |
| 655 return id(42); | |
| 656 } finally { | |
| 657 await new Future.error("err"); | |
| 658 } | |
| 659 } | |
| 660 return f().then((v) { fail("didn't throw"); }, | |
| 661 onError: (e) { expect(e, equals("err")); }); | |
| 662 }); | |
| 663 | |
| 664 test("await err in both", () { | |
| 665 f() async { | |
| 666 try { | |
| 667 await new Future.error("not err"); | |
| 668 } finally { | |
| 669 await new Future.error("err"); | |
| 670 } | |
| 671 } | |
| 672 return f().then((v) { fail("didn't throw"); }, | |
| 673 onError: (e) { expect(e, equals("err")); }); | |
| 674 }); | |
| 675 | |
| 676 test("await err in body, override in finally", () { | |
| 677 f() async { | |
| 678 try { | |
| 679 return await new Future.error("err"); | |
| 680 } finally { | |
| 681 return id(42); | |
| 682 } | |
| 683 } | |
| 684 return f().then((v) { | |
| 685 expect(v, equals(42)); | |
| 686 }); | |
| 687 }); | |
| 688 | |
| 689 test("await in body, override in finally", () { | |
| 690 f() async { | |
| 691 label: try { | |
| 692 return await new Future.value(37); | |
| 693 } finally { | |
| 694 break label; | |
| 695 } | |
| 696 return id(42); | |
| 697 } | |
| 698 return f().then((v) { | |
| 699 expect(v, equals(42)); | |
| 700 }); | |
| 701 }); | |
| 702 | |
| 703 test("await, override in finally", () { | |
| 704 var x = 0; | |
| 705 f() async { | |
| 706 label: try { | |
| 707 return 87; | |
| 708 } finally { | |
| 709 x = await new Future.value(37); | |
| 710 break label; | |
| 711 } | |
| 712 return id(42); | |
| 713 } | |
| 714 return f().then((v) { | |
| 715 expect(v, equals(42)); | |
| 716 expect(x, equals(37)); | |
| 717 }); | |
| 718 }); | |
| 719 | |
| 720 test("throw in body, await, override in finally 3", () { | |
| 721 var x = 0; | |
| 722 f() async { | |
| 723 label: try { | |
| 724 throw "err"; | |
| 725 } finally { | |
| 726 x = await new Future.value(37); | |
| 727 break label; | |
| 728 } | |
| 729 return id(42); | |
| 730 } | |
| 731 return f().then((v) { | |
| 732 expect(v, equals(42)); | |
| 733 expect(x, equals(37)); | |
| 734 }); | |
| 735 }); | |
| 736 | |
| 737 test("await err in body, override in finally 2", () { | |
| 738 f() async { | |
| 739 label: try { | |
| 740 return await new Future.error("err"); | |
| 741 } finally { | |
| 742 break label; | |
| 743 } | |
| 744 return id(42); | |
| 745 } | |
| 746 return f().then((v) { | |
| 747 expect(v, equals(42)); | |
| 748 }); | |
| 749 }); | |
| 750 | |
| 751 test("await in body, no-exit in finally", () { | |
| 752 f() async { | |
| 753 for (int i = 0; i < 10; i++) { | |
| 754 try { | |
| 755 return await i; | |
| 756 } finally { | |
| 757 continue; | |
| 758 } | |
| 759 } | |
| 760 return id(42); | |
| 761 } | |
| 762 return f().then((v) { | |
| 763 expect(v, equals(42)); | |
| 764 }); | |
| 765 }); | |
| 766 | |
| 767 test("no-exit after await in finally", () { | |
| 768 f() async { | |
| 769 int i = 0; | |
| 770 for (; i < 10; i++) { | |
| 771 try { | |
| 772 break; | |
| 773 } finally { | |
| 774 await new Future.value(42); | |
| 775 continue; | |
| 776 } | |
| 777 } | |
| 778 return id(i); | |
| 779 } | |
| 780 return f().then((v) { | |
| 781 expect(v, equals(10)); | |
| 782 }); | |
| 783 }); | |
| 784 | |
| 785 test("exit after continue, await in finally", () { | |
| 786 f() async { | |
| 787 int i = 0; | |
| 788 for (; i < 10; i++) { | |
| 789 try { | |
| 790 continue; | |
| 791 } finally { | |
| 792 await new Future.value(42); | |
| 793 break; | |
| 794 } | |
| 795 } | |
| 796 return id(i); | |
| 797 } | |
| 798 return f().then((v) { | |
| 799 expect(v, equals(0)); | |
| 800 }); | |
| 801 }); | |
| 802 | |
| 803 test("no-exit before await in finally 2", () { | |
| 804 f() async { | |
| 805 for (int i = 0; i < 10; i++) { | |
| 806 try { | |
| 807 return i; | |
| 808 } finally { | |
| 809 if (i >= 0) continue; | |
| 810 await new Future.value(42); | |
| 811 } | |
| 812 } | |
| 813 return id(42); | |
| 814 } | |
| 815 return f().then((v) { | |
| 816 expect(v, equals(42)); | |
| 817 }); | |
| 818 }); | |
| 819 | |
| 820 test("no-exit after await in finally", () { | |
| 821 f() async { | |
| 822 for (int i = 0; i < 10; i++) { | |
| 823 try { | |
| 824 return i; | |
| 825 } finally { | |
| 826 await new Future.value(42); | |
| 827 continue; | |
| 828 } | |
| 829 } | |
| 830 return id(42); | |
| 831 } | |
| 832 return f().then((v) { | |
| 833 expect(v, equals(42)); | |
| 834 }); | |
| 835 }); | |
| 836 | |
| 837 test("nested finallies", () { | |
| 838 var x = 0; | |
| 839 f() async { | |
| 840 try { | |
| 841 try { | |
| 842 return 42; | |
| 843 } finally { | |
| 844 x = await new Future.value(37); | |
| 845 } | |
| 846 } finally { | |
| 847 x += await new Future.value(37); | |
| 848 } | |
| 849 } | |
| 850 return f().then((v) { | |
| 851 expect(v, equals(42)); | |
| 852 expect(x, equals(74)); | |
| 853 }); | |
| 854 }); | |
| 855 | |
| 856 test("nested finallies 2", () { | |
| 857 var x = 0; | |
| 858 f() async { | |
| 859 label: try { | |
| 860 try { | |
| 861 break label; | |
| 862 } finally { | |
| 863 x = await new Future.value(37); | |
| 864 } | |
| 865 } finally { | |
| 866 x += await new Future.value(37); | |
| 867 } | |
| 868 return 42; | |
| 869 } | |
| 870 return f().then((v) { | |
| 871 expect(v, equals(42)); | |
| 872 expect(x, equals(74)); | |
| 873 }); | |
| 874 }); | |
| 875 | |
| 876 test("nested finallies 3", () { | |
| 877 var x = 0; | |
| 878 f() async { | |
| 879 label: try { | |
| 880 try { | |
| 881 break label; | |
| 882 } finally { | |
| 883 return await new Future.value(42); | |
| 884 } | |
| 885 } finally { | |
| 886 break label; | |
| 887 } | |
| 888 return 42; | |
| 889 } | |
| 890 return f().then((v) { | |
| 891 expect(v, equals(42)); | |
| 892 }); | |
| 893 }); | |
| 894 | |
| 895 test("nested finallies, throw", () { | |
| 896 var x = 0; | |
| 897 f() async { | |
| 898 try { | |
| 899 try { | |
| 900 throw "err"; | |
| 901 } finally { | |
| 902 x = await new Future.value(37); | |
| 903 } | |
| 904 } finally { | |
| 905 x += await new Future.value(37); | |
| 906 } | |
| 907 } | |
| 908 return f().then((v) { fail("didn't throw"); }, | |
| 909 onError: (e) { expect(e, equals("err")); }); | |
|
Søren Gjesse
2015/03/03 08:08:41
Also checck x is 74.
Lasse Reichstein Nielsen
2015/03/03 11:22:42
Done.
| |
| 910 }); | |
| 911 }); | |
| 912 | |
| 913 group("try-catch-finally", () { | |
| 914 test("await in body", () { | |
| 915 f() async { | |
| 916 try { | |
| 917 return await new Future.value(42); | |
| 918 } catch (e) { | |
| 919 throw null; | |
| 920 } finally { | |
| 921 if (id(42) == id(10)) return 10; | |
| 922 } | |
| 923 } | |
| 924 return f().then((v) { | |
| 925 expect(v, equals(42)); | |
| 926 }); | |
| 927 }); | |
| 928 | |
| 929 test("await in catch, not hit", () { | |
| 930 f() async { | |
| 931 try { | |
| 932 return id(42); | |
| 933 } catch (e) { | |
| 934 await new Future.error("err"); | |
| 935 } finally { | |
| 936 if (id(42) == id(10)) return 10; | |
| 937 } | |
| 938 } | |
| 939 return f().then((v) { | |
| 940 expect(v, equals(42)); | |
| 941 }); | |
| 942 }); | |
| 943 | |
| 944 test("await in catch, hit", () { | |
| 945 f() async { | |
| 946 try { | |
| 947 return throw id(42); | |
| 948 } catch (e) { | |
| 949 return await new Future.value(e); | |
| 950 } finally { | |
| 951 if (id(42) == id(10)) return 10; | |
| 952 } | |
| 953 } | |
| 954 return f().then((v) { | |
| 955 expect(v, equals(42)); | |
| 956 }); | |
| 957 }); | |
| 958 | |
| 959 test("await in finally", () { | |
| 960 var x = 0; | |
| 961 f() async { | |
| 962 try { | |
| 963 return id(42); | |
| 964 } catch (e) { | |
| 965 throw null; | |
| 966 } finally { | |
| 967 x = await new Future.value(37); | |
| 968 if (id(42) == id(10)) return 10; | |
| 969 } | |
| 970 } | |
| 971 return f().then((v) { | |
| 972 expect(v, equals(42)); | |
| 973 expect(x, equals(37)); | |
| 974 }); | |
| 975 }); | |
| 976 }); | |
| 977 } | |
| 978 | |
| 979 | |
| 980 // Attempt to obfuscates value to avoid too much constant folding. | |
| 981 id(v) { | |
|
Søren Gjesse
2015/03/03 08:08:41
Maybe move this up to mkStream (or move mkStream d
Lasse Reichstein Nielsen
2015/03/03 11:22:42
Moving mkStream down. I have added many more helpe
| |
| 982 try { | |
| 983 throw v; | |
| 984 } catch (e) { | |
| 985 return e; | |
| 986 } | |
| 987 return null; | |
| 988 } | |
| OLD | NEW |