// 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 test_noPatternField() async { await assertNoDiagnostics(''' void f(Object o) { if (o case int i) {} } '''); } Future test_patternField_dynamic() async { await assertDiagnosticsFromMarkdown(''' void f(Object o) { if (o case dynamic([!isEven!]:var isEven)) {} } '''); } Future test_patternField_explicit() async { await assertDiagnosticsFromMarkdown(''' void f(Object o) { if (o case int([!isEven!]:var isEven)) {} } '''); } Future test_patternField_function() async { await assertDiagnosticsFromMarkdown(''' void f(Object o) { if (o case Function([!call!]:var call)) {} } '''); } Future test_patternField_implicit() async { await assertNoDiagnostics(''' void f(Object o) { if (o case int(:var isEven)) {} } '''); } Future test_patternField_otherName() async { await assertNoDiagnostics(''' void f(Object o) { if (o case int(isEven:var other)) {} } '''); } Future test_patternField_parenthesized() async { await assertDiagnosticsFromMarkdown(''' void f(Object o) { if (o case int([!isEven!]:((var isEven)))) {} } '''); } Future 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 test_recordDestructuring() async { await assertDiagnosticsFromMarkdown(''' void f((int, {String name}) record) { var (x, [!name!]: name) = record; } '''); } Future 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 test_typedef_if() async { await assertDiagnosticsFromMarkdown(''' typedef O = Object; void f(Object o) { if (o case O([!hashCode!]: final hashCode)) {} } '''); } Future test_typedef_record() async { await assertDiagnosticsFromMarkdown(''' typedef R = ({int value}); void f(Object o) { if (o case R([!value!]: var value)) {} } '''); } Future test_typedef_typeParameter() async { await assertDiagnosticsFromMarkdown(''' typedef O = T; void f(O o) { if (o case O([!hashCode!]: var hashCode)) {} } '''); } Future test_typedef_variableDeclaration() async { await assertDiagnosticsFromMarkdown(''' typedef O = Object; void f(Object o) { final O([!hashCode!]: hashCode) = o; } '''); } Future test_typeParameter() async { await assertDiagnosticsFromMarkdown(''' void f(T o) { if (o case T([!hashCode!]: var hashCode)) {} } '''); } Future test_typeParameter_functionType() async { await assertDiagnosticsFromMarkdown(''' void f(F o) { if (o case F([!call!]: var call)) {} } '''); } Future test_typeParameter_record() async { await assertDiagnosticsFromMarkdown(''' void f(R o) { if (o case R([!value!]: var value)) {} } '''); } Future test_typeParameter_typedef_record() async { await assertDiagnosticsFromMarkdown(''' void f(T o) { if (o case T([!value!]: var value)) {} } typedef R = ({int value}); '''); } Future test_typeParameter_typeParameterBounded() async { await assertDiagnosticsFromMarkdown(''' void f(O o) { if (o case O([!hashCode!]: var hashCode)) {} } '''); } }