Skip to content

Commit

Permalink
api test [nfc]: Simplify some unawaited checks
Browse files Browse the repository at this point in the history
The `finish` helper is only needed when the future that needs to finish
isn't already one created by `check` (which takes care of the effect of
`finish` for itself).
  • Loading branch information
gnprice committed Oct 12, 2024
1 parent 3c0ed49 commit 1aef868
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions test/api/core_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -65,18 +66,16 @@ void main() {

test('send rejects off-realm URL (with default useAuth)', () async {
void checkAllow(String realmUrl, String requestUrl) {
finish(() async {
check(await makeRequest(realmUrl, requestUrl))
.isA<http.Request>()
.url.asString.equals(requestUrl);
}());
// No need to await directly; `check` ensures the future completes
// before the enclosing test is considered complete.
unawaited(check(makeRequest(realmUrl, requestUrl))
.completes((it) => it.isA<http.Request>()
.url.asString.equals(requestUrl)));
}

void checkDeny(String realmUrl, String requestUrl) async {
finish(() async {
await check(makeRequest(realmUrl, requestUrl))
.throws<StateError>();
}());
void checkDeny(String realmUrl, String requestUrl) {
unawaited(check(makeRequest(realmUrl, requestUrl))
.throws<StateError>());
}

// Baseline: normal requests are allowed.
Expand Down Expand Up @@ -246,7 +245,7 @@ void main() {
test('API network errors', () async {
void checkRequest<T extends Object>(
T exception, Condition<NetworkException> condition) {
finish(check(tryRequest(exception: exception))
unawaited(check(tryRequest(exception: exception))
.throws<NetworkException>((it) => it
..routeName.equals(kExampleRouteName)
..cause.equals(exception)
Expand Down Expand Up @@ -300,7 +299,7 @@ void main() {
void checkMalformed({
int httpStatus = 400, Map<String, dynamic>? json, String? body}) {
assert((json == null) != (body == null));
finish(check(tryRequest(httpStatus: httpStatus, json: json, body: body))
unawaited(check(tryRequest(httpStatus: httpStatus, json: json, body: body))
.throws<MalformedServerResponseException>((it) => it
..routeName.equals(kExampleRouteName)
..httpStatus.equals(httpStatus)
Expand Down

0 comments on commit 1aef868

Please sign in to comment.