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