Files
sdk/pkg/linter/test/rules/use_string_buffers_test.dart
Brian Wilkerson a16de199d7 Convert many lint tests to use markdown
This is a rediculously large CL, and if you want me to split it up I'm
willing to do so.

However, the changes were all made by running a script I wrote and then
running the formatter over the code, so hopefully a spot-check will be
sufficient.

Change-Id: Ifc59b2cc3bf9e4edf0229a130cd587dc73f95615
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/505042
Reviewed-by: Samuel Rawlins <srawlins@google.com>
SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
2026-05-20 11:27:11 -07:00

202 lines
3.7 KiB
Dart

// 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';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(UseStringBuffersTest);
});
}
@reflectiveTest
class UseStringBuffersTest extends LintRuleTest {
@override
String get lintRule => LintNames.use_string_buffers;
test_field_nonString_plus() async {
await assertNoDiagnostics(r'''
class B {
operator +(B other) => this;
void m() {
B b = B();
for (var i = 0; i < 10; i++) {
b = b + this;
}
}
}
''');
}
test_field_nonString_plusEquals() async {
await assertNoDiagnostics(r'''
class B {
operator +(B other) => this;
void m() {
B b = B();
for (var i = 0; i < 10; i++) {
b += this;
}
}
}
''');
}
test_field_plus_bufferReference() async {
await assertNoDiagnostics(r'''
class A {
String buffer = '';
void foo() {
buffer = buffer + buffer;
}
}
''');
}
test_field_plusEquals_nonStringLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
class A {
String buffer = '';
void foo(int n) {
int aux = n;
while (aux-- > 0) {
[!buffer += ''.toLowerCase()!];
}
}
}
''');
}
test_field_plusEquals_stringLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
class A {
String buffer = '';
void foo(int n) {
int aux = n;
while (aux-- > 0) {
[!buffer += 'a'!];
}
}
}
''');
}
test_localVariable_assignment_interpolatedStringLiteralAsPrefix() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
for (int i = 0; i < 10; i++) {
[!buffer!] = '${buffer}a';
}
}
''');
}
test_localVariable_assignment_interpolatedStringLiteralAsPrefixWithPlus() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
for (int i = 0; i < 10; i++) {
[!buffer!] = '${buffer + 'a'}a';
}
}
''');
}
test_localVariable_assignment_interpolatedStringLiteralNotAsPrefix() async {
await assertNoDiagnostics(r'''
void foo() {
var buffer = '';
for (int i = 0; i < 10; i++) {
buffer = 'a$buffer';
}
}
''');
}
test_localVariable_doLoop_plusEquals_stringLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
do {
[!buffer += 'a'!];
} while (buffer.length < 10);
}
''');
}
test_localVariable_plus_stringLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
for (int i = 0; i < 10; i++) {
[!buffer!] = buffer + 'a';
}
}
''');
}
test_localVariable_plusEquals_nonStringLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
for (final s in ['a']) {
[!buffer += s!];
}
}
''');
}
test_localVariable_plusEquals_nonStringLiteral_parenthesized() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
for (final s in ['a']) {
([!buffer += s!]);
}
}
''');
}
test_localVariable_plusEquals_stringLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
for (int i = 0; i < 10; i++) {
[!buffer += 'a'!];
}
}
''');
}
test_localVariable_whileLoop_plusEquals_stringLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
void foo() {
var buffer = '';
while (buffer.length < 10) {
[!buffer += 'a'!];
}
}
''');
}
test_loopVariable_plusEquals_nonStringLiteral() async {
await assertNoDiagnostics(r'''
void foo() {
for (final s in [ 'a', 'b']) {
var buffer = '';
buffer += s;
}
}
''');
}
}