From 054d90537476318f2ce542ec48093546e766c537 Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Tue, 24 Mar 2015 02:24:05 +0300 Subject: [PATCH] add NavigateToStdlibSourceTest --- .../navigation/NavigateToStdlibSourceTest.kt | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt new file mode 100644 index 00000000000..6b796c936b3 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/NavigateToStdlibSourceTest.kt @@ -0,0 +1,82 @@ +/* + * 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.openapi.application.ApplicationManager +import com.intellij.psi.PsiElement +import com.intellij.testFramework.LightPlatformTestCase +import junit.framework.TestCase +import org.jetbrains.kotlin.idea.JetFileType +import org.jetbrains.kotlin.idea.KotlinCodeInsightTestCase +import org.jetbrains.kotlin.idea.testUtils.ModuleKind +import org.jetbrains.kotlin.idea.testUtils.configureAs + +public class NavigateToStdlibSourceTest : KotlinCodeInsightTestCase() { + + private val FILE_TEXT = "fun foo() { println() }" + + public fun testRefToPrintlnWithJVM() { + doTest("Console.kt", ModuleKind.KOTLIN_JVM_WITH_STDLIB_SOURCES) + } + + public fun testRefToPrintlnWithJVMAndJS() { + doTest("Console.kt", ModuleKind.KOTLIN_JVM_WITH_STDLIB_SOURCES, ModuleKind.KOTLIN_JAVASCRIPT) + } + + public fun testRefToPrintlnWithJS() { + doTest("core.kt", ModuleKind.KOTLIN_JAVASCRIPT) + } + + public fun testRefToPrintlnWithJSAndJVM() { + doTest("core.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.getContainingFile().getName()) + } + + override fun tearDown() { + // Copied verbatim from NavigateToStdlibSourceRegressionTest. + // 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 + super.tearDown() + ApplicationManager.getApplication().runWriteAction(object : Runnable { + override fun run() { + LightPlatformTestCase.closeAndDeleteProject() + } + }) + } + + protected fun configureAndResolve( + text: String, + mainModuleKind: ModuleKind, + additionalModuleKind: ModuleKind? = null + ): PsiElement { + configureByText(JetFileType.INSTANCE, text) + getModule().configureAs(mainModuleKind) + + if (additionalModuleKind != null) { + val additionalModule = this.createModule("additional-module") + additionalModule.configureAs(additionalModuleKind) + } + + val ref = getFile().findReferenceAt(getEditor().getCaretModel().getOffset()) + return ref!!.resolve()!!.getNavigationElement() + } +} \ No newline at end of file