From 60e3787800d3ec94b5750f8bd3c861e83a5fe8d8 Mon Sep 17 00:00:00 2001 From: Vadim Brilyantov Date: Wed, 26 Jun 2019 18:44:33 +0300 Subject: [PATCH] Tests for Sealed Class move + Fix invalid move declaration test MoveDeclarationToSeparateFile.testSealed test was threated to check that the intention is not applicable but it currently is. --- .../jetbrains/kotlin/idea/roots/projectRootUtils.kt | 10 ++++------ .../intentions/moveDeclarationToSeparateFile/sealed.kt | 2 +- .../moveDeclarationToSeparateFile/sealed.kt.after | 1 + .../after/bar/SealedClass.kt | 5 +++++ .../after/foo/KotlinReferences.kt | 5 +++++ .../after/foo/SealedClass.kt | 5 +++++ .../before/foo/KotlinReferences.kt | 3 +++ .../before/foo/SealedClass.kt | 7 +++++++ .../conflicts.txt | 1 + ...moveSealedClassWithNestedImplsToAnotherPackage.test | 5 +++++ .../after/bar/SealedClass.kt | 6 ++++++ .../after/foo/KotlinReferences.kt | 5 +++++ .../after/foo/SealedClass.kt | 2 ++ .../before/foo/KotlinReferences.kt | 3 +++ .../before/foo/SealedClass.kt | 6 ++++++ ...moveSealedClassWithNestedImplsToAnotherPackage.test | 5 +++++ .../idea/refactoring/move/MoveTestGenerated.java | 10 ++++++++++ 17 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt.after create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/bar/SealedClass.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/KotlinReferences.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/SealedClass.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/KotlinReferences.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/SealedClass.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/conflicts.txt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/bar/SealedClass.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/KotlinReferences.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/SealedClass.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/KotlinReferences.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/SealedClass.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test diff --git a/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt b/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt index ce189dd532f..102e01cc7d9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt @@ -73,12 +73,10 @@ private fun Module.collectKotlinAwareDestinationSourceRoots(): List } fun isOutsideSourceRootSet(psiFile : PsiFile?, sourceRootTypes: Set>): Boolean { - if (psiFile == null) return false - if (psiFile is PsiCodeFragment) return false - val file = psiFile.getVirtualFile() - if (file == null) return false - if (file.getFileSystem() is NonPhysicalFileSystem) return false - val projectFileIndex = ProjectRootManager.getInstance(psiFile.getProject()).getFileIndex() + if (psiFile == null || psiFile is PsiCodeFragment) return false + val file = psiFile.virtualFile ?: return false + if (file.fileSystem is NonPhysicalFileSystem) return false + val projectFileIndex = ProjectRootManager.getInstance(psiFile.project).fileIndex return !projectFileIndex.isUnderSourceRootOfType(file, sourceRootTypes) && !projectFileIndex.isInLibrary(file) } diff --git a/idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt b/idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt index e20cef61e18..12f3634fc4f 100644 --- a/idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt +++ b/idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt @@ -1,3 +1,3 @@ -// IS_APPLICABLE: false +// INTENTION_TEXT: Extract 'A' and subclasses from current file sealed class A class B : A() \ No newline at end of file diff --git a/idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt.after b/idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt.after new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/idea/testData/intentions/moveDeclarationToSeparateFile/sealed.kt.after @@ -0,0 +1 @@ + diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/bar/SealedClass.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/bar/SealedClass.kt new file mode 100644 index 00000000000..2994bf4dd8f --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/bar/SealedClass.kt @@ -0,0 +1,5 @@ +package bar + +public sealed class SealedClass { + public class Impl1 : SealedClass() {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/KotlinReferences.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/KotlinReferences.kt new file mode 100644 index 00000000000..a5105b77f32 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/KotlinReferences.kt @@ -0,0 +1,5 @@ +package foo + +import bar.SealedClass + +val v = SealedClass::Impl1 \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/SealedClass.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/SealedClass.kt new file mode 100644 index 00000000000..18534fbbc34 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/after/foo/SealedClass.kt @@ -0,0 +1,5 @@ +package foo + +import bar.SealedClass + +public class Impl2 : SealedClass() {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/KotlinReferences.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/KotlinReferences.kt new file mode 100644 index 00000000000..84b14682a40 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/KotlinReferences.kt @@ -0,0 +1,3 @@ +package foo + +val v = SealedClass::Impl1 \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/SealedClass.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/SealedClass.kt new file mode 100644 index 00000000000..f7e9572c67b --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/before/foo/SealedClass.kt @@ -0,0 +1,7 @@ +package foo + +public sealed class SealedClass { + public class Impl1 : SealedClass() {} +} + +public class Impl2 : SealedClass() {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/conflicts.txt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/conflicts.txt new file mode 100644 index 00000000000..4e6bd3f2df2 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/conflicts.txt @@ -0,0 +1 @@ +Sealed class 'SealedClass' must be moved with all its subclasses diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test new file mode 100644 index 00000000000..75e06c3b8e6 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test @@ -0,0 +1,5 @@ +{ + "mainFile": "foo/SealedClass.kt", + "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", + "targetPackage": "bar" +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/bar/SealedClass.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/bar/SealedClass.kt new file mode 100644 index 00000000000..33ddc88de7d --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/bar/SealedClass.kt @@ -0,0 +1,6 @@ +package bar + +public sealed class SealedClass { + public class Impl1 : SealedClass() {} + public class Impl2 : SealedClass() {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/KotlinReferences.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/KotlinReferences.kt new file mode 100644 index 00000000000..a5105b77f32 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/KotlinReferences.kt @@ -0,0 +1,5 @@ +package foo + +import bar.SealedClass + +val v = SealedClass::Impl1 \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/SealedClass.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/SealedClass.kt new file mode 100644 index 00000000000..d1b1429cb4d --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/after/foo/SealedClass.kt @@ -0,0 +1,2 @@ +package foo + diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/KotlinReferences.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/KotlinReferences.kt new file mode 100644 index 00000000000..84b14682a40 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/KotlinReferences.kt @@ -0,0 +1,3 @@ +package foo + +val v = SealedClass::Impl1 \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/SealedClass.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/SealedClass.kt new file mode 100644 index 00000000000..8a22546ef7b --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/before/foo/SealedClass.kt @@ -0,0 +1,6 @@ +package foo + +public sealed class SealedClass { + public class Impl1 : SealedClass() {} + public class Impl2 : SealedClass() {} +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test new file mode 100644 index 00000000000..75e06c3b8e6 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test @@ -0,0 +1,5 @@ +{ + "mainFile": "foo/SealedClass.kt", + "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", + "targetPackage": "bar" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java index 965c1357784..87fc8bab4c3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java @@ -739,6 +739,16 @@ public class MoveTestGenerated extends AbstractMoveTest { runTest("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/movePropertyToPackage/movePropertyToPackage.test"); } + @TestMetadata("kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test") + public void testKotlin_moveTopLevelDeclarations_moveSealedClassWithImplsToAnotherPackage_MoveSealedClassWithNestedImplsToAnotherPackage() throws Exception { + runTest("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test"); + } + + @TestMetadata("kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test") + public void testKotlin_moveTopLevelDeclarations_moveSealedClassWithNestedImplsToAnotherPackage_MoveSealedClassWithNestedImplsToAnotherPackage() throws Exception { + runTest("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/moveSealedClassWithNestedImplsToAnotherPackage/moveSealedClassWithNestedImplsToAnotherPackage.test"); + } + @TestMetadata("kotlin/moveTopLevelDeclarations/objectAlreadyInaccessible/objectAlreadyInaccessible.test") public void testKotlin_moveTopLevelDeclarations_objectAlreadyInaccessible_ObjectAlreadyInaccessible() throws Exception { runTest("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/objectAlreadyInaccessible/objectAlreadyInaccessible.test");