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