OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "dart:async"; |
| 6 import "package:unittest/unittest.dart"; |
| 7 |
| 8 main() { |
| 9 test("stream iterator basic", () async { |
| 10 var stream = createStream(); |
| 11 StreamIterator iterator = new StreamIterator(stream); |
| 12 expect(iterator.current, isNull); |
| 13 expect(await iterator.moveNext(), isTrue); |
| 14 expect(iterator.current, 42); |
| 15 expect(await iterator.moveNext(), isTrue); |
| 16 expect(iterator.current, 37); |
| 17 expect(await iterator.moveNext(), isFalse); |
| 18 expect(iterator.current, isNull); |
| 19 expect(await iterator.moveNext(), isFalse); |
| 20 }); |
| 21 |
| 22 test("stream iterator prefilled", () async { |
| 23 Stream stream = createStream(); |
| 24 StreamIterator iterator = new StreamIterator(stream); |
| 25 await new Future.delayed(Duration.ZERO); |
| 26 expect(iterator.current, isNull); |
| 27 expect(await iterator.moveNext(), isTrue); |
| 28 expect(iterator.current, 42); |
| 29 expect(await iterator.moveNext(), isTrue); |
| 30 expect(iterator.current, 37); |
| 31 expect(await iterator.moveNext(), isFalse); |
| 32 expect(iterator.current, isNull); |
| 33 expect(await iterator.moveNext(), isFalse); |
| 34 }); |
| 35 |
| 36 test("stream iterator error", () async { |
| 37 Stream stream = createErrorStream(); |
| 38 StreamIterator iterator = new StreamIterator(stream); |
| 39 expect(await iterator.moveNext(), isTrue); |
| 40 expect(iterator.current, 42); |
| 41 var hasNext = iterator.moveNext(); |
| 42 expect(hasNext, throwsA("BAD")); // This is an async expectation, |
| 43 await hasNext.catchError((_){}); // so we have to wait for the future too. |
| 44 expect(iterator.current, isNull); |
| 45 expect(await iterator.moveNext(), isFalse); |
| 46 expect(iterator.current, isNull); |
| 47 }); |
| 48 |
| 49 test("stream iterator current/moveNext during move", () async { |
| 50 Stream stream = createStream(); |
| 51 StreamIterator iterator = new StreamIterator(stream); |
| 52 var hasNext = iterator.moveNext(); |
| 53 expect(iterator.moveNext, throwsA(isStateError)); |
| 54 expect(await hasNext, isTrue); |
| 55 expect(iterator.current, 42); |
| 56 iterator.cancel(); |
| 57 }); |
| 58 |
| 59 test("stream iterator error during cancel", () async { |
| 60 Stream stream = createCancelErrorStream(); |
| 61 StreamIterator iterator = new StreamIterator(stream); |
| 62 for (int i = 0; i < 10; i++) { |
| 63 expect(await iterator.moveNext(), isTrue); |
| 64 expect(iterator.current, i); |
| 65 } |
| 66 var hasNext = iterator.moveNext(); // active moveNext will be completed. |
| 67 var cancel = iterator.cancel(); |
| 68 expect(cancel, throwsA("BAD")); |
| 69 expect(await hasNext, isFalse); |
| 70 expect(await iterator.moveNext(), isFalse); |
| 71 }); |
| 72 |
| 73 } |
| 74 |
| 75 Stream createStream() async* { |
| 76 yield 42; |
| 77 yield 37; |
| 78 } |
| 79 |
| 80 Stream createErrorStream() async* { |
| 81 yield 42; |
| 82 // Emit an error without stopping the generator. |
| 83 yield* (new Future.error("BAD").asStream()); |
| 84 yield 37; |
| 85 } |
| 86 |
| 87 /// Create a stream that throws when cancelled. |
| 88 Stream createCancelErrorStream() async* { |
| 89 int i = 0; |
| 90 try { |
| 91 while (true) yield i++; |
| 92 } finally { |
| 93 throw "BAD"; |
| 94 } |
| 95 } |
OLD | NEW |