Skip to content

Commit

Permalink
linter: Migrate prefer_conditional_assignment tests
Browse files Browse the repository at this point in the history
Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try
Change-Id: I0e09a9b51df81bad301cb7451534856afa7aea58
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/377727
Reviewed-by: Phil Quitslund <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Aug 1, 2024
1 parent 3b19354 commit cbac320
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 132 deletions.
3 changes: 3 additions & 0 deletions pkg/linter/test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ import 'prefer_asserts_in_initializer_lists_test.dart'
as prefer_asserts_in_initializer_lists;
import 'prefer_asserts_with_message_test.dart' as prefer_asserts_with_message;
import 'prefer_collection_literals_test.dart' as prefer_collection_literals;
import 'prefer_conditional_assignment_test.dart'
as prefer_conditional_assignment;
import 'prefer_const_constructors_in_immutables_test.dart'
as prefer_const_constructors_in_immutables;
import 'prefer_const_constructors_test.dart' as prefer_const_constructors;
Expand Down Expand Up @@ -408,6 +410,7 @@ void main() {
prefer_asserts_in_initializer_lists.main();
prefer_asserts_with_message.main();
prefer_collection_literals.main();
prefer_conditional_assignment.main();
prefer_const_constructors_in_immutables.main();
prefer_const_constructors.main();
prefer_const_declarations.main();
Expand Down
153 changes: 153 additions & 0 deletions pkg/linter/test/rules/prefer_conditional_assignment_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright (c) 2024, 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(PreferConditionalAssignmentTest);
});
}

@reflectiveTest
class PreferConditionalAssignmentTest extends LintRuleTest {
@override
String get lintRule => 'prefer_conditional_assignment';

test_field_ifEqNull() async {
await assertDiagnostics(r'''
class C {
String? x;
void f(String s) {
if (x == null) {
x = s;
}
}
}
''', [
lint(49, 35),
]);
}

test_field_ifEqNull_conditionWrappedInParens() async {
await assertDiagnostics(r'''
class C {
String? x;
void f(String s) {
if ((x == null)) {
x = s;
}
}
}
''', [
lint(48, 37),
]);
}

test_field_ifEqNull_eachWrappedInParens() async {
await assertDiagnostics(r'''
class C {
String? x;
void f(String s) {
if ((x) == (null)) {
x = s;
}
}
}
''', [
lint(48, 39),
]);
}

test_field_ifEqNull_statementBody() async {
await assertDiagnostics(r'''
class C {
String? x;
String? f(String s) {
if (x == null)
x = s;
return x;
}
}
''', [
lint(51, 27),
]);
}

test_field_ifHasElse() async {
await assertNoDiagnostics(r'''
class C {
String? x;
void f() {
if (x == null) {
x = foo(this);
} else {}
}
}
String foo(C c) => '';
''');
}

test_field_onOtherTarget() async {
await assertNoDiagnostics(r'''
class C {
String? x;
void f(C a, C b) {
if (a.x == null) {
b.x = '';
}
}
}
''');
}

test_field_onSameTarget() async {
await assertDiagnostics(r'''
class C {
String? x;
void f(C a) {
if (a.x == null) {
a.x = '';
}
}
}
''', [
lint(43, 40),
]);
}

test_field_unrelatedAssignment() async {
await assertNoDiagnostics(r'''
class C {
String? x;
var y = 1;
void f() {
if (x == null) {
y = 0;
}
}
}
''');
}

test_field_unrelatedAssignment_thenAssignment() async {
await assertNoDiagnostics(r'''
class C {
String? x;
var y = 1;
void f() {
if (x == null) {
y = 0;
x = '';
}
}
}
''');
}
}
132 changes: 0 additions & 132 deletions pkg/linter/test_data/rules/prefer_conditional_assignment.dart

This file was deleted.

0 comments on commit cbac320

Please sign in to comment.