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_start_test; | 5 library async_start_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 group("basic", () { | 11 group("basic", () { |
12 test("empty", () { | 12 test("empty", () { |
13 f() async* {} | 13 f() async* {} |
14 return f().toList().then((v) { | 14 return f().toList().then((v) { |
15 expect(v, equals([])); | 15 expect(v, equals([])); |
16 }); | 16 }); |
17 }); | 17 }); |
18 | 18 |
19 test("single", () { | 19 test("single", () { |
20 f() async* { | 20 f() async* { yield 42; } |
21 yield 42; | |
22 } | |
23 | |
24 return f().toList().then((v) { | 21 return f().toList().then((v) { |
25 expect(v, equals([42])); | 22 expect(v, equals([42])); |
26 }); | 23 }); |
27 }); | 24 }); |
28 | 25 |
29 test("call delays", () { | 26 test("call delays", () { |
30 var list = []; | 27 var list = []; |
31 f() async* { | 28 f() async* { list.add(1); yield 2; } |
32 list.add(1); | |
33 yield 2; | |
34 } | |
35 | |
36 // TODO(jmesserly): use tear off. For now this is a workaround for: | 29 // TODO(jmesserly): use tear off. For now this is a workaround for: |
37 // https://github.com/dart-lang/dev_compiler/issues/269 | 30 // https://github.com/dart-lang/dev_compiler/issues/269 |
38 var res = f().forEach((x) => list.add(x)); | 31 var res = f().forEach((x) => list.add(x)); |
39 list.add(0); | 32 list.add(0); |
40 return res.whenComplete(() { | 33 return res.whenComplete(() { |
41 expect(list, equals([0, 1, 2])); | 34 expect(list, equals([0, 1, 2])); |
42 }); | 35 }); |
43 }); | 36 }); |
44 | 37 |
45 test("throws", () { | 38 test("throws", () { |
46 f() async* { | 39 f() async* { yield 1; throw 2; } |
47 yield 1; | |
48 throw 2; | |
49 } | |
50 | |
51 var completer = new Completer(); | 40 var completer = new Completer(); |
52 var list = []; | 41 var list = []; |
53 // TODO(jmesserly): use tear off. For now this is a workaround for: | 42 // TODO(jmesserly): use tear off. For now this is a workaround for: |
54 // https://github.com/dart-lang/dev_compiler/issues/269 | 43 // https://github.com/dart-lang/dev_compiler/issues/269 |
55 f().listen((x) => list.add(x), | 44 f().listen((x) => list.add(x), |
56 onError: (v) => list.add("$v"), onDone: completer.complete); | 45 onError: (v) => list.add("$v"), |
| 46 onDone: completer.complete); |
57 return completer.future.whenComplete(() { | 47 return completer.future.whenComplete(() { |
58 expect(list, equals([1, "2"])); | 48 expect(list, equals([1, "2"])); |
59 }); | 49 }); |
60 }); | 50 }); |
61 | 51 |
62 test("multiple", () { | 52 test("multiple", () { |
63 f() async* { | 53 f() async* { |
64 for (int i = 0; i < 10; i++) { | 54 for (int i = 0; i < 10; i++) { |
65 yield i; | 55 yield i; |
66 } | 56 } |
67 } | 57 } |
68 | |
69 return expectList(f(), new List.generate(10, id)); | 58 return expectList(f(), new List.generate(10, id)); |
70 }); | 59 }); |
71 | 60 |
72 test("allows await", () { | 61 test("allows await", () { |
73 f() async* { | 62 f() async* { |
74 var x = await new Future.value(42); | 63 var x = await new Future.value(42); |
75 yield x; | 64 yield x; |
76 x = await new Future.value(42); | 65 x = await new Future.value(42); |
77 } | 66 } |
78 | |
79 return expectList(f(), [42]); | 67 return expectList(f(), [42]); |
80 }); | 68 }); |
81 | 69 |
82 test("allows await in loop", () { | 70 test("allows await in loop", () { |
83 f() async* { | 71 f() async* { |
84 for (int i = 0; i < 10; i++) { | 72 for (int i = 0; i < 10; i++) { |
85 yield await i; | 73 yield await i; |
86 } | 74 } |
87 } | 75 } |
88 | |
89 return expectList(f(), new List.generate(10, id)); | 76 return expectList(f(), new List.generate(10, id)); |
90 }); | 77 }); |
91 | 78 |
92 test("allows yield*", () { | 79 test("allows yield*", () { |
93 f() async* { | 80 f() async* { |
94 yield* new Stream.fromIterable([1, 2, 3]); | 81 yield* new Stream.fromIterable([1, 2, 3]); |
95 } | 82 } |
96 | |
97 return expectList(f(), [1, 2, 3]); | 83 return expectList(f(), [1, 2, 3]); |
98 }); | 84 }); |
99 | 85 |
100 test("allows yield* of async*", () { | 86 test("allows yield* of async*", () { |
101 f(n) async* { | 87 f(n) async* { |
102 yield n; | 88 yield n; |
103 if (n == 0) return; | 89 if (n == 0) return; |
104 yield* f(n - 1); | 90 yield* f(n - 1); |
105 yield n; | 91 yield n; |
106 } | 92 } |
107 | |
108 return expectList(f(3), [3, 2, 1, 0, 1, 2, 3]); | 93 return expectList(f(3), [3, 2, 1, 0, 1, 2, 3]); |
109 }); | 94 }); |
110 | 95 |
111 test("Cannot yield* non-stream", () { | 96 test("Cannot yield* non-stream", () { |
112 f(s) async* { | 97 f(s) async* { |
113 yield* s; | 98 yield* s; |
114 } | 99 } |
115 | |
116 return f(42).transform(getErrors).single.then((v) { | 100 return f(42).transform(getErrors).single.then((v) { |
117 // Not implementing Stream. | 101 // Not implementing Stream. |
118 expect(v is Error, isTrue); | 102 expect(v is Error, isTrue); |
119 }); | 103 }); |
120 }); | 104 }); |
121 | 105 |
122 test("Cannot yield* non-stream", () { | 106 test("Cannot yield* non-stream", () { |
123 f(s) async* { | 107 f(s) async* { |
124 yield* s; | 108 yield* s; |
125 } | 109 } |
126 | |
127 return f(new NotAStream()).transform(getErrors).single.then((v) { | 110 return f(new NotAStream()).transform(getErrors).single.then((v) { |
128 // Not implementing Stream. | 111 // Not implementing Stream. |
129 expect(v is Error, isTrue); | 112 expect(v is Error, isTrue); |
130 }); | 113 }); |
131 }); | 114 }); |
132 }); | 115 }); |
133 | 116 |
134 group("yield statement context", () { | 117 group("yield statement context", () { |
135 test("plain", () { | 118 test("plain", () { |
136 f() async* { | 119 f() async* { |
137 yield 0; | 120 yield 0; |
138 } | 121 } |
139 | |
140 return expectList(f(), [0]); | 122 return expectList(f(), [0]); |
141 }); | 123 }); |
142 | 124 |
143 test("if-then-else", () { | 125 test("if-then-else", () { |
144 f(b) async* { | 126 f(b) async* { |
145 if (b) | 127 if (b) yield 0; else yield 1; |
146 yield 0; | |
147 else | |
148 yield 1; | |
149 } | 128 } |
150 | |
151 return expectList(f(true), [0]).whenComplete(() { | 129 return expectList(f(true), [0]).whenComplete(() { |
152 expectList(f(false), [1]); | 130 expectList(f(false), [1]); |
153 }); | 131 }); |
154 }); | 132 }); |
155 | 133 |
156 test("block", () { | 134 test("block", () { |
157 f() async* { | 135 f() async* { |
158 yield 0; | 136 yield 0; |
159 { | 137 { yield 1; } |
160 yield 1; | |
161 } | |
162 yield 2; | 138 yield 2; |
163 } | 139 } |
164 | |
165 return expectList(f(), [0, 1, 2]); | 140 return expectList(f(), [0, 1, 2]); |
166 }); | 141 }); |
167 | 142 |
168 test("labeled", () { | 143 test("labeled", () { |
169 f() async* { | 144 f() async* { |
170 label1: | 145 label1: yield 0; |
171 yield 0; | |
172 } | 146 } |
173 | |
174 return expectList(f(), [0]); | 147 return expectList(f(), [0]); |
175 }); | 148 }); |
176 | 149 |
177 // VM issue 2238 | 150 // VM issue 2238 |
178 test("labeled 2", () { // //# 01: ok | 151 test("labeled 2", () { // //# 01: ok |
179 f() async* { // //# 01: continued | 152 f() async* { // //# 01: continued |
180 label1: label2: yield 0; // //# 01: continued | 153 label1: label2: yield 0; // //# 01: continued |
181 } // //# 01: continued | 154 } // //# 01: continued |
182 return expectList(f(), [0]); // //# 01: continued | 155 return expectList(f(), [0]); // //# 01: continued |
183 }); // //# 01: continued | 156 }); // //# 01: continued |
184 | 157 |
185 test("for-loop", () { | 158 test("for-loop", () { |
186 f() async* { | 159 f() async* { |
187 for (int i = 0; i < 3; i++) yield i; | 160 for (int i = 0; i < 3; i++) yield i; |
188 } | 161 } |
189 | |
190 return expectList(f(), [0, 1, 2]); | 162 return expectList(f(), [0, 1, 2]); |
191 }); | 163 }); |
192 | 164 |
193 test("for-in-loop", () { | 165 test("for-in-loop", () { |
194 f() async* { | 166 f() async* { |
195 for (var i in [0, 1, 2]) yield i; | 167 for (var i in [0, 1, 2]) yield i; |
196 } | 168 } |
197 | |
198 return expectList(f(), [0, 1, 2]); | 169 return expectList(f(), [0, 1, 2]); |
199 }); | 170 }); |
200 | 171 |
201 test("await for-in-loop", () { | 172 test("await for-in-loop", () { |
202 f() async* { | 173 f() async* { |
203 await for (var i in new Stream.fromIterable([0, 1, 2])) yield i; | 174 await for (var i in new Stream.fromIterable([0, 1, 2])) yield i; |
204 } | 175 } |
205 | |
206 return expectList(f(), [0, 1, 2]); | 176 return expectList(f(), [0, 1, 2]); |
207 }); | 177 }); |
208 | 178 |
209 test("while-loop", () { | 179 test("while-loop", () { |
210 f() async* { | 180 f() async* { |
211 int i = 0; | 181 int i = 0; |
212 while (i < 3) yield i++; | 182 while (i < 3) yield i++; |
213 } | 183 } |
214 | |
215 return expectList(f(), [0, 1, 2]); | 184 return expectList(f(), [0, 1, 2]); |
216 }); | 185 }); |
217 | 186 |
218 test("do-while-loop", () { | 187 test("do-while-loop", () { |
219 f() async* { | 188 f() async* { |
220 int i = 0; | 189 int i = 0; |
221 do yield i++; while (i < 3); | 190 do yield i++; while (i < 3); |
222 } | 191 } |
223 | |
224 return expectList(f(), [0, 1, 2]); | 192 return expectList(f(), [0, 1, 2]); |
225 }); | 193 }); |
226 | 194 |
227 test("try-catch-finally", () { | 195 test("try-catch-finally", () { |
228 f() async* { | 196 f() async* { |
229 try { | 197 try { yield 0; } catch (e) { yield 1; } finally { yield 2; } |
230 yield 0; | |
231 } catch (e) { | |
232 yield 1; | |
233 } finally { | |
234 yield 2; | |
235 } | |
236 } | 198 } |
237 | |
238 return expectList(f(), [0, 2]); | 199 return expectList(f(), [0, 2]); |
239 }); | 200 }); |
240 | 201 |
241 test("try-catch-finally 2", () { | 202 test("try-catch-finally 2", () { |
242 f() async* { | 203 f() async* { |
243 try { | 204 try { yield throw 0; } catch (e) { yield 1; } finally { yield 2; } |
244 yield throw 0; | |
245 } catch (e) { | |
246 yield 1; | |
247 } finally { | |
248 yield 2; | |
249 } | |
250 } | 205 } |
251 | |
252 return expectList(f(), [1, 2]); | 206 return expectList(f(), [1, 2]); |
253 }); | 207 }); |
254 | 208 |
255 // TODO(jmesserly): restore this when we fix | 209 // TODO(jmesserly): restore this when we fix |
256 // https://github.com/dart-lang/dev_compiler/issues/263 | 210 // https://github.com/dart-lang/dev_compiler/issues/263 |
257 /*test("switch-case", () { | 211 /*test("switch-case", () { |
258 f(v) async* { | 212 f(v) async* { |
259 switch (v) { | 213 switch (v) { |
260 case 0: | 214 case 0: |
261 yield 0; | 215 yield 0; |
(...skipping 11 matching lines...) Expand all Loading... |
273 }).whenComplete(() { | 227 }).whenComplete(() { |
274 return expectList(f(2), [2]); | 228 return expectList(f(2), [2]); |
275 }); | 229 }); |
276 });*/ | 230 });*/ |
277 | 231 |
278 test("dead-code return", () { | 232 test("dead-code return", () { |
279 f() async* { | 233 f() async* { |
280 return; | 234 return; |
281 yield 1; | 235 yield 1; |
282 } | 236 } |
283 | |
284 return expectList(f(), []); | 237 return expectList(f(), []); |
285 }); | 238 }); |
286 | 239 |
287 test("dead-code throw", () { | 240 test("dead-code throw", () { |
288 f() async* { | 241 f() async* { |
289 try { | 242 try { |
290 throw 0; | 243 throw 0; |
291 yield 1; | 244 yield 1; |
292 } catch (_) {} | 245 } catch (_) {} |
293 } | 246 } |
294 | |
295 return expectList(f(), []); | 247 return expectList(f(), []); |
296 }); | 248 }); |
297 | 249 |
298 test("dead-code break", () { | 250 test("dead-code break", () { |
299 f() async* { | 251 f() async* { |
300 while (true) { | 252 while (true) { |
301 break; | 253 break; |
302 yield 1; | 254 yield 1; |
303 } | 255 } |
304 } | 256 } |
305 | |
306 return expectList(f(), []); | 257 return expectList(f(), []); |
307 }); | 258 }); |
308 | 259 |
309 test("dead-code break 2", () { | 260 test("dead-code break 2", () { |
310 f() async* { | 261 f() async* { |
311 label: | 262 label: { |
312 { | |
313 break label; | 263 break label; |
314 yield 1; | 264 yield 1; |
315 } | 265 } |
316 } | 266 } |
317 | |
318 return expectList(f(), []); | 267 return expectList(f(), []); |
319 }); | 268 }); |
320 | 269 |
321 test("dead-code continue", () { | 270 test("dead-code continue", () { |
322 f() async* { | 271 f() async* { |
323 do { | 272 do { |
324 continue; | 273 continue; |
325 yield 1; | 274 yield 1; |
326 } while (false); | 275 } while (false); |
327 } | 276 } |
328 | |
329 return expectList(f(), []); | 277 return expectList(f(), []); |
330 }); | 278 }); |
331 }); | 279 }); |
332 | 280 |
333 group("yield expressions", () { | 281 group("yield expressions", () { |
334 test("local variable", () { | 282 test("local variable", () { |
335 f() async* { | 283 f() async* { |
336 var x = 42; | 284 var x = 42; |
337 yield x; | 285 yield x; |
338 } | 286 } |
339 | |
340 return expectList(f(), [42]); | 287 return expectList(f(), [42]); |
341 }); | 288 }); |
342 | 289 |
343 test("constant variable", () { | 290 test("constant variable", () { |
344 f() async* { | 291 f() async* { |
345 const x = 42; | 292 const x = 42; |
346 yield x; | 293 yield x; |
347 } | 294 } |
348 | |
349 return expectList(f(), [42]); | 295 return expectList(f(), [42]); |
350 }); | 296 }); |
351 | 297 |
352 test("function call", () { | 298 test("function call", () { |
353 g() => 42; | 299 g() => 42; |
354 f() async* { | 300 f() async* { |
355 yield g(); | 301 yield g(); |
356 } | 302 } |
357 | |
358 return expectList(f(), [42]); | 303 return expectList(f(), [42]); |
359 }); | 304 }); |
360 | 305 |
361 test("unary operator", () { | 306 test("unary operator", () { |
362 f() async* { | 307 f() async* { |
363 var x = -42; | 308 var x = -42; |
364 yield -x; | 309 yield -x; |
365 } | 310 } |
366 | |
367 return expectList(f(), [42]); | 311 return expectList(f(), [42]); |
368 }); | 312 }); |
369 | 313 |
370 test("binary operator", () { | 314 test("binary operator", () { |
371 f() async* { | 315 f() async* { |
372 var x = 21; | 316 var x = 21; |
373 yield x + x; | 317 yield x + x; |
374 } | 318 } |
375 | |
376 return expectList(f(), [42]); | 319 return expectList(f(), [42]); |
377 }); | 320 }); |
378 | 321 |
379 test("ternary operator", () { | 322 test("ternary operator", () { |
380 f() async* { | 323 f() async* { |
381 var x = 21; | 324 var x = 21; |
382 yield x == 21 ? x + x : x; | 325 yield x == 21 ? x + x : x; |
383 } | 326 } |
384 | |
385 return expectList(f(), [42]); | 327 return expectList(f(), [42]); |
386 }); | 328 }); |
387 | 329 |
388 test("suffix post-increment", () { | 330 test("suffix post-increment", () { |
389 f() async* { | 331 f() async* { |
390 var x = 42; | 332 var x = 42; |
391 yield x++; | 333 yield x++; |
392 } | 334 } |
393 | |
394 return expectList(f(), [42]); | 335 return expectList(f(), [42]); |
395 }); | 336 }); |
396 | 337 |
397 test("suffix pre-increment", () { | 338 test("suffix pre-increment", () { |
398 f() async* { | 339 f() async* { |
399 var x = 41; | 340 var x = 41; |
400 yield ++x; | 341 yield ++x; |
401 } | 342 } |
402 | |
403 return expectList(f(), [42]); | 343 return expectList(f(), [42]); |
404 }); | 344 }); |
405 | 345 |
406 test("assignment", () { | 346 test("assignment", () { |
407 f() async* { | 347 f() async* { |
408 var x = 37; | 348 var x = 37; |
409 yield x = 42; | 349 yield x = 42; |
410 } | 350 } |
411 | |
412 return expectList(f(), [42]); | 351 return expectList(f(), [42]); |
413 }); | 352 }); |
414 | 353 |
415 test("assignment op", () { | 354 test("assignment op", () { |
416 f() async* { | 355 f() async* { |
417 var x = 41; | 356 var x = 41; |
418 yield x += 1; | 357 yield x += 1; |
419 } | 358 } |
420 | |
421 return expectList(f(), [42]); | 359 return expectList(f(), [42]); |
422 }); | 360 }); |
423 | 361 |
424 test("await", () { | 362 test("await", () { |
425 f() async* { | 363 f() async* { |
426 yield await new Future.value(42); | 364 yield await new Future.value(42); |
427 } | 365 } |
428 | |
429 return expectList(f(), [42]); | 366 return expectList(f(), [42]); |
430 }); | 367 }); |
431 | 368 |
432 test("index operator", () { | 369 test("index operator", () { |
433 f() async* { | 370 f() async* { |
434 var x = [42]; | 371 var x = [42]; |
435 yield x[0]; | 372 yield x[0]; |
436 } | 373 } |
437 | |
438 return expectList(f(), [42]); | 374 return expectList(f(), [42]); |
439 }); | 375 }); |
440 | 376 |
441 test("function expression block", () { | 377 test("function expression block", () { |
442 var o = new Object(); | 378 var o = new Object(); |
443 f() async* { | 379 f() async* { |
444 yield () { | 380 yield () { return o; }; |
445 return o; | |
446 }; | |
447 } | 381 } |
448 | |
449 return f().first.then((v) { | 382 return f().first.then((v) { |
450 expect(v(), same(o)); | 383 expect(v(), same(o)); |
451 }); | 384 }); |
452 }); | 385 }); |
453 | 386 |
454 test("function expression arrow", () { | 387 test("function expression arrow", () { |
455 var o = new Object(); | 388 var o = new Object(); |
456 f() async* { | 389 f() async* { |
457 yield () => o; | 390 yield () => o; |
458 } | 391 } |
459 | |
460 return f().first.then((v) { | 392 return f().first.then((v) { |
461 expect(v(), same(o)); | 393 expect(v(), same(o)); |
462 }); | 394 }); |
463 }); | 395 }); |
464 | 396 |
465 test("function expression block async", () { | 397 test("function expression block async", () { |
466 var o = new Object(); | 398 var o = new Object(); |
467 f() async* { | 399 f() async* { |
468 yield () async { | 400 yield () async { return o; }; |
469 return o; | |
470 }; | |
471 } | 401 } |
472 | |
473 return f().first.then((v) => v()).then((v) { | 402 return f().first.then((v) => v()).then((v) { |
474 expect(v, same(o)); | 403 expect(v, same(o)); |
475 }); | 404 }); |
476 }); | 405 }); |
477 | 406 |
478 test("function expression arrow async", () { | 407 test("function expression arrow async", () { |
479 var o = new Object(); | 408 var o = new Object(); |
480 f() async* { | 409 f() async* { |
481 yield () async => o; | 410 yield () async => o; |
482 } | 411 } |
483 | |
484 return f().first.then((v) => v()).then((v) { | 412 return f().first.then((v) => v()).then((v) { |
485 expect(v, same(o)); | 413 expect(v, same(o)); |
486 }); | 414 }); |
487 }); | 415 }); |
488 | 416 |
489 test("function expression block async*", () { | 417 test("function expression block async*", () { |
490 var o = new Object(); | 418 var o = new Object(); |
491 f() async* { | 419 f() async* { |
492 yield () async* { | 420 yield () async* { yield o; }; |
493 yield o; | |
494 }; | |
495 } | 421 } |
496 | |
497 return f().first.then((v) => v().first).then((v) { | 422 return f().first.then((v) => v().first).then((v) { |
498 expect(v, same(o)); | 423 expect(v, same(o)); |
499 }); | 424 }); |
500 }); | 425 }); |
501 }); | 426 }); |
502 | 427 |
503 group("loops", () { | 428 group("loops", () { |
504 test("simple yield", () { | 429 test("simple yield", () { |
505 f() async* { | 430 f() async* { |
506 for (int i = 0; i < 3; i++) { | 431 for (int i = 0; i < 3; i++) { |
507 yield i; | 432 yield i; |
508 } | 433 } |
509 } | 434 } |
510 | |
511 return expectList(f(), [0, 1, 2]); | 435 return expectList(f(), [0, 1, 2]); |
512 }); | 436 }); |
513 | 437 |
514 test("yield in double loop", () { | 438 test("yield in double loop", () { |
515 f() async* { | 439 f() async* { |
516 for (int i = 0; i < 3; i++) { | 440 for (int i = 0; i < 3; i++) { |
517 for (int j = 0; j < 2; j++) { | 441 for (int j = 0; j < 2; j++) { |
518 yield i * 2 + j; | 442 yield i * 2 + j; |
519 } | 443 } |
520 } | 444 } |
521 } | 445 } |
522 | |
523 return expectList(f(), [0, 1, 2, 3, 4, 5]); | 446 return expectList(f(), [0, 1, 2, 3, 4, 5]); |
524 }); | 447 }); |
525 | 448 |
526 test("yield in try body", () { | 449 test("yield in try body", () { |
527 var list = []; | 450 var list = []; |
528 f() async* { | 451 f() async* { |
529 for (int i = 0; i < 3; i++) { | 452 for (int i = 0; i < 3; i++) { |
530 try { | 453 try { |
531 yield i; | 454 yield i; |
532 } finally { | 455 } finally { |
533 list.add("$i"); | 456 list.add("$i"); |
534 } | 457 } |
535 } | 458 } |
536 } | 459 } |
537 | |
538 return expectList(f(), [0, 1, 2]).whenComplete(() { | 460 return expectList(f(), [0, 1, 2]).whenComplete(() { |
539 expect(list, equals(["0", "1", "2"])); | 461 expect(list, equals(["0", "1", "2"])); |
540 }); | 462 }); |
541 }); | 463 }); |
542 | 464 |
543 test("yield in catch", () { | 465 test("yield in catch", () { |
544 var list = []; | 466 var list = []; |
545 f() async* { | 467 f() async* { |
546 for (int i = 0; i < 3; i++) { | 468 for (int i = 0; i < 3; i++) { |
547 try { | 469 try { |
548 throw i; | 470 throw i; |
549 } catch (e) { | 471 } catch (e) { |
550 yield e; | 472 yield e; |
551 } finally { | 473 } finally { |
552 list.add("$i"); | 474 list.add("$i"); |
553 } | 475 } |
554 } | 476 } |
555 } | 477 } |
556 | |
557 return expectList(f(), [0, 1, 2]).whenComplete(() { | 478 return expectList(f(), [0, 1, 2]).whenComplete(() { |
558 expect(list, equals(["0", "1", "2"])); | 479 expect(list, equals(["0", "1", "2"])); |
559 }); | 480 }); |
560 }); | 481 }); |
561 | 482 |
562 test("yield in finally", () { | 483 test("yield in finally", () { |
563 var list = []; | 484 var list = []; |
564 f() async* { | 485 f() async* { |
565 for (int i = 0; i < 3; i++) { | 486 for (int i = 0; i < 3; i++) { |
566 try { | 487 try { |
567 throw i; | 488 throw i; |
568 } finally { | 489 } finally { |
569 yield i; | 490 yield i; |
570 list.add("$i"); | 491 list.add("$i"); |
571 continue; | 492 continue; |
572 } | 493 } |
573 } | 494 } |
574 } | 495 } |
575 | |
576 return expectList(f(), [0, 1, 2]).whenComplete(() { | 496 return expectList(f(), [0, 1, 2]).whenComplete(() { |
577 expect(list, equals(["0", "1", "2"])); | 497 expect(list, equals(["0", "1", "2"])); |
578 }); | 498 }); |
579 }); | 499 }); |
580 | 500 |
581 test("keep yielding after cancel", () { | 501 test("keep yielding after cancel", () { |
582 f() async* { | 502 f() async* { |
583 for (int i = 0; i < 10; i++) { | 503 for (int i = 0; i < 10; i++) { |
584 try { | 504 try { |
585 yield i; | 505 yield i; |
586 } finally { | 506 } finally { |
587 continue; | 507 continue; |
588 } | 508 } |
589 } | 509 } |
590 } | 510 } |
591 | |
592 return expectList(f().take(3), [0, 1, 2]); | 511 return expectList(f().take(3), [0, 1, 2]); |
593 }); | 512 }); |
594 }); | 513 }); |
595 | 514 |
596 group("canceling", () { | 515 group("canceling", () { |
597 // Stream.take(n) automatically cancels after seeing the n'th value. | 516 // Stream.take(n) automatically cancels after seeing the n'th value. |
598 | 517 |
599 test("cancels at yield", () { | 518 test("cancels at yield", () { |
600 Completer exits = new Completer(); | 519 Completer exits = new Completer(); |
601 var list = []; | 520 var list = []; |
602 f() async* { | 521 f() async* { |
603 try { | 522 try { |
604 list.add(0); | 523 list.add(0); |
605 yield list.add(1); | 524 yield list.add(1); |
606 list.add(2); | 525 list.add(2); |
607 } finally { | 526 } finally { |
608 exits.complete(3); | 527 exits.complete(3); |
609 } | 528 } |
610 } | 529 } |
611 | |
612 // No events must be fired synchronously in response to a listen. | 530 // No events must be fired synchronously in response to a listen. |
613 var subscription = f().listen((v) { | 531 var subscription = f().listen((v) { fail("Received event $v"); }, |
614 fail("Received event $v"); | 532 onDone: () { fail("Received done"); }); |
615 }, onDone: () { | |
616 fail("Received done"); | |
617 }); | |
618 // No events must be delivered after a cancel. | 533 // No events must be delivered after a cancel. |
619 subscription.cancel(); | 534 subscription.cancel(); |
620 return exits.future.then((v) { | 535 return exits.future.then((v) { |
621 expect(v, equals(3)); | 536 expect(v, equals(3)); |
622 expect(list, equals([0, 1])); | 537 expect(list, equals([0, 1])); |
623 }); | 538 }); |
624 }); | 539 }); |
625 | 540 |
626 test("does cancel eventually", () { | 541 test("does cancel eventually", () { |
627 var exits = new Completer(); | 542 var exits = new Completer(); |
628 var list = []; | 543 var list = []; |
629 f() async* { | 544 f() async* { |
630 int i = 0; | 545 int i = 0; |
631 try { | 546 try { |
632 while (true) yield i++; | 547 while (true) yield i++; |
633 } finally { | 548 } finally { |
634 list.add("a"); | 549 list.add("a"); |
635 exits.complete(i); | 550 exits.complete(i); |
636 } | 551 } |
637 } | 552 } |
638 | |
639 return expectList(f().take(5), [0, 1, 2, 3, 4]) | 553 return expectList(f().take(5), [0, 1, 2, 3, 4]) |
640 .then((_) => exits.future) | 554 .then((_) => exits.future) |
641 .then((v) { | 555 .then((v) { |
642 expect(v, greaterThan(4)); | 556 expect(v, greaterThan(4)); |
643 expect(list, ["a"]); | 557 expect(list, ["a"]); |
644 }); | 558 }); |
645 }); | 559 }); |
646 | 560 |
647 group("at index", () { | 561 group("at index", () { |
648 f() async* { | 562 f() async* { |
649 try { | 563 try { |
650 yield await new Future.microtask(() => 1); | 564 yield await new Future.microtask(() => 1); |
651 } finally { | 565 } finally { |
652 try { | 566 try { |
653 yield await new Future.microtask(() => 2); | 567 yield await new Future.microtask(() => 2); |
654 } finally { | 568 } finally { |
655 yield await new Future.microtask(() => 3); | 569 yield await new Future.microtask(() => 3); |
656 } | 570 } |
657 } | 571 } |
658 } | 572 } |
659 | |
660 test("- all, sanity check", () { | 573 test("- all, sanity check", () { |
661 return expectList(f(), [1, 2, 3]); | 574 return expectList(f(), [1, 2, 3]); |
662 }); | 575 }); |
663 test("after end", () { | 576 test("after end", () { |
664 return expectList(f().take(4), [1, 2, 3]); | 577 return expectList(f().take(4), [1, 2, 3]); |
665 }); | 578 }); |
666 test("at end", () { | 579 test("at end", () { |
667 return expectList(f().take(3), [1, 2, 3]); | 580 return expectList(f().take(3), [1, 2, 3]); |
668 }); | 581 }); |
669 test("before end", () { | 582 test("before end", () { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 // } | 614 // } |
702 | 615 |
703 // return fugl(3).whenComplete(() => fugl(2)) | 616 // return fugl(3).whenComplete(() => fugl(2)) |
704 // .whenComplete(() => fugl(1)) | 617 // .whenComplete(() => fugl(1)) |
705 // .whenComplete(() { | 618 // .whenComplete(() { |
706 // expect(res, ["fisk 3", "+fisk", 0, 1, "-fisk", "done", | 619 // expect(res, ["fisk 3", "+fisk", 0, 1, "-fisk", "done", |
707 // "fisk 2", "+fisk", 0, 1, "-fisk", "done", | 620 // "fisk 2", "+fisk", 0, 1, "-fisk", "done", |
708 // "fisk 1", "+fisk", 0, "done", "-fisk", ]); | 621 // "fisk 1", "+fisk", 0, "done", "-fisk", ]); |
709 // }); | 622 // }); |
710 // }); | 623 // }); |
| 624 |
711 }); | 625 }); |
712 | 626 |
713 group("pausing", () { | 627 group("pausing", () { |
714 test("pauses execution at yield for at least a microtask", () { | 628 test("pauses execution at yield for at least a microtask", () { |
715 var list = []; | 629 var list = []; |
716 f() async* { | 630 f() async* { |
717 list.add(1); | 631 list.add(1); |
718 yield 2; | 632 yield 2; |
719 list.add(3); | 633 list.add(3); |
720 yield 4; | 634 yield 4; |
721 list.add(5); | 635 list.add(5); |
722 } | 636 } |
723 | |
724 var done = new Completer(); | 637 var done = new Completer(); |
725 var sub = f().listen((v) { | 638 var sub = f().listen((v) { |
726 if (v == 2) { | 639 if (v == 2) { |
727 expect(list, equals([1])); | 640 expect(list, equals([1])); |
728 } else if (v == 4) { | 641 } else if (v == 4) { |
729 expect(list, equals([1, 3])); | 642 expect(list, equals([1, 3])); |
730 } else { | 643 } else { |
731 fail("Unexpected value $v"); | 644 fail("Unexpected value $v"); |
732 } | 645 } |
733 }, onDone: () { | 646 }, onDone: () { |
734 expect(list, equals([1, 3, 5])); | 647 expect(list, equals([1, 3, 5])); |
735 done.complete(); | 648 done.complete(); |
736 }); | 649 }); |
737 return done.future; | 650 return done.future; |
738 }); | 651 }); |
739 | 652 |
740 test("pause stops execution at yield", () { | 653 test("pause stops execution at yield", () { |
741 var list = []; | 654 var list = []; |
742 f() async* { | 655 f() async* { |
743 list.add(1); | 656 list.add(1); |
744 yield 2; | 657 yield 2; |
745 list.add(3); | 658 list.add(3); |
746 yield 4; | 659 yield 4; |
747 list.add(5); | 660 list.add(5); |
748 } | 661 } |
749 | |
750 var done = new Completer(); | 662 var done = new Completer(); |
751 var sub; | 663 var sub; |
752 sub = f().listen((v) { | 664 sub = f().listen((v) { |
753 if (v == 2) { | 665 if (v == 2) { |
754 expect(list, equals([1])); | 666 expect(list, equals([1])); |
755 sub.pause(); | 667 sub.pause(); |
756 new Timer(MS * 300, () { | 668 new Timer(MS * 300, () { |
757 expect(list.length, lessThan(3)); | 669 expect(list.length, lessThan(3)); |
758 sub.resume(); | 670 sub.resume(); |
759 }); | 671 }); |
(...skipping 12 matching lines...) Expand all Loading... |
772 test("pause stops execution at yield 2", () { | 684 test("pause stops execution at yield 2", () { |
773 var list = []; | 685 var list = []; |
774 f() async* { | 686 f() async* { |
775 int i = 0; | 687 int i = 0; |
776 while (true) { | 688 while (true) { |
777 yield i; | 689 yield i; |
778 list.add(i); | 690 list.add(i); |
779 i++; | 691 i++; |
780 } | 692 } |
781 } | 693 } |
782 | |
783 int expected = 0; | 694 int expected = 0; |
784 var done = new Completer(); | 695 var done = new Completer(); |
785 var sub; | 696 var sub; |
786 sub = f().listen((v) { | 697 sub = f().listen((v) { |
787 expect(v, equals(expected++)); | 698 expect(v, equals(expected++)); |
788 if (v % 5 == 0) { | 699 if (v % 5 == 0) { |
789 sub.pause(new Future.delayed(MS * 300)); | 700 sub.pause(new Future.delayed(MS * 300)); |
790 } else if (v == 17) { | 701 } else if (v == 17) { |
791 sub.cancel(); | 702 sub.cancel(); |
792 done.complete(); | 703 done.complete(); |
(...skipping 12 matching lines...) Expand all Loading... |
805 f() async* { // //# 02: cont
inued | 716 f() async* { // //# 02: cont
inued |
806 list.add("*1"); // //# 02: cont
inued | 717 list.add("*1"); // //# 02: cont
inued |
807 yield 1; // //# 02: cont
inued | 718 yield 1; // //# 02: cont
inued |
808 await sync.wait(); // //# 02: cont
inued | 719 await sync.wait(); // //# 02: cont
inued |
809 sync.release(); // //# 02: cont
inued | 720 sync.release(); // //# 02: cont
inued |
810 list.add("*2"); // //# 02: cont
inued | 721 list.add("*2"); // //# 02: cont
inued |
811 yield 2; // //# 02: cont
inued | 722 yield 2; // //# 02: cont
inued |
812 list.add("*3"); // //# 02: cont
inued | 723 list.add("*3"); // //# 02: cont
inued |
813 }; // //# 02: cont
inued | 724 }; // //# 02: cont
inued |
814 var stream = f(); // //# 02: cont
inued | 725 var stream = f(); // //# 02: cont
inued |
815 // TODO(jmesserly): added workaround for: | 726 // TODO(jmesserly): added workaround for: |
816 // https://github.com/dart-lang/dev_compiler/issues/269 | 727 // https://github.com/dart-lang/dev_compiler/issues/269 |
817 var sub = stream.listen((x) => list.add(x)); // //
# 02: continued | 728 var sub = stream.listen((x) => list.add(x)); // //
# 02: continued |
818 return sync.wait().whenComplete(() { // //# 02: cont
inued | 729 return sync.wait().whenComplete(() { // //# 02: cont
inued |
819 expect(list, equals(["*1", 1])); // //# 02: cont
inued | 730 expect(list, equals(["*1", 1])); // //# 02: cont
inued |
820 sub.pause(); // //# 02: cont
inued | 731 sub.pause(); // //# 02: cont
inued |
821 return sync.wait(); // //# 02: cont
inued | 732 return sync.wait(); // //# 02: cont
inued |
822 }).whenComplete(() { // //# 02: cont
inued | 733 }).whenComplete(() { // //# 02: cont
inued |
823 expect(list, equals(["*1", 1, "*2"])); // //# 02: cont
inued | 734 expect(list, equals(["*1", 1, "*2"])); // //# 02: cont
inued |
824 sub.cancel(); // //# 02: cont
inued | 735 sub.cancel(); // //# 02: cont
inued |
825 return new Future.delayed(MS * 200, () { // //# 02: cont
inued | 736 return new Future.delayed(MS * 200, () { // //# 02: cont
inued |
826 // Should not have yielded 2 or added *3 while paused. // //# 02: cont
inued | 737 // Should not have yielded 2 or added *3 while paused. // //# 02: cont
inued |
827 expect(list, equals(["*1", 1, "*2"])); // //# 02: cont
inued | 738 expect(list, equals(["*1", 1, "*2"])); // //# 02: cont
inued |
828 }); // //# 02: cont
inued | 739 }); // //# 02: cont
inued |
829 }); // //# 02: cont
inued | 740 }); // //# 02: cont
inued |
830 }); // //# 02: cont
inued | 741 }); // //# 02: cont
inued |
831 }); | 742 }); |
832 | 743 |
833 group("await for", () { | 744 group("await for", () { |
834 mkStream(int n) async* { | 745 mkStream(int n) async* { |
835 for (int i = 0; i < n; i++) yield i; | 746 for (int i = 0; i < n; i++) yield i; |
836 } | 747 } |
837 | 748 |
838 test("simple stream", () { | 749 test("simple stream", () { |
839 f(s) async { | 750 f(s) async { |
840 var r = 0; | 751 var r = 0; |
841 await for (var v in s) r += v; | 752 await for(var v in s) r += v; |
842 return r; | 753 return r; |
843 } | 754 } |
844 | |
845 return f(mkStream(5)).then((v) { | 755 return f(mkStream(5)).then((v) { |
846 expect(v, equals(10)); | 756 expect(v, equals(10)); |
847 }); | 757 }); |
848 }); | 758 }); |
849 | 759 |
850 test("simple stream, await", () { | 760 test("simple stream, await", () { |
851 f(s) async { | 761 f(s) async { |
852 var r = 0; | 762 var r = 0; |
853 await for (var v in s) r += await new Future.microtask(() => v); | 763 await for(var v in s) r += await new Future.microtask(() => v); |
854 return r; | 764 return r; |
855 } | 765 } |
856 | |
857 return f(mkStream(5)).then((v) { | 766 return f(mkStream(5)).then((v) { |
858 expect(v, equals(10)); | 767 expect(v, equals(10)); |
859 }); | 768 }); |
860 }); | 769 }); |
861 | 770 |
862 test("simple stream - take", () { // //# 03: ok | 771 test("simple stream - take", () { // //# 03: ok |
863 f(s) async { // //# 03: continued | 772 f(s) async { // //# 03: continued |
864 var r = 0; // //# 03: continued | 773 var r = 0; // //# 03: continued |
865 await for(var v in s.take(5)) r += v; // //# 03: continued | 774 await for(var v in s.take(5)) r += v; // //# 03: continued |
866 return r; // //# 03: continued | 775 return r; // //# 03: continued |
867 } // //# 03: continued | 776 } // //# 03: continued |
868 return f(mkStream(10)).then((v) { // //# 03: continued | 777 return f(mkStream(10)).then((v) { // //# 03: continued |
869 expect(v, equals(10)); // //# 03: continued | 778 expect(v, equals(10)); // //# 03: continued |
870 }); // //# 03: continued | 779 }); // //# 03: continued |
871 }); // //# 03: continued | 780 }); // //# 03: continued |
872 | 781 |
873 test("simple stream reyield", () { | 782 test("simple stream reyield", () { |
874 f(s) async* { | 783 f(s) async* { |
875 var r = 0; | 784 var r = 0; |
876 await for (var v in s) yield r += v; | 785 await for(var v in s) yield r += v; |
877 } | 786 } |
878 | |
879 return expectList(f(mkStream(5)), [0, 1, 3, 6, 10]); | 787 return expectList(f(mkStream(5)), [0, 1, 3, 6, 10]); |
880 }); | 788 }); |
881 | 789 |
882 test("simple stream, await, reyield", () { | 790 test("simple stream, await, reyield", () { |
883 f(s) async* { | 791 f(s) async* { |
884 var r = 0; | 792 var r = 0; |
885 await for (var v in s) yield r += await new Future.microtask(() => v); | 793 await for(var v in s) yield r += await new Future.microtask(() => v); |
886 } | 794 } |
887 | |
888 return expectList(f(mkStream(5)), [0, 1, 3, 6, 10]); | 795 return expectList(f(mkStream(5)), [0, 1, 3, 6, 10]); |
889 }); | 796 }); |
890 | 797 |
891 test("simple stream - take, reyield", () { // //# 04: ok | 798 test("simple stream - take, reyield", () { // //# 04: ok |
892 f(s) async* { // //# 04: continue
d | 799 f(s) async* { // //# 04: continue
d |
893 var r = 0; // //# 04: continue
d | 800 var r = 0; // //# 04: continue
d |
894 await for(var v in s.take(5)) yield r += v; // //# 04: continue
d | 801 await for(var v in s.take(5)) yield r += v; // //# 04: continue
d |
895 } // //# 04: continue
d | 802 } // //# 04: continue
d |
896 return expectList(f(mkStream(10)), [0, 1, 3, 6, 10]); // //# 04: continue
d | 803 return expectList(f(mkStream(10)), [0, 1, 3, 6, 10]); // //# 04: continue
d |
897 }); // //# 04: continue
d | 804 }); // //# 04: continue
d |
898 | 805 |
899 test("nested", () { | 806 test("nested", () { |
900 f() async { | 807 f() async { |
901 var r = 0; | 808 var r = 0; |
902 await for (var i in mkStream(5)) { | 809 await for (var i in mkStream(5)) { |
903 await for (var j in mkStream(3)) { | 810 await for (var j in mkStream(3)) { |
904 r += i * j; | 811 r += i * j; |
905 } | 812 } |
906 } | 813 } |
907 return r; | 814 return r; |
908 } | 815 } |
909 | |
910 return f().then((v) { | 816 return f().then((v) { |
911 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); | 817 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); |
912 }); | 818 }); |
913 }); | 819 }); |
914 | 820 |
915 test("nested, await", () { | 821 test("nested, await", () { |
916 f() async { | 822 f() async { |
917 var r = 0; | 823 var r = 0; |
918 await for (var i in mkStream(5)) { | 824 await for (var i in mkStream(5)) { |
919 await for (var j in mkStream(3)) { | 825 await for (var j in mkStream(3)) { |
920 r += await new Future.microtask(() => i * j); | 826 r += await new Future.microtask(() => i * j); |
921 } | 827 } |
922 } | 828 } |
923 return r; | 829 return r; |
924 } | 830 } |
925 | |
926 return f().then((v) { | 831 return f().then((v) { |
927 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); | 832 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); |
928 }); | 833 }); |
929 }); | 834 }); |
930 | 835 |
931 test("nested, await * 2", () { | 836 test("nested, await * 2", () { |
932 f() async { | 837 f() async { |
933 var r = 0; | 838 var r = 0; |
934 await for (var i in mkStream(5)) { | 839 await for (var i in mkStream(5)) { |
935 var ai = await new Future.microtask(() => i); | 840 var ai = await new Future.microtask(() => i); |
936 await for (var j in mkStream(3)) { | 841 await for (var j in mkStream(3)) { |
937 r += await new Future.microtask(() => ai * j); | 842 r += await new Future.microtask(() => ai * j); |
938 } | 843 } |
939 } | 844 } |
940 return r; | 845 return r; |
941 } | 846 } |
942 | |
943 return f().then((v) { | 847 return f().then((v) { |
944 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); | 848 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); |
945 }); | 849 }); |
946 }); | 850 }); |
947 | 851 |
948 test("await pauses loop", () { // //# 05: o
k | 852 test("await pauses loop", () { // //# 05: o
k |
949 var sc; // //# 05: c
ontinued | 853 var sc; // //# 05: c
ontinued |
950 var i = 0; // //# 05: c
ontinued | 854 var i = 0; // //# 05: c
ontinued |
951 void send() { // //# 05: c
ontinued | 855 void send() { // //# 05: c
ontinued |
952 if (i == 5) { // //# 05: c
ontinued | 856 if (i == 5) { // //# 05: c
ontinued |
(...skipping 28 matching lines...) Expand all Loading... |
981 } | 885 } |
982 | 886 |
983 expectList(stream, list) { | 887 expectList(stream, list) { |
984 return stream.toList().then((v) { | 888 return stream.toList().then((v) { |
985 expect(v, equals(list)); | 889 expect(v, equals(list)); |
986 }); | 890 }); |
987 } | 891 } |
988 | 892 |
989 const MS = const Duration(milliseconds: 1); | 893 const MS = const Duration(milliseconds: 1); |
990 | 894 |
991 var getErrors = new StreamTransformer.fromHandlers(handleData: (data, sink) { | 895 var getErrors = new StreamTransformer.fromHandlers( |
992 fail("Unexpected value"); | 896 handleData:(data, sink) { fail("Unexpected value"); }, |
993 }, handleError: (e, s, sink) { | 897 handleError: (e, s, sink) { sink.add(e); }, |
994 sink.add(e); | 898 handleDone: (sink) { sink.close(); }); |
995 }, handleDone: (sink) { | |
996 sink.close(); | |
997 }); | |
998 | 899 |
999 class NotAStream { | 900 class NotAStream { |
1000 listen(oData, {onError, onDone, cancelOnError}) { | 901 listen(oData, {onError, onDone, cancelOnError}) { |
1001 fail("Not implementing Stream."); | 902 fail("Not implementing Stream."); |
1002 } | 903 } |
1003 } | 904 } |
1004 | 905 |
1005 /** | 906 /** |
1006 * Allows two asynchronous executions to synchronize. | 907 * Allows two asynchronous executions to synchronize. |
1007 * | 908 * |
(...skipping 12 matching lines...) Expand all Loading... |
1020 } | 921 } |
1021 | 922 |
1022 // Release whoever is currently waiting. | 923 // Release whoever is currently waiting. |
1023 void release([v]) { | 924 void release([v]) { |
1024 if (_completer != null) { | 925 if (_completer != null) { |
1025 _completer.complete(v); | 926 _completer.complete(v); |
1026 _completer = null; | 927 _completer = null; |
1027 } | 928 } |
1028 } | 929 } |
1029 } | 930 } |
OLD | NEW |