From da7a206fa4628aa7a7d09a85156a88d3962ac66c Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Mon, 28 Oct 2024 16:29:09 +0000 Subject: [PATCH] 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 Commit-Queue: Phil Quitslund Auto-Submit: Samuel Rawlins --- pkg/linter/test/rules/all.dart | 2 + .../test/rules/no_default_cases_test.dart | 78 ++++++++++ .../test/rules/overridden_fields_test.dart | 103 +++++++++++++ .../test/rules/unnecessary_lambdas_test.dart | 27 ++++ .../test_data/rules/no_default_cases.dart | 47 ------ .../test_data/rules/overridden_fields.dart | 142 ------------------ .../test_data/rules/unnecessary_lambdas.dart | 43 ------ 7 files changed, 210 insertions(+), 232 deletions(-) create mode 100644 pkg/linter/test/rules/no_default_cases_test.dart delete mode 100644 pkg/linter/test_data/rules/no_default_cases.dart delete mode 100644 pkg/linter/test_data/rules/overridden_fields.dart delete mode 100644 pkg/linter/test_data/rules/unnecessary_lambdas.dart diff --git a/pkg/linter/test/rules/all.dart b/pkg/linter/test/rules/all.dart index 729cb3b8974c..0bfe7306361d 100644 --- a/pkg/linter/test/rules/all.dart +++ b/pkg/linter/test/rules/all.dart @@ -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; @@ -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(); diff --git a/pkg/linter/test/rules/no_default_cases_test.dart b/pkg/linter/test/rules/no_default_cases_test.dart new file mode 100644 index 000000000000..5c9e2368907e --- /dev/null +++ b/pkg/linter/test/rules/no_default_cases_test.dart @@ -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'); + } +} +'''); + } +} diff --git a/pkg/linter/test/rules/overridden_fields_test.dart b/pkg/linter/test/rules/overridden_fields_test.dart index 4ae4a4e25b32..ad9b235e2fee 100644 --- a/pkg/linter/test/rules/overridden_fields_test.dart +++ b/pkg/linter/test/rules/overridden_fields_test.dart @@ -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 { @@ -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 @@ -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 { diff --git a/pkg/linter/test/rules/unnecessary_lambdas_test.dart b/pkg/linter/test/rules/unnecessary_lambdas_test.dart index a92fbcc0094f..870034be7383 100644 --- a/pkg/linter/test/rules/unnecessary_lambdas_test.dart +++ b/pkg/linter/test/rules/unnecessary_lambdas_test.dart @@ -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 = () {}; @@ -343,6 +352,24 @@ class C { }; } } +'''); + } + + test_targetIsFinalParameter() async { + await assertDiagnostics(r''' +void f(List list) { + list.where((final e) => ((a) => e.contains(a))(e)); +} +''', [ + lint(55, 20), + ]); + } + + test_targetIsVarParameter() async { + await assertNoDiagnostics(r''' +void main(List list) { + list.where((e) => ((a) => e.contains(a))(e)); +} '''); } } diff --git a/pkg/linter/test_data/rules/no_default_cases.dart b/pkg/linter/test_data/rules/no_default_cases.dart deleted file mode 100644 index c257d7e3c2ee..000000000000 --- a/pkg/linter/test_data/rules/no_default_cases.dart +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2020, 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. - -// Enum-like -class EL { - final int i; - const EL._(this.i); - - static const e = EL._(1); - static const f = EL._(2); - static const g = EL._(3); -} - -void el(EL e) { - switch(e) { - case EL.e : - print('e'); - break; - default : // LINT - print('default'); - } -} - -enum E { - e, f, g, -} - -void e(E e) { - switch(e) { - case E.e : - print('e'); - break; - default : // LINT - print('default'); - } -} - -void i(int i) { - switch(i) { - case 1 : - print('1'); - break; - default : // OK - print('default'); - } -} diff --git a/pkg/linter/test_data/rules/overridden_fields.dart b/pkg/linter/test_data/rules/overridden_fields.dart deleted file mode 100644 index 969994ce1405..000000000000 --- a/pkg/linter/test_data/rules/overridden_fields.dart +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright (c) 2016, 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. - -class Base { - Object field = 'lorem'; - - Object something = 'change'; -} - -mixin BaseMixin { - Object something = 'change'; -} - -class Bad1 extends Base { - final x = 1, field = 'ipsum'; // LINT -} - -class Bad2 extends Base { - @override - Object something = 'done'; // LINT -} - -class Bad3 extends Object with BaseMixin { - @override - Object something = 'done'; // LINT -} - -class Ok extends Base { - Object newField = 0; // OK - - final Object newFinal = 'ignore'; // OK -} - -class OK2 implements Base { - @override - Object something = 'done'; // OK - - @override - Object field = 0; -} - -abstract class OK3 implements Base { - @override - Object something = 'done'; // OK -} - -class GC11 extends Bad1 { - @override - Object something = 'done'; // LINT - - Object gc33 = 'gc33'; -} - -abstract class GC12 implements Bad1 { - @override - Object something = 'done'; // OK -} - -abstract class GC22 implements GC11 { - @override - Object something = 'done'; // OK -} - -class Super1 {} - -class Sub1 extends Super1 { - int y = 0; -} - -class Super2 { - int x = 0, y = 0; -} - -class Sub2 extends Super2 { - @override - int y = 0; // LINT -} - -class Super3 { - int x = 0; -} - -class Sub3 extends Super3 { - int x = 0; // LINT -} - -class A1 { - int f = 0; -} - -class B1 extends A1 {} - -abstract class C1 implements A1 {} - -class D1 extends B1 implements C1 { - @override - int f = 0; // LINT -} - -class StaticsNo { - static int a = 0; -} - -class VerifyStatic extends StaticsNo { - static int a = 0; -} - -mixin M on A1 { - @override - int f = 0; // LINT - - int g = 0; // OK -} - -abstract class BB { - abstract String s; -} - -mixin AbstractMixin { - abstract String s; -} - -class AA extends BB { - /// Overriding abstracts in NNBD is OK. - @override - String s = ''; // OK -} - -class AAA with AbstractMixin { - @override - String s = ''; // OK -} - -abstract class BBB { - abstract final String s; -} - -class AAAA extends BBB { - @override - String s = ''; // OK -} \ No newline at end of file diff --git a/pkg/linter/test_data/rules/unnecessary_lambdas.dart b/pkg/linter/test_data/rules/unnecessary_lambdas.dart deleted file mode 100644 index 850658acd45d..000000000000 --- a/pkg/linter/test_data/rules/unnecessary_lambdas.dart +++ /dev/null @@ -1,43 +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. - -// ignore: avoid_annotating_with_dynamic -typedef F = int Function(dynamic s); - -void main() { - final List finalList = []; - - // Linted because parameter is final. - finalList.where((final e) => - ((a) => e.contains(a))(e)); // LINT - finalList.where((final e) => - (e.contains)(e)); // OK - finalList.where((e) => - ((a) => e.contains(a))(e)); // OK - - finalList.where((e) => // OK - ((a) => e?.contains(a) ?? false)(e)); // OK - - var deeplyNestedVariable = (a, b) { // OK - foo(foo(b)).foo(a, b); - }; -} - -foo(a) {} - -void method() { - List names = []; - names.add(names); - - var a = names.where((e) => ((e) => e.contains(e))(e)); // LINT - var b = names.where((e) => // LINT - ((e) => e?.contains(e))(e)); - names.where((e) => e?.contains(e) ?? false); // OK - - var c = names.where((e) { // LINT - return ((e) { - return e.contains(e); - })(e); - }); -}