Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions LibTest/async/Stream/Stream.multi_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2024, 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.

/// @assertion @Since("2.9")
/// Stream<T>.multi(
/// void onListen( MultiStreamController<T> ), {
/// bool isBroadcast = false,
/// })
/// Creates a multi-subscription stream.
///
/// Each time the created stream is listened to, the `onListen` callback is
/// invoked with a new [MultiStreamController], which forwards events to the
/// [StreamSubscription] returned by that [listen] call.
///
/// This allows each listener to be treated as an individual stream.
///
/// The [MultiStreamController] does not support reading its
/// [StreamController.stream]. Setting its [StreamController.onListen] has no
/// effect since the `onListen` callback is called instead, and the
/// [StreamController.onListen] won't be called later. The controller acts like
/// an asynchronous controller, but provides extra methods for delivering events
/// synchronously.
///
/// If `isBroadcast` is set to true, the returned stream's [Stream.isBroadcast]
/// will be true. This has no effect on the stream behavior, it is up to the
/// `onListen` function to act like a broadcast stream if it claims to be one.
///
/// A multi-subscription stream can behave like any other stream. If the
/// `onListen` callback throws on every call after the first, the stream behaves
/// like a single-subscription stream. If the stream emits the same events to
/// all current listeners, it behaves like a broadcast stream.
///
/// @description Checks that `Stream.multi()` constructor creates a
/// multi-subscription stream.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart(2);
var stream = Stream<int>.multi((controller) {
for (var v in [1, 2, 3, 4, 5]) {
controller.add(v);
}
controller.close();
});
Expect.isFalse(stream.isBroadcast);

int i = 1;
stream.listen((v) {
Expect.equals(v, i++);
}, onDone: () {
Expect.equals(6, i);
asyncEnd();
});

int j = 1;
stream.listen((v) {
Expect.equals(v, j++);
}, onDone: () {
Expect.equals(6, j);
asyncEnd();
});
}
63 changes: 63 additions & 0 deletions LibTest/async/Stream/Stream.multi_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2024, 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.

/// @assertion @Since("2.9")
/// Stream<T>.multi(
/// void onListen( MultiStreamController<T> ), {
/// bool isBroadcast = false,
/// })
/// Creates a multi-subscription stream.
///
/// Each time the created stream is listened to, the `onListen` callback is
/// invoked with a new [MultiStreamController], which forwards events to the
/// [StreamSubscription] returned by that [listen] call.
///
/// This allows each listener to be treated as an individual stream.
///
/// The [MultiStreamController] does not support reading its
/// [StreamController.stream]. Setting its [StreamController.onListen] has no
/// effect since the `onListen` callback is called instead, and the
/// [StreamController.onListen] won't be called later. The controller acts like
/// an asynchronous controller, but provides extra methods for delivering events
/// synchronously.
///
/// If `isBroadcast` is set to true, the returned stream's [Stream.isBroadcast]
/// will be true. This has no effect on the stream behavior, it is up to the
/// `onListen` function to act like a broadcast stream if it claims to be one.
///
/// A multi-subscription stream can behave like any other stream. If the
/// `onListen` callback throws on every call after the first, the stream behaves
/// like a single-subscription stream. If the stream emits the same events to
/// all current listeners, it behaves like a broadcast stream.
///
/// @description Checks that each time the created stream is listened to, the
/// `onListen` callback is invoked with a new instance of
/// [MultiStreamController].
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart();
MultiStreamController<int>? c1, c2;
var stream = Stream<int>.multi((MultiStreamController<int> controller) {
if (c1 == null) {
c1 = controller;
} else {
c2 = controller;
}
for (var v in [1, 2, 3, 4, 5]) {
controller.add(v);
}
controller.close();
});

stream.listen((v) {}, onDone: () {
stream.listen((v) {}, onDone: () {
Expect.notIdentical(c1, c2);
asyncEnd();
});
});
}
60 changes: 60 additions & 0 deletions LibTest/async/Stream/Stream.multi_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2024, 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.

/// @assertion @Since("2.9")
/// Stream<T>.multi(
/// void onListen( MultiStreamController<T> ), {
/// bool isBroadcast = false,
/// })
/// Creates a multi-subscription stream.
///
/// Each time the created stream is listened to, the `onListen` callback is
/// invoked with a new [MultiStreamController], which forwards events to the
/// [StreamSubscription] returned by that [listen] call.
///
/// This allows each listener to be treated as an individual stream.
///
/// The [MultiStreamController] does not support reading its
/// [StreamController.stream]. Setting its [StreamController.onListen] has no
/// effect since the `onListen` callback is called instead, and the
/// [StreamController.onListen] won't be called later. The controller acts like
/// an asynchronous controller, but provides extra methods for delivering events
/// synchronously.
///
/// If `isBroadcast` is set to true, the returned stream's [Stream.isBroadcast]
/// will be true. This has no effect on the stream behavior, it is up to the
/// `onListen` function to act like a broadcast stream if it claims to be one.
///
/// A multi-subscription stream can behave like any other stream. If the
/// `onListen` callback throws on every call after the first, the stream behaves
/// like a single-subscription stream. If the stream emits the same events to
/// all current listeners, it behaves like a broadcast stream.
///
/// @description Checks that reading of [MultiStreamController.stream] is not
/// supported.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart();
var stream = Stream<int>.multi((MultiStreamController<int> controller) {
Expect.throws(() {
controller.stream;
});
for (var v in [1, 2, 3, 4, 5]) {
controller.add(v);
}
Expect.throws(() {
controller.stream;
});
controller.close();
Expect.throws(() {
controller.stream;
});
});

stream.listen((v) {}, onDone: asyncEnd);
}
53 changes: 53 additions & 0 deletions LibTest/async/Stream/Stream.multi_A04_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024, 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.

/// @assertion @Since("2.9")
/// Stream<T>.multi(
/// void onListen( MultiStreamController<T> ), {
/// bool isBroadcast = false,
/// })
/// Creates a multi-subscription stream.
///
/// Each time the created stream is listened to, the `onListen` callback is
/// invoked with a new [MultiStreamController], which forwards events to the
/// [StreamSubscription] returned by that [listen] call.
///
/// This allows each listener to be treated as an individual stream.
///
/// The [MultiStreamController] does not support reading its
/// [StreamController.stream]. Setting its [StreamController.onListen] has no
/// effect since the `onListen` callback is called instead, and the
/// [StreamController.onListen] won't be called later. The controller acts like
/// an asynchronous controller, but provides extra methods for delivering events
/// synchronously.
///
/// If `isBroadcast` is set to true, the returned stream's [Stream.isBroadcast]
/// will be true. This has no effect on the stream behavior, it is up to the
/// `onListen` function to act like a broadcast stream if it claims to be one.
///
/// A multi-subscription stream can behave like any other stream. If the
/// `onListen` callback throws on every call after the first, the stream behaves
/// like a single-subscription stream. If the stream emits the same events to
/// all current listeners, it behaves like a broadcast stream.
///
/// @description Checks that setting of [MultiStreamController.onListen] has no
/// effect.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart();
var stream = Stream<int>.multi((MultiStreamController<int> controller) {
controller.onListen = () {
Expect.fail("MultiStreamController.onListen invoked");
};
for (var v in [1, 2, 3, 4, 5]) {
controller.add(v);
}
controller.close();
});
stream.listen((v) {}, onDone: asyncEnd);
}
49 changes: 49 additions & 0 deletions LibTest/async/Stream/Stream.multi_A05_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2024, 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.

/// @assertion @Since("2.9")
/// Stream<T>.multi(
/// void onListen( MultiStreamController<T> ), {
/// bool isBroadcast = false,
/// })
/// Creates a multi-subscription stream.
///
/// Each time the created stream is listened to, the `onListen` callback is
/// invoked with a new [MultiStreamController], which forwards events to the
/// [StreamSubscription] returned by that [listen] call.
///
/// This allows each listener to be treated as an individual stream.
///
/// The [MultiStreamController] does not support reading its
/// [StreamController.stream]. Setting its [StreamController.onListen] has no
/// effect since the `onListen` callback is called instead, and the
/// [StreamController.onListen] won't be called later. The controller acts like
/// an asynchronous controller, but provides extra methods for delivering events
/// synchronously.
///
/// If `isBroadcast` is set to true, the returned stream's [Stream.isBroadcast]
/// will be true. This has no effect on the stream behavior, it is up to the
/// `onListen` function to act like a broadcast stream if it claims to be one.
///
/// A multi-subscription stream can behave like any other stream. If the
/// `onListen` callback throws on every call after the first, the stream behaves
/// like a single-subscription stream. If the stream emits the same events to
/// all current listeners, it behaves like a broadcast stream.
///
/// @description Checks that if `isBroadcast` is set to true, the returned
/// stream's [Stream.isBroadcast] is true.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "../../../Utils/expect.dart";

main() {
var stream = Stream<int>.multi((controller) {
for (var v in [1, 2, 3, 4, 5]) {
controller.add(v);
}
controller.close();
}, isBroadcast: true);
Expect.isTrue(stream.isBroadcast);
}
47 changes: 47 additions & 0 deletions LibTest/async/Stream/Stream.multi_all_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024, 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.

/// @assertion @Since("2.9")
/// Stream<T>.multi(
/// void onListen( MultiStreamController<T> ), {
/// bool isBroadcast = false,
/// })
/// Creates a multi-subscription stream.
///
/// Each time the created stream is listened to, the `onListen` callback is
/// invoked with a new [MultiStreamController], which forwards events to the
/// [StreamSubscription] returned by that [listen] call.
///
/// This allows each listener to be treated as an individual stream.
///
/// The [MultiStreamController] does not support reading its
/// [StreamController.stream]. Setting its [StreamController.onListen] has no
/// effect since the `onListen` callback is called instead, and the
/// [StreamController.onListen] won't be called later. The controller acts like
/// an asynchronous controller, but provides extra methods for delivering events
/// synchronously.
///
/// If `isBroadcast` is set to true, the returned stream's [Stream.isBroadcast]
/// will be true. This has no effect on the stream behavior, it is up to the
/// `onListen` function to act like a broadcast stream if it claims to be one.
///
/// A multi-subscription stream can behave like any other stream. If the
/// `onListen` callback throws on every call after the first, the stream behaves
/// like a single-subscription stream. If the stream emits the same events to
/// all current listeners, it behaves like a broadcast stream.
///
/// @description Checks Stream interface methods.
/// @author sgrekhov22@gmail.com

import "dart:async";
import "allTests_A01.lib.dart" as all;

main() {
all.test(<T>(Iterable<T> data) => Stream<T>.multi((controller) {
for (var v in data) {
controller.add(v);
}
controller.close();
}));
}
9 changes: 9 additions & 0 deletions Utils/expect_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class Expect {
}
}

/// Checks whether the expected and actual values are identical (using
/// `identical`).
static void notIdentical(var expected, var actual, [String reason = '']) {
if (_identical(expected, actual)) {
_fail('Expect.notIdentical(expected: <$expected>, '
'actual: <$actual>$reason) fails.');
}
}

/// Unconditional failure.
static void fail(String reason) {
_fail('Expect.fail($reason)');
Expand Down
26 changes: 26 additions & 0 deletions Utils/tests/Expect/notIdentical_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2024, 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.

/// @assertion static void notIdentical(
/// var expected, var actual, [String reason = ''])
/// Checks whether the expected and actual values are not identical.
///
/// @description Checks that no exception is thrown when the arguments are not
/// identical.
/// @author sgrekhov22@gmail.com

import "../../../Utils/expect.dart";

class C {
final value;
const C(this.value);
}

main() {
Expect.notIdentical("x", String.fromCharCode("x".codeUnitAt(0)));
Expect.notIdentical(Object(), Object());
Expect.notIdentical(Object(), const Object());
Expect.notIdentical(C(42), C(42));
Expect.notIdentical(C(42), const C(42));
}
Loading