Skip to content

Commit

Permalink
more test diagnostic cleanup (dart-lang/linter#2984)
Browse files Browse the repository at this point in the history
* cleanup asserts

* test diagnostic cleanup
  • Loading branch information
pq authored Sep 30, 2021
1 parent c319a40 commit 7d7012a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 28 deletions.
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();
}
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),
]);
}
}
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),
]);
}
}
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ f(o) {
f('long line\t' 'is long'); // OK

f(RegExp('(\n)+' '(\n)+' '(\n)+')); // OK
new Unresolved('aaa' 'bbb'); // OK
matches('(\n)+' '(\n)+' '(\n)+'); // OK

f('Hello' // OK
Expand Down
4 changes: 2 additions & 2 deletions test_data/rules/prefer_collection_literals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void main() {

printUnresolved(LinkedHashSet<int>()); // OK

Set<int> ss7 = LinkedHashSet.from([1, 2, 3]); // LINT
LinkedHashSet<int> ss8 = LinkedHashSet.from([1, 2, 3]); // OK
Set<int> ss8 = LinkedHashSet.from([1, 2, 3]); // LINT
LinkedHashSet<int> ss9 = LinkedHashSet.from([1, 2, 3]); // OK

Iterable iter = Iterable.empty(); // OK
var sss = Set.from(iter); // OK
Expand Down
44 changes: 22 additions & 22 deletions test_data/rules/prefer_initializing_formals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ num sin(num theta) {
}

class SimpleBadCase {
num x, y;
num x = 0, y = 0;
SimpleBadCase(num x, num y) {
this.x = x; // LINT
this.y = y; // LINT
Expand All @@ -66,15 +66,15 @@ class SimpleGoodCase {
}

class SimpleBadCaseWithOnlyOneLint {
num x, y;
num x, y = 0;
SimpleBadCaseWithOnlyOneLint(this.x, num y) {
this.y = y; // LINT
}
}

/// https://github.com/dart-lang/linter/issues/2605
class RenamedFieldsForReadability {
num x, y;
num x = 0, y = 0;
RenamedFieldsForReadability(num a, num b) {
x = a; // OK
y = b; // OK
Expand All @@ -83,16 +83,16 @@ class RenamedFieldsForReadability {

/// https://github.com/dart-lang/linter/issues/2605
class RenamedFieldsForReadability2 {
num x;
num y;
num x = 0;
num y = 0;
RenamedFieldsForReadability2(num a, num b) {
x = a; // OK
y = b; // OK
}
}

class NoFieldsJustSetters {
String name;
String name = '';
NoFieldsJustSetters(num x, num y) {
this.x = x; // OK
this.y = y; // OK
Expand All @@ -107,12 +107,12 @@ class NoFieldsJustSetters {
}

class NoFieldsJustSettersWithoutThisAndWithOneGetter {
String name;
String name = '';
NoFieldsJustSettersWithoutThisAndWithOneGetter(num a, num b) {
x = a; // OK
y = b; // OK
}
get x {
num get x {
return 0;
}

Expand All @@ -126,7 +126,7 @@ class NoFieldsJustSettersWithoutThisAndWithOneGetter {
}

class SuperCallWithConstructorParameters extends SimpleGoodCase {
num r, theta;
num r = 0, theta = 0;
SuperCallWithConstructorParameters(num r, num theta)
: super(r * cos(theta), r * sin(theta)) {
this.r = r; // LINT
Expand Down Expand Up @@ -154,7 +154,7 @@ class BadCaseWithNamedConstructorAndSuperCall extends SimpleGoodCase {

class GoodCaseWithPrivateFields {
// ignore: unused_field
num _x, _y;
num? _x, _y;
GoodCaseWithPrivateFields(num x, num y) {
this._x = x; // OK // This should be lint for other rule
this._y = y; // OK // This should be lint for other rule
Expand All @@ -163,7 +163,7 @@ class GoodCaseWithPrivateFields {

class GoodCaseWithPrivateFieldsWithoutThis {
// ignore: unused_field
num _x, _y;
num _x = 0, _y = 0;
GoodCaseWithPrivateFieldsWithoutThis(num x, num y) {
_x = x; // OK // This should be lint for other rule
_y = y; // OK // This should be lint for other rule
Expand All @@ -187,22 +187,22 @@ class GoodCaseWithPrivateFieldsInInitializersWithoutThis {
}

class BadCaseWithTwoFieldsOneArgument {
num x, y;
num x = 0, y = 0;
BadCaseWithTwoFieldsOneArgument(num x) {
this.x = x; // LINT
this.y = x; // OK
}
}

class GoodCaseWithTwoFieldsOneArgument {
num x, y;
num x = 0, y = 0;
GoodCaseWithTwoFieldsOneArgument(this.x) {
y = this.x; // OK
}
}

class GoodCaseWithTwoFieldsOneArgumentWithoutThis {
num x, y;
num x = 0, y = 0;
GoodCaseWithTwoFieldsOneArgumentWithoutThis(this.x) {
y = x; // OK
}
Expand Down Expand Up @@ -231,31 +231,31 @@ class GoodCaseWithOneParameterToTwoFieldsBecauseTheyHaveDifferentNames {
}

class BadCaseWithNamedArgs {
num x, y;
BadCaseWithNamedArgs({num x, num y = 1}) {
num? x, y;
BadCaseWithNamedArgs({num? x, num y = 1}) {
this.x = x; // LINT
this.y = y; // LINT
}
}

class GoodCaseWithDifferentNamedArgs {
num x, y;
GoodCaseWithDifferentNamedArgs({num a, num b = 1}) {
num? x, y;
GoodCaseWithDifferentNamedArgs({num? a, num b = 1}) {
this.x = a; // OK
this.y = b; // OK
}
}

class BadCaseWithNamedArgsInitializer {
num x, y;
BadCaseWithNamedArgsInitializer({num x, num y = 1})
num? x, y;
BadCaseWithNamedArgsInitializer({num? x, num y = 1})
: this.x = x, // LINT
this.y = y; // LINT
}

class GoodCaseWithDifferentNamedArgsInitializer {
num x, y;
GoodCaseWithDifferentNamedArgsInitializer({num a, num b = 1})
num? x, y;
GoodCaseWithDifferentNamedArgsInitializer({num? a, num b = 1})
: this.x = a, // OK
this.y = b; // OK
}

0 comments on commit 7d7012a

Please sign in to comment.