From ebd6c98cfb3c8b0f0be2d243130ac32421f928f1 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Mon, 10 Jul 2023 07:42:25 -0700 Subject: [PATCH] Move use_setters_to_change_properties tests (dart-lang/linter#4544) --- test/rules/all.dart | 3 + ...use_setters_to_change_properties_test.dart | 110 ++++++++++++++++++ .../use_setters_to_change_properties.dart | 55 --------- 3 files changed, 113 insertions(+), 55 deletions(-) create mode 100644 test/rules/use_setters_to_change_properties_test.dart delete mode 100644 test_data/rules/use_setters_to_change_properties.dart diff --git a/test/rules/all.dart b/test/rules/all.dart index a12342e7160d..a0c50b0aa236 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -174,6 +174,8 @@ import 'use_key_in_widget_constructors_test.dart' import 'use_late_for_private_fields_and_variables_test.dart' as use_late_for_private_fields_and_variables; import 'use_named_constants_test.dart' as use_named_constants; +import 'use_setters_to_change_properties_test.dart' + as use_setters_to_change_properties; import 'use_super_parameters_test.dart' as use_super_parameters; import 'use_test_throws_matchers_test.dart' as use_test_throws_matchers; import 'void_checks_test.dart' as void_checks; @@ -305,6 +307,7 @@ void main() { use_key_in_widget_constructors.main(); use_late_for_private_fields_and_variables.main(); use_named_constants.main(); + use_setters_to_change_properties.main(); use_super_parameters.main(); use_test_throws_matchers.main(); void_checks.main(); diff --git a/test/rules/use_setters_to_change_properties_test.dart b/test/rules/use_setters_to_change_properties_test.dart new file mode 100644 index 000000000000..e3d92d7013ca --- /dev/null +++ b/test/rules/use_setters_to_change_properties_test.dart @@ -0,0 +1,110 @@ +// Copyright (c) 2023, 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(() { + // TODO(srawlins): Add test with setter-like with multiple statements; add + // test with non-trivial right side (`this.x = x + 1`). + defineReflectiveTests(UseSettersToChangePropertiesTest); + }); +} + +@reflectiveTest +class UseSettersToChangePropertiesTest extends LintRuleTest { + @override + String get lintRule => 'use_setters_to_change_properties'; + + test_setterLike_expressionBody() async { + await assertDiagnostics(r''' +abstract class A { + int x = 0; + void setX(int x) => this.x = x; +} +''', [ + lint(39, 4), + ]); + } + + test_setterLike_blockBody() async { + await assertDiagnostics(r''' +abstract class A { + int x = 0; + void setX(int x) { + this.x = x; + } +} +''', [ + lint(39, 4), + ]); + } + + test_combo() async { + await assertNoDiagnostics(r''' +abstract class A { + int x = 0; + void setX(int x) => this.x += x; +} +'''); + } + + test_inheritedFromSuperclass() async { + await assertNoDiagnostics(r''' +abstract class A { + void setX(int x); +} + +class B extends A { + int x = 0; + + void setX(int x) { + this.x = x; + } +} +'''); + } + + test_abstract() async { + await assertNoDiagnostics(r''' +abstract class A { + void setX(int x); +} +'''); + } + + test_inheritedFromSuperInterface() async { + await assertNoDiagnostics(r''' +abstract class A { + void setX(int x); +} + +class B implements A { + int x = 0; + + void setX(int x) { + this.x = x; + } +} +'''); + } + + test_extension() async { + await assertDiagnostics(r''' +class A { + int x = 0; +} + +extension E on A { + void setX(int x) { + this.x = x; + } +} +''', [ + lint(52, 4), + ]); + } +} diff --git a/test_data/rules/use_setters_to_change_properties.dart b/test_data/rules/use_setters_to_change_properties.dart deleted file mode 100644 index c2e1695c4c11..000000000000 --- a/test_data/rules/use_setters_to_change_properties.dart +++ /dev/null @@ -1,55 +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. - -// test w/ `dart test -N use_setters_to_change_properties` - -// ignore_for_file: unused_field - -abstract class A { - int _w = 0; - int _x = 0; - - void setW(int w) => _w = w; // LINT - - void setW1(int w) => this._w = w; // LINT - - void setX(int x) { // LINT - _x = x; - } - - void setX1(int x) { // LINT - this._x = x; - } - - void setY(int y); - - void grow1(int value) => _w += value; //OK -} - -class B extends A { - int _y = 0; - - void setY(int y) { // OK because it is an inherited method. - this._y = y; - } -} - -abstract class C { - void setY(int y); -} - -class D implements C { - int _y = 0; - int dd = 0; - - void setY(int y) { // OK because it is an implementation method. - this._y = y; - } -} - -extension E on D { - void setDD(int dd) { // LINT - this.dd = dd; - } -}