Skip to content

Commit b77b34c

Browse files
srawlinsCommit Queue
authored andcommitted
lint_rules: migrate tests for three lint rules:
* prefer_final_fields * prefer_is_not_empty * prefer_is_not_operator Change-Id: Id20282fcad91f7d2f8d8891eb7062fd78f1da2a4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390000 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Auto-Submit: Samuel Rawlins <[email protected]>
1 parent ceba7a5 commit b77b34c

File tree

7 files changed

+378
-226
lines changed

7 files changed

+378
-226
lines changed

pkg/linter/test/rules/all.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ import 'prefer_int_literals_test.dart' as prefer_int_literals;
210210
import 'prefer_interpolation_to_compose_strings_test.dart'
211211
as prefer_interpolation_to_compose_strings;
212212
import 'prefer_is_empty_test.dart' as prefer_is_empty;
213+
import 'prefer_is_not_empty_test.dart' as prefer_is_not_empty;
214+
import 'prefer_is_not_operator_test.dart' as prefer_is_not_operator;
213215
import 'prefer_iterable_whereType_test.dart' as prefer_iterable_whereType;
214216
import 'prefer_mixin_test.dart' as prefer_mixin;
215217
import 'prefer_null_aware_method_calls_test.dart'
@@ -464,6 +466,8 @@ void main() {
464466
prefer_int_literals.main();
465467
prefer_interpolation_to_compose_strings.main();
466468
prefer_is_empty.main();
469+
prefer_is_not_empty.main();
470+
prefer_is_not_operator.main();
467471
prefer_iterable_whereType.main();
468472
prefer_mixin.main();
469473
prefer_null_aware_method_calls.main();

pkg/linter/test/rules/prefer_final_fields_test.dart

Lines changed: 256 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/error/analyzer_error_code.dart';
56
import 'package:test_reflective_loader/test_reflective_loader.dart';
67

78
import '../rule_test_support.dart';
89

910
main() {
1011
defineReflectiveSuite(() {
11-
defineReflectiveTests(PreferFinalFieldsTest);
1212
defineReflectiveTests(PreferFinalFieldsExtensionTypesTest);
13+
defineReflectiveTests(PreferFinalFieldsTest);
1314
});
1415
}
1516

@@ -58,9 +59,36 @@ extension type E(Object o) {
5859

5960
@reflectiveTest
6061
class PreferFinalFieldsTest extends LintRuleTest {
62+
@override
63+
List<AnalyzerErrorCode> get ignoredErrorCodes => [
64+
WarningCode.UNUSED_FIELD,
65+
WarningCode.UNUSED_LOCAL_VARIABLE,
66+
];
67+
6168
@override
6269
String get lintRule => LintNames.prefer_final_fields;
6370

71+
test_assignedInConstructorInitializer() async {
72+
await assertDiagnostics(r'''
73+
class C {
74+
int _x;
75+
C() : _x = 7;
76+
}
77+
''', [
78+
lint(16, 2),
79+
]);
80+
}
81+
82+
test_assignedInConstructorInitializer_butNotAll() async {
83+
await assertNoDiagnostics(r'''
84+
class C {
85+
var _x;
86+
C(this._x);
87+
C.named();
88+
}
89+
''');
90+
}
91+
6492
test_assignedInPart() async {
6593
newFile('$testPackageLibPath/part.dart', r'''
6694
part of 'test.dart';
@@ -77,6 +105,30 @@ class C {
77105
''');
78106
}
79107

108+
test_assignedInTopLevelFunction() async {
109+
await assertNoDiagnostics(r'''
110+
class C {
111+
int _x = 0;
112+
}
113+
114+
void f() {
115+
var c = C();
116+
c._x = 42;
117+
}
118+
''');
119+
}
120+
121+
test_assignment_plusEquals() async {
122+
await assertNoDiagnostics(r'''
123+
class C {
124+
var _x = 1;
125+
void f() {
126+
_x += 2;
127+
}
128+
}
129+
''');
130+
}
131+
80132
test_declaredInPart() async {
81133
newFile('$testPackageLibPath/part.dart', r'''
82134
part of 'test.dart';
@@ -106,6 +158,36 @@ enum A {
106158
]);
107159
}
108160

161+
test_final_multiple() async {
162+
await assertNoDiagnostics(r'''
163+
class C {
164+
final _x = 1, _y = 2;
165+
}
166+
''');
167+
}
168+
169+
test_final_public_multiple() async {
170+
await assertNoDiagnostics(r'''
171+
class C {
172+
final x = 1, y = 2;
173+
}
174+
''');
175+
}
176+
177+
test_indexAssignment() async {
178+
await assertDiagnostics(r'''
179+
class C {
180+
var _x = [];
181+
182+
void f() {
183+
_x[0] = 3;
184+
}
185+
}
186+
''', [
187+
lint(16, 7),
188+
]);
189+
}
190+
109191
test_overrideField_extends() async {
110192
await assertNoDiagnostics(r'''
111193
class A {
@@ -190,6 +272,179 @@ class B implements A {
190272
print(_a);
191273
}
192274
}
275+
''');
276+
}
277+
278+
test_postfixExpression_decrement() async {
279+
await assertNoDiagnostics(r'''
280+
class C {
281+
int _x = 1;
282+
void f() {
283+
_x--;
284+
}
285+
}
286+
''');
287+
}
288+
289+
test_postfixExpression_increment() async {
290+
await assertNoDiagnostics(r'''
291+
class C {
292+
int _x = 1;
293+
void f() {
294+
_x++;
295+
}
296+
}
297+
''');
298+
}
299+
300+
test_prefixExpression_decrement() async {
301+
await assertNoDiagnostics(r'''
302+
class C {
303+
int _x = 1;
304+
void f() {
305+
--_x;
306+
}
307+
}
308+
''');
309+
}
310+
311+
test_prefixExpression_increment() async {
312+
await assertNoDiagnostics(r'''
313+
class C {
314+
int _x = 1;
315+
void f() {
316+
++_x;
317+
}
318+
}
319+
''');
320+
}
321+
322+
test_prefixExpression_not() async {
323+
await assertDiagnostics(r'''
324+
class C {
325+
bool _x = false;
326+
void f() {
327+
!_x;
328+
}
329+
}
330+
''', [
331+
lint(17, 10),
332+
]);
333+
}
334+
335+
test_prefixExpression_tilde() async {
336+
await assertDiagnostics(r'''
337+
class C {
338+
int _x = 0xffff;
339+
void f() {
340+
~_x;
341+
}
342+
}
343+
''', [
344+
lint(16, 11),
345+
]);
346+
}
347+
348+
test_propertyAccess() async {
349+
await assertDiagnostics(r'''
350+
class C {
351+
int _x = 1;
352+
void f() {
353+
_x.isEven;
354+
}
355+
}
356+
''', [
357+
lint(16, 6),
358+
]);
359+
}
360+
361+
test_readInInstanceMethod() async {
362+
await assertDiagnostics(r'''
363+
class C {
364+
int _x = 0;
365+
366+
void f() {
367+
var a = _x;
368+
}
369+
}
370+
''', [
371+
lint(16, 6),
372+
]);
373+
}
374+
375+
test_reassigned() async {
376+
await assertNoDiagnostics(r'''
377+
class C {
378+
var _x = 1;
379+
void f() {
380+
_x = 2;
381+
}
382+
}
383+
''');
384+
}
385+
386+
test_referencedInFieldFormalParameters() async {
387+
await assertDiagnostics(r'''
388+
class C {
389+
int _x;
390+
C(this._x);
391+
C.named(this._x);
392+
}
393+
''', [
394+
lint(16, 2),
395+
]);
396+
}
397+
398+
test_subclassOnGenericClass() async {
399+
await assertNoDiagnostics(r'''
400+
abstract class C<T> {
401+
int _x = 0;
402+
}
403+
404+
class D extends C<int> {
405+
void f() {
406+
_x = 1;
407+
}
408+
}
409+
''');
410+
}
411+
412+
test_unused() async {
413+
await assertDiagnostics(r'''
414+
class C {
415+
var _x = 1;
416+
}
417+
''', [
418+
lint(16, 6),
419+
]);
420+
}
421+
422+
test_unused_multiple() async {
423+
await assertDiagnostics(r'''
424+
class C {
425+
var _x = 1, _y = 2;
426+
void f() {
427+
_x = 2;
428+
}
429+
}
430+
''', [
431+
lint(24, 6),
432+
]);
433+
}
434+
435+
test_unused_public() async {
436+
await assertNoDiagnostics(r'''
437+
class C {
438+
var x = 1;
439+
}
440+
''');
441+
}
442+
443+
test_unused_uninitialized() async {
444+
await assertNoDiagnostics(r'''
445+
class C {
446+
int? _x;
447+
}
193448
''');
194449
}
195450
}

0 commit comments

Comments
 (0)