Skip to content

Commit a96c690

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
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>
1 parent ddf4a68 commit a96c690

5 files changed

+347
-175
lines changed

pkg/linter/test/rules/all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// ignore_for_file: library_prefixes
66

77
import 'always_declare_return_types_test.dart' as always_declare_return_types;
8+
import 'always_put_control_body_on_new_line_test.dart'
9+
as always_put_control_body_on_new_line;
810
import 'always_put_required_named_parameters_first_test.dart'
911
as always_put_required_named_parameters_first;
1012
import 'always_specify_types_test.dart' as always_specify_types;
@@ -313,6 +315,7 @@ import 'void_checks_test.dart' as void_checks;
313315

314316
void main() {
315317
always_declare_return_types.main();
318+
always_put_control_body_on_new_line.main();
316319
always_put_required_named_parameters_first.main();
317320
always_specify_types.main();
318321
always_use_package_imports.main();
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/analyzer_error_code.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../rule_test_support.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(AlwaysPutControlBodyOnNewLineTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class AlwaysPutControlBodyOnNewLineTest extends LintRuleTest {
18+
@override
19+
List<AnalyzerErrorCode> get ignoredErrorCodes => [
20+
WarningCode.DEAD_CODE,
21+
WarningCode.UNUSED_LOCAL_VARIABLE,
22+
];
23+
24+
@override
25+
String get lintRule => 'always_put_control_body_on_new_line';
26+
27+
test_doWhile_bodyAdjacent() async {
28+
await assertDiagnostics(r'''
29+
void f() {
30+
do print('');
31+
while (true);
32+
}
33+
''', [
34+
lint(16, 5),
35+
]);
36+
}
37+
38+
test_doWhile_bodyOnNewline() async {
39+
await assertNoDiagnostics(r'''
40+
void f() {
41+
do
42+
print('');
43+
while (true);
44+
}
45+
''');
46+
}
47+
48+
test_forEachLoop_blockBody_empty() async {
49+
await assertNoDiagnostics(r'''
50+
void f() {
51+
for (var i in []) {}
52+
}
53+
''');
54+
}
55+
56+
test_forEachLoop_bodyAdjacent() async {
57+
await assertDiagnostics(r'''
58+
void f() {
59+
for (var i in []) return;
60+
}
61+
''', [
62+
lint(31, 6),
63+
]);
64+
}
65+
66+
test_forEachLoop_bodyOnNewline() async {
67+
await assertNoDiagnostics(r'''
68+
void f() {
69+
for (var i in [])
70+
return;
71+
}
72+
''');
73+
}
74+
75+
test_forLoop_blockBody_empty() async {
76+
await assertNoDiagnostics(r'''
77+
void f() {
78+
for (;;) {}
79+
}
80+
''');
81+
}
82+
83+
test_forLoop_bodyAdjacent() async {
84+
await assertDiagnostics(r'''
85+
void f() {
86+
for (;;) return;
87+
}
88+
''', [
89+
lint(22, 6),
90+
]);
91+
}
92+
93+
test_forLoop_bodyOnNewline() async {
94+
await assertNoDiagnostics(r'''
95+
void f() {
96+
for (;;)
97+
return;
98+
}
99+
''');
100+
}
101+
102+
test_ifStatement_blockElse_empty() async {
103+
await assertNoDiagnostics(r'''
104+
void f() {
105+
if (false) {
106+
return;
107+
}
108+
else {}
109+
}
110+
''');
111+
}
112+
113+
test_ifStatement_blockElseIfThen_empty() async {
114+
await assertNoDiagnostics(r'''
115+
void f() {
116+
if (false)
117+
return;
118+
else if (false) {}
119+
else
120+
return;
121+
}
122+
''');
123+
}
124+
125+
test_ifStatement_blockThen_empty() async {
126+
await assertNoDiagnostics(r'''
127+
void f() {
128+
if (false) {}
129+
}
130+
''');
131+
}
132+
133+
test_ifStatement_elseAdjacent() async {
134+
await assertDiagnostics(r'''
135+
void f() {
136+
if (false)
137+
return;
138+
else return;
139+
}
140+
''', [
141+
lint(43, 6),
142+
]);
143+
}
144+
145+
test_ifStatement_elseOnNewline() async {
146+
await assertNoDiagnostics(r'''
147+
void f() {
148+
if (false) {
149+
return;
150+
}
151+
else
152+
return;
153+
}
154+
''');
155+
}
156+
157+
test_ifStatement_thenAdjacent() async {
158+
await assertDiagnostics(r'''
159+
void f() {
160+
if (false) return;
161+
}
162+
''', [
163+
lint(24, 6),
164+
]);
165+
}
166+
167+
test_ifStatement_thenAdjacent_multiline() async {
168+
await assertDiagnostics(r'''
169+
void f() {
170+
if (false) print(
171+
'text'
172+
'text');
173+
}
174+
''', [
175+
lint(24, 5),
176+
]);
177+
}
178+
179+
test_ifStatement_thenIsBlock_adjacentStatement() async {
180+
await assertDiagnostics(r'''
181+
void f() {
182+
if (false) { print('');
183+
}
184+
}
185+
''', [
186+
lint(24, 1),
187+
]);
188+
}
189+
190+
test_ifStatement_thenIsEmpty() async {
191+
await assertNoDiagnostics(r'''
192+
void f() {
193+
if (false) {}
194+
}
195+
''');
196+
}
197+
198+
test_ifStatement_thenOnNewline() async {
199+
await assertNoDiagnostics(r'''
200+
void f() {
201+
if (false)
202+
return;
203+
}
204+
''');
205+
}
206+
207+
test_whileLoop_blockBody_empty() async {
208+
await assertNoDiagnostics(r'''
209+
void f() {
210+
while (true) {}
211+
}
212+
''');
213+
}
214+
215+
test_whileLoop_bodyAdjacent() async {
216+
await assertDiagnostics(r'''
217+
void f() {
218+
while (true) return;
219+
}
220+
''', [
221+
lint(26, 6),
222+
]);
223+
}
224+
225+
test_whileLoop_bodyOnNewline() async {
226+
await assertNoDiagnostics(r'''
227+
void f() {
228+
while (true)
229+
return;
230+
}
231+
''');
232+
}
233+
}

pkg/linter/test/rules/avoid_redundant_argument_values_test.dart

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ class AvoidRedundantArgumentValuesTest extends LintRuleTest {
3636
@override
3737
String get lintRule => 'avoid_redundant_argument_values';
3838

39+
test_constructor_redundant() async {
40+
await assertDiagnostics(r'''
41+
void f() {
42+
A(p: true);
43+
}
44+
class A {
45+
A({bool p = true});
46+
}
47+
''', [
48+
lint(18, 4),
49+
]);
50+
}
51+
52+
test_constructor_tearoff_redundant() async {
53+
await assertDiagnostics(r'''
54+
void f() {
55+
var aNew = A.new;
56+
aNew(p: true);
57+
}
58+
class A {
59+
A({bool p = true});
60+
}
61+
''', [
62+
lint(41, 4),
63+
]);
64+
}
65+
3966
/// https://github.com/dart-lang/linter/issues/3617
4067
test_enumDeclaration() async {
4168
await assertDiagnostics(r'''
@@ -66,6 +93,90 @@ void g() {
6693
''');
6794
}
6895

96+
test_function_optionalPositional_followedByPositional() async {
97+
await assertNoDiagnostics(r'''
98+
void f() {
99+
g(0, 1);
100+
}
101+
void g([int a = 0, int? b]) {}
102+
''');
103+
}
104+
105+
test_function_optionalPositional_subsequent_different() async {
106+
await assertNoDiagnostics(r'''
107+
void f() {
108+
g(0, 2);
109+
}
110+
void g([int? a, int? b = 1]) {}
111+
''');
112+
}
113+
114+
test_function_optionalPositional_subsequent_redundant() async {
115+
await assertDiagnostics(r'''
116+
void f() {
117+
g(0, 1);
118+
}
119+
void g([int? a, int? b = 1]) {}
120+
''', [
121+
lint(18, 1),
122+
]);
123+
}
124+
125+
test_localFunction_optionalNamed_different() async {
126+
await assertNoDiagnostics(r'''
127+
void f() {
128+
void g({bool p = true}) {}
129+
g(p: false);
130+
}
131+
''');
132+
}
133+
134+
test_localFunction_optionalNamed_redundant() async {
135+
await assertDiagnostics(r'''
136+
void f() {
137+
void g({bool p = true}) {}
138+
g(p: true);
139+
}
140+
''', [
141+
lint(47, 4),
142+
]);
143+
}
144+
145+
test_method_noDefault() async {
146+
await assertNoDiagnostics(r'''
147+
void f(A a) {
148+
a.g(p: false);
149+
}
150+
class A {
151+
void g({bool? p}) {}
152+
}
153+
''');
154+
}
155+
156+
test_method_optionalNamed_variable() async {
157+
await assertNoDiagnostics(r'''
158+
void f(A a, bool v) {
159+
a.g(p: v);
160+
}
161+
class A {
162+
void g({bool p = true}) {}
163+
}
164+
''');
165+
}
166+
167+
test_method_redundant() async {
168+
await assertDiagnostics(r'''
169+
void f(A a) {
170+
a.g(p: true);
171+
}
172+
class A {
173+
void g({bool p = true}) {}
174+
}
175+
''', [
176+
lint(23, 4),
177+
]);
178+
}
179+
69180
test_redirectingFactoryConstructor() async {
70181
await assertNoDiagnostics(r'''
71182
class A {

0 commit comments

Comments
 (0)