mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:21:07 +00:00
a2b556b937
Change-Id: Ia5c41dda6503e1fbb8cc6099835d07588425e2ba Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128310 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
104 lines
3.6 KiB
Dart
104 lines
3.6 KiB
Dart
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:expect/expect.dart';
|
|
import 'dart:async';
|
|
|
|
_defaultData(x) {}
|
|
_defaultError(e, [st]) {}
|
|
_defaultDone() {}
|
|
|
|
/// Dummy StreamSubscription.
|
|
class MyStreamSubscription<T> implements StreamSubscription<T> {
|
|
final Stream stream;
|
|
final bool cancelOnError;
|
|
Function? handleData = null;
|
|
Function? handleError = null;
|
|
Function? handleDone = null;
|
|
|
|
MyStreamSubscription(this.stream, this.cancelOnError);
|
|
|
|
Future cancel() => Future.value();
|
|
void onData(void handleData(T data)?) {
|
|
this.handleData = handleData == null ? _defaultData : handleData;
|
|
}
|
|
|
|
void onError(Function? handleError) {
|
|
this.handleError = handleError == null ? _defaultError : handleError;
|
|
}
|
|
|
|
void onDone(void handleDone()?) {
|
|
this.handleDone = handleDone == null ? _defaultDone : handleDone;
|
|
}
|
|
|
|
void pause([Future<void>? resumeSignal]) {}
|
|
void resume() {}
|
|
|
|
final isPaused = false;
|
|
Future<E> asFuture<E>([E? futureValue]) => Future.value(futureValue as E);
|
|
}
|
|
|
|
main() {
|
|
var transformer = new StreamTransformer<int, String>(
|
|
(stream, cancelOnError) =>
|
|
new MyStreamSubscription(stream, cancelOnError));
|
|
|
|
var controller = new StreamController<int>(sync: true);
|
|
var stream = controller.stream;
|
|
var transformed = stream.transform(transformer);
|
|
|
|
Expect.isFalse(transformed.isBroadcast);
|
|
|
|
var handleData = (String _) => 499;
|
|
var handleError = (e, st) => 42;
|
|
var handleDone = () => 99;
|
|
|
|
MyStreamSubscription<String> subscription =
|
|
transformed.listen(handleData, onError: handleError, onDone: handleDone) as MyStreamSubscription<String>;
|
|
|
|
Expect.identical(stream, subscription.stream);
|
|
Expect.equals(false, subscription.cancelOnError);
|
|
Expect.identical(handleData, subscription.handleData);
|
|
Expect.identical(handleError, subscription.handleError);
|
|
Expect.identical(handleDone, subscription.handleDone);
|
|
|
|
// Note that we reuse the transformer.
|
|
|
|
controller = new StreamController(sync: true);
|
|
stream = controller.stream;
|
|
transformed = stream.transform(transformer);
|
|
subscription = transformed.listen(null) as MyStreamSubscription<String>;
|
|
|
|
Expect.identical(stream, subscription.stream);
|
|
Expect.equals(false, subscription.cancelOnError);
|
|
Expect.identical(_defaultData, subscription.handleData);
|
|
Expect.identical(_defaultError, subscription.handleError);
|
|
Expect.identical(_defaultDone, subscription.handleDone);
|
|
|
|
controller = new StreamController(sync: true);
|
|
stream = controller.stream;
|
|
transformed = stream.transform(transformer);
|
|
subscription =
|
|
transformed.listen(null, onDone: handleDone, cancelOnError: true) as MyStreamSubscription<String>;
|
|
|
|
Expect.identical(stream, subscription.stream);
|
|
Expect.equals(true, subscription.cancelOnError);
|
|
Expect.identical(_defaultData, subscription.handleData);
|
|
Expect.identical(_defaultError, subscription.handleError);
|
|
Expect.identical(handleDone, subscription.handleDone);
|
|
|
|
controller = new StreamController.broadcast(sync: true);
|
|
stream = controller.stream;
|
|
transformed = stream.transform(transformer);
|
|
Expect.isTrue(transformed.isBroadcast);
|
|
subscription =
|
|
transformed.listen(null, onDone: handleDone, cancelOnError: true) as MyStreamSubscription<String>;
|
|
|
|
Expect.identical(stream, subscription.stream);
|
|
Expect.equals(true, subscription.cancelOnError);
|
|
Expect.identical(_defaultData, subscription.handleData);
|
|
Expect.identical(_defaultError, subscription.handleError);
|
|
Expect.identical(handleDone, subscription.handleDone);
|
|
}
|