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 merging streams. | 5 // Test merging streams. |
6 library dart.test.stream_from_iterable; | 6 library dart.test.stream_from_iterable; |
7 | 7 |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 import "dart:async"; | 9 import "dart:async"; |
10 import '../../../pkg/unittest/lib/unittest.dart'; | 10 import '../../../pkg/unittest/lib/unittest.dart'; |
11 import 'event_helper.dart'; | 11 import 'event_helper.dart'; |
12 | 12 |
13 class IterableTest<T> { | 13 class IterableTest<T> { |
14 static int counter = 0; | 14 static int counter = 0; |
15 Iterable<T> iterable; | 15 Iterable<T> iterable; |
16 IterableTest(this.iterable); | 16 IterableTest(this.iterable); |
17 void run() { | 17 void run() { |
18 test("stream from iterable ${counter++}", () { | 18 test("stream from iterable ${counter++}", () { |
19 Events expected = new Events.fromIterable(iterable); | 19 Events expected = new Events.fromIterable(iterable); |
20 Stream<T> stream = new Stream<T>.fromIterable(iterable); | 20 Stream<T> stream = new Stream<T>.fromIterable(iterable); |
21 Events actual = new Events.capture(stream); | 21 Events actual = new Events.capture(stream); |
22 actual.onDone(expectAsync0(() { | 22 actual.onDone(expectAsync(() { |
23 Expect.listEquals(expected.events, actual.events); | 23 Expect.listEquals(expected.events, actual.events); |
24 })); | 24 })); |
25 }); | 25 }); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 main() { | 29 main() { |
30 new IterableTest([]).run(); | 30 new IterableTest([]).run(); |
31 new IterableTest([1]).run(); | 31 new IterableTest([1]).run(); |
32 new IterableTest([1, "two", true, null]).run(); | 32 new IterableTest([1, "two", true, null]).run(); |
33 new IterableTest<int>([1, 2, 3, 4]).run(); | 33 new IterableTest<int>([1, 2, 3, 4]).run(); |
34 new IterableTest<String>(["one", "two", "three", "four"]).run(); | 34 new IterableTest<String>(["one", "two", "three", "four"]).run(); |
35 new IterableTest<int>(new Iterable<int>.generate(1000, (i) => i)).run(); | 35 new IterableTest<int>(new Iterable<int>.generate(1000, (i) => i)).run(); |
36 new IterableTest<String>(new Iterable<int>.generate(1000, (i) => i) | 36 new IterableTest<String>(new Iterable<int>.generate(1000, (i) => i) |
37 .map((i) => "$i")).run(); | 37 .map((i) => "$i")).run(); |
38 | 38 |
39 Iterable<int> iter = new Iterable.generate(25, (i) => i * 2); | 39 Iterable<int> iter = new Iterable.generate(25, (i) => i * 2); |
40 | 40 |
41 test("iterable-toList", () { | 41 test("iterable-toList", () { |
42 new Stream.fromIterable(iter).toList().then(expectAsync1((actual) { | 42 new Stream.fromIterable(iter).toList().then(expectAsync((actual) { |
43 List expected = iter.toList(); | 43 List expected = iter.toList(); |
44 Expect.equals(25, expected.length); | 44 Expect.equals(25, expected.length); |
45 Expect.listEquals(expected, actual); | 45 Expect.listEquals(expected, actual); |
46 })); | 46 })); |
47 }); | 47 }); |
48 | 48 |
49 test("iterable-mapped-toList", () { | 49 test("iterable-mapped-toList", () { |
50 new Stream.fromIterable(iter) | 50 new Stream.fromIterable(iter) |
51 .map((i) => i * 3) | 51 .map((i) => i * 3) |
52 .toList() | 52 .toList() |
53 .then(expectAsync1((actual) { | 53 .then(expectAsync((actual) { |
54 List expected = iter.map((i) => i * 3).toList(); | 54 List expected = iter.map((i) => i * 3).toList(); |
55 Expect.listEquals(expected, actual); | 55 Expect.listEquals(expected, actual); |
56 })); | 56 })); |
57 }); | 57 }); |
58 | 58 |
59 test("iterable-paused", () { | 59 test("iterable-paused", () { |
60 Stream stream = new Stream.fromIterable(iter); | 60 Stream stream = new Stream.fromIterable(iter); |
61 Events actual = new Events(); | 61 Events actual = new Events(); |
62 StreamSubscription subscription; | 62 StreamSubscription subscription; |
63 subscription = stream.listen((int value) { | 63 subscription = stream.listen((int value) { |
64 actual.add(value); | 64 actual.add(value); |
65 // Do a 10 ms pause during the playback of the iterable. | 65 // Do a 10 ms pause during the playback of the iterable. |
66 Duration duration = const Duration(milliseconds: 10); | 66 Duration duration = const Duration(milliseconds: 10); |
67 if (value == 20) { | 67 if (value == 20) { |
68 subscription.pause(new Future.delayed(duration, () {})); | 68 subscription.pause(new Future.delayed(duration, () {})); |
69 } | 69 } |
70 }, onDone: expectAsync0(() { | 70 }, onDone: expectAsync(() { |
71 actual.close(); | 71 actual.close(); |
72 Events expected = new Events.fromIterable(iter); | 72 Events expected = new Events.fromIterable(iter); |
73 Expect.listEquals(expected.events, actual.events); | 73 Expect.listEquals(expected.events, actual.events); |
74 })); | 74 })); |
75 }); | 75 }); |
76 | 76 |
77 test("iterable-single-subscription", () { | 77 test("iterable-single-subscription", () { |
78 Stream stream = new Stream.fromIterable(iter); | 78 Stream stream = new Stream.fromIterable(iter); |
79 stream.listen((x){}); | 79 stream.listen((x){}); |
80 Expect.throws(() { stream.listen((x){}); }, | 80 Expect.throws(() { stream.listen((x){}); }, |
81 (e) => e is StateError); | 81 (e) => e is StateError); |
82 }); | 82 }); |
83 | 83 |
84 test("regression-14332", () { | 84 test("regression-14332", () { |
85 // Regression test for http://dartbug.com/14332. | 85 // Regression test for http://dartbug.com/14332. |
86 // This should succeede. | 86 // This should succeede. |
87 var from = new Stream.fromIterable([1,2,3,4,5]); | 87 var from = new Stream.fromIterable([1,2,3,4,5]); |
88 | 88 |
89 var c = new StreamController(); | 89 var c = new StreamController(); |
90 var sink = c.sink; | 90 var sink = c.sink; |
91 | 91 |
92 var done = expectAsync0((){}, count: 2); | 92 var done = expectAsync((){}, count: 2); |
93 | 93 |
94 // if this goes first, test failed (hanged). Swapping addStream and toList | 94 // if this goes first, test failed (hanged). Swapping addStream and toList |
95 // made failure go away. | 95 // made failure go away. |
96 sink.addStream(from).then((_) { | 96 sink.addStream(from).then((_) { |
97 c.close(); | 97 c.close(); |
98 done(); | 98 done(); |
99 }); | 99 }); |
100 | 100 |
101 c.stream.toList().then((x) { | 101 c.stream.toList().then((x) { |
102 Expect.listEquals([1,2,3,4,5], x); | 102 Expect.listEquals([1,2,3,4,5], x); |
103 done(); | 103 done(); |
104 }); | 104 }); |
105 }); | 105 }); |
106 | 106 |
107 test("regression-14334-a", () { | 107 test("regression-14334-a", () { |
108 var from = new Stream.fromIterable([1,2,3,4,5]); | 108 var from = new Stream.fromIterable([1,2,3,4,5]); |
109 | 109 |
110 // odd numbers as data events, even numbers as error events | 110 // odd numbers as data events, even numbers as error events |
111 from = from.map((x) => x.isOdd ? x : throw x); | 111 from = from.map((x) => x.isOdd ? x : throw x); |
112 | 112 |
113 var c = new StreamController(); | 113 var c = new StreamController(); |
114 var sink = c.sink; | 114 var sink = c.sink; |
115 | 115 |
116 var done = expectAsync0((){}, count: 2); | 116 var done = expectAsync((){}, count: 2); |
117 | 117 |
118 var data = [], errors = []; | 118 var data = [], errors = []; |
119 c.stream.listen(data.add, onError: errors.add, onDone: () { | 119 c.stream.listen(data.add, onError: errors.add, onDone: () { |
120 Expect.listEquals([1], data); | 120 Expect.listEquals([1], data); |
121 Expect.listEquals([2], errors); | 121 Expect.listEquals([2], errors); |
122 done(); | 122 done(); |
123 }); | 123 }); |
124 sink.addStream(from).then((_) { | 124 sink.addStream(from).then((_) { |
125 c.close(); | 125 c.close(); |
126 done(); | 126 done(); |
127 }); | 127 }); |
128 }); | 128 }); |
129 | 129 |
130 test("regression-14334-b", () { | 130 test("regression-14334-b", () { |
131 var from = new Stream.fromIterable([1,2,3,4,5]); | 131 var from = new Stream.fromIterable([1,2,3,4,5]); |
132 | 132 |
133 // odd numbers as data events, even numbers as error events | 133 // odd numbers as data events, even numbers as error events |
134 from = from.map((x) => x.isOdd ? x : throw x); | 134 from = from.map((x) => x.isOdd ? x : throw x); |
135 | 135 |
136 var c = new StreamController(); | 136 var c = new StreamController(); |
137 | 137 |
138 var done = expectAsync0((){}, count: 2); | 138 var done = expectAsync((){}, count: 2); |
139 | 139 |
140 var data = [], errors = []; | 140 var data = [], errors = []; |
141 c.stream.listen(data.add, onError: errors.add, onDone: () { | 141 c.stream.listen(data.add, onError: errors.add, onDone: () { |
142 Expect.listEquals([1, 3, 5], data); | 142 Expect.listEquals([1, 3, 5], data); |
143 Expect.listEquals([2, 4], errors); | 143 Expect.listEquals([2, 4], errors); |
144 done(); | 144 done(); |
145 }); | 145 }); |
146 c.addStream(from, cancelOnError: false).then((_) { | 146 c.addStream(from, cancelOnError: false).then((_) { |
147 c.close(); | 147 c.close(); |
148 done(); | 148 done(); |
149 }); | 149 }); |
150 }); | 150 }); |
151 } | 151 } |
OLD | NEW |