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 import 'package:expect/expect.dart'; |
| 6 import 'package:async_helper/async_helper.dart'; |
| 7 import 'dart:async'; |
| 8 import 'event_helper.dart'; |
| 9 |
| 10 _defaultData(x) {} |
| 11 _defaultError(e, [st]) {} |
| 12 _defaultDone() {} |
| 13 |
| 14 /// Dummy StreamSubscription. |
| 15 class MyStreamSubscription<T> implements StreamSubscription<T> { |
| 16 final Stream stream; |
| 17 final bool cancelOnError; |
| 18 Function handleData = null; |
| 19 Function handleError = null; |
| 20 Function handleDone = null; |
| 21 |
| 22 MyStreamSubscription(this.stream, this.cancelOnError); |
| 23 |
| 24 void cancel() {} |
| 25 void onData(void handleData(T data)) { |
| 26 this.handleData = handleData == null ? _defaultData: handleData; |
| 27 } |
| 28 void onError(Function handleError) { |
| 29 this.handleError = handleError == null ? _defaultError: handleError; |
| 30 } |
| 31 void onDone(void handleDone()) { |
| 32 this.handleDone = handleDone == null ? _defaultDone: handleDone; |
| 33 } |
| 34 |
| 35 void pause([Future resumeSignal]) {} |
| 36 final isPaused = false; |
| 37 Future asFuture([var futureValue]) => null; |
| 38 } |
| 39 |
| 40 main() { |
| 41 var transformer = new StreamTransformer<int, String> ( |
| 42 (stream, cancelOnError) => new MyStreamSubscription(stream, cancelOnError)); |
| 43 |
| 44 var controller = new StreamController(sync: true); |
| 45 var stream = controller.stream; |
| 46 var transformed = stream.transform(transformer); |
| 47 |
| 48 var handleData = (String _) => 499; |
| 49 var handleError = (e,st) => 42; |
| 50 var handleDone = () => 99; |
| 51 |
| 52 var subscription = |
| 53 transformed.listen(handleData, onError: handleError, onDone: handleDone); |
| 54 |
| 55 Expect.identical(stream, subscription.stream); |
| 56 Expect.equals(false, subscription.cancelOnError); |
| 57 Expect.identical(handleData, subscription.handleData); |
| 58 Expect.identical(handleError, subscription.handleError); |
| 59 Expect.identical(handleDone, subscription.handleDone); |
| 60 |
| 61 // Note that we reuse the transformer. |
| 62 |
| 63 controller = new StreamController(sync: true); |
| 64 stream = controller.stream; |
| 65 transformed = stream.transform(transformer); |
| 66 subscription = transformed.listen(null); |
| 67 |
| 68 Expect.identical(stream, subscription.stream); |
| 69 Expect.equals(false, subscription.cancelOnError); |
| 70 Expect.identical(_defaultData, subscription.handleData); |
| 71 Expect.identical(_defaultError, subscription.handleError); |
| 72 Expect.identical(_defaultDone, subscription.handleDone); |
| 73 |
| 74 controller = new StreamController(sync: true); |
| 75 stream = controller.stream; |
| 76 transformed = stream.transform(transformer); |
| 77 subscription = |
| 78 transformed.listen(null, onDone: handleDone, cancelOnError: true); |
| 79 |
| 80 Expect.identical(stream, subscription.stream); |
| 81 Expect.equals(true, subscription.cancelOnError); |
| 82 Expect.identical(_defaultData, subscription.handleData); |
| 83 Expect.identical(_defaultError, subscription.handleError); |
| 84 Expect.identical(handleDone, subscription.handleDone); |
| 85 } |
OLD | NEW |