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 Future cancel() => null; |
| 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 void resume() {} |
| 37 |
| 38 final isPaused = false; |
| 39 Future asFuture([var futureValue]) => null; |
| 40 } |
| 41 |
| 42 main() { |
| 43 var transformer = new StreamTransformer<int, String> ( |
| 44 (stream, cancelOnError) => new MyStreamSubscription(stream, cancelOnError)); |
| 45 |
| 46 var controller = new StreamController(sync: true); |
| 47 var stream = controller.stream; |
| 48 var transformed = stream.transform(transformer); |
| 49 |
| 50 var handleData = (String _) => 499; |
| 51 var handleError = (e,st) => 42; |
| 52 var handleDone = () => 99; |
| 53 |
| 54 var subscription = |
| 55 transformed.listen(handleData, onError: handleError, onDone: handleDone); |
| 56 |
| 57 Expect.identical(stream, subscription.stream); |
| 58 Expect.equals(false, subscription.cancelOnError); |
| 59 Expect.identical(handleData, subscription.handleData); |
| 60 Expect.identical(handleError, subscription.handleError); |
| 61 Expect.identical(handleDone, subscription.handleDone); |
| 62 |
| 63 // Note that we reuse the transformer. |
| 64 |
| 65 controller = new StreamController(sync: true); |
| 66 stream = controller.stream; |
| 67 transformed = stream.transform(transformer); |
| 68 subscription = transformed.listen(null); |
| 69 |
| 70 Expect.identical(stream, subscription.stream); |
| 71 Expect.equals(false, subscription.cancelOnError); |
| 72 Expect.identical(_defaultData, subscription.handleData); |
| 73 Expect.identical(_defaultError, subscription.handleError); |
| 74 Expect.identical(_defaultDone, subscription.handleDone); |
| 75 |
| 76 controller = new StreamController(sync: true); |
| 77 stream = controller.stream; |
| 78 transformed = stream.transform(transformer); |
| 79 subscription = |
| 80 transformed.listen(null, onDone: handleDone, cancelOnError: true); |
| 81 |
| 82 Expect.identical(stream, subscription.stream); |
| 83 Expect.equals(true, subscription.cancelOnError); |
| 84 Expect.identical(_defaultData, subscription.handleData); |
| 85 Expect.identical(_defaultError, subscription.handleError); |
| 86 Expect.identical(handleDone, subscription.handleDone); |
| 87 } |
OLD | NEW |