From d8fda8b1534f9cb6198ea6ffe87aeb184dda4b39 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 12 Oct 2018 12:41:09 +0300 Subject: [PATCH] Tests for navigation to stdlib multiplatform sources (KT-26004) #KT-26004 Fixed --- ...tlinJdkAndMultiplatformStdlibDescriptor.kt | 60 +++++++++++++++++++ .../KotlinLightCodeInsightFixtureTestCase.kt | 15 ++++- ...tlinLightCodeInsightFixtureTestCase.kt.173 | 10 ++++ .../idea/test/projectDescriptorAnnotation.kt | 12 ++++ .../LightNavigateToStdlibSourceTest.kt | 42 +++++++++++++ 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinJdkAndMultiplatformStdlibDescriptor.kt create mode 100644 idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/projectDescriptorAnnotation.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinJdkAndMultiplatformStdlibDescriptor.kt b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinJdkAndMultiplatformStdlibDescriptor.kt new file mode 100644 index 00000000000..6172193db32 --- /dev/null +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinJdkAndMultiplatformStdlibDescriptor.kt @@ -0,0 +1,60 @@ +/* + * 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.module.ModuleType +import com.intellij.openapi.module.StdModuleTypes +import com.intellij.openapi.projectRoots.Sdk +import com.intellij.openapi.roots.ModifiableRootModel +import com.intellij.openapi.roots.OrderRootType +import com.intellij.openapi.roots.libraries.Library +import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor +import com.intellij.openapi.vfs.VfsUtil +import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime +import java.io.File + +class KotlinJdkAndMultiplatformStdlibDescriptor private constructor(private val withSources: Boolean) : KotlinLightProjectDescriptor() { + override fun getModuleType(): ModuleType<*> = StdModuleTypes.JAVA + + override fun getSdk(): Sdk? = PluginTestCaseBase.mockJdk8() + + override fun configureModule(module: Module, model: ModifiableRootModel) { + model.addLib( + STDLIB_COMMON_LIB_NAME, + withSources, + ForTestCompileRuntime.stdlibCommonForTests(), + ForTestCompileRuntime.stdlibCommonSourcesForTests() + ) + + model.addLib( + STDLIB_LIB_NAME, + withSources, + ForTestCompileRuntime.runtimeJarForTests(), + ForTestCompileRuntime.stdlibMavenSourcesJarForTests() + ) + } + + companion object { + val JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES = KotlinJdkAndMultiplatformStdlibDescriptor(true) + + private fun ModifiableRootModel.addLib(name: String, withSources: Boolean, classesJar: File, sourcesJar: File?): Library { + val editor = NewLibraryEditor().apply { + this.name = name + addRoot(VfsUtil.getUrlForLibraryRoot(classesJar), OrderRootType.CLASSES) + + if (withSources && sourcesJar != null) { + addRoot(VfsUtil.getUrlForLibraryRoot(sourcesJar), OrderRootType.SOURCES) + } + } + + return ConfigLibraryUtil.addLibrary(editor, this) + } + + private const val STDLIB_COMMON_LIB_NAME = "stdlib-common" + private const val STDLIB_LIB_NAME = "stdlib" + } +} \ No newline at end of file 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 dbe61533043..f1ccc541ce5 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 @@ -5,7 +5,6 @@ package org.jetbrains.kotlin.idea.test -import com.intellij.codeInsight.CodeInsightTestCase import com.intellij.codeInsight.daemon.impl.EditorTracker import com.intellij.ide.highlighter.JavaFileType import com.intellij.ide.startup.impl.StartupManagerImpl @@ -31,7 +30,9 @@ import com.intellij.testFramework.LightProjectDescriptor import com.intellij.testFramework.LoggedErrorProcessor import org.apache.log4j.Logger import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments -import org.jetbrains.kotlin.config.* +import org.jetbrains.kotlin.config.CompilerSettings +import org.jetbrains.kotlin.config.LanguageFeature +import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.idea.KotlinFileType import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCommonCompilerArgumentsHolder import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerSettings @@ -94,6 +95,16 @@ abstract class KotlinLightCodeInsightFixtureTestCase : KotlinLightCodeInsightFix override fun getProjectDescriptor(): LightProjectDescriptor = getProjectDescriptorFromFileDirective() + protected fun getProjectDescriptorFromAnnotation(): LightProjectDescriptor { + val testMethod = this::class.java.getDeclaredMethod(name) + val platformId = testMethod.getAnnotation(ProjectDescriptorKind::class.java)?.value + + return when (platformId) { + JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES -> KotlinJdkAndMultiplatformStdlibDescriptor.JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES + else -> throw IllegalStateException("Unknown value for project descriptor kind") + } + } + protected fun getProjectDescriptorFromTestName(): LightProjectDescriptor { val testName = StringUtil.toLowerCase(getTestName(false)) diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt.173 b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt.173 index 57f2adb1e9e..17785a729f3 100644 --- a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt.173 +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt.173 @@ -95,6 +95,16 @@ abstract class KotlinLightCodeInsightFixtureTestCase : KotlinLightCodeInsightFix override fun getProjectDescriptor(): LightProjectDescriptor = getProjectDescriptorFromFileDirective() + protected fun getProjectDescriptorFromAnnotation(): LightProjectDescriptor { + val testMethod = this::class.java.getDeclaredMethod(name) + val platformId = testMethod.getAnnotation(ProjectDescriptorKind::class.java)?.value + + return when (platformId) { + JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES -> KotlinJdkAndMultiplatformStdlibDescriptor.JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES + else -> throw IllegalStateException("Unknown value for project descriptor kind") + } + } + protected fun getProjectDescriptorFromTestName(): LightProjectDescriptor { val testName = StringUtil.toLowerCase(getTestName(false)) 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 new file mode 100644 index 00000000000..29a48a08deb --- /dev/null +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/projectDescriptorAnnotation.kt @@ -0,0 +1,12 @@ +/* + * 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 + +@Retention(AnnotationRetention.RUNTIME) +@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 diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt new file mode 100644 index 00000000000..8317f3d066a --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/LightNavigateToStdlibSourceTest.kt @@ -0,0 +1,42 @@ +/* + * 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.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 + +class LightNavigateToStdlibSourceTest : KotlinLightCodeInsightFixtureTestCase() { + @ProjectDescriptorKind(JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES) + fun testNavigateToCommonDeclarationWhenPlatformSpecificOverloadAvailable() { + doTest( + "fun some() { mapOf(1 to 2, 3 to 4) }", + "Maps.kt" + ) + } + + @ProjectDescriptorKind(JDK_AND_MULTIPLATFORM_STDLIB_WITH_SOURCES) + fun testNavigateToJVMSpecificationWithoutExpectActual() { + doTest( + "fun some() { mapOf(1 to 2) }", + "MapsJVM.kt" + ) + } + + override fun getProjectDescriptor() = getProjectDescriptorFromAnnotation() + + private fun doTest(text: String, sourceFileName: String) { + myFixture.configureByText(KotlinFileType.INSTANCE, text) + + val ref = file.findReferenceAt(editor.caretModel.offset) + val resolve = ref!!.resolve() + val navigationElement = resolve!!.navigationElement + + TestCase.assertEquals(sourceFileName, navigationElement.containingFile.name) + } +} \ No newline at end of file