Skip to content

Commit

Permalink
Move prefer_null_aware_method_calls tests (#4584)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored Jul 20, 2023
1 parent 26b307b commit 9437471
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 31 deletions.
3 changes: 3 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ import 'prefer_inlined_adds_test.dart' as prefer_inlined_adds;
import 'prefer_interpolation_to_compose_strings_test.dart'
as prefer_interpolation_to_compose_strings;
import 'prefer_mixin_test.dart' as prefer_mixin;
import 'prefer_null_aware_method_calls_test.dart'
as prefer_null_aware_method_calls;
import 'prefer_relative_imports_test.dart' as prefer_relative_imports;
import 'prefer_single_quotes_test.dart' as prefer_single_quotes;
import 'prefer_spread_collections_test.dart' as prefer_spread_collections;
Expand Down Expand Up @@ -279,6 +281,7 @@ void main() {
prefer_inlined_adds.main();
prefer_interpolation_to_compose_strings.main();
prefer_mixin.main();
prefer_null_aware_method_calls.main();
prefer_relative_imports.main();
prefer_single_quotes.main();
prefer_spread_collections.main();
Expand Down
103 changes: 103 additions & 0 deletions test/rules/prefer_null_aware_method_calls_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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(PreferNullAwareMethodCallsTest);
});
}

@reflectiveTest
class PreferNullAwareMethodCallsTest extends LintRuleTest {
@override
String get lintRule => 'prefer_null_aware_method_calls';

test_conditional_differentTargets() async {
await assertNoDiagnostics(r'''
Function()? f1, f2;
void f() {
f1 != null ? f2!() : null;
}
''');
}

test_conditional_propertyAccess_differentProperties() async {
await assertNoDiagnostics(r'''
void f(dynamic p) {
p.a != null ? p.m!() : null;
}
''');
}

test_conditional_propertyAccess_sameProperty() async {
await assertDiagnostics(r'''
void f(dynamic p) {
p.m != null ? p.m!() : null;
}
''', [
lint(36, 6),
]);
}

test_conditional_sameTarget() async {
await assertDiagnostics(r'''
Function()? func;
void f() {
func != null ? func!() : null;
}
''', [
lint(46, 7),
]);
}

test_ifNotNull_propertyAccess_differentProperties() async {
await assertNoDiagnostics(r'''
void f(dynamic p) {
if (p.a != null) {
p.m!();
}
}
''');
}

test_ifNotNull_propertyAccess_sameProperty() async {
await assertDiagnostics(r'''
void f(dynamic p) {
if (p.m != null) {
p.m!();
}
}
''', [
lint(45, 6),
]);
}

test_ifNotNull_sameTarget_blockStatement() async {
await assertDiagnostics(r'''
Function()? func;
void f() {
if (func != null) {
func!();
}
}
''', [
lint(55, 7),
]);
}

test_ifNotNull_sameTarget_expressionStatement() async {
await assertDiagnostics(r'''
Function()? func;
void f() {
if (func != null) func!();
}
''', [
lint(49, 7),
]);
}
}
31 changes: 0 additions & 31 deletions test_data/rules/prefer_null_aware_method_calls.dart

This file was deleted.

0 comments on commit 9437471

Please sign in to comment.