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

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

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

Powered by Google App Engine
This is Rietveld 408576698