Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
cleanup asserts (#2982)
Browse files Browse the repository at this point in the history
  • Loading branch information
pq authored Sep 30, 2021
1 parent 4a21bc0 commit 9443d83
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 68 deletions.
27 changes: 1 addition & 26 deletions test/rule_test_support.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:linter/src/rules.dart';
import 'package:meta/meta.dart';
import 'package:test/test.dart';

export 'package:analyzer/src/dart/error/syntactic_errors.dart';
export 'package:analyzer/src/error/codes.dart';
export 'package:analyzer/src/test_utilities/package_config_file_builder.dart';

Expand Down Expand Up @@ -214,32 +215,6 @@ abstract class LintRuleTest extends PubPackageResolutionTest {
fail(buffer.toString());
}
}

/// todo(pq): consider migrating all calls to assertDiagnostics
Future<void> assertLint(String code) async {
addTestFile(code);
await resolveTestFile();

for (var error in errors) {
if (error.errorCode.name == lintRule) {
return;
}
}

fail('Expected: $lintRule, found none');
}

/// todo(pq): consider migrating all calls to assertDiagnostics
Future<void> assertNoLint(String code) async {
addTestFile(code);
await resolveTestFile();

for (var error in errors) {
if (error.errorCode.name == lintRule) {
fail(error.message);
}
}
}
}

class PubPackageResolutionTest extends _ContextResolutionTest {
Expand Down
8 changes: 5 additions & 3 deletions test/rules/avoid_function_literals_in_foreach_calls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class AvoidFunctionLiteralsInForeachCalls extends LintRuleTest {
String get lintRule => 'avoid_function_literals_in_foreach_calls';

test_expectedIdentifier() async {
// Produces an expected identifier diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
void f(dynamic iter) => iter?.forEach(...);
''');
''', [
// No lint
error(ParserErrorCode.MISSING_IDENTIFIER, 38, 3),
]);
}
}
38 changes: 24 additions & 14 deletions test/rules/avoid_init_to_null.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,53 @@ class AvoidInitToNullTest extends LintRuleTest {
String get lintRule => 'avoid_init_to_null';

test_invalidAssignment_field() async {
// Produces an invalid_assignment compilation error.
await assertNoLint(r'''
await assertDiagnostics(r'''
class X {
int x = null;
}
''');
''', [
// No lint
error(CompileTimeErrorCode.INVALID_ASSIGNMENT, 20, 4),
]);
}

test_invalidAssignment_namedParameter() async {
// Produces an invalid_assignment compilation error.
await assertNoLint(r'''
await assertDiagnostics(r'''
class X {
X({int a: null});
}
''');
''', [
// No lint
error(CompileTimeErrorCode.INVALID_ASSIGNMENT, 22, 4),
]);
}

test_invalidAssignment_namedParameter_fieldFormal() async {
// Produces an invalid_assignment compilation error.
await assertNoLint(r'''
await assertDiagnostics(r'''
class X {
int x;
X({this.x: null});
}
''');
''', [
// No lint
error(CompileTimeErrorCode.INVALID_ASSIGNMENT, 32, 4),
]);
}

test_invalidAssignment_topLevelVariable() async {
// Produces an invalid_assignment compilation error.
await assertNoLint(r'''
await assertDiagnostics(r'''
int i = null;
''');
''', [
// No lint
error(CompileTimeErrorCode.INVALID_ASSIGNMENT, 8, 4),
]);
}

test_nullable_topLevelVariable() async {
await assertLint(r'''
await assertDiagnostics(r'''
int? ii = null;
''');
''', [
lint('avoid_init_to_null', 5, 9),
]);
}
}
14 changes: 10 additions & 4 deletions test/rules/overridden_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,31 @@ class OverriddenFieldsTest extends LintRuleTest {

/// https://github.com/dart-lang/linter/issues/2874
test_conflictingStaticAndInstance() async {
await assertNoLint(r'''
await assertDiagnostics(r'''
class A {
static final String field = 'value';
}
class B extends A {
String field = 'otherValue';
}
''');
''', [
// No lint
]);
}

test_recursiveInterfaceInheritance() async {
// Produces a recursive_interface_inheritance diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
class A extends B {}
class B extends A {
int field = 0;
}
''');
''', [
// No lint
error(CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE, 6, 1),
error(CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE, 27, 1),
]);
}

test_mixinInheritsFromNotObject() async {
Expand Down
8 changes: 5 additions & 3 deletions test/rules/prefer_asserts_in_initializer_lists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class PreferAssertsInInitializerListsTest extends LintRuleTest {
String get lintRule => 'prefer_asserts_in_initializer_lists';

test_nonBoolExpression() async {
// Produces an non_bool_expression diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
class A {
bool? f;
A() {
Expand All @@ -30,6 +29,9 @@ class A {
});
}
}
''');
''', [
// No lint
error(CompileTimeErrorCode.NON_BOOL_EXPRESSION, 40, 50),
]);
}
}
9 changes: 6 additions & 3 deletions test/rules/prefer_const_constructors_in_immutables.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class PreferConstConstructorsInImmutablesTest extends LintRuleTest {
String get lintRule => 'prefer_const_constructors_in_immutables';

test_returnOfInvalidType() async {
// Produces an return_of_invalid_type diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
class F {
factory F.fc() => null;
}
''');
''', [
// No lint
error(
CompileTimeErrorCode.RETURN_OF_INVALID_TYPE_FROM_CONSTRUCTOR, 30, 4),
]);
}
}
16 changes: 10 additions & 6 deletions test/rules/prefer_contains.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ class PreferContainsTest extends LintRuleTest {
String get lintRule => 'prefer_contains';

test_argumentTypeNotAssignable() async {
// Produces an argument_type_not_assignable diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
List<int> list = [];
condition() {
var next;
while ((next = list.indexOf('{')) != -1) {}
}
''');
''', [
// No lint
error(HintCode.UNUSED_LOCAL_VARIABLE, 41, 4),
]);
}

test_unnecessaryCast() async {
// Produces an unnecessary_cast diagnostic.
await assertLint(r'''
await assertDiagnostics(r'''
bool le3 = ([].indexOf(1) as int) > -1;
''');
''', [
lint('prefer_contains', 11, 27),
error(HintCode.UNNECESSARY_CAST, 12, 20),
]);
}
}
9 changes: 6 additions & 3 deletions test/rules/prefer_spread_collections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class PreferSpreadCollectionsTest extends LintRuleTest {
String get lintRule => 'prefer_spread_collections';

test_constInitializedWithNonConstantValue() async {
// Produces a const_initialized_with_non_constant_value diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
const thangs = [];
const cc = []..addAll(thangs);
''');
''', [
// No lint
error(CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE, 30,
18),
]);
}
}
16 changes: 10 additions & 6 deletions test/rules/void_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@ class VoidChecksTest extends LintRuleTest {
String get lintRule => 'void_checks';

test_extraPositionalArgument() async {
// Produces an extra_positional_arguments diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
missing_parameter_for_argument() {
void foo() {}
foo(0);
}
''');
''', [
// No lint
error(CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS, 57, 1),
]);
}

test_returnOfInvalidType() async {
// Produces a return_of_invalid_type diagnostic.
await assertNoLint(r'''
await assertDiagnostics(r'''
void bug2813() {
return 1;
}
''');
''', [
// No lint
error(CompileTimeErrorCode.RETURN_OF_INVALID_TYPE_FROM_FUNCTION, 26, 1),
]);
}
}

0 comments on commit 9443d83

Please sign in to comment.