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 // Test the Stream.single method. |
| 6 library stream_single_test; |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 import 'dart:async'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 |
| 12 main() { |
| 13 test("subscription.asFuture success", () { |
| 14 Stream stream = new Stream.fromIterable([1, 2, 3]); |
| 15 var output = []; |
| 16 var subscription = stream.listen((x) { output.add(x); }); |
| 17 subscription.asFuture(output).then(expectAsync((o) { |
| 18 Expect.listEquals([1, 2, 3], o); |
| 19 })); |
| 20 }); |
| 21 |
| 22 test("subscription.asFuture success2", () { |
| 23 StreamController controller = new StreamController(sync: true); |
| 24 [1, 2, 3].forEach(controller.add); |
| 25 controller.close(); |
| 26 Stream stream = controller.stream; |
| 27 var output = []; |
| 28 var subscription = stream.listen((x) { output.add(x); }); |
| 29 subscription.asFuture(output).then(expectAsync((o) { |
| 30 Expect.listEquals([1, 2, 3], o); |
| 31 })); |
| 32 }); |
| 33 |
| 34 test("subscription.asFuture success 3", () { |
| 35 Stream stream = new Stream.fromIterable([1, 2, 3]).map((x) => x); |
| 36 var output = []; |
| 37 var subscription = stream.listen((x) { output.add(x); }); |
| 38 subscription.asFuture(output).then(expectAsync((o) { |
| 39 Expect.listEquals([1, 2, 3], o); |
| 40 })); |
| 41 }); |
| 42 |
| 43 test("subscription.asFuture different type", () { |
| 44 Stream stream = new Stream<int>.fromIterable([1, 2, 3]); |
| 45 var asyncCallback = expectAsync(() => {}); |
| 46 var output = []; |
| 47 var subscription = stream.listen((x) { output.add(x); }); |
| 48 subscription.asFuture("string").then((String o) { |
| 49 Expect.listEquals([1, 2, 3], output); |
| 50 Expect.equals("string", o); |
| 51 asyncCallback(); |
| 52 }); |
| 53 }); |
| 54 |
| 55 test("subscription.asFuture failure", () { |
| 56 StreamController controller = new StreamController(sync: true); |
| 57 [1, 2, 3].forEach(controller.add); |
| 58 controller.addError("foo"); |
| 59 controller.close(); |
| 60 Stream stream = controller.stream; |
| 61 var output = []; |
| 62 var subscription = stream.listen((x) { output.add(x); }); |
| 63 subscription.asFuture(output).catchError(expectAsync((error) { |
| 64 Expect.equals(error, "foo"); |
| 65 })); |
| 66 }); |
| 67 |
| 68 test("subscription.asFuture failure2", () { |
| 69 Stream stream = new Stream.fromIterable([1, 2, 3, 4]) |
| 70 .map((x) { |
| 71 if (x == 4) throw "foo"; |
| 72 return x; |
| 73 }); |
| 74 var output = []; |
| 75 var subscription = stream.listen((x) { output.add(x); }); |
| 76 subscription.asFuture(output).catchError(expectAsync((error) { |
| 77 Expect.equals(error, "foo"); |
| 78 })); |
| 79 }); |
| 80 |
| 81 test("subscription.asFuture delayed cancel", () { |
| 82 var completer = new Completer(); |
| 83 var controller = |
| 84 new StreamController(onCancel: () => completer.future, sync: true); |
| 85 [1, 2, 3].forEach(controller.add); |
| 86 controller.addError("foo"); |
| 87 controller.close(); |
| 88 Stream stream = controller.stream; |
| 89 var output = []; |
| 90 var subscription = stream.listen((x) { output.add(x); }); |
| 91 bool catchErrorHasRun = false; |
| 92 subscription.asFuture(output).catchError(expectAsync((error) { |
| 93 Expect.equals(error, "foo"); |
| 94 catchErrorHasRun = true; |
| 95 })); |
| 96 Timer.run(expectAsync(() { |
| 97 Expect.isFalse(catchErrorHasRun); |
| 98 completer.complete(); |
| 99 })); |
| 100 }); |
| 101 |
| 102 test("subscription.asFuture failure in cancel", () { |
| 103 runZoned(() { |
| 104 var completer = new Completer(); |
| 105 var controller = |
| 106 new StreamController(onCancel: () => completer.future, sync: true); |
| 107 [1, 2, 3].forEach(controller.add); |
| 108 controller.addError("foo"); |
| 109 controller.close(); |
| 110 Stream stream = controller.stream; |
| 111 var output = []; |
| 112 var subscription = stream.listen((x) { output.add(x); }); |
| 113 bool catchErrorHasRun = false; |
| 114 subscription.asFuture(output).catchError(expectAsync((error) { |
| 115 Expect.equals(error, "foo"); |
| 116 catchErrorHasRun = true; |
| 117 })); |
| 118 Timer.run(expectAsync(() { |
| 119 Expect.isFalse(catchErrorHasRun); |
| 120 completer.completeError(499); |
| 121 })); |
| 122 }, onError: expectAsync((e) { |
| 123 Expect.equals(499, e); |
| 124 })); |
| 125 }); |
| 126 } |
OLD | NEW |