dart-sdk/tests/lib/async/unawaited_test.dart
Lasse R.H. Nielsen bdbd3c0602 Make unawaited accept a nullable argument.
The associated `unawaited_futures` lint triggers for expressions
of type `Future?` as well, so we should allow the workaround to
handle such expressions.

This change makes a static function more permissive, and is not expected
to be breaking in any way.
(It does mean that you need Dart 2.15.0 to use the function with
nullable arguments. I've changed the annotation to say `@Since("2.15")`.
That means anyone using a 2.15 SDK should assume that they need a 2.15
SDK to use it, and someone using a 2.14 SDK will not see the argument
as nullable. If anyone uses `@Since` annotations for anything at all.)

Change-Id: Ib2da4b353104cc88a834208a6ebd788ae55b4544
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214406
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
2021-09-27 11:58:16 +00:00

56 lines
1.4 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.
import 'package:async_helper/async_helper.dart';
import "package:expect/expect.dart";
import 'dart:async' show Completer, runZonedGuarded, unawaited;
import 'dart:async' as prefix;
import '../../language/static_type_helper.dart';
void main() {
testUnawaited();
}
void testUnawaited() {
// Exists where expected.
prefix.unawaited.expectStaticType<Exactly<void Function(Future<Object?>?)>>();
var future = Future<int>.value(42);
captureStaticType(unawaited(future), <T>(value) {
Expect.equals(typeOf<void>(), T);
});
Future<Never>? noFuture = null;
unawaited(noFuture); // Doesn't throw on null.
asyncStart();
// Unawaited futures still throw.
{
var c = Completer<int>();
var f = c.future;
unawaited(f);
asyncStart();
f.catchError((e) {
Expect.equals("ERROR1", e);
asyncEnd();
return 0;
});
c.completeError("ERROR1");
}
// Unawaited futures are still uncaught errors.
{
asyncStart();
runZonedGuarded(() {
var c = Completer<int>();
var f = c.future;
unawaited(f);
c.completeError("ERROR2");
}, (e, s) {
Expect.equals("ERROR2", e);
asyncEnd();
});
}
asyncEnd();
}