OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.streams.concat_test; |
| 16 |
| 17 import 'dart:async'; |
| 18 |
| 19 import 'package:test/test.dart'; |
| 20 import 'package:quiver/streams.dart'; |
| 21 |
| 22 main() { |
| 23 group('concat', () { |
| 24 test('should produce no events for no streams', |
| 25 () => concat([]).toList().then((events) => expect(events, isEmpty))); |
| 26 |
| 27 test('should echo events of a single stream', () { |
| 28 var controller = new StreamController<String>(); |
| 29 var concatenated = concat([controller.stream]); |
| 30 var expectation = concatenated.toList().then((e) { |
| 31 expect(e, ['a', 'b', 'c']); |
| 32 }); |
| 33 ['a', 'b', 'c'].forEach(controller.add); |
| 34 return Future.wait([controller.close(), expectation]); |
| 35 }); |
| 36 |
| 37 test('should handle empty streams', () { |
| 38 var concatenated = concat([new Stream.fromIterable([])]); |
| 39 return concatenated.toList().then((e) { |
| 40 expect(e, []); |
| 41 }); |
| 42 }); |
| 43 |
| 44 test('should concatenate stream data events', () { |
| 45 var controller1 = new StreamController<String>(); |
| 46 var controller2 = new StreamController<String>(); |
| 47 var concatenated = concat([controller1.stream, controller2.stream]); |
| 48 var expectation = concatenated.toList().then((e) { |
| 49 expect(e, ['a', 'b', 'c', 'd', 'e', 'f']); |
| 50 }); |
| 51 ['a', 'b', 'c'].forEach(controller1.add); |
| 52 ['d', 'e', 'f'].forEach(controller2.add); |
| 53 return Future |
| 54 .wait([controller1.close(), controller2.close(), expectation]); |
| 55 }); |
| 56 |
| 57 test('should concatenate stream error events', () { |
| 58 var controller1 = new StreamController<String>(); |
| 59 var controller2 = new StreamController<String>(); |
| 60 var concatenated = concat([controller1.stream, controller2.stream]); |
| 61 var errors = []; |
| 62 concatenated.listen(null, onError: errors.add); |
| 63 ['e1', 'e2'].forEach(controller1.addError); |
| 64 ['e3', 'e4'].forEach(controller2.addError); |
| 65 return Future.wait([controller1.close(), controller2.close()]).then((_) { |
| 66 expect(errors, ['e1', 'e2', 'e3', 'e4']); |
| 67 }); |
| 68 }); |
| 69 |
| 70 test('should forward pause, resume, and cancel to current stream', () { |
| 71 var wasPaused = false, |
| 72 wasResumed = false, |
| 73 wasCanceled = false; |
| 74 var controller = new StreamController<String>( |
| 75 onPause: () => wasPaused = true, |
| 76 onResume: () => wasResumed = true, |
| 77 onCancel: () { |
| 78 wasCanceled = true; |
| 79 }); |
| 80 var concatenated = concat([controller.stream]); |
| 81 var subscription = concatenated.listen(null); |
| 82 controller.add('a'); |
| 83 return new Future.value() |
| 84 .then((_) => subscription.pause()) |
| 85 .then((_) => subscription.resume()) |
| 86 |
| 87 // Give resume a chance to take effect. |
| 88 .then((_) => controller.add('b')) |
| 89 .then((_) => new Future(subscription.cancel)) |
| 90 .then((_) { |
| 91 expect(wasPaused, isTrue, reason: 'was not paused'); |
| 92 expect(wasResumed, isTrue, reason: 'was not resumed'); |
| 93 expect(wasCanceled, isTrue, reason: 'was not canceled'); |
| 94 }).then((_) => controller.close()); |
| 95 }); |
| 96 |
| 97 test('should forward iteration error and stop', () { |
| 98 var data = [], |
| 99 errors = []; |
| 100 var badIteration = |
| 101 ['e', 'this should not get thrown'].map((message) => throw message); |
| 102 var concatenated = concat(badIteration); |
| 103 var completer = new Completer(); |
| 104 concatenated |
| 105 .listen(data.add, onError: errors.add, onDone: completer.complete); |
| 106 return completer.future.then((_) { |
| 107 expect(data, []); |
| 108 expect(errors, ['e']); |
| 109 }); |
| 110 }); |
| 111 }); |
| 112 } |
OLD | NEW |