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