| 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 // Test the Stream.single method. | 5 // Test the Stream.single method. |
| 6 library stream_single_test; | 6 library stream_single_test; |
| 7 | 7 |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| 11 | 11 |
| 12 main() { | 12 main() { |
| 13 test("subscription.asFuture success", () { | 13 test("subscription.asStream success", () { |
| 14 Stream stream = new Stream.fromIterable([1, 2, 3]); | 14 Stream stream = new Stream.fromIterable([1, 2, 3]); |
| 15 var output = []; | 15 var output = []; |
| 16 var subscription = stream.listen((x) { output.add(x); }); | 16 var subscription = stream.listen((x) { output.add(x); }); |
| 17 subscription.asFuture(output).then(expectAsync((o) { | 17 subscription.asFuture(output).then(expectAsync((o) { |
| 18 Expect.listEquals([1, 2, 3], o); | 18 Expect.listEquals([1, 2, 3], o); |
| 19 })); | 19 })); |
| 20 }); | 20 }); |
| 21 | 21 |
| 22 test("subscription.asFuture success2", () { | 22 test("subscription.asStream success2", () { |
| 23 StreamController controller = new StreamController(sync: true); | 23 StreamController controller = new StreamController(sync: true); |
| 24 [1, 2, 3].forEach(controller.add); | 24 [1, 2, 3].forEach(controller.add); |
| 25 controller.close(); | 25 controller.close(); |
| 26 Stream stream = controller.stream; | 26 Stream stream = controller.stream; |
| 27 var output = []; | 27 var output = []; |
| 28 var subscription = stream.listen((x) { output.add(x); }); | 28 var subscription = stream.listen((x) { output.add(x); }); |
| 29 subscription.asFuture(output).then(expectAsync((o) { | 29 subscription.asFuture(output).then(expectAsync((o) { |
| 30 Expect.listEquals([1, 2, 3], o); | 30 Expect.listEquals([1, 2, 3], o); |
| 31 })); | 31 })); |
| 32 }); | 32 }); |
| 33 | 33 |
| 34 test("subscription.asFuture success 3", () { | 34 test("subscription.asStream success 3", () { |
| 35 Stream stream = new Stream.fromIterable([1, 2, 3]).map((x) => x); | 35 Stream stream = new Stream.fromIterable([1, 2, 3]).map((x) => x); |
| 36 var output = []; | 36 var output = []; |
| 37 var subscription = stream.listen((x) { output.add(x); }); | 37 var subscription = stream.listen((x) { output.add(x); }); |
| 38 subscription.asFuture(output).then(expectAsync((o) { | 38 subscription.asFuture(output).then(expectAsync((o) { |
| 39 Expect.listEquals([1, 2, 3], o); | 39 Expect.listEquals([1, 2, 3], o); |
| 40 })); | 40 })); |
| 41 }); | 41 }); |
| 42 | 42 |
| 43 test("subscription.asFuture different type", () { | 43 test("subscription.asStream different type", () { |
| 44 Stream stream = new Stream<int>.fromIterable([1, 2, 3]); | 44 Stream stream = new Stream<int>.fromIterable([1, 2, 3]); |
| 45 var asyncCallback = expectAsync(() => {}); | 45 var asyncCallback = expectAsync(() => {}); |
| 46 var output = []; | 46 var output = []; |
| 47 var subscription = stream.listen((x) { output.add(x); }); | 47 var subscription = stream.listen((x) { output.add(x); }); |
| 48 subscription.asFuture("string").then((String o) { | 48 subscription.asFuture("string").then((String o) { |
| 49 Expect.listEquals([1, 2, 3], output); | 49 Expect.listEquals([1, 2, 3], output); |
| 50 Expect.equals("string", o); | 50 Expect.equals("string", o); |
| 51 asyncCallback(); | 51 asyncCallback(); |
| 52 }); | 52 }); |
| 53 }); | 53 }); |
| 54 | 54 |
| 55 test("subscription.asFuture failure", () { | 55 test("subscription.asStream failure", () { |
| 56 StreamController controller = new StreamController(sync: true); | 56 StreamController controller = new StreamController(sync: true); |
| 57 [1, 2, 3].forEach(controller.add); | 57 [1, 2, 3].forEach(controller.add); |
| 58 controller.addError("foo"); | 58 controller.addError("foo"); |
| 59 controller.close(); | 59 controller.close(); |
| 60 Stream stream = controller.stream; | 60 Stream stream = controller.stream; |
| 61 var output = []; | 61 var output = []; |
| 62 var subscription = stream.listen((x) { output.add(x); }); | 62 var subscription = stream.listen((x) { output.add(x); }); |
| 63 subscription.asFuture(output).catchError(expectAsync((error) { | 63 subscription.asFuture(output).catchError(expectAsync((error) { |
| 64 Expect.equals(error, "foo"); | 64 Expect.equals(error, "foo"); |
| 65 })); | 65 })); |
| 66 }); | 66 }); |
| 67 | 67 |
| 68 test("subscription.asFuture failure2", () { | 68 test("subscription.asStream failure2", () { |
| 69 Stream stream = new Stream.fromIterable([1, 2, 3, 4]) | 69 Stream stream = new Stream.fromIterable([1, 2, 3, 4]) |
| 70 .map((x) { | 70 .map((x) { |
| 71 if (x == 4) throw "foo"; | 71 if (x == 4) throw "foo"; |
| 72 return x; | 72 return x; |
| 73 }); | 73 }); |
| 74 var output = []; | 74 var output = []; |
| 75 var subscription = stream.listen((x) { output.add(x); }); | 75 var subscription = stream.listen((x) { output.add(x); }); |
| 76 subscription.asFuture(output).catchError(expectAsync((error) { | 76 subscription.asFuture(output).catchError(expectAsync((error) { |
| 77 Expect.equals(error, "foo"); | 77 Expect.equals(error, "foo"); |
| 78 })); | 78 })); |
| 79 }); | 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 } | 80 } |
| OLD | NEW |