Skip to content

Commit

Permalink
#2933. Add tests for Stream.error and Stream.value constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Oct 14, 2024
1 parent f53cb22 commit 79ceb2d
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
37 changes: 37 additions & 0 deletions LibTest/async/Stream/Stream.error_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -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<T>.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);
}
}
28 changes: 28 additions & 0 deletions LibTest/async/Stream/Stream.error_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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<T>.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.
/// @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(stackTrace, st);
}
}
30 changes: 30 additions & 0 deletions LibTest/async/Stream/Stream.error_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -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<T>.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 and then calls 'onDone'.
/// @author sgrekhov22@gmail.com
import "dart:async";
import "../../../Utils/expect.dart";

main() {
asyncStart();
Stream stream = Stream.error("Stream.error");
stream.listen((v) {
Expect.fail("Unexpected event $v");
}, onError: (e) {
Expect.equals("Stream.error", e);
}, onDone: () {
asyncEnd();
});
}
27 changes: 27 additions & 0 deletions LibTest/async/Stream/Stream.value_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -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<T>.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 `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<String>.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);
}
27 changes: 27 additions & 0 deletions LibTest/async/Stream/Stream.value_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -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<T>.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 `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() {
asyncStart();
Stream stream = Stream.value("Stream.value");
stream.listen((v) {
Expect.equals("Stream.value", v);
}, onDone: () {
asyncEnd();
});
}

0 comments on commit 79ceb2d

Please sign in to comment.