mirror of
https://github.com/dart-lang/sdk
synced 2024-09-30 06:31:55 +00:00
linter rules: Migrate tests for two rules:
* always_put_control_body_on_new_line * avoid_redundant_argument_values Change-Id: I1870cbe7ffed2100e4a94d09ca4e53cd67ca189d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/386961 Reviewed-by: Phil Quitslund <pquitslund@google.com> Auto-Submit: Samuel Rawlins <srawlins@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
parent
ddf4a687a2
commit
a96c690c4e
|
@ -5,6 +5,8 @@
|
|||
// ignore_for_file: library_prefixes
|
||||
|
||||
import 'always_declare_return_types_test.dart' as always_declare_return_types;
|
||||
import 'always_put_control_body_on_new_line_test.dart'
|
||||
as always_put_control_body_on_new_line;
|
||||
import 'always_put_required_named_parameters_first_test.dart'
|
||||
as always_put_required_named_parameters_first;
|
||||
import 'always_specify_types_test.dart' as always_specify_types;
|
||||
|
@ -313,6 +315,7 @@ import 'void_checks_test.dart' as void_checks;
|
|||
|
||||
void main() {
|
||||
always_declare_return_types.main();
|
||||
always_put_control_body_on_new_line.main();
|
||||
always_put_required_named_parameters_first.main();
|
||||
always_specify_types.main();
|
||||
always_use_package_imports.main();
|
||||
|
|
|
@ -0,0 +1,233 @@
|
|||
// Copyright (c) 2024, 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:analyzer/src/error/analyzer_error_code.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import '../rule_test_support.dart';
|
||||
|
||||
main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(AlwaysPutControlBodyOnNewLineTest);
|
||||
});
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class AlwaysPutControlBodyOnNewLineTest extends LintRuleTest {
|
||||
@override
|
||||
List<AnalyzerErrorCode> get ignoredErrorCodes => [
|
||||
WarningCode.DEAD_CODE,
|
||||
WarningCode.UNUSED_LOCAL_VARIABLE,
|
||||
];
|
||||
|
||||
@override
|
||||
String get lintRule => 'always_put_control_body_on_new_line';
|
||||
|
||||
test_doWhile_bodyAdjacent() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
do print('');
|
||||
while (true);
|
||||
}
|
||||
''', [
|
||||
lint(16, 5),
|
||||
]);
|
||||
}
|
||||
|
||||
test_doWhile_bodyOnNewline() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
do
|
||||
print('');
|
||||
while (true);
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_forEachLoop_blockBody_empty() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
for (var i in []) {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_forEachLoop_bodyAdjacent() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
for (var i in []) return;
|
||||
}
|
||||
''', [
|
||||
lint(31, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_forEachLoop_bodyOnNewline() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
for (var i in [])
|
||||
return;
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_forLoop_blockBody_empty() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
for (;;) {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_forLoop_bodyAdjacent() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
for (;;) return;
|
||||
}
|
||||
''', [
|
||||
lint(22, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_forLoop_bodyOnNewline() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
for (;;)
|
||||
return;
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_ifStatement_blockElse_empty() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
if (false) {
|
||||
return;
|
||||
}
|
||||
else {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_ifStatement_blockElseIfThen_empty() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
if (false)
|
||||
return;
|
||||
else if (false) {}
|
||||
else
|
||||
return;
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_ifStatement_blockThen_empty() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
if (false) {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_ifStatement_elseAdjacent() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
if (false)
|
||||
return;
|
||||
else return;
|
||||
}
|
||||
''', [
|
||||
lint(43, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_ifStatement_elseOnNewline() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
if (false) {
|
||||
return;
|
||||
}
|
||||
else
|
||||
return;
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_ifStatement_thenAdjacent() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
if (false) return;
|
||||
}
|
||||
''', [
|
||||
lint(24, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_ifStatement_thenAdjacent_multiline() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
if (false) print(
|
||||
'text'
|
||||
'text');
|
||||
}
|
||||
''', [
|
||||
lint(24, 5),
|
||||
]);
|
||||
}
|
||||
|
||||
test_ifStatement_thenIsBlock_adjacentStatement() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
if (false) { print('');
|
||||
}
|
||||
}
|
||||
''', [
|
||||
lint(24, 1),
|
||||
]);
|
||||
}
|
||||
|
||||
test_ifStatement_thenIsEmpty() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
if (false) {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_ifStatement_thenOnNewline() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
if (false)
|
||||
return;
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_whileLoop_blockBody_empty() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
while (true) {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_whileLoop_bodyAdjacent() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
while (true) return;
|
||||
}
|
||||
''', [
|
||||
lint(26, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
test_whileLoop_bodyOnNewline() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
while (true)
|
||||
return;
|
||||
}
|
||||
''');
|
||||
}
|
||||
}
|
|
@ -36,6 +36,33 @@ class AvoidRedundantArgumentValuesTest extends LintRuleTest {
|
|||
@override
|
||||
String get lintRule => 'avoid_redundant_argument_values';
|
||||
|
||||
test_constructor_redundant() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
A(p: true);
|
||||
}
|
||||
class A {
|
||||
A({bool p = true});
|
||||
}
|
||||
''', [
|
||||
lint(18, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
test_constructor_tearoff_redundant() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
var aNew = A.new;
|
||||
aNew(p: true);
|
||||
}
|
||||
class A {
|
||||
A({bool p = true});
|
||||
}
|
||||
''', [
|
||||
lint(41, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
/// https://github.com/dart-lang/linter/issues/3617
|
||||
test_enumDeclaration() async {
|
||||
await assertDiagnostics(r'''
|
||||
|
@ -66,6 +93,90 @@ void g() {
|
|||
''');
|
||||
}
|
||||
|
||||
test_function_optionalPositional_followedByPositional() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
g(0, 1);
|
||||
}
|
||||
void g([int a = 0, int? b]) {}
|
||||
''');
|
||||
}
|
||||
|
||||
test_function_optionalPositional_subsequent_different() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
g(0, 2);
|
||||
}
|
||||
void g([int? a, int? b = 1]) {}
|
||||
''');
|
||||
}
|
||||
|
||||
test_function_optionalPositional_subsequent_redundant() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
g(0, 1);
|
||||
}
|
||||
void g([int? a, int? b = 1]) {}
|
||||
''', [
|
||||
lint(18, 1),
|
||||
]);
|
||||
}
|
||||
|
||||
test_localFunction_optionalNamed_different() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f() {
|
||||
void g({bool p = true}) {}
|
||||
g(p: false);
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_localFunction_optionalNamed_redundant() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f() {
|
||||
void g({bool p = true}) {}
|
||||
g(p: true);
|
||||
}
|
||||
''', [
|
||||
lint(47, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
test_method_noDefault() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f(A a) {
|
||||
a.g(p: false);
|
||||
}
|
||||
class A {
|
||||
void g({bool? p}) {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_method_optionalNamed_variable() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
void f(A a, bool v) {
|
||||
a.g(p: v);
|
||||
}
|
||||
class A {
|
||||
void g({bool p = true}) {}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
test_method_redundant() async {
|
||||
await assertDiagnostics(r'''
|
||||
void f(A a) {
|
||||
a.g(p: true);
|
||||
}
|
||||
class A {
|
||||
void g({bool p = true}) {}
|
||||
}
|
||||
''', [
|
||||
lint(23, 4),
|
||||
]);
|
||||
}
|
||||
|
||||
test_redirectingFactoryConstructor() async {
|
||||
await assertNoDiagnostics(r'''
|
||||
class A {
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
// Copyright (c) 2017, 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.
|
||||
|
||||
testIfElse() {
|
||||
if (false) return; // LINT
|
||||
|
||||
if (false) {} // OK
|
||||
|
||||
if (false)
|
||||
return; // OK
|
||||
|
||||
if (false) { // OK
|
||||
}
|
||||
|
||||
if (false)
|
||||
return; // OK
|
||||
else return; // LINT
|
||||
|
||||
if (false) {
|
||||
}
|
||||
else if (false) { // OK
|
||||
}
|
||||
else {
|
||||
}
|
||||
|
||||
if (false) { // OK
|
||||
} else if (false) { // OK
|
||||
} else { // OK
|
||||
}
|
||||
|
||||
if (false) { } // OK
|
||||
else return; // LINT
|
||||
|
||||
if (false)
|
||||
return; // OK
|
||||
else
|
||||
return; // OK
|
||||
|
||||
if (false){ }// OK
|
||||
else {} // OK
|
||||
|
||||
if (false) print( // LINT
|
||||
'First argument'
|
||||
'Second argument');
|
||||
|
||||
if (false) { print('should be on next line'); // LINT
|
||||
}
|
||||
}
|
||||
|
||||
testWhile() {
|
||||
while (true) return; // LINT
|
||||
|
||||
while (true) {} // OK
|
||||
|
||||
while (true)
|
||||
return; // OK
|
||||
}
|
||||
|
||||
testForEach(List l) {
|
||||
for (var i in l) return; // LINT
|
||||
|
||||
for (var i in l) {} // OK
|
||||
|
||||
for (var i in l)
|
||||
return; // OK
|
||||
}
|
||||
|
||||
testFor() {
|
||||
for (;;) return; // LINT
|
||||
|
||||
for (;;) {} // OK
|
||||
|
||||
for (;;)
|
||||
return; // OK
|
||||
}
|
||||
|
||||
testDo() {
|
||||
do print(''); // LINT
|
||||
while (true);
|
||||
|
||||
do
|
||||
print(''); // OK
|
||||
while (true);
|
||||
}
|
|
@ -1,90 +0,0 @@
|
|||
// Copyright (c) 2019, 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:meta/meta.dart';
|
||||
|
||||
class A {
|
||||
A({bool valWithDefault = true, bool? val});
|
||||
void f({bool valWithDefault = true, bool? val}) {}
|
||||
void g({int valWithDefault = 1, bool? val}) {}
|
||||
void h({String valWithDefault = 'default', bool? val}) {}
|
||||
}
|
||||
|
||||
enum TestEnum {
|
||||
a(test: false); // LINT
|
||||
|
||||
const TestEnum({this.test = false});
|
||||
|
||||
final bool test;
|
||||
}
|
||||
|
||||
f(void g([bool? b])) {
|
||||
// Function Expression Invocation.
|
||||
g(null); // TODO: https://github.com/dart-lang/linter/issues/4368
|
||||
}
|
||||
|
||||
bool q() => true;
|
||||
|
||||
void ff({bool valWithDefault = true, bool? val}) {}
|
||||
void g({@required bool valWithDefault = true, bool? val}) {}
|
||||
|
||||
void gg(int x, [int y = 0]) {}
|
||||
void ggg([int a = 1, int b = 2]) {}
|
||||
void gggg([int a = 0, int? b]) {}
|
||||
|
||||
void h([int? a, int? b = 1]) {}
|
||||
|
||||
void main() {
|
||||
|
||||
// Tear-off
|
||||
var aCons = A.new;
|
||||
aCons(valWithDefault: true); //LINT
|
||||
|
||||
A(valWithDefault: true); //LINT
|
||||
A().f(valWithDefault: true); //LINT
|
||||
A().g(valWithDefault: 1); //LINT
|
||||
A().h(valWithDefault: 'default'); //LINT
|
||||
|
||||
A().f(val: false); //OK
|
||||
A().f(val: false, valWithDefault: false); //OK
|
||||
|
||||
final v = true;
|
||||
A().f(val: false, valWithDefault: v); //OK
|
||||
A().f(val: false, valWithDefault: q()); //OK
|
||||
|
||||
ff(valWithDefault: true); //LINT
|
||||
ff(val: false); //OK
|
||||
ff(val: false, valWithDefault: false); //OK
|
||||
|
||||
ff(val: false, valWithDefault: v); //OK
|
||||
ff(val: false, valWithDefault: q()); //OK
|
||||
|
||||
void fff({bool valWithDefault = true, bool? val}) {}
|
||||
|
||||
fff(valWithDefault: true); //LINT
|
||||
fff(val: false); //OK
|
||||
fff(val: false, valWithDefault: false); //OK
|
||||
|
||||
fff(val: false, valWithDefault: v); //OK
|
||||
fff(val: false, valWithDefault: q()); //OK
|
||||
|
||||
// Required.
|
||||
g(valWithDefault: true); //OK
|
||||
|
||||
// Optional positional.
|
||||
gg(1, 0); //LINT
|
||||
gg(1, 1); //OK
|
||||
gg(1); //OK
|
||||
|
||||
ggg(
|
||||
1, // OK the - first argument is required so that we can provide the second argument.
|
||||
3);
|
||||
ggg(1,
|
||||
2); // LINT
|
||||
|
||||
gggg(0, 1); //OK
|
||||
|
||||
h(0,
|
||||
1); //LINT
|
||||
}
|
Loading…
Reference in a new issue