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