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 }); | |
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 test("suffix operator", () { | |
1508 f() async { | |
1509 var v = [42]; | |
1510 return await v[0]; | |
1511 } | |
1512 return expect42(f()); | |
1513 }); | |
1514 | |
1515 // http://dartbug.com/22634 | |
1516 // test("unary postfix operator", () { | |
1517 // f() async { | |
1518 // var x = 42; | |
1519 // return await x++; | |
1520 // } | |
1521 // return expect42(f()); | |
1522 // }); | |
1523 | |
1524 // test("suffix operator + increment", () { | |
1525 // f() async { | |
1526 // var v = [42]; | |
1527 // return await v[0]++; | |
1528 // } | |
1529 // return expect42(f()); | |
1530 // }); | |
1531 | |
1532 test("unary pre-increment operator", () { | |
1533 f() async { | |
1534 var x = 41; | |
1535 return await ++x; | |
1536 } | |
1537 return expect42(f()); | |
1538 }); | |
1539 | |
1540 test("suffix operator + pre-increment", () { | |
1541 f() async { | |
1542 var v = [41]; | |
1543 return await ++v[0]; | |
1544 } | |
1545 return expect42(f()); | |
1546 }); | |
1547 | |
1548 test("assignment operator", () { | |
1549 f() async { | |
1550 var x = 37; | |
1551 return await (x = 42); | |
1552 } | |
1553 return expect42(f()); | |
1554 }); | |
1555 | |
1556 test("assignment-op operator", () { | |
1557 f() async { | |
1558 var x = 37; | |
1559 return await (x += 5); | |
1560 } | |
1561 return expect42(f()); | |
1562 }); | |
1563 | |
1564 test("binary operator", () { | |
1565 f() async { | |
1566 return await (10 + 11) + await (10 + 11); | |
1567 } | |
1568 return expect42(f()); | |
1569 }); | |
1570 | |
1571 test("ternary operator", () { | |
1572 f(v) async { | |
1573 return await ((v == 10) ? new Future.value(42) : 37); | |
1574 } | |
1575 return expect42(f(10)); | |
1576 }); | |
1577 | |
1578 test("top-level function call", () { | |
1579 f() async { | |
1580 return await topMethod(42); | |
1581 } | |
1582 return expect42(f()); | |
1583 }); | |
1584 | |
1585 test("static function call", () { | |
1586 f() async { | |
1587 return await Async.staticMethod(42); | |
1588 } | |
1589 return expect42(f()); | |
1590 }); | |
1591 | |
1592 test("instance function call", () { | |
1593 f() async { | |
1594 var a = new Async(); | |
1595 return await a.instanceMethod(42); | |
1596 } | |
1597 return expect42(f()); | |
1598 }); | |
1599 | |
1600 test("top-level function call w/ await", () { | |
1601 f() async { | |
1602 return await topMethod(await 42); | |
1603 } | |
1604 return expect42(f()); | |
1605 }); | |
1606 | |
1607 test("static function call w/ await", () { | |
1608 f() async { | |
1609 return await Async.staticMethod(await 42); | |
1610 } | |
1611 return expect42(f()); | |
1612 }); | |
1613 | |
1614 test("instance function call w/ await", () { | |
1615 f() async { | |
1616 var a = new Async(); | |
1617 return await a.instanceMethod(await 42); | |
1618 } | |
1619 return expect42(f()); | |
1620 }); | |
1621 | |
1622 test("top-level getter call", () { | |
1623 f() async { | |
1624 return await topGetter; | |
1625 } | |
1626 return expect42(f()); | |
1627 }); | |
1628 | |
1629 test("static getter call", () { | |
1630 f() async { | |
1631 return await Async.staticGetter; | |
1632 } | |
1633 return expect42(f()); | |
1634 }); | |
1635 | |
1636 test("top-level getter call", () { | |
1637 f() async { | |
1638 var a = new Async(); | |
1639 return await a.instanceGetter; | |
1640 } | |
1641 return expect42(f()); | |
1642 }); | |
1643 }); | |
1644 } | |
1645 | |
1646 | |
1647 // Attempt to obfuscates value to avoid too much constant folding. | |
1648 id(v) { | |
1649 try { | |
1650 if (v != null) throw v; | |
1651 } catch (e) { | |
1652 return e; | |
1653 } | |
1654 return null; | |
1655 } | |
1656 | |
1657 // Create a stream for testing "async for-in". | |
1658 Stream mkStream() { | |
1659 var c; | |
1660 int i = 0; | |
1661 next() { | |
1662 c.add(i++); | |
1663 if (i == 10) { | |
1664 c.close(); | |
1665 } else { | |
1666 scheduleMicrotask(next); | |
1667 } | |
1668 } | |
1669 c = new StreamController(onListen: () { | |
1670 scheduleMicrotask(next); | |
1671 }); | |
1672 return c.stream; | |
1673 } | |
1674 | |
1675 // Check that future contains the error "err". | |
1676 Future throwsErr(Future future) { | |
1677 return future.then((v) { fail("didn't throw"); }, | |
1678 onError: (e) { expect(e, equals("err")); }); | |
1679 } | |
1680 | |
1681 // Check that future contains the value 42. | |
1682 Future expect42(Future future) { | |
1683 return future.then((v) { | |
1684 expect(v, equals(42)); | |
1685 }); | |
1686 } | |
1687 | |
1688 | |
1689 // Various async declarations. | |
1690 | |
1691 Future topMethod(f) async { return await f; } | |
1692 | |
1693 Future topArrowMethod(f) async => await f; | |
1694 | |
1695 Future get topGetter async { | |
1696 return await new Future.value(42); | |
1697 } | |
1698 | |
1699 // Future get topArrowGetter async => await new Future.value(42); | |
1700 | |
1701 Future topLocal(f) { | |
1702 local() async { return await f; } | |
1703 return local(); | |
1704 } | |
1705 | |
1706 Future topArrowLocal(f) { | |
1707 local() async => await f; | |
1708 return local(); | |
1709 } | |
1710 | |
1711 Future topExpression(f) { | |
1712 return () async { return await f; } (); | |
1713 } | |
1714 | |
1715 Future topArrowExpression(f) { | |
1716 return (() async => await f) (); | |
1717 } | |
1718 | |
1719 var topVarExpression = (f) async { return await f; }; | |
1720 | |
1721 var topVarArrowExpression = (f) async => await f; | |
1722 | |
1723 class Async { | |
1724 var initValue; | |
1725 Async(); | |
1726 | |
1727 Async.initializer(f) : initValue = (() async { return await f; } ()); | |
1728 | |
1729 Async.initializerArrow(f) : initValue = ((() async => await f) ()); | |
1730 | |
1731 /* static */ | |
1732 static Future staticMethod(f) async { return await f; } | |
1733 | |
1734 static Future staticArrowMethod(f) async => await f; | |
1735 | |
1736 static Future get staticGetter async { | |
1737 return await new Future.value(42); | |
1738 } | |
1739 | |
1740 // static Future get staticArrowGetter async => await new Future.value(42); | |
1741 | |
1742 static Future staticLocal(f) { | |
1743 local() async { return await f; } | |
1744 return local(); | |
1745 } | |
1746 | |
1747 static Future staticArrowLocal(f) { | |
1748 local() async => await f; | |
1749 return local(); | |
1750 } | |
1751 | |
1752 static Future staticExpression(f) { | |
1753 return () async { return await f; } (); | |
1754 } | |
1755 | |
1756 static Future staticArrowExpression(f) { | |
1757 return (() async => await f) (); | |
1758 } | |
1759 | |
1760 static var staticVarExpression = (f) async { return await f; }; | |
1761 | |
1762 static var staticVarArrowExpression = (f) async => await f; | |
1763 | |
1764 /* instance */ | |
1765 Future instanceMethod(f) async { return await f; } | |
1766 | |
1767 Future instanceArrowMethod(f) async => await f; | |
1768 | |
1769 Future get instanceGetter async { | |
1770 return await new Future.value(42); | |
1771 } | |
1772 | |
1773 // Future get instanceArrowGetter async => await new Future.value(42); | |
1774 | |
1775 Future instanceLocal(f) { | |
1776 local() async { return await f; } | |
1777 return local(); | |
1778 } | |
1779 | |
1780 Future instanceArrowLocal(f) { | |
1781 local() async => await f; | |
1782 return local(); | |
1783 } | |
1784 | |
1785 Future instanceExpression(f) { | |
1786 return () async { return await f; } (); | |
1787 } | |
1788 | |
1789 Future instanceArrowExpression(f) { | |
1790 return (() async => await f) (); | |
1791 } | |
1792 | |
1793 var instanceVarExpression = (f) async { return await f; }; | |
1794 | |
1795 var instanceVarArrowExpression = (f) async => await f; | |
1796 } | |
1797 | |
1798 Future asyncInAsync(f) async { | |
1799 inner(f) async { | |
1800 return await f; | |
1801 } | |
1802 return await inner(f); | |
1803 } | |
1804 | |
1805 Future asyncInSync(f) { | |
1806 inner(f) async { | |
1807 return await f; | |
1808 } | |
1809 return inner(f); | |
1810 } | |
1811 | |
1812 Future syncInAsync(f) async { | |
1813 inner(f) { | |
1814 return f; | |
1815 } | |
1816 return await inner(f); | |
1817 } | |
1818 | |
1819 /** | |
1820 * A non-standard implementation of Future with a value. | |
1821 */ | |
1822 class FakeValueFuture implements Future { | |
1823 final _value; | |
1824 FakeValueFuture(this._value); | |
1825 Future then(callback(value), [Function onError]) { | |
sigurdm
2015/03/03 13:24:39
onError should be a named argument.
| |
1826 return new Future.microtask(() => callback(_value)); | |
1827 } | |
1828 Future whenComplete(callback()) { | |
1829 return new Future.microtask(() { callback(); }); | |
1830 } | |
1831 Future catchError(Function onError, [bool test(error)]) => this; | |
1832 Stream asStream() => (new StreamController()..add(_value)..close()).stream; | |
1833 Future timeout(Duration duration, {onTimeout}) => this; | |
1834 } | |
1835 | |
1836 typedef BinaryFunction(a, b); | |
1837 | |
1838 /** | |
1839 * A non-standard implementation of Future with an error. | |
1840 */ | |
1841 class FakeErrorFuture implements Future { | |
1842 final _error; | |
1843 FakeErrorFuture(this._error); | |
1844 Future then(callback(value), [Function onError]) { | |
1845 if (onError != null) { | |
1846 if (onError is BinaryFunction) { | |
1847 return new Future.microtask(() => onError(_error, null)); | |
1848 } | |
1849 return new Future.microtask(() => onError(_error)); | |
1850 } | |
1851 return this; | |
1852 } | |
1853 Future whenComplete(callback()) { | |
1854 return new Future.microtask(() { callback(); }).then((_) => this); | |
1855 } | |
1856 Future catchError(Function onError, [bool test(error)]) { | |
1857 return new Future.microtask(() { | |
1858 if (test != null && !test(_error)) return this; | |
1859 if (onError is BinaryFunction) { | |
1860 return onError(_error, null); | |
1861 } | |
1862 return onError(_error); | |
1863 }); | |
1864 } | |
1865 Stream asStream() => | |
1866 (new StreamController()..addError(_error)..close()).stream; | |
1867 Future timeout(Duration duration, {onTimeout}) => this; | |
1868 } | |
OLD | NEW |