Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: tests/language/async_await_test.dart

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

Powered by Google App Engine
This is Rietveld 408576698