-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…2934) Add tests for `Stream.error` and `Stream.value` constructors.
- Loading branch information
Showing
5 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 [email protected] | ||
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); | ||
} | ||
} |
This file contains 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,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<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 it is available when a | ||
/// `catch` clause is executed to catch the error. | ||
/// @author [email protected] | ||
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()); | ||
} | ||
} |
This file contains 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,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<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 the `Stream.error()` constructor creates a stream | ||
/// which emits a single error event and then calls 'onDone'. | ||
/// @author [email protected] | ||
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(); | ||
}); | ||
} |
This file contains 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,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 the `Stream.value()` constructor creates a stream | ||
/// which emits a single data event before closing. | ||
/// @author [email protected] | ||
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); | ||
} |
This file contains 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,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>.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 [email protected] | ||
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(); | ||
}); | ||
} |