Skip to content

Commit

Permalink
linter: Move tests for 3 lint rules:
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
srawlins authored and Commit Queue committed Oct 28, 2024
1 parent b1e740f commit da7a206
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 232 deletions.
2 changes: 2 additions & 0 deletions pkg/linter/test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ import 'missing_code_block_language_in_doc_comment_test.dart'
import 'missing_whitespace_between_adjacent_strings_test.dart'
as missing_whitespace_between_adjacent_strings;
import 'no_adjacent_strings_in_list_test.dart' as no_adjacent_strings_in_list;
import 'no_default_cases_test.dart' as no_default_cases;
import 'no_duplicate_case_values_test.dart' as no_duplicate_case_values;
import 'no_leading_underscores_for_library_prefixes_test.dart'
as no_leading_underscores_for_library_prefixes;
Expand Down Expand Up @@ -419,6 +420,7 @@ void main() {
missing_code_block_language_in_doc_comment.main();
missing_whitespace_between_adjacent_strings.main();
no_adjacent_strings_in_list.main();
no_default_cases.main();
no_duplicate_case_values.main();
no_leading_underscores_for_library_prefixes.main();
no_leading_underscores_for_local_identifiers.main();
Expand Down
78 changes: 78 additions & 0 deletions pkg/linter/test/rules/no_default_cases_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(NoDefaultCasesTest);
});
}

@reflectiveTest
class NoDefaultCasesTest extends LintRuleTest {
@override
String get lintRule => 'no_default_cases';

test_enumLikeType() async {
await assertDiagnostics(r'''
class C {
final int i;
const C._(this.i);
static const a = C._(1);
static const b = C._(2);
static const c = C._(3);
}
void f(C c) {
switch (c) {
case C.a :
print('a');
break;
default:
print('default');
}
}
''', [
lint(210, 32),
]);
}

test_enumType() async {
await assertDiagnostics(r'''
void f(E e) {
switch(e) {
case E.a :
print('a');
break;
default:
print('default');
}
}
enum E {
a, b, c;
}
''', [
lint(78, 32),
]);
}

test_intType() async {
await assertNoDiagnostics(r'''
void f(int i) {
switch (i) {
case 1 :
print('1');
break;
default:
print('default');
}
}
''');
}
}
103 changes: 103 additions & 0 deletions pkg/linter/test/rules/overridden_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,57 @@ class B extends A {
''');
}

test_extendingClass_multipleDeclarations() async {
await assertDiagnostics(r'''
class A {
int y = 1;
}
class B extends A {
final x = 1, y = 2;
}
''', [
lint(60, 1),
]);
}

test_extendingClass_overridingAbstract() async {
await assertNoDiagnostics(r'''
abstract class A {
abstract int x;
}
class B extends A {
@override
int x = 1;
}
''');
}

test_extendingClass_staticField() async {
await assertNoDiagnostics(r'''
class A {
static int x = 1;
}
class B extends A {
static int x = 2;
}
''');
}

test_extendsClass_indirectly() async {
await assertDiagnostics(r'''
class A {
int x = 0;
}
class B extends A {}
class C extends B {
@override
int x = 1;
}
''', [
lint(84, 1),
]);
}

test_externalLibrary() async {
newFile('$testPackageLibPath/a.dart', r'''
class A {
Expand Down Expand Up @@ -141,6 +192,44 @@ class B extends A {
''');
}

test_implementingClass() async {
await assertNoDiagnostics(r'''
class A {
int x = 1;
}
class B implements A {
@override
int x = 2;
}
''');
}

test_mixingInMixin() async {
await assertDiagnostics(r'''
mixin M {
int x = 1;
}
class A with M {
@override
int x = 2;
}
''', [
lint(60, 1),
]);
}

test_mixingInMixin_overridingAbstract() async {
await assertNoDiagnostics(r'''
mixin M {
abstract int x;
}
class A with M {
@override
int x = 2;
}
''');
}

test_mixinInheritsFromNotObject() async {
// See: https://github.com/dart-lang/linter/issues/2969
// Preserves existing testing logic but has so many misuses of mixins that
Expand Down Expand Up @@ -230,6 +319,20 @@ class GC34 extends GC33 {
]);
}

test_mixinSuperclassConstraint() async {
await assertDiagnostics(r'''
class A {
int x = 1;
}
mixin M on A {
@override
final x = 1;
}
''', [
lint(60, 1),
]);
}

test_overridingAbstractField() async {
await assertNoDiagnostics(r'''
abstract class A {
Expand Down
27 changes: 27 additions & 0 deletions pkg/linter/test/rules/unnecessary_lambdas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ var x = [].map((x) => C(3));
''');
}

test_deeplyNestedVariable() async {
await assertNoDiagnostics(r'''
void f() {
var x = (a, b) => foo(foo(b)).foo(a, b);
}
dynamic foo(a) => 7;
''');
}

test_emptyLambda() async {
await assertNoDiagnostics(r'''
var f = () {};
Expand Down Expand Up @@ -343,6 +352,24 @@ class C {
};
}
}
''');
}

test_targetIsFinalParameter() async {
await assertDiagnostics(r'''
void f(List<String> list) {
list.where((final e) => ((a) => e.contains(a))(e));
}
''', [
lint(55, 20),
]);
}

test_targetIsVarParameter() async {
await assertNoDiagnostics(r'''
void main(List<String> list) {
list.where((e) => ((a) => e.contains(a))(e));
}
''');
}
}
47 changes: 0 additions & 47 deletions pkg/linter/test_data/rules/no_default_cases.dart

This file was deleted.

Loading

0 comments on commit da7a206

Please sign in to comment.