From 9a9f23fd3581e5ea1df7efda7c10475bc6b930d0 Mon Sep 17 00:00:00 2001 From: Sigmund Cherem Date: Wed, 14 Feb 2024 21:43:47 +0000 Subject: [PATCH] [tests] update await_for_test to test behavior variations. This introduces an asyncExpectThrowsWhen to async_helper, similar to Expect.throwsWhen, to allow specifying semantics under behavior variations in this test. We also update await_for_test to no longer skip expectations in dart2js. Change-Id: Ie147f74f384a0e196e40b75c59fe585f011ede49 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/351721 Reviewed-by: Lasse Nielsen Reviewed-by: Stephen Adams Commit-Queue: Sigmund Cherem --- pkg/async_helper/lib/async_helper.dart | 13 ++++++++++ .../await_for_test.dart | 26 +++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/async_helper/lib/async_helper.dart b/pkg/async_helper/lib/async_helper.dart index bddbe38b20f..b5926370b03 100644 --- a/pkg/async_helper/lib/async_helper.dart +++ b/pkg/async_helper/lib/async_helper.dart @@ -142,3 +142,16 @@ Future asyncExpectThrows(Future result, return error; }); } + +/// Checks that the asynchronous [result] throws a [T] if and only if +/// [condition] is `true`. +/// +/// When [condition] is `false`, [result] is expected to complete without +/// errors. +Future asyncExpectThrowsWhen( + bool condition, Future result, + [String reason = ""]) { + return condition + ? asyncExpectThrows(result, reason) + : result.then((_) => null); +} diff --git a/tests/language/control_flow_collections/await_for_test.dart b/tests/language/control_flow_collections/await_for_test.dart index 75fdc08dfe7..521cfdb9c58 100644 --- a/tests/language/control_flow_collections/await_for_test.dart +++ b/tests/language/control_flow_collections/await_for_test.dart @@ -281,42 +281,46 @@ Future testKeyOrder() async { Expect.equals("1:a,2:a", set.join(",")); } -Future testRuntimeErrors() async { - // TODO(54798): split these and move them closer to each expectation. - if (!v.checkedParameters || !v.checkedImplicitDowncasts) { - return; +asyncExpectThrowsTypeErrorOrNSM(Future result) { + if (v.checkedParameters) { + return asyncExpectThrows(result); + } else { + return asyncExpectThrows(result); } +} +Future testRuntimeErrors() async { // Cast variable. dynamic nonStream = 3; - asyncExpectThrows(() async { + asyncExpectThrowsTypeErrorOrNSM(() async { [await for (int i in nonStream) 1]; }()); - asyncExpectThrows(() async { + asyncExpectThrowsTypeErrorOrNSM(() async { {await for (int i in nonStream) 1: 1}; }()); - asyncExpectThrows(() async { + asyncExpectThrowsTypeErrorOrNSM(() async { {await for (int i in nonStream) 1}; }()); // Wrong element type. dynamic nonInt = "string"; - asyncExpectThrows(() async { + asyncExpectThrowsWhen(v.checkedImplicitDowncasts, () async { [ await for (var i in stream([1])) nonInt ]; }()); - asyncExpectThrows(() async { + asyncExpectThrowsWhen(v.checkedImplicitDowncasts, () async { { await for (var i in stream([1])) nonInt: 1 }; }()); - asyncExpectThrows(() async { + asyncExpectThrowsWhen(v.checkedImplicitDowncasts, () async { { await for (var i in stream([1])) 1: nonInt }; }()); - asyncExpectThrows(() async { + + asyncExpectThrowsWhen(v.checkedImplicitDowncasts, () async { { await for (var i in stream([1])) nonInt };