Files
sdk/tests/lib/js/js_util/async_test.dart
T
Lasse R.H. Nielsen cd0606e425 Simplify asyncExpectThrows and enhance Expect.throws.
For `asyncExpectThrows`, instead of taking a function,
then checking if that function can be called with zero arguments,
and then immediately calling it and checking that it returns a future,
just take the future as argument.

Since synchronus errors from calling the function were not caught
anyway, doing the entire `Future` computation directly shouldn't
change behavior.

Also make `asyncExpectThrows` and `Expect.throws` return the caught error,
so that you can use normal `Expect.something` checks on it afterwards,
instead of doing that in the `check` function.
That basically makes the `check` function unnecessary (but hard to remove
with the existing test corpus using it heavily).

This makes some uses of the `asyncExpectThrows` function slightly more
complicated, those that have no other way to create a future than calling
the argument function anyway, but other uses become simpler
when they can avoid adding the function wrapper.

TEST= Refactoring. If the tests keep running, it's successful.

Change-Id: I983eb65ea4805760339073fabc27f78c57f9a471
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/226102
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2022-01-06 17:06:47 +00:00

61 lines
1.5 KiB
Dart

// Copyright (c) 2021, 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.
@JS()
library js_util_async_test;
import 'dart:async';
import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util;
import 'package:expect/minitest.dart';
import 'package:async_helper/async_helper.dart';
@JS()
external void eval(String code);
@JS()
abstract class Promise<T> {}
@JS()
external Promise get resolvedPromise;
@JS()
external Promise get rejectedPromise;
@JS()
external Promise getResolvedPromise();
main() {
eval(r"""
var rejectedPromise = new Promise((resolve, reject) => reject('rejected'));
var resolvedPromise = new Promise(resolve => resolve('resolved'));
function getResolvedPromise() {
return resolvedPromise;
}
""");
Future<void> testResolvedPromise() async {
final String result = await js_util.promiseToFuture(resolvedPromise);
expect(result, equals('resolved'));
}
Future<void> testRejectedPromise() async {
final String error = await asyncExpectThrows<String>(
js_util.promiseToFuture(rejectedPromise));
expect(error, equals('rejected'));
}
Future<void> testReturnResolvedPromise() async {
final String result = await js_util.promiseToFuture(getResolvedPromise());
expect(result, equals('resolved'));
}
asyncTest(() async {
await testResolvedPromise();
await testRejectedPromise();
await testReturnResolvedPromise();
});
}