diff --git a/pkg/analyzer/test/src/dart/resolution/language_version_test.dart b/pkg/analyzer/test/src/dart/resolution/language_version_test.dart index 2391856395d..d200a01b6e6 100644 --- a/pkg/analyzer/test/src/dart/resolution/language_version_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/language_version_test.dart @@ -2,16 +2,12 @@ // 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/context/packages.dart'; import 'package:analyzer/src/dart/error/syntactic_errors.dart'; import 'package:analyzer/src/error/codes.dart'; -import 'package:analyzer/src/test_utilities/find_element.dart'; -import 'package:analyzer/src/test_utilities/find_node.dart'; import 'package:analyzer/src/test_utilities/mock_sdk.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; -import 'driver_resolution.dart'; -import 'with_null_safety_mixin.dart'; +import 'context_collection_resolution.dart'; main() { defineReflectiveSuite(() { @@ -37,14 +33,14 @@ class NullSafetyExperimentGlobalTest extends _FeaturesTest }, { "name": "aaa", - "rootUri": "${toUriStr('/aaa')}", + "rootUri": "${toUriStr('$workspaceRootPath/aaa')}", "packageUri": "lib/" } ] } '''); - newFile('/aaa/lib/a.dart', content: r''' + newFile('$workspaceRootPath/aaa/lib/a.dart', content: r''' int a = 0; '''); @@ -73,7 +69,7 @@ var z = pi; }, { "name": "aaa", - "rootUri": "${toUriStr('/aaa')}", + "rootUri": "${toUriStr('$workspaceRootPath/aaa')}", "packageUri": "lib/", "languageVersion": "2.7" } @@ -81,7 +77,7 @@ var z = pi; } '''); - newFile('/aaa/lib/a.dart', content: r''' + newFile('$workspaceRootPath/aaa/lib/a.dart', content: r''' int a = 0; '''); @@ -124,16 +120,17 @@ class NullSafetyUsingAllowedExperimentsTest extends _FeaturesTest { } '''); - var path = convertPath('/test/bin/a.dart'); - await _resolveFile(path, r''' + var path = '$testPackageRootPath/bin/a.dart'; + + await resolveFileCode(path, r''' var x = 0; '''); - assertErrorsInList(result.errors, []); + assertErrorsInResult([]); assertType(findElement.topVar('x').type, 'int*'); - // Upgrade the language version to `2.10`, so enabled Null Safety. - driver.changeFile(path); - await _resolveFile(path, r''' + // Upgrade the language version to `2.10`, so enable Null Safety. + _changeFile(path); + await resolveFileCode(path, r''' // @dart = 2.10 var x = 0; '''); @@ -157,13 +154,16 @@ var x = 0; } '''); - await assertNoErrorsInCode(''' + var path = testFilePath; + + await resolveFileCode(path, ''' var x = 0; '''); + assertErrorsInResult([]); assertType(findElement.topVar('x').type, 'int*'); - // Upgrade the language version to `2.10`, so enabled Null Safety. - _changeTestFile(); + // Upgrade the language version to `2.10`, so enable Null Safety. + _changeFile(path); await assertNoErrorsInCode(''' // @dart = 2.10 var x = 0; @@ -187,16 +187,17 @@ var x = 0; } '''); - var path = convertPath('/test/bin/a.dart'); - await _resolveFile(path, r''' + var path = '$testPackageRootPath/bin/a.dart'; + + await resolveFileCode(path, r''' var x = 0; '''); assertErrorsInList(result.errors, []); assertType(findElement.topVar('x').type, 'int'); // Downgrade the version to `2.8`, so disable Null Safety. - driver.changeFile(path); - await _resolveFile(path, r''' + _changeFile(path); + await resolveFileCode(path, r''' // @dart = 2.8 var x = 0; '''); @@ -219,13 +220,16 @@ var x = 0; } '''); - await assertNoErrorsInCode(''' + var path = testFilePath; + + await resolveFileCode(path, ''' var x = 0; '''); + assertErrorsInResult([]); assertType(findElement.topVar('x').type, 'int'); // Downgrade the version to `2.8`, so disable Null Safety. - _changeTestFile(); + _changeFile(path); await assertNoErrorsInCode(''' // @dart = 2.8 var x = 0; @@ -233,11 +237,6 @@ var x = 0; assertType(findElement.topVar('x').type, 'int*'); } - void _changeTestFile() { - var path = convertPath('/test/lib/test.dart'); - driver.changeFile(path); - } - void _configureAllowedExperimentsTestNullSafety() { _newSdkExperimentsFile(r''' { @@ -265,14 +264,6 @@ var x = 0; content: content, ); } - - Future _resolveFile(String path, String content) async { - newFile(path, content: content); - result = await resolveFile(path); - - findNode = FindNode(result.content, result.unit); - findElement = FindElement(result.unit); - } } @reflectiveTest @@ -324,15 +315,20 @@ extension E on int {} } } -class _FeaturesTest extends DriverResolutionTest { - void _configureTestWithJsonConfig(String content) { - newFile('/test/.dart_tool/package_config.json', content: content); +class _FeaturesTest extends PubPackageResolutionTest { + /// Do necessary work to ensure that the file with the [path] is considered + /// changed for the purpose of following analysis. + /// + /// Currently we just dispose the whole analysis context collection, so when + /// we ask to analyze anything again, we will pick up the new file content. + void _changeFile(String path) { + disposeAnalysisContextCollection(); + } - driver.configure( - packages: findPackagesFrom( - resourceProvider, - getFolder('/test'), - ), + void _configureTestWithJsonConfig(String content) { + newFile( + '$testPackageRootPath/.dart_tool/package_config.json', + content: content, ); } } diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart index 1ac4382093f..2f784fbe4e0 100644 --- a/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart +++ b/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart @@ -2,13 +2,10 @@ // 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/file_system/file_system.dart'; import 'package:analyzer/src/error/codes.dart'; -import 'package:analyzer/src/test_utilities/package_mixin.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; -import '../../generated/test_support.dart'; -import '../dart/resolution/driver_resolution.dart'; +import '../dart/resolution/context_collection_resolution.dart'; main() { defineReflectiveSuite(() { @@ -17,116 +14,125 @@ main() { } @reflectiveTest -class SubtypeOfSealedClassTest extends DriverResolutionTest with PackageMixin { +class SubtypeOfSealedClassTest extends PubPackageResolutionTest { + @override + void setUp() { + super.setUp(); + writeTestPackageConfigWithMeta(); + } + test_extendingSealedClass() async { - addMetaPackage(); - _addPackage('foo', r''' + writeTestPackageConfigWith({ + 'foo': '$workspaceRootPath/foo', + }, meta: true); + + await newFile('$workspaceRootPath/foo/lib/foo.dart', content: r''' import 'package:meta/meta.dart'; @sealed class Foo {} '''); - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await assertErrorsInCode(r''' import 'package:foo/foo.dart'; class Bar extends Foo {} -'''); - await _resolveFile('/pkg1/lib/lib1.dart', [ +''', [ error(HintCode.SUBTYPE_OF_SEALED_CLASS, 31, 24), ]); } test_implementingSealedClass() async { - addMetaPackage(); - _addPackage('foo', r''' + writeTestPackageConfigWith({ + 'foo': '$workspaceRootPath/foo', + }, meta: true); + + newFile('$workspaceRootPath/foo/lib/foo.dart', content: r''' import 'package:meta/meta.dart'; @sealed class Foo {} '''); - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await assertErrorsInCode(r''' import 'package:foo/foo.dart'; class Bar implements Foo {} -'''); - await _resolveFile('/pkg1/lib/lib1.dart', [ +''', [ error(HintCode.SUBTYPE_OF_SEALED_CLASS, 31, 27), ]); } test_mixinApplicationOfSealedClass() async { - addMetaPackage(); - _addPackage('foo', r''' + writeTestPackageConfigWith({ + 'foo': '$workspaceRootPath/foo', + }, meta: true); + + newFile('$workspaceRootPath/foo/lib/foo.dart', content: r''' import 'package:meta/meta.dart'; @sealed class Foo {} '''); - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await assertErrorsInCode(r''' import 'package:foo/foo.dart'; class Bar1 {} class Bar2 = Bar1 with Foo; -'''); - await _resolveFile('/pkg1/lib/lib1.dart', [ +''', [ error(HintCode.SUBTYPE_OF_SEALED_CLASS, 45, 27), ]); } test_mixinApplicationOfSealedMixin() async { - addMetaPackage(); - _addPackage('foo', r''' + writeTestPackageConfigWith({ + 'foo': '$workspaceRootPath/foo', + }, meta: true); + + newFile('$workspaceRootPath/foo/lib/foo.dart', content: r''' import 'package:meta/meta.dart'; @sealed mixin Foo {} '''); - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await assertErrorsInCode(r''' import 'package:foo/foo.dart'; class Bar1 {} class Bar2 = Bar1 with Foo; -'''); - await _resolveFile('/pkg1/lib/lib1.dart', [ +''', [ error(HintCode.SUBTYPE_OF_SEALED_CLASS, 45, 27), ]); } test_mixingInWithSealedMixin() async { - addMetaPackage(); - _addPackage('foo', r''' + writeTestPackageConfigWith({ + 'foo': '$workspaceRootPath/foo', + }, meta: true); + + newFile('$workspaceRootPath/foo/lib/foo.dart', content: r''' import 'package:meta/meta.dart'; @sealed mixin Foo {} '''); - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await assertErrorsInCode(r''' import 'package:foo/foo.dart'; class Bar extends Object with Foo {} -'''); - await _resolveFile('/pkg1/lib/lib1.dart', [ +''', [ error(HintCode.SUBTYPE_OF_SEALED_CLASS, 31, 36), ]); } test_mixinImplementsSealedClass() async { - addMetaPackage(); - _addPackage('foo', r''' + writeTestPackageConfigWith({ + 'foo': '$workspaceRootPath/foo', + }, meta: true); + + newFile('$workspaceRootPath/foo/lib/foo.dart', content: r''' import 'package:meta/meta.dart'; @sealed class Foo {} '''); - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await assertErrorsInCode(r''' import 'package:foo/foo.dart'; mixin Bar implements Foo {} -'''); - await _resolveFile('/pkg1/lib/lib1.dart', [ +''', [ error(HintCode.SUBTYPE_OF_SEALED_CLASS, 31, 27), ]); } test_withinLibrary_OK() async { - addMetaPackage(); - - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await assertNoErrorsInCode(r''' import 'package:meta/meta.dart'; @sealed class Foo {} @@ -135,88 +141,63 @@ class Bar2 implements Foo {} class Bar4 = Bar1 with Foo; mixin Bar5 implements Foo {} '''); - await _resolveFile('/pkg1/lib/lib1.dart'); } test_withinPackageLibDirectory_OK() async { - addMetaPackage(); - - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await newFile('$testPackageLibPath/a.dart', content: r''' import 'package:meta/meta.dart'; @sealed class Foo {} '''); - newFile('/pkg1/lib/src/lib2.dart', content: r''' -import '../lib1.dart'; + + await resolveFileCode('$testPackageLibPath/src/b.dart', r''' +import '../a.dart'; class Bar1 extends Foo {} class Bar2 implements Foo {} class Bar4 = Bar1 with Foo; mixin Bar5 implements Foo {} '''); - await _resolveFile('/pkg1/lib/lib1.dart'); - await _resolveFile('/pkg1/lib/src/lib2.dart'); + assertNoErrorsInResult(); } test_withinPackageTestDirectory_OK() async { - addMetaPackage(); - - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + await newFile('$testPackageLibPath/a.dart', content: r''' import 'package:meta/meta.dart'; @sealed class Foo {} '''); - newFile('/pkg1/test/test.dart', content: r''' -import '../lib/lib1.dart'; + + await resolveFileCode('$testPackageRootPath/test/test.dart', r''' +import 'package:test/a.dart'; + class Bar1 extends Foo {} class Bar2 implements Foo {} class Bar4 = Bar1 with Foo; mixin Bar5 implements Foo {} '''); - await _resolveFile('/pkg1/lib/lib1.dart'); - await _resolveFile('/pkg1/test/test.dart'); + assertNoErrorsInResult(); } test_withinPart_OK() async { - addMetaPackage(); + var libPath = '$testPackageLibPath/a.dart'; + var partPath = '$testPackageLibPath/b.dart'; - _newPubPackageRoot('/pkg1'); - newFile('/pkg1/lib/lib1.dart', content: r''' + newFile(libPath, content: r''' import 'package:meta/meta.dart'; -part 'part1.dart'; +part 'b.dart'; @sealed class Foo {} '''); - newFile('/pkg1/lib/part1.dart', content: r''' -part of 'lib1.dart'; + + await newFile(partPath, content: r''' +part of 'a.dart'; class Bar1 extends Foo {} class Bar2 implements Foo {} class Bar4 = Bar1 with Foo; mixin Bar5 implements Foo {} '''); - await _resolveFile('/pkg1/lib/lib1.dart'); - } - /// Add a package named [name], and one library file, with content - /// [libraryContent]. - void _addPackage(String name, String libraryContent) { - Folder lib = addPubPackage(name); - newFile(join(lib.path, '$name.dart'), content: libraryContent); - } + await resolveFile2(libPath); + assertNoErrorsInResult(); - /// Write a pubspec file at [root], so that BestPracticesVerifier can see - /// that [root] is the root of a PubWorkspace, and a PubWorkspacePackage. - void _newPubPackageRoot(String root) { - newFile('$root/pubspec.yaml'); - configureWorkspace(root: root); - } - - /// Resolve the file with the given [path]. - /// - /// Similar to ResolutionTest.resolveTestFile, but a custom path is supported. - Future _resolveFile( - String path, [ - List expectedErrors = const [], - ]) async { - result = await resolveFile(convertPath(path)); - assertErrorsInResolvedUnit(result, expectedErrors); + await resolveFile2(partPath); + assertNoErrorsInResult(); } }