Skip to content

Commit

Permalink
lint rules: Migrate tests for two rules:
Browse files Browse the repository at this point in the history
* unawaited_futures
* void_checks

Change-Id: I7746c3f66d178a84fc8996dac09d53efc664d14a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394560
Reviewed-by: Phil Quitslund <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
Auto-Submit: Samuel Rawlins <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Nov 11, 2024
1 parent d18d32d commit 0b41ef1
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 258 deletions.
122 changes: 122 additions & 0 deletions pkg/linter/test/rules/unawaited_futures_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,128 @@ class UnawaitedFuturesTest extends LintRuleTest {
@override
String get lintRule => LintNames.unawaited_futures;

test_classImplementsFuture() async {
// https://github.com/dart-lang/linter/issues/2211
await assertDiagnostics(r'''
void f(Future2 p) async {
g(p);
}
Future2 g(Future2 p) => p;
abstract class Future2 implements Future {}
''', [
lint(28, 5),
]);
}

test_functionCall_assigned() async {
await assertNoDiagnostics(r'''
Future<int> f() async {
var x = g();
return x;
}
Future<int> g() => Future.value(0);
''');
}

test_functionCall_awaited() async {
await assertNoDiagnostics(r'''
void f() async {
await g();
}
Future<int> g() => Future.value(0);
''');
}

test_functionCall_inListContext() async {
await assertNoDiagnostics(r'''
void f() async {
var x = [g()];
x..[0] = g();
}
Future<int> g() => Future.value(0);
''');
}

test_functionCall_interpolated_unawaited() async {
await assertDiagnostics(r'''
void f() async {
'${g()}';
}
Future<int> g() => Future.value(0);
''', [
lint(22, 3),
]);
}

test_functionCall_returnedWithFutureType() async {
await assertNoDiagnostics(r'''
void f() async {
<String, Future>{}.putIfAbsent('foo', () => g());
}
Future<int> g() => Future.value(0);
''');
}

test_functionCall_unawaited() async {
await assertDiagnostics(r'''
void f() async {
g();
}
Future<int> g() => Future.value(0);
''', [
lint(19, 4),
]);
}

test_functionCallInCascade_assignment() async {
await assertNoDiagnostics(r'''
void f() async {
C()..futureField = g();
}
Future<int> g() => Future.value(0);
class C {
Future<int>? futureField;
}
''');
}

test_functionCallInCascade_inAsync() async {
await assertDiagnostics(r'''
void f() async {
C()..doAsync();
}
class C {
Future<void> doAsync() async {}
}
''', [
lint(22, 11),
]);
}

test_functionCallInCascade_indexAssignment() async {
await assertNoDiagnostics(r'''
void f() async {
C()
..x?[0] = g();
}
Future<int> g() => Future.value(0);
class C {
List<Future<void>>? x = [];
}
''');
}

test_functionCallInCascade_inSync() async {
await assertNoDiagnostics(r'''
void foo() {
C()..doAsync();
}
class C {
Future<void> doAsync() async {}
}
''');
}

test_undefinedIdentifier() async {
await assertDiagnostics(r'''
f() async {
Expand Down
161 changes: 161 additions & 0 deletions pkg/linter/test/rules/void_checks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ class VoidChecksTest extends LintRuleTest {
@override
String get lintRule => LintNames.void_checks;

test_assert_blockBody_returnStatement() async {
await assertNoDiagnostics(r'''
void f() {
assert(() {
return true;
}());
}
''');
}

test_constructorArgument_genericParameter() async {
await assertDiagnostics(r'''
void f(dynamic p) {
Expand Down Expand Up @@ -126,6 +136,28 @@ void m([void v]) {}
]);
}

test_functionExpression_blockBody_returnStatement_genericContext() async {
await assertNoDiagnostics(r'''
generics_with_function() {
g<T>(T Function() p) => p();
g(() {
return 1;
});
}
''');
}

test_functionExpression_blockBody_returnStatement_voidContext() async {
await assertNoDiagnostics(r'''
void f() {
void g(Function p) {}
g(() async {
return 1;
});
}
''');
}

// https://github.com/dart-lang/linter/issues/2685
test_functionType_FutureOrVoidReturnType_Never() async {
await assertNoDiagnostics(r'''
Expand Down Expand Up @@ -168,6 +200,93 @@ void f() {
''');
}

test_futureOrVoidField_assignDynamic() async {
await assertNoDiagnostics(r'''
import 'dart:async';
void f(A a, dynamic p) {
a.x = p; // OK
}
class A {
FutureOr<void> x;
A(this.x);
}
''');
}

test_futureOrVoidField_assignFutureOrVoid() async {
await assertNoDiagnostics(r'''
import 'dart:async';
void f(A a, FutureOr<void> p) {
a.x = p;
}
class A {
FutureOr<void> x;
A(this.x);
}
''');
}

test_futureOrVoidField_assignFutureVoid() async {
await assertNoDiagnostics(r'''
import 'dart:async';
void f(A a) {
a.x = Future.value();
}
class A {
FutureOr<void> x;
A(this.x);
}
''');
}

test_futureOrVoidField_assignInt() async {
await assertDiagnostics(r'''
import 'dart:async';
void f(A a) {
a.x = 1;
}
class A {
FutureOr<void> x;
A(this.x);
}
''', [
lint(37, 7),
]);
}

test_futureOrVoidField_assignNull() async {
await assertNoDiagnostics(r'''
import 'dart:async';
void f(A a) {
a.x = null; // OK
}
class A {
FutureOr<void> x;
A(this.x);
}
''');
}

test_futureOrVoidFunction_blockBody_returnsFuture() async {
await assertNoDiagnostics(r'''
import 'dart:async';
FutureOr<void> f() {
return Future.value();
}
''');
}

test_futureOrVoidFunction_blockBody_returnStatement() async {
await assertDiagnostics(r'''
import 'dart:async';
FutureOr<void> f() {
return 1;
}
''', [
lint(44, 9),
]);
}

test_listPattern_local() async {
await assertDiagnostics(r'''
void f() {
Expand All @@ -190,6 +309,15 @@ void f(void p) {
]);
}

test_localFunction_emptyBlockBody_matchingFutureOrVoidSignature() async {
await assertNoDiagnostics(r'''
import 'dart:async';
void f(FutureOr<void> Function() p) {
p = () {};
}
''');
}

test_neverReturningCallbackThrows() async {
await assertNoDiagnostics(r'''
import 'dart:async';
Expand All @@ -204,6 +332,15 @@ void f() async {
''');
}

test_nonVoidFunction_assignedToVoidFunction() async {
await assertNoDiagnostics(r'''
void f(void Function() p) {
int g() => 1;
p = g;
}
''');
}

test_recordPattern() async {
await assertDiagnostics(r'''
void f(void p) {
Expand Down Expand Up @@ -260,4 +397,28 @@ class A<T> {
lint(33, 7),
]);
}

test_voidFunction_blockBody_returnStatement() async {
await assertDiagnostics(r'''
void f(dynamic p) {
return p;
}
''', [
lint(22, 9),
]);
}

test_voidFunction_blockBody_returnStatement_empty() async {
await assertNoDiagnostics(r'''
void f() {
return;
}
''');
}

test_voidFunction_expressionBody() async {
await assertNoDiagnostics(r'''
void f() => 7;
''');
}
}
Loading

0 comments on commit 0b41ef1

Please sign in to comment.