diff --git a/pkg/async_helper/lib/async_helper.dart b/pkg/async_helper/lib/async_helper.dart index 965385e4a3a..1c9c10e5da2 100644 --- a/pkg/async_helper/lib/async_helper.dart +++ b/pkg/async_helper/lib/async_helper.dart @@ -38,7 +38,10 @@ Exception _buildException(String msg) { } /// Call this method before an asynchronous test is created. -void asyncStart() { +/// +/// If [count] is provided, expect [count] [asyncEnd] calls instead of just one. +void asyncStart([int count = 1]) { + if (count <= 0) return; if (_initialized && _asyncLevel == 0) { throw _buildException('asyncStart() was called even though we are done ' 'with testing.'); @@ -48,7 +51,7 @@ void asyncStart() { _initialized = true; _port = new ReceivePort(); } - _asyncLevel++; + _asyncLevel += count; } /// Call this after an asynchronous test has ended successfully. diff --git a/pkg/dev_compiler/test/codegen/async_helper.dart b/pkg/dev_compiler/test/codegen/async_helper.dart index bfde1a48fa5..49e33ca360c 100644 --- a/pkg/dev_compiler/test/codegen/async_helper.dart +++ b/pkg/dev_compiler/test/codegen/async_helper.dart @@ -35,7 +35,7 @@ Exception _buildException(String msg) { } /// Implementation method called from language_tests.js. -/// Registers the callback that will be used complete the test. +/// Registers the callback that will be used to complete the test. void asyncTestInitialize(_Action0 callback) { _asyncLevel = 0; _initialized = false; @@ -47,7 +47,10 @@ void asyncTestInitialize(_Action0 callback) { bool get asyncTestStarted => _initialized; /// Call this method before an asynchronous test is created. -void asyncStart() { +/// +/// If [count] is provided, expect [count] [asyncEnd] calls instead of just one. +void asyncStart([int count = 1]) { + if (count <= 0) return; if (_initialized && _asyncLevel == 0) { throw _buildException('asyncStart() was called even though we are done ' 'with testing.'); @@ -61,7 +64,7 @@ void asyncStart() { print('unittest-suite-wait-for-done'); _initialized = true; } - _asyncLevel++; + _asyncLevel += count; } /// Call this after an asynchronous test has ended successfully. diff --git a/tests/language_2/await_test.dart b/tests/language_2/await_test.dart index e626409cc64..ff1fe91318d 100644 --- a/tests/language_2/await_test.dart +++ b/tests/language_2/await_test.dart @@ -5,6 +5,7 @@ // VMOptions=---optimization-counter-threshold=10 import 'package:expect/expect.dart'; +import 'package:async_helper/async_helper.dart'; import 'dart:async'; @@ -89,9 +90,6 @@ instanceMembers() async { Expect.equals(e, 5); } -await() => 4; -nonAsyncFunction() => await(); - others() async { var a = "${globalVariable} ${await dummy()} " + await "someString"; Expect.equals(a, "1 1 someString"); @@ -103,7 +101,6 @@ others() async { Expect.equals(b[cnt], 1); var e = b[0] + await dummy(); Expect.equals(e, 2); - Expect.equals(nonAsyncFunction(), 4); } conditionals() async { @@ -120,12 +117,124 @@ conditionals() async { } catch (e) {} } -main() { - for (int i = 0; i < 10; i++) { - staticMembers(); - topLevelMembers(); - instanceMembers(); - conditionals(); - others(); +asserts() async { + for (final FutureOr Function(T) func in [id, future]) { + assert(await func(true)); + assert(id(true), await func("message")); + assert(await func(true), await (func("message"))); + bool success = true; + try { + assert(await func(false), await (func("message"))); + if (assertStatementsEnabled) Expect.fail("Didn't throw"); + } on AssertionError catch (e) { + Expect.equals("message", e.message); + } } } + +controlFlow() async { + for (final FutureOr Function(T) func in [id, future]) { + // For. + var c = 0; + for (var i = await (func(0)); await func(i < 5); await func(i++)) { + c++; + } + Expect.equals(5, c); + // While. + c = 0; + while (await func(c < 5)) c++; + Expect.equals(5, c); + // Do-while. + c = 0; + do { + c++; + } while (await func(c < 5)); + Expect.equals(5, c); + // If. + if (await func(c == 5)) { + Expect.equals(5, c); + } else { + Expect.fail("unreachable"); + } + // Throw. + try { + throw await func("string"); + } on String { + // OK. + } + + try { + await (throw "string"); + } on String { + // OK. + } + // Try/catch/finally + try { + try { + throw "string"; + } catch (e) { + Expect.equals("string", e); + Expect.equals(0, await func(0)); + rethrow; + } finally { + Expect.equals(0, await func(0)); + } + } catch (e) { + Expect.equals(0, await func(0)); + Expect.equals("string", e); + } finally { + Expect.equals(0, await func(0)); + } + // Switch + switch (await func(2)) { + case 2: + break; + default: + Expect.fail("unreachable"); + } + // Return. + Expect.equals( + 42, + await () async { + return await func(42); + }()); + Expect.equals( + 42, + await () async { + return func(42); + }()); + // Yield. + Stream testStream1() async* { + yield await func(42); + } + + Expect.listEquals([42], await testStream1().toList()); + // Yield* + Stream testStream2() async* { + yield* await func(intStream()); + } + + Expect.listEquals([42], await testStream2().toList()); + } +} + +FutureOr future(T value) async => value; +FutureOr id(T value) => value; + +Stream intStream() async* { + yield 42; +} + +main() { + asyncStart(); + for (int i = 0; i < 11; i++) { + asyncTest(staticMembers); + asyncTest(topLevelMembers); + asyncTest(instanceMembers); + asyncTest(conditionals); + asyncTest(others); + asyncTest(asserts); + asyncTest(controlFlow); + } + asyncEnd(); +} diff --git a/tests/language_2/language_2_analyzer.status b/tests/language_2/language_2_analyzer.status index afb8c7ec1f3..dabac006b8a 100644 --- a/tests/language_2/language_2_analyzer.status +++ b/tests/language_2/language_2_analyzer.status @@ -467,6 +467,7 @@ generic_no_such_method_dispatcher_simple_test: Skip # This test is just for kern fuzzy_arrows_test/01: MissingCompileTimeError built_in_identifier_type_annotation_test/22: MissingCompileTimeError # Issue 28813 built_in_identifier_type_annotation_test/85: Crash # Issue 28813 +await_test: Crash # Issue 31540 [ $compiler == dart2analyzer && !$strong ] combiner_type_lookup_indexed_test: StaticWarning # Issue #31484 diff --git a/tests/language_2/language_2_dartdevc.status b/tests/language_2/language_2_dartdevc.status index 58305372add..579852574c9 100644 --- a/tests/language_2/language_2_dartdevc.status +++ b/tests/language_2/language_2_dartdevc.status @@ -121,6 +121,7 @@ assertion_initializer_const_error2_test/cc11: Pass # Issue #31319 assertion_initializer_const_error2_test/none: Pass assertion_initializer_const_function_test/01: Crash assertion_initializer_test: CompileTimeError +await_test: Crash # Issue 31540 black_listed_test/none: fail # Issue 14228 built_in_identifier_prefix_test: CompileTimeError built_in_identifier_type_annotation_test/22: MissingCompileTimeError # Issue 28816 @@ -256,6 +257,7 @@ async_or_generator_return_type_stacktrace_test/02: MissingCompileTimeError async_or_generator_return_type_stacktrace_test/03: MissingCompileTimeError async_return_types_test/tooManyTypeParameters: MissingCompileTimeError async_return_types_test/wrongReturnType: Crash # Maltyped input from Fasta, issue 31414 +await_test: CompileTimeError # Issue 31541 bad_named_parameters2_test/01: MissingCompileTimeError bad_named_parameters_test/01: MissingCompileTimeError bad_named_parameters_test/02: MissingCompileTimeError diff --git a/tests/language_2/language_2_kernel.status b/tests/language_2/language_2_kernel.status index dd9fc3693f9..960cd828d45 100644 --- a/tests/language_2/language_2_kernel.status +++ b/tests/language_2/language_2_kernel.status @@ -66,6 +66,7 @@ async_star_test/03: CompileTimeError # Issue 31402 (Invocation arguments) async_star_test/04: CompileTimeError # Issue 31402 (Invocation arguments) async_star_test/05: CompileTimeError # Issue 31402 (Invocation arguments) async_star_test/none: CompileTimeError # Issue 31402 (Invocation arguments) +await_test: CompileTimeError # Issue 31541 bad_named_parameters2_test/01: MissingCompileTimeError bad_named_parameters_test/01: MissingCompileTimeError bad_named_parameters_test/02: MissingCompileTimeError