diff --git a/test/rules/all.dart b/test/rules/all.dart index e6f5b8380..22fe4e837 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -155,6 +155,8 @@ import 'unnecessary_overrides_test.dart' as unnecessary_overrides; import 'unnecessary_parenthesis_test.dart' as unnecessary_parenthesis; import 'unnecessary_statements_test.dart' as unnecessary_statements; import 'unnecessary_string_escapes_test.dart' as unnecessary_string_escapes; +import 'unnecessary_string_interpolations_test.dart' + as unnecessary_string_interpolations; import 'unnecessary_this_test.dart' as unnecessary_this; import 'unnecessary_to_list_in_spreads_test.dart' as unnecessary_to_list_in_spreads; @@ -297,6 +299,7 @@ void main() { unnecessary_parenthesis.main(); unnecessary_statements.main(); unnecessary_string_escapes.main(); + unnecessary_string_interpolations.main(); unnecessary_this.main(); unnecessary_to_list_in_spreads.main(); unreachable_from_main.main(); diff --git a/test/rules/unnecessary_string_interpolations_test.dart b/test/rules/unnecessary_string_interpolations_test.dart new file mode 100644 index 000000000..6171444b1 --- /dev/null +++ b/test/rules/unnecessary_string_interpolations_test.dart @@ -0,0 +1,92 @@ +// 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(UnnecessaryStringInterpolationsTest); + }); +} + +@reflectiveTest +class UnnecessaryStringInterpolationsTest extends LintRuleTest { + @override + String get lintRule => 'unnecessary_string_interpolations'; + + test_necessaryInterpolation_adjascentStrings() async { + await assertNoDiagnostics(r''' +var a = ''; +var b = 'x' '$a'; +'''); + } + + test_necessaryInterpolation_nullableString() async { + await assertNoDiagnostics(r''' +class Node { + String? x = ''; + String f() => '$x'; +} +'''); + } + + test_necessaryInterpolation_property() async { + await assertNoDiagnostics(r''' +var a = ''; +var b = '${a.length}'; +'''); + } + + test_necessaryInterpolation_single() async { + await assertNoDiagnostics(r''' +var a = ''; +var b = 'x$a'; +'''); + } + + test_necessaryInterpolation_triple() async { + await assertNoDiagnostics(r""" +var a = ''; +var b = '''x$a'''; +"""); + } + + test_unnecessaryInterpolation_single() async { + await assertDiagnostics(r''' +var a = ''; +var b = '$a'; +''', [ + lint(20, 4), + ]); + } + + test_unnecessaryInterpolation_substring() async { + await assertDiagnostics(r''' +var a = ''; +var b = '${a.substring(1)}'; +''', [ + lint(20, 19), + ]); + } + + test_unnecessaryInterpolation_triple() async { + await assertDiagnostics(r""" +var a = ''; +var b = '''$a'''; +""", [ + lint(20, 8), + ]); + } + + test_unnecessaryInterpolation_withBraces() async { + await assertDiagnostics(r''' +var a = ''; +var b = '${a}'; +''', [ + lint(20, 6), + ]); + } +} diff --git a/test_data/rules/unnecessary_string_interpolations.dart b/test_data/rules/unnecessary_string_interpolations.dart deleted file mode 100644 index a6871d7a8..000000000 --- a/test_data/rules/unnecessary_string_interpolations.dart +++ /dev/null @@ -1,24 +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. - -// test w/ `dart test -N unnecessary_string_interpolations` - -class Node { - final String? text = ''; - @override - String toString() => '$text'; // OK -} - -String o = ''; - -f() { - o = '$o'; // LINT - o = '''$o'''; // LINT - o = '${o}'; // LINT - o = '${o.substring(1)}'; // LINT - o = '${o.length}'; // OK - o = 'a$o'; // OK - o = '''a$o'''; // OK - o = 'a' '$o'; // OK -}