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

Side by Side Diff: test/codegen/language/async_await_test.dart

Issue 1243503007: fixes #221, initial sync*, async, async* implementation (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 4 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
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 // TODO(jmesserly): restore this when we fix
1191 // https://github.com/dart-lang/dev_compiler/issues/263
1192 /*test("continue before await in case", () {
1193 f(v) async {
1194 switch (v) {
1195 label:
1196 case 1: return 42;
1197 case 2:
1198 if (v <= 2) continue label;
1199 return await new Future.value(10);
1200 default: return 3;
1201 }
1202 return null;
1203 }
1204 return expect42(f(2));
1205 });
1206
1207 test("continue after await in case", () {
1208 f(v) async {
1209 switch (v) {
1210 label:
1211 case 1: return 42;
1212 case 2:
1213 await new Future.value(10);
1214 continue label;
1215 default: return 3;
1216 }
1217 return null;
1218 }
1219 return expect42(f(2));
1220 });*/
1221 });
1222
1223 group("if", () {
1224 test("await in test", () {
1225 f(v) async {
1226 if (await new Future.value(v)) {
1227 return 42;
1228 } else {
1229 return 37;
1230 }
1231 }
1232 return expect42(f(true));
1233 });
1234
1235 test("await err in test", () {
1236 f(v) async {
1237 if (await new Future.error("err")) {
1238 return 42;
1239 } else {
1240 return 37;
1241 }
1242 }
1243 return throwsErr(f(true));
1244 });
1245
1246 test("await in then", () {
1247 f(v) async {
1248 if (v) {
1249 return await new Future.value(42);
1250 }
1251 return 37;
1252 }
1253 return expect42(f(true));
1254 });
1255
1256 test("await err in then", () {
1257 f(v) async {
1258 if (v) {
1259 return await new Future.error("err");
1260 }
1261 return 37;
1262 }
1263 return throwsErr(f(true));
1264 });
1265
1266 test("await in then with else", () {
1267 f(v) async {
1268 if (v) {
1269 return await new Future.value(42);
1270 } else {
1271 return 87;
1272 }
1273 return 37;
1274 }
1275 return expect42(f(true));
1276 });
1277
1278 test("await err in then with else", () {
1279 f(v) async {
1280 if (v) {
1281 return await new Future.error("err");
1282 } else {
1283 return 87;
1284 }
1285 return 37;
1286 }
1287 return throwsErr(f(true));
1288 });
1289
1290 test("await in else", () {
1291 f(v) async {
1292 if (v) {
1293 return 37;
1294 } else {
1295 return await new Future.value(42);
1296 }
1297 return 87;
1298 }
1299 return expect42(f(false));
1300 });
1301
1302 test("await err in else", () {
1303 f(v) async {
1304 if (v) {
1305 return 37;
1306 } else {
1307 return await new Future.error("err");
1308 }
1309 return 87;
1310 }
1311 return throwsErr(f(false));
1312 });
1313
1314 test("await in else-if test", () {
1315 f(v) async {
1316 if (v) {
1317 return 37;
1318 } else if (!await new Future.value(v)) {
1319 return 42;
1320 } else {
1321 return 37;
1322 }
1323 return 87;
1324 }
1325 return expect42(f(false));
1326 });
1327
1328 test("await in else-if then", () {
1329 f(v) async {
1330 if (v) {
1331 return 37;
1332 } else if (!v) {
1333 return await new Future.value(42);
1334 } else {
1335 return 37;
1336 }
1337 return 87;
1338 }
1339 return expect42(f(false));
1340 });
1341 });
1342
1343 group("conditional operator", () {
1344 test("await in test", () {
1345 f(v) async {
1346 return (await new Future.value(v)) ? 42 : 37;
1347 }
1348 return expect42(f(true));
1349 });
1350
1351 test("await err in test", () {
1352 f(v) async {
1353 return (await new Future.error("err")) ? 42 : 37;
1354 }
1355 return throwsErr(f(true));
1356 });
1357
1358 test("await in then", () {
1359 f(v) async {
1360 return v ? (await new Future.value(42)) : 37;
1361 }
1362 return expect42(f(true));
1363 });
1364
1365 test("await err in then", () {
1366 f(v) async {
1367 return v ? (await new Future.error("err")) : 37;
1368 }
1369 return throwsErr(f(true));
1370 });
1371
1372 test("await in else", () {
1373 f(v) async {
1374 return v ? 37 : (await new Future.value(42));
1375 }
1376 return expect42(f(false));
1377 });
1378
1379 test("await err in else", () {
1380 f(v) async {
1381 return v ? 37 : (await new Future.error("err"));
1382 }
1383 return throwsErr(f(false));
1384 });
1385 });
1386
1387 group("async declarations", () {
1388 var f42 = new Future.value(42);
1389
1390 // Top-level declarations or local declarations in top-level functions.
1391 test("topMethod", () {
1392 return expect42(topMethod(f42));
1393 });
1394
1395 test("topArrowMethod", () {
1396 return expect42(topArrowMethod(f42));
1397 });
1398
1399 test("topGetter", () {
1400 return expect42(topGetter);
1401 });
1402
1403 test("topArrowGetter", () {
1404 return expect42(topArrowGetter);
1405 });
1406
1407 test("topLocal", () {
1408 return expect42(topLocal(f42));
1409 });
1410
1411 test("topArrowLocal", () {
1412 return expect42(topArrowLocal(f42));
1413 });
1414
1415 test("topExpression", () {
1416 return expect42(topExpression(f42));
1417 });
1418
1419 test("topArrowExpression", () {
1420 return expect42(topArrowExpression(f42));
1421 });
1422
1423 test("topVarExpression", () {
1424 return expect42(topVarExpression(f42));
1425 });
1426
1427 test("topVarArrowExpression", () {
1428 return expect42(topVarArrowExpression(f42));
1429 });
1430
1431 // Static declarations or local declarations in static functions.
1432 test("staticMethod", () {
1433 return expect42(Async.staticMethod(f42));
1434 });
1435
1436 test("staticArrowMethod", () {
1437 return expect42(Async.staticArrowMethod(f42));
1438 });
1439
1440 test("staticGetter", () {
1441 return expect42(Async.staticGetter);
1442 });
1443
1444 test("staticArrowGetter", () {
1445 return expect42(Async.staticArrowGetter);
1446 });
1447
1448 test("staticLocal", () {
1449 return expect42(Async.staticLocal(f42));
1450 });
1451
1452 test("staticArrowLocal", () {
1453 return expect42(Async.staticArrowLocal(f42));
1454 });
1455
1456 test("staticExpression", () {
1457 return expect42(Async.staticExpression(f42));
1458 });
1459
1460 test("staticArrowExpression", () {
1461 return expect42(Async.staticArrowExpression(f42));
1462 });
1463
1464 test("staticVarExpression", () {
1465 return expect42(Async.staticVarExpression(f42));
1466 });
1467
1468 test("staticVarArrowExpression", () {
1469 return expect42(Async.staticVarArrowExpression(f42));
1470 });
1471
1472 // Instance declarations or local declarations in instance functions.
1473 var async = new Async();
1474
1475 test("instanceMethod", () {
1476 return expect42(async.instanceMethod(f42));
1477 });
1478
1479 test("instanceArrowMethod", () {
1480 return expect42(async.instanceArrowMethod(f42));
1481 });
1482
1483 test("instanceGetter", () {
1484 return expect42(async.instanceGetter);
1485 });
1486
1487 test("instanceArrowGetter", () {
1488 return expect42(async.instanceArrowGetter);
1489 });
1490
1491 test("instanceLocal", () {
1492 return expect42(async.instanceLocal(f42));
1493 });
1494
1495 test("instanceArrowLocal", () {
1496 return expect42(async.instanceArrowLocal(f42));
1497 });
1498
1499 test("instanceExpression", () {
1500 return expect42(async.instanceExpression(f42));
1501 });
1502
1503 test("instanceArrowExpression", () {
1504 return expect42(async.instanceArrowExpression(f42));
1505 });
1506
1507 test("instanceVarExpression", () {
1508 return expect42(async.instanceVarExpression(f42));
1509 });
1510
1511 test("instanceVarArrowExpression", () {
1512 return expect42(async.instanceVarArrowExpression(f42));
1513 });
1514
1515 // Local functions in constructor initializer list.
1516 test("initializerExpression", () {
1517 var async = new Async.initializer(f42);
1518 return expect42(async.initValue);
1519 });
1520
1521 test("initializerArrowExpression", () {
1522 var async = new Async.initializerArrow(f42);
1523 return expect42(async.initValue);
1524 });
1525
1526 test("async in async", () {
1527 return expect42(asyncInAsync(f42));
1528 });
1529
1530 test("sync in async", () {
1531 return expect42(syncInAsync(f42));
1532 });
1533
1534 test("async in sync", () {
1535 return expect42(asyncInSync(f42));
1536 });
1537
1538 // Equality and identity.
1539 test("Identical and equals", () {
1540 expect(async.instanceMethod, equals(async.instanceMethod));
1541 expect(Async.staticMethod, same(Async.staticMethod));
1542 expect(topMethod, same(topMethod));
1543 }, skip: 'https://github.com/dart-lang/dev_compiler/issues/265');
1544 });
1545
1546 group("await expression", () {
1547 const c42 = 42;
1548 final v42 = 42;
1549
1550 test("local variable", () {
1551 var l42 = 42;
1552 f() async {
1553 return await l42;
1554 }
1555 return expect42(f());
1556 });
1557
1558 test("parameter", () {
1559 f(p) async {
1560 return await p;
1561 }
1562 return expect42(f(42));
1563 });
1564
1565 test("final local variable", () {
1566 f() async {
1567 return await v42;
1568 }
1569 return expect42(f());
1570 });
1571
1572 test("const local variable", () {
1573 f() async {
1574 return await c42;
1575 }
1576 return expect42(f());
1577 });
1578
1579 test("unary prefix operator", () {
1580 f() async {
1581 return -await -42;
1582 }
1583 return expect42(f());
1584 });
1585
1586 test("suffix operator", () {
1587 f() async {
1588 var v = [42];
1589 return await v[0];
1590 }
1591 return expect42(f());
1592 });
1593
1594 test("unary postfix operator", () {
1595 f() async {
1596 var x = 42;
1597 return await x++;
1598 }
1599 return expect42(f());
1600 });
1601
1602 test("suffix operator + increment", () {
1603 f() async {
1604 var v = [42];
1605 return await v[0]++;
1606 }
1607 return expect42(f());
1608 });
1609
1610 test("suffix operator + increment 2", () {
1611 f() async {
1612 var v = [42];
1613 return await v[await 0]++;
1614 }
1615 return expect42(f());
1616 });
1617
1618 test("unary pre-increment operator", () {
1619 f() async {
1620 var x = 41;
1621 return await ++x;
1622 }
1623 return expect42(f());
1624 });
1625
1626 test("suffix operator + pre-increment", () {
1627 f() async {
1628 var v = [41];
1629 return await ++v[0];
1630 }
1631 return expect42(f());
1632 }, skip: 'https://github.com/dart-lang/dev_compiler/issues/267');
1633
1634 test("assignment operator", () {
1635 f() async {
1636 var x = 37;
1637 return await (x = 42);
1638 }
1639 return expect42(f());
1640 });
1641
1642 test("assignment-op operator", () {
1643 f() async {
1644 var x = 37;
1645 return await (x += 5);
1646 }
1647 return expect42(f());
1648 });
1649
1650 test("binary operator", () {
1651 f() async {
1652 return await (10 + 11) + await (10 + 11);
1653 }
1654 return expect42(f());
1655 });
1656
1657 test("ternary operator", () {
1658 f(v) async {
1659 return await ((v == 10) ? new Future.value(42) : 37);
1660 }
1661 return expect42(f(10));
1662 });
1663
1664 test("top-level function call", () {
1665 f() async {
1666 return await topMethod(42);
1667 }
1668 return expect42(f());
1669 });
1670
1671 test("static function call", () {
1672 f() async {
1673 return await Async.staticMethod(42);
1674 }
1675 return expect42(f());
1676 });
1677
1678 test("instance function call", () {
1679 f() async {
1680 var a = new Async();
1681 return await a.instanceMethod(42);
1682 }
1683 return expect42(f());
1684 });
1685
1686 test("top-level function call w/ await", () {
1687 f() async {
1688 return await topMethod(await 42);
1689 }
1690 return expect42(f());
1691 });
1692
1693 test("static function call w/ await", () {
1694 f() async {
1695 return await Async.staticMethod(await 42);
1696 }
1697 return expect42(f());
1698 });
1699
1700 test("instance function call w/ await", () {
1701 f() async {
1702 var a = new Async();
1703 return await a.instanceMethod(await 42);
1704 }
1705 return expect42(f());
1706 });
1707
1708 test("top-level getter call", () {
1709 f() async {
1710 return await topGetter;
1711 }
1712 return expect42(f());
1713 });
1714
1715 test("static getter call", () {
1716 f() async {
1717 return await Async.staticGetter;
1718 }
1719 return expect42(f());
1720 });
1721
1722 test("top-level getter call", () {
1723 f() async {
1724 var a = new Async();
1725 return await a.instanceGetter;
1726 }
1727 return expect42(f());
1728 });
1729
1730 if (!checkedMode) return;
1731
1732 test("inside assert, true", () { /// 03: ok
1733 f() async { /// 03: continued
1734 assert(await new Future.microtask(() => true)); /// 03: continued
1735 return 42; /// 03: continued
1736 } /// 03: continued
1737 return expect42(f()); /// 03: continued
1738 }); /// 03: continued
1739
1740 test("inside assert, false", () { /// 03: continued
1741 f() async { /// 03: continued
1742 assert(await new Future.microtask(() => false)); /// 03: continued
1743 return 42; /// 03: continued
1744 } /// 03: continued
1745 return f().then((_) { /// 03: continued
1746 fail("assert didn't throw"); /// 03: continued
1747 }, onError: (e, s) { /// 03: continued
1748 expect(e is AssertionError, isTrue); /// 03: continued
1749 }); /// 03: continued
1750 }); /// 03: continued
1751
1752 test("inside assert, function -> false", () { /// 03: continued
1753 f() async { /// 03: continued
1754 assert(await new Future.microtask(() => false)); /// 03: continued
1755 return 42; /// 03: continued
1756 } /// 03: continued
1757 return f().then((_) { /// 03: continued
1758 fail("assert didn't throw"); /// 03: continued
1759 }, onError: (e, s) { /// 03: continued
1760 expect(e is AssertionError, isTrue); /// 03: continued
1761 }); /// 03: continued
1762 }); /// 03: continued
1763
1764 });
1765
1766 group("syntax", () {
1767 test("async as variable", () {
1768 // Valid identifiers outside of async function.
1769 var async = 42;
1770 expect(async, equals(42));
1771 });
1772
1773 test("await as variable", () { /// 02: ok
1774 // Valid identifiers outside of async function. /// 02: continued
1775 var await = 42; /// 02: continued
1776 expect(await, equals(42)); /// 02: continued
1777 }); /// 02: continued
1778
1779 test("yield as variable", () {
1780 // Valid identifiers outside of async function.
1781 var yield = 42;
1782 expect(yield, equals(42));
1783 });
1784 });
1785 }
1786
1787
1788 // Attempt to obfuscates value to avoid too much constant folding.
1789 id(v) {
1790 try {
1791 if (v != null) throw v;
1792 } catch (e) {
1793 return e;
1794 }
1795 return null;
1796 }
1797
1798 // Create a stream for testing "async for-in".
1799 Stream mkStream() {
1800 var c;
1801 int i = 0;
1802 next() {
1803 c.add(i++);
1804 if (i == 10) {
1805 c.close();
1806 } else {
1807 scheduleMicrotask(next);
1808 }
1809 }
1810 c = new StreamController(onListen: () {
1811 scheduleMicrotask(next);
1812 });
1813 return c.stream;
1814 }
1815
1816 // Check that future contains the error "err".
1817 Future throwsErr(Future future) {
1818 return future.then((v) { fail("didn't throw"); },
1819 onError: (e) { expect(e, equals("err")); });
1820 }
1821
1822 // Check that future contains the value 42.
1823 Future expect42(Future future) {
1824 return future.then((v) {
1825 expect(v, equals(42));
1826 });
1827 }
1828
1829
1830 // Various async declarations.
1831
1832 Future topMethod(f) async { return await f; }
1833
1834 Future topArrowMethod(f) async => await f;
1835
1836 Future get topGetter async {
1837 return await new Future.value(42);
1838 }
1839
1840 Future get topArrowGetter async => await new Future.value(42);
1841
1842 Future topLocal(f) {
1843 local() async { return await f; }
1844 return local();
1845 }
1846
1847 Future topArrowLocal(f) {
1848 local() async => await f;
1849 return local();
1850 }
1851
1852 Future topExpression(f) {
1853 return () async { return await f; } ();
1854 }
1855
1856 Future topArrowExpression(f) {
1857 return (() async => await f) ();
1858 }
1859
1860 var topVarExpression = (f) async { return await f; };
1861
1862 var topVarArrowExpression = (f) async => await f;
1863
1864 class Async {
1865 var initValue;
1866 Async();
1867
1868 Async.initializer(f) : initValue = (() async { return await f; } ());
1869
1870 Async.initializerArrow(f) : initValue = ((() async => await f) ());
1871
1872 /* static */
1873 static Future staticMethod(f) async { return await f; }
1874
1875 static Future staticArrowMethod(f) async => await f;
1876
1877 static Future get staticGetter async {
1878 return await new Future.value(42);
1879 }
1880
1881 static Future get staticArrowGetter async => await new Future.value(42);
1882
1883 static Future staticLocal(f) {
1884 local() async { return await f; }
1885 return local();
1886 }
1887
1888 static Future staticArrowLocal(f) {
1889 local() async => await f;
1890 return local();
1891 }
1892
1893 static Future staticExpression(f) {
1894 return () async { return await f; } ();
1895 }
1896
1897 static Future staticArrowExpression(f) {
1898 return (() async => await f) ();
1899 }
1900
1901 static var staticVarExpression = (f) async { return await f; };
1902
1903 static var staticVarArrowExpression = (f) async => await f;
1904
1905 /* instance */
1906 Future instanceMethod(f) async { return await f; }
1907
1908 Future instanceArrowMethod(f) async => await f;
1909
1910 Future get instanceGetter async {
1911 return await new Future.value(42);
1912 }
1913
1914 Future get instanceArrowGetter async => await new Future.value(42);
1915
1916 Future instanceLocal(f) {
1917 local() async { return await f; }
1918 return local();
1919 }
1920
1921 Future instanceArrowLocal(f) {
1922 local() async => await f;
1923 return local();
1924 }
1925
1926 Future instanceExpression(f) {
1927 return () async { return await f; } ();
1928 }
1929
1930 Future instanceArrowExpression(f) {
1931 return (() async => await f) ();
1932 }
1933
1934 var instanceVarExpression = (f) async { return await f; };
1935
1936 var instanceVarArrowExpression = (f) async => await f;
1937 }
1938
1939 Future asyncInAsync(f) async {
1940 inner(f) async {
1941 return await f;
1942 }
1943 return await inner(f);
1944 }
1945
1946 Future asyncInSync(f) {
1947 inner(f) async {
1948 return await f;
1949 }
1950 return inner(f);
1951 }
1952
1953 Future syncInAsync(f) async {
1954 inner(f) {
1955 return f;
1956 }
1957 return await inner(f);
1958 }
1959
1960 /**
1961 * A non-standard implementation of Future with a value.
1962 */
1963 class FakeValueFuture implements Future {
1964 final _value;
1965 FakeValueFuture(this._value);
1966 Future then(callback(value), {Function onError}) {
1967 return new Future.microtask(() => callback(_value));
1968 }
1969 Future whenComplete(callback()) {
1970 return new Future.microtask(() { callback(); });
1971 }
1972 Future catchError(Function onError, {bool test(error)}) => this;
1973 Stream asStream() => (new StreamController()..add(_value)..close()).stream;
1974 Future timeout(Duration duration, {onTimeout()}) => this;
1975 }
1976
1977 typedef BinaryFunction(a, b);
1978
1979 /**
1980 * A non-standard implementation of Future with an error.
1981 */
1982 class FakeErrorFuture implements Future {
1983 final _error;
1984 FakeErrorFuture(this._error);
1985 Future then(callback(value), {Function onError}) {
1986 if (onError != null) {
1987 if (onError is BinaryFunction) {
1988 return new Future.microtask(() => onError(_error, null));
1989 }
1990 return new Future.microtask(() => onError(_error));
1991 }
1992 return this;
1993 }
1994 Future whenComplete(callback()) {
1995 return new Future.microtask(() { callback(); }).then((_) => this);
1996 }
1997 Future catchError(Function onError, {bool test(error)}) {
1998 return new Future.microtask(() {
1999 if (test != null && !test(_error)) return this;
2000 if (onError is BinaryFunction) {
2001 return onError(_error, null);
2002 }
2003 return onError(_error);
2004 });
2005 }
2006 Stream asStream() =>
2007 (new StreamController()..addError(_error)..close()).stream;
2008 Future timeout(Duration duration, {onTimeout()}) => this;
2009 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698