OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 import "package:expect/expect.dart"; | |
6 import "package:async_helper/async_helper.dart"; | |
7 import 'dart:async'; | |
8 | |
9 | |
10 class TestStreamConsumer implements StreamConsumer { | |
11 final List expectedEvents; | |
12 final List events = []; | |
13 | |
14 TestStreamConsumer(this.expectedEvents); | |
15 | |
16 Future addStream(Stream stream) { | |
17 return stream.listen(events.add).asFuture(); | |
18 } | |
19 | |
20 Future close() { | |
21 check(); | |
22 return new Future.value(); | |
23 } | |
24 | |
25 void check() { | |
26 Expect.listEquals(expectedEvents, events); | |
27 } | |
28 } | |
29 | |
30 | |
31 // Test several adds follewed by a close. | |
32 void testAddClose() { | |
33 asyncStart(); | |
34 var sink = new StreamSinkAdapter(new TestStreamConsumer([1, 2, 3])); | |
35 sink.add(1); | |
36 sink.add(2); | |
37 sink.add(3); | |
38 sink.close().then((_) { | |
39 asyncEnd(); | |
40 }); | |
41 } | |
42 | |
43 | |
44 // Test several adds follewed by a flush. | |
45 void testAddFlush() { | |
46 asyncStart(); | |
47 var consumer = new TestStreamConsumer([1, 2, 3]); | |
48 var sink = new StreamSinkAdapter(consumer); | |
49 sink.add(1); | |
50 sink.add(2); | |
51 sink.add(3); | |
52 sink.flush().then((_) { | |
53 consumer.check(); | |
54 asyncEnd(); | |
55 }); | |
56 // Not valid during flush. | |
57 Expect.throws(() => sink.add(4)); | |
58 Expect.throws(() => sink.addError("error")); | |
59 Expect.throws(() => sink.addStream(new Stream.fromIterable([]))); | |
60 Expect.throws(() => sink.close()); | |
61 sink.done; // No error. | |
62 } | |
63 | |
64 | |
65 // Test addStream followed by close (pipe). | |
66 void testAddStreamClose() { | |
67 asyncStart(); | |
68 var list = [1, 2, 3]; | |
69 var sink = new StreamSinkAdapter(new TestStreamConsumer(list)); | |
70 new Stream.fromIterable(list).pipe(sink).then((_) { | |
71 asyncEnd(); | |
72 }); | |
73 // Not valid during addStream. | |
74 Expect.throws(() => sink.add(4)); | |
75 Expect.throws(() => sink.addError("error")); | |
76 Expect.throws(() => sink.addStream(new Stream.fromIterable([]))); | |
77 Expect.throws(() => sink.close()); | |
78 sink.done; // No error. | |
79 } | |
80 | |
81 | |
82 // Test several adds followed by addStream and close (pipe). | |
83 void testAddAddStreamClose() { | |
84 asyncStart(); | |
85 var list = [1, 2, 3, 4, 5, 6]; | |
86 var sink = new StreamSinkAdapter(new TestStreamConsumer(list)); | |
87 sink.add(1); | |
88 sink.add(2); | |
89 sink.add(3); | |
90 new Stream.fromIterable(list.skip(3)).pipe(sink).then((_) { | |
91 asyncEnd(); | |
92 }); | |
93 // Not valid during addStream. | |
94 Expect.throws(() => sink.add(4)); | |
95 Expect.throws(() => sink.addError("error")); | |
96 Expect.throws(() => sink.addStream(new Stream.fromIterable([]))); | |
97 Expect.throws(() => sink.close()); | |
98 sink.done; // No error. | |
99 } | |
100 | |
101 | |
102 // Test addError. | |
103 void testAddError() { | |
104 asyncStart(); | |
105 var sink = new StreamSinkAdapter(new TestStreamConsumer([])); | |
106 new Future.error("error").asStream().pipe(sink).catchError((error) { | |
107 Expect.equals("error", error); | |
108 sink.close(); | |
109 asyncEnd(); | |
110 }); | |
111 } | |
112 | |
113 | |
114 void main() { | |
115 asyncStart(); | |
116 testAddClose(); | |
117 testAddFlush(); | |
118 testAddStreamClose(); | |
119 testAddAddStreamClose(); | |
120 testAddError(); | |
121 asyncEnd(); | |
122 } | |
OLD | NEW |