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