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

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

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

Powered by Google App Engine
This is Rietveld 408576698