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

more test diagnostic cleanup #2984

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrmm. Looks like I didn't rebase. Luckily the change that removes assertLint has landed. Sorry for the noise!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


/// 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
5 changes: 5 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@
import 'avoid_function_literals_in_foreach_calls.dart'
as avoid_function_literals_in_foreach_calls;
import 'avoid_init_to_null.dart' as avoid_init_to_null;
import 'missing_whitespace_between_adjacent_strings.dart'
as missing_whitespace_between_adjacent_strings;
import 'overridden_fields.dart' as overridden_fields;
import 'prefer_asserts_in_initializer_lists.dart'
as prefer_asserts_in_initializer_lists;
import 'prefer_const_constructors_in_immutables.dart'
as prefer_const_constructors_in_immutables;
import 'prefer_contains.dart' as prefer_contains;
import 'prefer_spread_collections.dart' as prefer_spread_collections;
import 'type_init_formals.dart' as type_init_formals;
import 'void_checks.dart' as void_checks;

void main() {
avoid_function_literals_in_foreach_calls.main();
avoid_init_to_null.main();
missing_whitespace_between_adjacent_strings.main();
overridden_fields.main();
prefer_asserts_in_initializer_lists.main();
prefer_const_constructors_in_immutables.main();
prefer_contains.main();
prefer_spread_collections.main();
type_init_formals.main();
void_checks.main();
}
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),
]);
}
}
30 changes: 30 additions & 0 deletions test/rules/missing_whitespace_between_adjacent_strings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(MissingWhitespaceBetweenAdjacentStringsTest);
});
}

@reflectiveTest
class MissingWhitespaceBetweenAdjacentStringsTest extends LintRuleTest {
@override
String get lintRule => 'missing_whitespace_between_adjacent_strings';

test_extraPositionalArgument() async {
await assertDiagnostics(r'''
void f() {
new Unresolved('aaa' 'bbb');
}
''', [
// No lint
error(CompileTimeErrorCode.NEW_WITH_NON_TYPE, 17, 10),
]);
}
}
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),
]);
}
}
33 changes: 33 additions & 0 deletions test/rules/type_init_formals.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(TypeInitFormalsTest);
});
}

@reflectiveTest
class TypeInitFormalsTest extends LintRuleTest {
@override
String get lintRule => 'type_init_formals';

test_extraPositionalArgument() async {
await assertDiagnostics(r'''
class A {
String? p1;
String p2 = '';
A.y({required String? this.p2});
}
''', [
// No lint
error(CompileTimeErrorCode.FIELD_INITIALIZING_FORMAL_NOT_ASSIGNABLE, 49,
24),
]);
}
}
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),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

class A {
String? p1;
String p2;
String p2 = '';

A.w({required String this.p1}); // OK
A.x({required String? this.p1}); // LINT
A.y({required String? this.p2}); // OK
A.z({required String this.p2}); // LINT
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// test w/ `dart test -N unrelated_type_equality_checks`

bool m(int? a1, num a2) {
void m(int? a1, num a2) {
var b1 = a1 == a2; // OK
var b2 = a2 == a1; // OK
}
Loading