diff --git a/pkg/linter/test/rule_test.dart b/pkg/linter/test/rule_test.dart index a3605399b6b0..d0f5bb2aa6bc 100644 --- a/pkg/linter/test/rule_test.dart +++ b/pkg/linter/test/rule_test.dart @@ -16,7 +16,6 @@ import 'package:analyzer/src/lint/registry.dart'; import 'package:linter/src/analyzer.dart'; import 'package:linter/src/ast.dart'; import 'package:linter/src/rules.dart'; -import 'package:linter/src/rules/implementation_imports.dart'; import 'package:linter/src/rules/package_prefixed_library_names.dart'; import 'package:linter/src/test_utilities/annotation.dart'; import 'package:linter/src/test_utilities/formatter.dart'; @@ -59,62 +58,6 @@ void defineRuleTests() { } void defineRuleUnitTests() { - group('uris', () { - group('isPackage', () { - for (var uri in [ - Uri.parse('package:foo/src/bar.dart'), - Uri.parse('package:foo/src/baz/bar.dart') - ]) { - test(uri.toString(), () { - expect(isPackage(uri), isTrue); - }); - } - for (var uri in [ - Uri.parse('foo/bar.dart'), - Uri.parse('src/bar.dart'), - Uri.parse('dart:async') - ]) { - test(uri.toString(), () { - expect(isPackage(uri), isFalse); - }); - } - }); - - group('samePackage', () { - test('identity', () { - expect( - samePackage(Uri.parse('package:foo/src/bar.dart'), - Uri.parse('package:foo/src/bar.dart')), - isTrue); - }); - test('foo/bar.dart', () { - expect( - samePackage(Uri.parse('package:foo/src/bar.dart'), - Uri.parse('package:foo/bar.dart')), - isTrue); - }); - }); - - group('implementation', () { - for (var uri in [ - Uri.parse('package:foo/src/bar.dart'), - Uri.parse('package:foo/src/baz/bar.dart') - ]) { - test(uri.toString(), () { - expect(isImplementation(uri), isTrue); - }); - } - for (var uri in [ - Uri.parse('package:foo/bar.dart'), - Uri.parse('src/bar.dart') - ]) { - test(uri.toString(), () { - expect(isImplementation(uri), isFalse); - }); - } - }); - }); - group('names', () { group('keywords', () { var good = ['class', 'if', 'assert', 'catch', 'import']; diff --git a/pkg/linter/test/rules/all.dart b/pkg/linter/test/rules/all.dart index 8e46357f9e29..440287cd1c0f 100644 --- a/pkg/linter/test/rules/all.dart +++ b/pkg/linter/test/rules/all.dart @@ -113,6 +113,7 @@ import 'exhaustive_cases_test.dart' as exhaustive_cases; import 'file_names_test.dart' as file_names; import 'flutter_style_todos_test.dart' as flutter_style_todos; import 'hash_and_equals_test.dart' as hash_and_equals; +import 'implementation_imports_test.dart' as implementation_imports; import 'implicit_call_tearoffs_test.dart' as implicit_call_tearoffs; import 'implicit_reopen_test.dart' as implicit_reopen; import 'invalid_case_patterns_test.dart' as invalid_case_patterns; @@ -381,6 +382,7 @@ void main() { file_names.main(); flutter_style_todos.main(); hash_and_equals.main(); + implementation_imports.main(); implicit_call_tearoffs.main(); implicit_reopen.main(); invalid_case_patterns.main(); diff --git a/pkg/linter/test/rules/implementation_imports_test.dart b/pkg/linter/test/rules/implementation_imports_test.dart new file mode 100644 index 000000000000..c6d7bd29dc0b --- /dev/null +++ b/pkg/linter/test/rules/implementation_imports_test.dart @@ -0,0 +1,94 @@ +// 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:linter/src/rules/implementation_imports.dart'; +import 'package:test/test.dart'; +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../rule_test_support.dart'; + +void main() { + group('uris', () { + group('isPackage', () { + for (var uri in [ + Uri.parse('package:foo/src/bar.dart'), + Uri.parse('package:foo/src/baz/bar.dart') + ]) { + test(uri.toString(), () { + expect(isPackage(uri), isTrue); + }); + } + for (var uri in [ + Uri.parse('foo/bar.dart'), + Uri.parse('src/bar.dart'), + Uri.parse('dart:async') + ]) { + test(uri.toString(), () { + expect(isPackage(uri), isFalse); + }); + } + }); + + group('samePackage', () { + test('identity', () { + expect( + samePackage(Uri.parse('package:foo/src/bar.dart'), + Uri.parse('package:foo/src/bar.dart')), + isTrue); + }); + test('foo/bar.dart', () { + expect( + samePackage(Uri.parse('package:foo/src/bar.dart'), + Uri.parse('package:foo/bar.dart')), + isTrue); + }); + }); + }); + + group('implementation', () { + for (var uri in [ + Uri.parse('package:foo/src/bar.dart'), + Uri.parse('package:foo/src/baz/bar.dart') + ]) { + test(uri.toString(), () { + expect(isImplementation(uri), isTrue); + }); + } + for (var uri in [ + Uri.parse('package:foo/bar.dart'), + Uri.parse('src/bar.dart') + ]) { + test(uri.toString(), () { + expect(isImplementation(uri), isFalse); + }); + } + }); + + defineReflectiveSuite(() { + defineReflectiveTests(ImplementationImportsTest); + }); +} + +@reflectiveTest +class ImplementationImportsTest extends LintRuleTest { + @override + bool get addFlutterPackageDep => true; + + @override + String get lintRule => 'implementation_imports'; + test_inPartFile() async { + newFile('$testPackageRootPath/test/a.dart', r''' +part 'test.dart'; +'''); + + await assertDiagnostics(r''' +part of 'a.dart'; + +import 'package:flutter/src/material/colors.dart'; +''', [ + error(WarningCode.UNUSED_IMPORT, 26, 42), + lint(26, 42), + ]); + } +}