OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import "dart:async"; | 5 import "dart:async"; |
6 import "package:unittest/unittest.dart"; | 6 import "package:unittest/unittest.dart"; |
7 | 7 |
8 main() { | 8 main() { |
9 test("stream iterator basic", () async { | 9 test("stream iterator basic", () async { |
10 var stream = createStream(); | 10 var stream = createStream(); |
(...skipping 21 matching lines...) Expand all Loading... |
32 expect(iterator.current, isNull); | 32 expect(iterator.current, isNull); |
33 expect(await iterator.moveNext(), isFalse); | 33 expect(await iterator.moveNext(), isFalse); |
34 }); | 34 }); |
35 | 35 |
36 test("stream iterator error", () async { | 36 test("stream iterator error", () async { |
37 Stream stream = createErrorStream(); | 37 Stream stream = createErrorStream(); |
38 StreamIterator iterator = new StreamIterator(stream); | 38 StreamIterator iterator = new StreamIterator(stream); |
39 expect(await iterator.moveNext(), isTrue); | 39 expect(await iterator.moveNext(), isTrue); |
40 expect(iterator.current, 42); | 40 expect(iterator.current, 42); |
41 var hasNext = iterator.moveNext(); | 41 var hasNext = iterator.moveNext(); |
42 expect(hasNext, throwsA("BAD")); // This is an async expectation, | 42 expect(hasNext, throwsA("BAD")); // This is an async expectation, |
43 await hasNext.catchError((_){}); // so we have to wait for the future too. | 43 await hasNext.catchError((_) {}); // so we have to wait for the future too. |
44 expect(iterator.current, isNull); | 44 expect(iterator.current, isNull); |
45 expect(await iterator.moveNext(), isFalse); | 45 expect(await iterator.moveNext(), isFalse); |
46 expect(iterator.current, isNull); | 46 expect(iterator.current, isNull); |
47 }); | 47 }); |
48 | 48 |
49 test("stream iterator current/moveNext during move", () async { | 49 test("stream iterator current/moveNext during move", () async { |
50 Stream stream = createStream(); | 50 Stream stream = createStream(); |
51 StreamIterator iterator = new StreamIterator(stream); | 51 StreamIterator iterator = new StreamIterator(stream); |
52 var hasNext = iterator.moveNext(); | 52 var hasNext = iterator.moveNext(); |
53 expect(iterator.moveNext, throwsA(isStateError)); | 53 expect(iterator.moveNext, throwsA(isStateError)); |
54 expect(await hasNext, isTrue); | 54 expect(await hasNext, isTrue); |
55 expect(iterator.current, 42); | 55 expect(iterator.current, 42); |
56 iterator.cancel(); | 56 iterator.cancel(); |
57 }); | 57 }); |
58 | 58 |
59 test("stream iterator error during cancel", () async { | 59 test("stream iterator error during cancel", () async { |
60 Stream stream = createCancelErrorStream(); | 60 Stream stream = createCancelErrorStream(); |
61 StreamIterator iterator = new StreamIterator(stream); | 61 StreamIterator iterator = new StreamIterator(stream); |
62 for (int i = 0; i < 10; i++) { | 62 for (int i = 0; i < 10; i++) { |
63 expect(await iterator.moveNext(), isTrue); | 63 expect(await iterator.moveNext(), isTrue); |
64 expect(iterator.current, i); | 64 expect(iterator.current, i); |
65 } | 65 } |
66 var hasNext = iterator.moveNext(); // active moveNext will be completed. | 66 var hasNext = iterator.moveNext(); // active moveNext will be completed. |
67 var cancel = iterator.cancel(); | 67 var cancel = iterator.cancel(); |
68 expect(cancel, throwsA("BAD")); | 68 expect(cancel, throwsA("BAD")); |
69 expect(await hasNext, isFalse); | 69 expect(await hasNext, isFalse); |
70 expect(await iterator.moveNext(), isFalse); | 70 expect(await iterator.moveNext(), isFalse); |
71 }); | 71 }); |
72 | |
73 } | 72 } |
74 | 73 |
75 Stream createStream() async* { | 74 Stream createStream() async* { |
76 yield 42; | 75 yield 42; |
77 yield 37; | 76 yield 37; |
78 } | 77 } |
79 | 78 |
80 Stream createErrorStream() async* { | 79 Stream createErrorStream() async* { |
81 yield 42; | 80 yield 42; |
82 // Emit an error without stopping the generator. | 81 // Emit an error without stopping the generator. |
83 yield* (new Future.error("BAD").asStream()); | 82 yield* (new Future.error("BAD").asStream()); |
84 yield 37; | 83 yield 37; |
85 } | 84 } |
86 | 85 |
87 /// Create a stream that throws when cancelled. | 86 /// Create a stream that throws when cancelled. |
88 Stream createCancelErrorStream() async* { | 87 Stream createCancelErrorStream() async* { |
89 int i = 0; | 88 int i = 0; |
90 try { | 89 try { |
91 while (true) yield i++; | 90 while (true) yield i++; |
92 } finally { | 91 } finally { |
93 throw "BAD"; | 92 throw "BAD"; |
94 } | 93 } |
95 } | 94 } |
OLD | NEW |