OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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.async.stream_router_test; |
| 16 |
| 17 import 'dart:async'; |
| 18 |
| 19 import 'package:test/test.dart'; |
| 20 import 'package:quiver/async.dart'; |
| 21 |
| 22 main() { |
| 23 group('StreamRouter', () { |
| 24 test('should route an event to the correct stream', () { |
| 25 var controller = new StreamController<String>(); |
| 26 new StreamRouter<String>(controller.stream) |
| 27 ..route((e) => e == 'foo').listen((e) { |
| 28 expect(e, 'foo'); |
| 29 }) |
| 30 ..route((e) => e == 'bar').listen((e) { |
| 31 fail('wrong stream'); |
| 32 }) |
| 33 ..route((e) => e == 'foo').listen((e) { |
| 34 fail('wrong stream'); |
| 35 }) |
| 36 ..defaultStream.listen((e) { |
| 37 fail('wrong stream'); |
| 38 }); |
| 39 controller.add('foo'); |
| 40 return controller.close(); |
| 41 }); |
| 42 |
| 43 test('should send events that match no predicate to defaultStream', () { |
| 44 var controller = new StreamController<String>(); |
| 45 new StreamRouter<String>(controller.stream) |
| 46 ..route((e) => e == 'foo').listen((e) { |
| 47 fail('wrong stream'); |
| 48 }) |
| 49 ..route((e) => e == 'bar').listen((e) { |
| 50 fail('wrong stream'); |
| 51 }) |
| 52 ..defaultStream.listen((e) { |
| 53 expect(e, 'baz'); |
| 54 }); |
| 55 controller.add('baz'); |
| 56 return controller.close(); |
| 57 }); |
| 58 |
| 59 test('should close child streams', () { |
| 60 var controller = new StreamController<int>(sync: true); |
| 61 var router = new StreamRouter<int>(controller.stream); |
| 62 // toList() will only complete when the child streams are closed |
| 63 var future = Future |
| 64 .wait([ |
| 65 router.route((e) => e % 2 == 0).toList(), |
| 66 router.route((e) => e % 2 == 1).toList(), |
| 67 ]) |
| 68 .then((l) { |
| 69 expect(l, [[4], [5]]); |
| 70 }); |
| 71 controller |
| 72 ..add(4) |
| 73 ..add(5); |
| 74 router.close(); |
| 75 return future; |
| 76 }); |
| 77 }); |
| 78 } |
OLD | NEW |