Skip to content

Commit da7a206

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
linter: Move tests for 3 lint rules:
* no_default_cases tests * overridden_fields * unnecessary_lambdas Change-Id: Ib84fca717b6ef8badae2ebe41bc42dfa34fb8407 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392160 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Auto-Submit: Samuel Rawlins <[email protected]>
1 parent b1e740f commit da7a206

7 files changed

+210
-232
lines changed

pkg/linter/test/rules/all.dart

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ import 'missing_code_block_language_in_doc_comment_test.dart'
143143
import 'missing_whitespace_between_adjacent_strings_test.dart'
144144
as missing_whitespace_between_adjacent_strings;
145145
import 'no_adjacent_strings_in_list_test.dart' as no_adjacent_strings_in_list;
146+
import 'no_default_cases_test.dart' as no_default_cases;
146147
import 'no_duplicate_case_values_test.dart' as no_duplicate_case_values;
147148
import 'no_leading_underscores_for_library_prefixes_test.dart'
148149
as no_leading_underscores_for_library_prefixes;
@@ -419,6 +420,7 @@ void main() {
419420
missing_code_block_language_in_doc_comment.main();
420421
missing_whitespace_between_adjacent_strings.main();
421422
no_adjacent_strings_in_list.main();
423+
no_default_cases.main();
422424
no_duplicate_case_values.main();
423425
no_leading_underscores_for_library_prefixes.main();
424426
no_leading_underscores_for_local_identifiers.main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(NoDefaultCasesTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class NoDefaultCasesTest extends LintRuleTest {
17+
@override
18+
String get lintRule => 'no_default_cases';
19+
20+
test_enumLikeType() async {
21+
await assertDiagnostics(r'''
22+
class C {
23+
final int i;
24+
const C._(this.i);
25+
26+
static const a = C._(1);
27+
static const b = C._(2);
28+
static const c = C._(3);
29+
}
30+
31+
void f(C c) {
32+
switch (c) {
33+
case C.a :
34+
print('a');
35+
break;
36+
default:
37+
print('default');
38+
}
39+
}
40+
''', [
41+
lint(210, 32),
42+
]);
43+
}
44+
45+
test_enumType() async {
46+
await assertDiagnostics(r'''
47+
void f(E e) {
48+
switch(e) {
49+
case E.a :
50+
print('a');
51+
break;
52+
default:
53+
print('default');
54+
}
55+
}
56+
57+
enum E {
58+
a, b, c;
59+
}
60+
''', [
61+
lint(78, 32),
62+
]);
63+
}
64+
65+
test_intType() async {
66+
await assertNoDiagnostics(r'''
67+
void f(int i) {
68+
switch (i) {
69+
case 1 :
70+
print('1');
71+
break;
72+
default:
73+
print('default');
74+
}
75+
}
76+
''');
77+
}
78+
}

pkg/linter/test/rules/overridden_fields_test.dart

+103
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,57 @@ class B extends A {
9797
''');
9898
}
9999

100+
test_extendingClass_multipleDeclarations() async {
101+
await assertDiagnostics(r'''
102+
class A {
103+
int y = 1;
104+
}
105+
class B extends A {
106+
final x = 1, y = 2;
107+
}
108+
''', [
109+
lint(60, 1),
110+
]);
111+
}
112+
113+
test_extendingClass_overridingAbstract() async {
114+
await assertNoDiagnostics(r'''
115+
abstract class A {
116+
abstract int x;
117+
}
118+
class B extends A {
119+
@override
120+
int x = 1;
121+
}
122+
''');
123+
}
124+
125+
test_extendingClass_staticField() async {
126+
await assertNoDiagnostics(r'''
127+
class A {
128+
static int x = 1;
129+
}
130+
class B extends A {
131+
static int x = 2;
132+
}
133+
''');
134+
}
135+
136+
test_extendsClass_indirectly() async {
137+
await assertDiagnostics(r'''
138+
class A {
139+
int x = 0;
140+
}
141+
class B extends A {}
142+
class C extends B {
143+
@override
144+
int x = 1;
145+
}
146+
''', [
147+
lint(84, 1),
148+
]);
149+
}
150+
100151
test_externalLibrary() async {
101152
newFile('$testPackageLibPath/a.dart', r'''
102153
class A {
@@ -141,6 +192,44 @@ class B extends A {
141192
''');
142193
}
143194

195+
test_implementingClass() async {
196+
await assertNoDiagnostics(r'''
197+
class A {
198+
int x = 1;
199+
}
200+
class B implements A {
201+
@override
202+
int x = 2;
203+
}
204+
''');
205+
}
206+
207+
test_mixingInMixin() async {
208+
await assertDiagnostics(r'''
209+
mixin M {
210+
int x = 1;
211+
}
212+
class A with M {
213+
@override
214+
int x = 2;
215+
}
216+
''', [
217+
lint(60, 1),
218+
]);
219+
}
220+
221+
test_mixingInMixin_overridingAbstract() async {
222+
await assertNoDiagnostics(r'''
223+
mixin M {
224+
abstract int x;
225+
}
226+
class A with M {
227+
@override
228+
int x = 2;
229+
}
230+
''');
231+
}
232+
144233
test_mixinInheritsFromNotObject() async {
145234
// See: https://github.com/dart-lang/linter/issues/2969
146235
// Preserves existing testing logic but has so many misuses of mixins that
@@ -230,6 +319,20 @@ class GC34 extends GC33 {
230319
]);
231320
}
232321

322+
test_mixinSuperclassConstraint() async {
323+
await assertDiagnostics(r'''
324+
class A {
325+
int x = 1;
326+
}
327+
mixin M on A {
328+
@override
329+
final x = 1;
330+
}
331+
''', [
332+
lint(60, 1),
333+
]);
334+
}
335+
233336
test_overridingAbstractField() async {
234337
await assertNoDiagnostics(r'''
235338
abstract class A {

pkg/linter/test/rules/unnecessary_lambdas_test.dart

+27
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ var x = [].map((x) => C(3));
128128
''');
129129
}
130130

131+
test_deeplyNestedVariable() async {
132+
await assertNoDiagnostics(r'''
133+
void f() {
134+
var x = (a, b) => foo(foo(b)).foo(a, b);
135+
}
136+
dynamic foo(a) => 7;
137+
''');
138+
}
139+
131140
test_emptyLambda() async {
132141
await assertNoDiagnostics(r'''
133142
var f = () {};
@@ -343,6 +352,24 @@ class C {
343352
};
344353
}
345354
}
355+
''');
356+
}
357+
358+
test_targetIsFinalParameter() async {
359+
await assertDiagnostics(r'''
360+
void f(List<String> list) {
361+
list.where((final e) => ((a) => e.contains(a))(e));
362+
}
363+
''', [
364+
lint(55, 20),
365+
]);
366+
}
367+
368+
test_targetIsVarParameter() async {
369+
await assertNoDiagnostics(r'''
370+
void main(List<String> list) {
371+
list.where((e) => ((a) => e.contains(a))(e));
372+
}
346373
''');
347374
}
348375
}

pkg/linter/test_data/rules/no_default_cases.dart

-47
This file was deleted.

0 commit comments

Comments
 (0)