-
Notifications
You must be signed in to change notification settings - Fork 29
#2933. Add tests for Stream.multi constructor.
#2935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| })); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.