Files
sdk/pkg/linter/test/rules/simplify_variable_pattern_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

189 lines
4.2 KiB
Dart

// Copyright (c) 2025, 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:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../rule_test_support.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(SimplifyVariablePatternTest);
});
}
@reflectiveTest
class SimplifyVariablePatternTest extends LintRuleTest {
@override
String get lintRule => LintNames.simplify_variable_pattern;
Future<void> test_noPatternField() async {
await assertNoDiagnostics('''
void f(Object o) {
if (o case int i) {}
}
''');
}
Future<void> test_patternField_dynamic() async {
await assertDiagnosticsFromMarkdown('''
void f(Object o) {
if (o case dynamic([!isEven!]:var isEven)) {}
}
''');
}
Future<void> test_patternField_explicit() async {
await assertDiagnosticsFromMarkdown('''
void f(Object o) {
if (o case int([!isEven!]:var isEven)) {}
}
''');
}
Future<void> test_patternField_function() async {
await assertDiagnosticsFromMarkdown('''
void f(Object o) {
if (o case Function([!call!]:var call)) {}
}
''');
}
Future<void> test_patternField_implicit() async {
await assertNoDiagnostics('''
void f(Object o) {
if (o case int(:var isEven)) {}
}
''');
}
Future<void> test_patternField_otherName() async {
await assertNoDiagnostics('''
void f(Object o) {
if (o case int(isEven:var other)) {}
}
''');
}
Future<void> test_patternField_parenthesized() async {
await assertDiagnosticsFromMarkdown('''
void f(Object o) {
if (o case int([!isEven!]:((var isEven)))) {}
}
''');
}
Future<void> test_patternField_unnexistingProperty() async {
await assertDiagnostics(
'''
void f(Object o) {
if (o case int(isEvenn:var isEvenn) when isEvenn) {}
}
''',
[error(diag.undefinedGetter, 36, 7)],
);
}
Future<void> test_recordDestructuring() async {
await assertDiagnosticsFromMarkdown('''
void f((int, {String name}) record) {
var (x, [!name!]: name) = record;
}
''');
}
Future<void> test_recordDestructuring_unnexistingField() async {
await assertDiagnostics(
'''
void f((int, {String name}) record) {
var (x, namee: namee) = record;
print(namee);
}
''',
[error(diag.patternTypeMismatchInIrrefutableContext, 44, 17)],
);
}
Future<void> test_typedef_if() async {
await assertDiagnosticsFromMarkdown('''
typedef O = Object;
void f(Object o) {
if (o case O([!hashCode!]: final hashCode)) {}
}
''');
}
Future<void> test_typedef_record() async {
await assertDiagnosticsFromMarkdown('''
typedef R = ({int value});
void f(Object o) {
if (o case R([!value!]: var value)) {}
}
''');
}
Future<void> test_typedef_typeParameter() async {
await assertDiagnosticsFromMarkdown('''
typedef O<T extends Object> = T;
void f(O o) {
if (o case O([!hashCode!]: var hashCode)) {}
}
''');
}
Future<void> test_typedef_variableDeclaration() async {
await assertDiagnosticsFromMarkdown('''
typedef O = Object;
void f(Object o) {
final O([!hashCode!]: hashCode) = o;
}
''');
}
Future<void> test_typeParameter() async {
await assertDiagnosticsFromMarkdown('''
void f<T extends Object>(T o) {
if (o case T([!hashCode!]: var hashCode)) {}
}
''');
}
Future<void> test_typeParameter_functionType() async {
await assertDiagnosticsFromMarkdown('''
void f<F extends void Function()>(F o) {
if (o case F([!call!]: var call)) {}
}
''');
}
Future<void> test_typeParameter_record() async {
await assertDiagnosticsFromMarkdown('''
void f<R extends ({int value})>(R o) {
if (o case R([!value!]: var value)) {}
}
''');
}
Future<void> test_typeParameter_typedef_record() async {
await assertDiagnosticsFromMarkdown('''
void f<T extends R>(T o) {
if (o case T([!value!]: var value)) {}
}
typedef R = ({int value});
''');
}
Future<void> test_typeParameter_typeParameterBounded() async {
await assertDiagnosticsFromMarkdown('''
void f<T extends Object, O extends T>(O o) {
if (o case O([!hashCode!]: var hashCode)) {}
}
''');
}
}