From 8a63a1655fdabb61617467097909c2e8f99b2c96 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 30 Nov 2018 18:44:33 +0300 Subject: [PATCH] Rewrite NavigateToStdlibSourceTest with project descriptors --- .../KotlinLightCodeInsightFixtureTestCase.kt | 21 +++++ .../KotlinMultiModuleProjectDescriptor.kt | 43 ++++++++++ .../idea/test/projectDescriptorAnnotation.kt | 10 ++- .../LightNavigateToStdlibSourceTest.kt | 33 +++++++- .../navigation/NavigateToStdlibSourceTest.kt | 80 ------------------- 5 files changed, 103 insertions(+), 84 deletions(-) create mode 100644 idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinMultiModuleProjectDescriptor.kt delete mode 100644 idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt index 2c7269c8382..79b650b944f 100644 --- a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt @@ -112,6 +112,27 @@ abstract class KotlinLightCodeInsightFixtureTestCase : KotlinLightCodeInsightFix return when (platformId) { JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES -> KotlinJdkAndMultiplatformStdlibDescriptor.JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES + + KOTLIN_JVM_WITH_STDLIB_SOURCES -> ProjectDescriptorWithStdlibSources.INSTANCE + + KOTLIN_JAVASCRIPT -> KotlinStdJSProjectDescriptor + + KOTLIN_JVM_WITH_STDLIB_SOURCES_WITH_ADDITIONAL_JS -> { + KotlinMultiModuleProjectDescriptor( + KOTLIN_JVM_WITH_STDLIB_SOURCES_WITH_ADDITIONAL_JS, + mainModuleDescriptor = ProjectDescriptorWithStdlibSources.INSTANCE, + additionalModuleDescriptor = KotlinStdJSProjectDescriptor + ) + } + + KOTLIN_JAVASCRIPT_WITH_ADDITIONAL_JVM_WITH_STDLIB -> { + KotlinMultiModuleProjectDescriptor( + KOTLIN_JAVASCRIPT_WITH_ADDITIONAL_JVM_WITH_STDLIB, + mainModuleDescriptor = KotlinStdJSProjectDescriptor, + additionalModuleDescriptor = ProjectDescriptorWithStdlibSources.INSTANCE + ) + } + else -> throw IllegalStateException("Unknown value for project descriptor kind") } } diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinMultiModuleProjectDescriptor.kt b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinMultiModuleProjectDescriptor.kt new file mode 100644 index 00000000000..307a3b4315b --- /dev/null +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinMultiModuleProjectDescriptor.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.test + +import com.intellij.openapi.module.Module +import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.ModifiableRootModel + +class KotlinMultiModuleProjectDescriptor( + val id: String, + private val mainModuleDescriptor: KotlinLightProjectDescriptor, + private val additionalModuleDescriptor: KotlinLightProjectDescriptor +) : KotlinLightProjectDescriptor() { + lateinit var additionalModule: Module + + override fun setUpProject(project: Project, handler: SetupHandler) { + super.setUpProject(project, handler) + + additionalModule = additionalModuleDescriptor.createMainModule(project) + } + + override fun configureModule(module: Module, model: ModifiableRootModel) { + mainModuleDescriptor.configureModule(module, model) + } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as KotlinMultiModuleProjectDescriptor + + if (id != other.id) return false + + return true + } + + override fun hashCode(): Int { + return id.hashCode() + } +} \ No newline at end of file diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/projectDescriptorAnnotation.kt b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/projectDescriptorAnnotation.kt index 29a48a08deb..b3bdf0d82b7 100644 --- a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/projectDescriptorAnnotation.kt +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/projectDescriptorAnnotation.kt @@ -9,4 +9,12 @@ package org.jetbrains.kotlin.idea.test @Target(AnnotationTarget.FUNCTION) annotation class ProjectDescriptorKind(val value: String) -const val JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES = "JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES" \ No newline at end of file +const val JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES = "JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES" + +const val KOTLIN_JVM_WITH_STDLIB_SOURCES = "KOTLIN_JVM_WITH_STDLIB_SOURCES" + +const val KOTLIN_JAVASCRIPT = "KOTLIN_JAVASCRIPT" + +const val KOTLIN_JVM_WITH_STDLIB_SOURCES_WITH_ADDITIONAL_JS = "KOTLIN_JVM_WITH_STDLIB_SOURCES_WITH_ADDITIONAL_JS" + +const val KOTLIN_JAVASCRIPT_WITH_ADDITIONAL_JVM_WITH_STDLIB = "KOTLIN_JAVASCRIPT_WITH_ADDITIONAL_JVM_WITH_STDLIB" \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt index 8317f3d066a..9543f0f3647 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt @@ -7,9 +7,7 @@ package org.jetbrains.kotlin.idea.decompiler.navigation import junit.framework.TestCase import org.jetbrains.kotlin.idea.KotlinFileType -import org.jetbrains.kotlin.idea.test.JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES -import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase -import org.jetbrains.kotlin.idea.test.ProjectDescriptorKind +import org.jetbrains.kotlin.idea.test.* class LightNavigateToStdlibSourceTest : KotlinLightCodeInsightFixtureTestCase() { @ProjectDescriptorKind(JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES) @@ -28,6 +26,35 @@ class LightNavigateToStdlibSourceTest : KotlinLightCodeInsightFixtureTestCase() ) } + @ProjectDescriptorKind(KOTLIN_JVM_WITH_STDLIB_SOURCES) + fun testRefToPrintlnWithJVM() { + doTest( + "fun foo() { println() }", + "Console.kt") + } + + @ProjectDescriptorKind(KOTLIN_JAVASCRIPT) + fun testRefToPrintlnWithJS() { + doTest( + "fun foo() { println() }", + "console.kt" + ) + } + + @ProjectDescriptorKind(KOTLIN_JVM_WITH_STDLIB_SOURCES_WITH_ADDITIONAL_JS) + fun testRefToPrintlnWithJVMAndJS() { + doTest( + "fun foo() { println() }", + "Console.kt") + } + + @ProjectDescriptorKind(KOTLIN_JAVASCRIPT_WITH_ADDITIONAL_JVM_WITH_STDLIB) + fun testRefToPrintlnWithJSAndJVM() { + doTest( + "fun foo() { println() }", + "console.kt") + } + override fun getProjectDescriptor() = getProjectDescriptorFromAnnotation() private fun doTest(text: String, sourceFileName: String) { diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt deleted file mode 100644 index 7f970c44eb0..00000000000 --- a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.idea.decompiler.navigation - -import com.intellij.psi.PsiElement -import junit.framework.TestCase -import org.jetbrains.kotlin.idea.KotlinFileType -import org.jetbrains.kotlin.idea.test.* - -class NavigateToStdlibSourceTest : KotlinCodeInsightTestCase() { - - private val FILE_TEXT = "fun foo() { println() }" - - fun testRefToPrintlnWithJVM() { - doTest("Console.kt", ModuleKind.KOTLIN_JVM_WITH_STDLIB_SOURCES) - } - - fun testRefToPrintlnWithJVMAndJS() { - doTest("Console.kt", ModuleKind.KOTLIN_JVM_WITH_STDLIB_SOURCES, ModuleKind.KOTLIN_JAVASCRIPT) - } - - fun testRefToPrintlnWithJS() { - doTest("console.kt", ModuleKind.KOTLIN_JAVASCRIPT) - } - - fun testRefToPrintlnWithJSAndJVM() { - doTest("console.kt", ModuleKind.KOTLIN_JAVASCRIPT, ModuleKind.KOTLIN_JVM_WITH_STDLIB_SOURCES) - } - - private fun doTest(sourceFileName: String, mainModule: ModuleKind, additionalModule: ModuleKind? = null) { - val navigationElement = configureAndResolve(FILE_TEXT, mainModule, additionalModule) - TestCase.assertEquals(sourceFileName, navigationElement.containingFile.name) - } - - override fun setUp() { - super.setUp() - - PluginTestCaseBase.addJdk(testRootDisposable) { ProjectDescriptorWithStdlibSources.INSTANCE.sdk } - } - - override fun tearDown() { - super.tearDown() - // Workaround for IDEA's bug during tests. - // After tests IDEA disposes VirtualFiles within LocalFileSystem, but doesn't rebuild indices. - // This causes library source files to be impossible to find via indices - closeAndDeleteProject() - } - - protected fun configureAndResolve( - text: String, - mainModuleKind: ModuleKind, - additionalModuleKind: ModuleKind? = null - ): PsiElement { - module.configureAs(mainModuleKind) - if (additionalModuleKind != null) { - val additionalModule = this.createModule("additional-module") - additionalModule.configureAs(additionalModuleKind) - } - - configureByText(KotlinFileType.INSTANCE, text) - - val ref = file.findReferenceAt(editor.caretModel.offset) - val resolve = ref!!.resolve() - return resolve!!.navigationElement - } -} \ No newline at end of file