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