blob: 601f072c75368ef76eff7cc9b7ff930afebb00da [file] [log] [blame]
// 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(() {
defineReflectiveTests(UnnecessaryGettersSettersTest);
});
}
@reflectiveTest
class UnnecessaryGettersSettersTest extends LintRuleTest {
@override
String get lintRule => 'unnecessary_getters_setters';
test_necessary_differentType() async {
await assertNoDiagnostics(r'''
class C {
String? _x;
dynamic get x {
return _x;
}
set x(dynamic value) {
_x = value;
}
}
''');
}
test_necessary_hasAnnotation() async {
await assertNoDiagnostics(r'''
class C {
String? _x;
@Annotation()
String? get x => _x;
set x(String? value) {
_x = value;
}
}
class Annotation {
const Annotation();
}
''');
}
test_necessary_nonTrivialSetter() async {
await assertNoDiagnostics(r'''
class C {
String? _x;
String? get x => _x;
set x(String? value) {
_x = value?.toLowerCase();
}
}
''');
}
test_necessary_nullAwareAssignment() async {
await assertNoDiagnostics(r'''
class C {
String? _x;
String? get x => _x;
set x(String? value) {
_x ??= value;
}
}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/linter/issues/4935')
test_unnecessary_augmentationAddedGetterAndSetter() async {
var a = newFile('$testPackageLibPath/a.dart', r'''
import augment 'b.dart';
class A {}
''');
var b = newFile('$testPackageLibPath/b.dart', r'''
augment library 'a.dart';
augment class A {
String? _x;
String? get x => _x;
set x(String? value) {
_x = value;
}
}
''');
result = await resolveFile(a.path);
await assertDiagnosticsIn(errors, [
// TODO(pq): in the absence of accessors in the augmented class, report on the class decl?
lint(33, 1),
]);
result = await resolveFile(b.path);
await assertNoDiagnosticsIn(errors);
}
@FailingTest(issue: 'https://github.com/dart-lang/linter/issues/4935')
test_unnecessary_augmentationAddedSetter() async {
var a = newFile('$testPackageLibPath/a.dart', r'''
import augment 'b.dart';
class A {
String? _x;
String? get x => _x;
}
''');
var b = newFile('$testPackageLibPath/b.dart', r'''
augment library 'a.dart';
augment class A {
set x(String? value) {
_x = value;
}
}
''');
result = await resolveFile(a.path);
await assertDiagnosticsIn(errors, [
lint(52, 1),
]);
result = await resolveFile(b.path);
await assertNoDiagnosticsIn(errors);
}
test_unnecessary_getterAndSetter_extensionType() async {
await assertDiagnostics(r'''
extension type E(int i) {
static int? _x;
static int? get x => _x;
static set x(int? value) {
_x = value;
}
}
''', [
lint(62, 1),
]);
}
test_unnecessary_getterAndSetterHaveBlockBody() async {
await assertDiagnostics(r'''
class C {
String? _x;
String? get x {
return _x;
}
set x(String? value) {
_x = value;
}
}
''', [
lint(39, 1),
]);
}
test_unnecessary_getterHasExpressionBody() async {
await assertDiagnostics(r'''
class C {
String? _x;
String? get x => _x;
set x(String? value)
{
_x = value;
}
}
''', [
lint(39, 1),
]);
}
test_unnecessary_setterHasExpressionBody() async {
await assertDiagnostics(r'''
class C {
String? _x;
String? get x {
return _x;
}
set x(String? value) => _x = value;
}
''', [
lint(39, 1),
]);
}
}