From 1f4639770d89411148421daf9160ab952d60ca87 Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Tue, 15 Oct 2024 19:31:01 +0300 Subject: [PATCH] #2933. Add tests for `Stream.error` and `Stream.value` constructors. (#2934) Add tests for `Stream.error` and `Stream.value` constructors. --- .../async/Stream/Stream.error_A01_t01.dart | 37 +++++++++++++++++++ .../async/Stream/Stream.error_A01_t02.dart | 29 +++++++++++++++ .../async/Stream/Stream.error_A01_t03.dart | 33 +++++++++++++++++ .../async/Stream/Stream.value_A01_t01.dart | 27 ++++++++++++++ .../async/Stream/Stream.value_A01_t02.dart | 30 +++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 LibTest/async/Stream/Stream.error_A01_t01.dart create mode 100644 LibTest/async/Stream/Stream.error_A01_t02.dart create mode 100644 LibTest/async/Stream/Stream.error_A01_t03.dart create mode 100644 LibTest/async/Stream/Stream.value_A01_t01.dart create mode 100644 LibTest/async/Stream/Stream.value_A01_t02.dart diff --git a/LibTest/async/Stream/Stream.error_A01_t01.dart b/LibTest/async/Stream/Stream.error_A01_t01.dart new file mode 100644 index 0000000000..8cdaedc6b3 --- /dev/null +++ b/LibTest/async/Stream/Stream.error_A01_t01.dart @@ -0,0 +1,37 @@ +// 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.5") +/// Stream.error( Object error, [ StackTrace? stackTrace ]) +/// +/// Creates a stream which emits a single error event before completing. +/// +/// This stream emits a single error event of `error` and `stackTrace` and then +/// completes with a done event. +/// +/// @description Checks that `Stream.error()` constructor creates a stream which +/// emits a single error event before completing. +/// @author sgrekhov22@gmail.com + +import "dart:async"; +import "../../../Utils/expect.dart"; + +main() async { + Stream stream1 = Stream.error("Stream.error"); + try { + await for (var x in stream1) { + Expect.fail("Unexpected event $x"); + } + } catch (e) { + Expect.equals("Stream.error", e); + } + + Stream stream2 = Stream.error("Stream.error"); + try { + await stream2.isEmpty; + Expect.fail("Expected an error"); + } catch (e) { + Expect.equals("Stream.error", e); + } +} diff --git a/LibTest/async/Stream/Stream.error_A01_t02.dart b/LibTest/async/Stream/Stream.error_A01_t02.dart new file mode 100644 index 0000000000..3727b328f4 --- /dev/null +++ b/LibTest/async/Stream/Stream.error_A01_t02.dart @@ -0,0 +1,29 @@ +// 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.5") +/// Stream.error( Object error, [ StackTrace? stackTrace ]) +/// +/// Creates a stream which emits a single error event before completing. +/// +/// This stream emits a single error event of `error` and `stackTrace` and then +/// completes with a done event. +/// +/// @description Checks that if `stackTrace` is specified it is available when a +/// `catch` clause is executed to catch the error. +/// @author sgrekhov22@gmail.com + +import "dart:async"; +import "../../../Utils/expect.dart"; + +main() async { + StackTrace stackTrace = StackTrace.fromString("My StackTrace"); + Stream stream = Stream.error("Stream.error", stackTrace); + try { + var v = await stream.first; + Expect.fail("Unexpected event $v"); + } catch (e, st) { + Expect.equals("My StackTrace", st.toString()); + } +} diff --git a/LibTest/async/Stream/Stream.error_A01_t03.dart b/LibTest/async/Stream/Stream.error_A01_t03.dart new file mode 100644 index 0000000000..e940935863 --- /dev/null +++ b/LibTest/async/Stream/Stream.error_A01_t03.dart @@ -0,0 +1,33 @@ +// 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.5") +/// Stream.error( Object error, [ StackTrace? stackTrace ]) +/// +/// Creates a stream which emits a single error event before completing. +/// +/// This stream emits a single error event of `error` and `stackTrace` and then +/// completes with a done event. +/// +/// @description Checks that the `Stream.error()` constructor creates a stream +/// which emits a single error event and then calls 'onDone'. +/// @author sgrekhov22@gmail.com + +import "dart:async"; +import "../../../Utils/expect.dart"; + +main() { + var onErrors = 0; + asyncStart(); + Stream stream = Stream.error("Stream.error"); + stream.listen((v) { + Expect.fail("Unexpected event $v"); + }, onError: (e) { + onErrors++; + Expect.equals("Stream.error", e); + }, onDone: () { + Expect.equals(onErrors, 1); + asyncEnd(); + }); +} diff --git a/LibTest/async/Stream/Stream.value_A01_t01.dart b/LibTest/async/Stream/Stream.value_A01_t01.dart new file mode 100644 index 0000000000..44e4cddb38 --- /dev/null +++ b/LibTest/async/Stream/Stream.value_A01_t01.dart @@ -0,0 +1,27 @@ +// 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.5") +/// Stream.value( T value ) +/// Creates a stream which emits a single data event before closing. +/// +/// This stream emits a single data event of value and then closes with a done +/// event. +/// +/// @description Checks that the `Stream.value()` constructor creates a stream +/// which emits a single data event before closing. +/// @author sgrekhov22@gmail.com + +import "dart:async"; +import "../../../Utils/expect.dart"; + +main() async { + var stream1 = Stream.value("Stream.value"); + var v = await stream1.first; + Expect.equals("Stream.value", v); + + Stream stream2 = Stream.value("Stream.value"); + var empty = await stream2.isEmpty; + Expect.isFalse(empty); +} diff --git a/LibTest/async/Stream/Stream.value_A01_t02.dart b/LibTest/async/Stream/Stream.value_A01_t02.dart new file mode 100644 index 0000000000..3376a47548 --- /dev/null +++ b/LibTest/async/Stream/Stream.value_A01_t02.dart @@ -0,0 +1,30 @@ +// 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.5") +/// Stream.value( T value ) +/// Creates a stream which emits a single data event before closing. +/// +/// This stream emits a single data event of value and then closes with a done +/// event. +/// +/// @description Checks that the `Stream.value()` constructor creates a stream +/// which emits a single data event before closing. +/// @author sgrekhov22@gmail.com + +import "dart:async"; +import "../../../Utils/expect.dart"; + +main() { + int counter = 0; + asyncStart(); + Stream stream = Stream.value("Stream.value"); + stream.listen((v) { + counter++; + Expect.equals("Stream.value", v); + }, onDone: () { + Expect.equals(1, counter); + asyncEnd(); + }); +}