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