diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCaseBase.java.192 b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCaseBase.java.192 new file mode 100644 index 00000000000..7f983233c29 --- /dev/null +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCaseBase.java.192 @@ -0,0 +1,97 @@ +/* + * Copyright 2010-2016 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.test; + +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import com.intellij.testFramework.TempFiles; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; +import gnu.trove.THashSet; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.util.Collection; + +public abstract class KotlinLightCodeInsightFixtureTestCaseBase extends LightCodeInsightFixtureTestCase { + @NotNull + @Override + public Project getProject() { + return super.getProject(); + } + + @NotNull + @Override + public Editor getEditor() { + return super.getEditor(); + } + + @Override + public PsiFile getFile() { + return super.getFile(); + } + + protected final Collection myFilesToDelete = new THashSet<>(); + private final TempFiles myTempFiles = new TempFiles(myFilesToDelete); + + @Override + protected void tearDown() throws Exception { + myTempFiles.deleteAll(); + super.tearDown(); + } + + @NotNull + public VirtualFile createTempFile( + @NonNls @NotNull String ext, + @Nullable byte[] bom, + @NonNls @NotNull String content, + @NotNull Charset charset + ) throws IOException { + File temp = FileUtil.createTempFile("copy", "." + ext); + setContentOnDisk(temp, bom, content, charset); + + myFilesToDelete.add(temp); + final VirtualFile file = getVirtualFile(temp); + assert file != null : temp; + return file; + } + + public static void setContentOnDisk(@NotNull File file, @Nullable byte[] bom, @NotNull String content, @NotNull Charset charset) + throws IOException { + FileOutputStream stream = new FileOutputStream(file); + if (bom != null) { + stream.write(bom); + } + try (OutputStreamWriter writer = new OutputStreamWriter(stream, charset)) { + writer.write(content); + } + } + + protected static VirtualFile getVirtualFile(@NotNull File file) { + return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file); + } + +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/AbstractNavigateFromLibrarySourcesTest.kt.192 b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/AbstractNavigateFromLibrarySourcesTest.kt.192 new file mode 100644 index 00000000000..d7a9b1353a1 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/AbstractNavigateFromLibrarySourcesTest.kt.192 @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * 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 com.intellij.openapi.roots.LibraryOrderEntry +import com.intellij.openapi.roots.ModuleRootManager +import com.intellij.openapi.roots.OrderRootType +import com.intellij.openapi.vfs.VirtualFileManager +import com.intellij.psi.PsiElement +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.utils.sure + +abstract class AbstractNavigateFromLibrarySourcesTest : LightCodeInsightFixtureTestCase() { + protected fun navigationElementForReferenceInLibrarySource(filePath: String, referenceText: String): PsiElement { + val libraryOrderEntry = ModuleRootManager.getInstance(module!!).orderEntries.first { it is LibraryOrderEntry } + val libSourcesRoot = libraryOrderEntry.getUrls(OrderRootType.SOURCES)[0] + val libUrl = "$libSourcesRoot/$filePath" + val vf = VirtualFileManager.getInstance().refreshAndFindFileByUrl(libUrl) + ?: error("Can't find library: $libUrl") + val psiFile = psiManager.findFile(vf)!! + val indexOf = psiFile.text!!.indexOf(referenceText) + val reference = psiFile.findReferenceAt(indexOf) + return reference.sure { "Couldn't find reference" }.resolve().sure { "Couldn't resolve reference" }.navigationElement!! + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/stubBuilder/ClsStubForWrongAbiVersionTest.kt.192 b/idea/tests/org/jetbrains/kotlin/idea/decompiler/stubBuilder/ClsStubForWrongAbiVersionTest.kt.192 new file mode 100644 index 00000000000..978ac733ebd --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/stubBuilder/ClsStubForWrongAbiVersionTest.kt.192 @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * 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.stubBuilder + +import com.intellij.testFramework.LightProjectDescriptor +import org.jetbrains.kotlin.idea.decompiler.textBuilder.findTestLibraryRoot +import org.jetbrains.kotlin.idea.test.KotlinJdkAndLibraryProjectDescriptor +import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner +import org.jetbrains.kotlin.test.KotlinTestUtils +import org.junit.runner.RunWith +import java.io.File + +@RunWith(JUnit3WithIdeaConfigurationRunner::class) +class ClsStubBuilderForWrongAbiVersionTest : AbstractClsStubBuilderTest() { + + fun testPackage() = testStubsForFileWithWrongAbiVersion("Wrong_packageKt") + + fun testClass() = testStubsForFileWithWrongAbiVersion("ClassWithWrongAbiVersion") + + private fun testStubsForFileWithWrongAbiVersion(className: String) { + val root = findTestLibraryRoot(module!!)!! + val result = root.findClassFileByName(className) + testClsStubsForFile(result, null) + } + + override fun getProjectDescriptor(): LightProjectDescriptor { + return KotlinJdkAndLibraryProjectDescriptor(File(KotlinTestUtils.getTestDataPathBase() + "/cli/jvm/wrongAbiVersionLib/bin")) + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/UpdateConfigurationQuickFixTest.kt.192 b/idea/tests/org/jetbrains/kotlin/idea/quickfix/UpdateConfigurationQuickFixTest.kt.192 new file mode 100644 index 00000000000..f6a522bf125 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/UpdateConfigurationQuickFixTest.kt.192 @@ -0,0 +1,209 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * 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.quickfix + +import com.intellij.facet.FacetManager +import com.intellij.facet.impl.FacetUtil +import com.intellij.openapi.roots.ModuleRootModificationUtil.updateModel +import com.intellij.openapi.roots.OrderRootType +import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor +import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.vfs.JarFileSystem +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.config.LanguageFeature +import org.jetbrains.kotlin.config.LanguageVersion +import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCommonCompilerArgumentsHolder +import org.jetbrains.kotlin.idea.configuration.KotlinJavaModuleConfigurator +import org.jetbrains.kotlin.idea.facet.KotlinFacetType +import org.jetbrains.kotlin.idea.facet.getRuntimeLibraryVersion +import org.jetbrains.kotlin.idea.project.getLanguageVersionSettings +import org.jetbrains.kotlin.idea.project.languageVersionSettings +import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil +import org.jetbrains.kotlin.idea.test.configureKotlinFacet +import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion +import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner +import org.junit.runner.RunWith +import java.io.File + +@RunWith(JUnit3WithIdeaConfigurationRunner::class) +class UpdateConfigurationQuickFixTest : LightPlatformCodeInsightFixtureTestCase() { + fun testDisableInlineClasses() { + configureRuntime("mockRuntime11") + resetProjectSettings(LanguageVersion.KOTLIN_1_3) + myFixture.configureByText("foo.kt", "inline class My(val n: Int)") + + assertEquals(LanguageFeature.State.ENABLED_WITH_WARNING, inlineClassesSupport) + myFixture.launchAction(myFixture.findSingleIntention("Disable inline classes support in the project")) + assertEquals(LanguageFeature.State.DISABLED, inlineClassesSupport) + } + + fun testEnableCoroutines() { + configureRuntime("mockRuntime11") + resetProjectSettings(LanguageVersion.KOTLIN_1_1) + myFixture.configureByText("foo.kt", "suspend fun foo()") + + assertEquals(CommonCompilerArguments.WARN, KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.coroutinesState) + assertEquals(LanguageFeature.State.ENABLED_WITH_WARNING, coroutineSupport) + myFixture.launchAction(myFixture.findSingleIntention("Enable coroutine support in the project")) + assertEquals(LanguageFeature.State.ENABLED, coroutineSupport) + } + + fun testDisableCoroutines() { + configureRuntime("mockRuntime11") + resetProjectSettings(LanguageVersion.KOTLIN_1_1) + myFixture.configureByText("foo.kt", "suspend fun foo()") + + assertEquals(CommonCompilerArguments.WARN, KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.coroutinesState) + assertEquals(LanguageFeature.State.ENABLED_WITH_WARNING, coroutineSupport) + myFixture.launchAction(myFixture.findSingleIntention("Disable coroutine support in the project")) + assertEquals(LanguageFeature.State.ENABLED_WITH_ERROR, coroutineSupport) + } + + fun testEnableCoroutinesFacet() { + configureRuntime("mockRuntime11") + val facet = configureKotlinFacet(module) { + settings.languageLevel = LanguageVersion.KOTLIN_1_1 + } + resetProjectSettings(LanguageVersion.KOTLIN_1_1) + myFixture.configureByText("foo.kt", "suspend fun foo()") + + assertEquals(LanguageFeature.State.ENABLED_WITH_WARNING, facet.configuration.settings.coroutineSupport) + myFixture.launchAction(myFixture.findSingleIntention("Enable coroutine support in the current module")) + assertEquals(LanguageFeature.State.ENABLED, facet.configuration.settings.coroutineSupport) + } + + fun testEnableCoroutines_UpdateRuntime() { + configureRuntime("mockRuntime106") + resetProjectSettings(LanguageVersion.KOTLIN_1_1) + myFixture.configureByText("foo.kt", "suspend fun foo()") + + assertEquals(LanguageFeature.State.ENABLED_WITH_WARNING, coroutineSupport) + myFixture.launchAction(myFixture.findSingleIntention("Enable coroutine support in the project")) + assertEquals(LanguageFeature.State.ENABLED, coroutineSupport) + assertEquals(bundledRuntimeVersion(), getRuntimeLibraryVersion(myFixture.module)) + } + + fun testIncreaseLangLevel() { + configureRuntime("mockRuntime11") + resetProjectSettings(LanguageVersion.KOTLIN_1_0) + myFixture.configureByText("foo.kt", "val x get() = 1") + + myFixture.launchAction(myFixture.findSingleIntention("Set project language version to 1.1")) + + assertEquals("1.1", KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.languageVersion) + assertEquals("1.0", KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.apiVersion) + } + + fun testIncreaseLangLevelFacet() { + configureRuntime("mockRuntime11") + resetProjectSettings(LanguageVersion.KOTLIN_1_0) + configureKotlinFacet(module) { + settings.languageLevel = LanguageVersion.KOTLIN_1_0 + settings.apiLevel = LanguageVersion.KOTLIN_1_0 + } + myFixture.configureByText("foo.kt", "val x get() = 1") + + assertEquals(LanguageVersion.KOTLIN_1_0, module.languageVersionSettings.languageVersion) + myFixture.launchAction(myFixture.findSingleIntention("Set module language version to 1.1")) + assertEquals(LanguageVersion.KOTLIN_1_1, module.languageVersionSettings.languageVersion) + } + + fun testIncreaseLangAndApiLevel() { + configureRuntime("mockRuntime11") + resetProjectSettings(LanguageVersion.KOTLIN_1_0) + myFixture.configureByText("foo.kt", "val x = \"s\"::length") + + myFixture.launchAction(myFixture.findSingleIntention("Set project language version to 1.1")) + + assertEquals("1.1", KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.languageVersion) + assertEquals("1.1", KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.apiVersion) + } + + fun testIncreaseLangAndApiLevel_10() { + configureRuntime("mockRuntime106") + resetProjectSettings(LanguageVersion.KOTLIN_1_0) + myFixture.configureByText("foo.kt", "val x = \"s\"::length") + + myFixture.launchAction(myFixture.findSingleIntention("Set project language version to 1.1")) + + assertEquals("1.1", KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.languageVersion) + assertEquals("1.1", KotlinCommonCompilerArgumentsHolder.getInstance(project).settings.apiVersion) + + assertEquals(bundledRuntimeVersion(), getRuntimeLibraryVersion(myFixture.module)) + } + + fun testIncreaseLangLevelFacet_10() { + configureRuntime("mockRuntime106") + resetProjectSettings(LanguageVersion.KOTLIN_1_0) + configureKotlinFacet(module) { + settings.languageLevel = LanguageVersion.KOTLIN_1_0 + settings.apiLevel = LanguageVersion.KOTLIN_1_0 + } + myFixture.configureByText("foo.kt", "val x = \"s\"::length") + + assertEquals(LanguageVersion.KOTLIN_1_0, module.languageVersionSettings.languageVersion) + myFixture.launchAction(myFixture.findSingleIntention("Set module language version to 1.1")) + assertEquals(LanguageVersion.KOTLIN_1_1, module.languageVersionSettings.languageVersion) + + assertEquals(bundledRuntimeVersion(), getRuntimeLibraryVersion(myFixture.module)) + } + + fun testAddKotlinReflect() { + configureRuntime("mockRuntime11") + myFixture.configureByText("foo.kt", """class Foo(val prop: Any) { + fun func() {} + } + + fun y01() = Foo::prop.getter + """) + myFixture.launchAction(myFixture.findSingleIntention("Add kotlin-reflect.jar to the classpath")) + val kotlinRuntime = KotlinJavaModuleConfigurator.instance.getKotlinLibrary(module)!! + val classes = kotlinRuntime.getFiles(OrderRootType.CLASSES).map { it.name } + assertContainsElements(classes, "kotlin-reflect.jar") + val sources = kotlinRuntime.getFiles(OrderRootType.SOURCES) + assertContainsElements(sources.map { it.name }, "kotlin-reflect-sources.jar") + } + + private fun configureRuntime(path: String) { + val name = if (path == "mockRuntime106") "kotlin-runtime.jar" else "kotlin-stdlib.jar" + val tempFile = File(FileUtil.createTempDirectory("kotlin-update-configuration", null), name) + FileUtil.copy(File("idea/testData/configuration/$path/$name"), tempFile) + val tempVFile = LocalFileSystem.getInstance().findFileByIoFile(tempFile)!! + + updateModel(myFixture.module) { model -> + val editor = NewLibraryEditor() + editor.name = "KotlinJavaRuntime" + + editor.addRoot(JarFileSystem.getInstance().getJarRootForLocalFile(tempVFile)!!, OrderRootType.CLASSES) + + ConfigLibraryUtil.addLibrary(editor, model) + } + } + + private fun resetProjectSettings(version: LanguageVersion) { + KotlinCommonCompilerArgumentsHolder.getInstance(project).update { + languageVersion = version.versionString + apiVersion = version.versionString + coroutinesState = CommonCompilerArguments.WARN + } + } + + private val coroutineSupport: LanguageFeature.State + get() = project.getLanguageVersionSettings().getFeatureSupport(LanguageFeature.Coroutines) + + private val inlineClassesSupport: LanguageFeature.State + get() = project.getLanguageVersionSettings().getFeatureSupport(LanguageFeature.InlineClasses) + + override fun tearDown() { + FacetManager.getInstance(module).getFacetByType(KotlinFacetType.TYPE_ID)?.let { + FacetUtil.deleteFacet(it) + } + ConfigLibraryUtil.removeLibrary(module, "KotlinJavaRuntime") + super.tearDown() + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt.192 b/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt.192 new file mode 100644 index 00000000000..e6230125f00 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt.192 @@ -0,0 +1,221 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * 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.scratch + +import com.intellij.ide.scratch.ScratchFileService +import com.intellij.ide.scratch.ScratchRootType +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.roots.CompilerModuleExtension +import com.intellij.openapi.roots.ModuleRootModificationUtil +import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess +import com.intellij.psi.PsiDocumentManager +import com.intellij.psi.PsiManager +import com.intellij.testFramework.FileEditorManagerTestCase +import com.intellij.testFramework.MapDataContext +import com.intellij.testFramework.PsiTestUtil +import com.intellij.testFramework.TestActionEvent +import com.intellij.util.ui.UIUtil +import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime +import org.jetbrains.kotlin.idea.KotlinLanguage +import org.jetbrains.kotlin.idea.core.script.ScriptDependenciesManager +import org.jetbrains.kotlin.idea.highlighter.KotlinHighlightingUtil +import org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction +import org.jetbrains.kotlin.idea.scratch.actions.ScratchCompilationSupport +import org.jetbrains.kotlin.idea.scratch.output.InlayScratchFileRenderer +import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor +import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.idea.util.application.runWriteAction +import org.jetbrains.kotlin.test.InTextDirectivesUtils +import org.jetbrains.kotlin.test.KotlinTestUtils +import org.jetbrains.kotlin.test.MockLibraryUtil +import org.jetbrains.kotlin.utils.PathUtil +import org.junit.Assert +import java.io.File + +abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { + + fun doReplTest(fileName: String) { + doTest(fileName, true) + } + + fun doCompilingTest(fileName: String) { + doTest(fileName, false) + } + + fun doMultiFileTest(dirName: String) { + val javaFiles = arrayListOf() + val kotlinFiles = arrayListOf() + val baseDir = File(testDataPath, dirName) + baseDir.walk().forEach { + if (it.isFile) { + if (it.extension == "java") javaFiles.add(it) + if (it.extension == "kt") kotlinFiles.add(it) + } + } + + javaFiles.forEach { myFixture.copyFileToProject(it.path, FileUtil.getRelativePath(baseDir, it)!!) } + kotlinFiles.forEach { myFixture.copyFileToProject(it.path, FileUtil.getRelativePath(baseDir, it)!!) } + + val outputDir = createTempDir(dirName) + + if (javaFiles.isNotEmpty()) { + val options = listOf("-d", outputDir.path) + KotlinTestUtils.compileJavaFiles(javaFiles, options) + } + + MockLibraryUtil.compileKotlin(baseDir.path, outputDir) + + PsiTestUtil.setCompilerOutputPath(module, outputDir.path, false) + + val mainFileName = "$dirName/${getTestName(true)}.kts" + doCompilingTest(mainFileName) + doReplTest(mainFileName) + + ModuleRootModificationUtil.updateModel(module) { model -> + model.getModuleExtension(CompilerModuleExtension::class.java).inheritCompilerOutputPath(true) + } + } + + fun doTest(fileName: String, isRepl: Boolean) { + val sourceFile = File(testDataPath, fileName) + val fileText = sourceFile.readText() + + val scratchFile = createScratchFile(sourceFile.name, fileText) + + ScriptDependenciesManager.updateScriptDependenciesSynchronously(scratchFile, project) + + val psiFile = PsiManager.getInstance(project).findFile(scratchFile) ?: error("Couldn't find psi file ${sourceFile.path}") + + if (!KotlinHighlightingUtil.shouldHighlight(psiFile)) error("Highlighting for scratch file is switched off") + + val (editor, scratchPanel) = getEditorWithScratchPanel(myManager, scratchFile) ?: error("Couldn't find scratch panel") + scratchPanel.scratchFile.saveOptions { + copy(isRepl = isRepl, isInteractiveMode = false) + } + + if (!InTextDirectivesUtils.isDirectiveDefined(fileText, "// NO_MODULE")) { + scratchPanel.setModule(myFixture.module) + } + + launchScratch(scratchFile) + + val doc = PsiDocumentManager.getInstance(project).getDocument(psiFile) ?: error("Document for ${psiFile.name} is null") + + val actualOutput = StringBuilder(psiFile.text) + for (line in doc.lineCount - 1 downTo 0) { + editor.editor.inlayModel.getInlineElementsInRange( + doc.getLineStartOffset(line), + doc.getLineEndOffset(line) + ).map { it.renderer } + .filterIsInstance() + .forEach { + val str = it.toString() + val offset = doc.getLineEndOffset(line); actualOutput.insert( + offset, + "${str.takeWhile { it.isWhitespace() }}// ${str.trim()}" + ) + } + } + + val expectedFileName = if (isRepl) { + fileName.replace(".kts", ".repl.after") + } else { + fileName.replace(".kts", ".comp.after") + } + val expectedFile = File(testDataPath, expectedFileName) + KotlinTestUtils.assertEqualsToFile(expectedFile, actualOutput.toString()) + } + + protected fun createScratchFile(name: String, text: String): VirtualFile { + val scratchFile = ScratchRootType.getInstance().createScratchFile( + project, + name, + KotlinLanguage.INSTANCE, + text, + ScratchFileService.Option.create_if_missing + ) ?: error("Couldn't create scratch file") + + myFixture.openFileInEditor(scratchFile) + return scratchFile + } + + protected fun launchScratch(scratchFile: VirtualFile) { + val action = RunScratchAction() + val e = getActionEvent(scratchFile, action) + + action.beforeActionPerformedUpdate(e) + Assert.assertTrue(e.presentation.isEnabled && e.presentation.isVisible) + action.actionPerformed(e) + + UIUtil.dispatchAllInvocationEvents() + + val start = System.currentTimeMillis() + // wait until output is displayed in editor or for 1 minute + while (ScratchCompilationSupport.isAnyInProgress() && (System.currentTimeMillis() - start) < 60000) { + Thread.sleep(100) + } + + UIUtil.dispatchAllInvocationEvents() + } + + private fun getActionEvent(virtualFile: VirtualFile, action: AnAction): TestActionEvent { + val context = MapDataContext() + context.put(CommonDataKeys.VIRTUAL_FILE_ARRAY, arrayOf(virtualFile)) + context.put(CommonDataKeys.PROJECT, project) + context.put(CommonDataKeys.EDITOR, myFixture.editor) + return TestActionEvent(context, action) + } + + override fun getTestDataPath() = KotlinTestUtils.getHomeDirectory() + + + override fun getProjectDescriptor(): com.intellij.testFramework.LightProjectDescriptor { + val testName = getTestName(false) + + return when { + testName.endsWith("WithKotlinTest") -> INSTANCE_WITH_KOTLIN_TEST + testName.endsWith("NoRuntime") -> INSTANCE_WITHOUT_RUNTIME + else -> KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE_FULL_JDK + } + } + + override fun setUp() { + super.setUp() + + VfsRootAccess.allowRootAccess(KotlinTestUtils.getHomeDirectory()) + + PluginTestCaseBase.addJdk(myFixture.projectDisposable) { PluginTestCaseBase.fullJdk() } + } + + override fun tearDown() { + super.tearDown() + + VfsRootAccess.disallowRootAccess(KotlinTestUtils.getHomeDirectory()) + + ScratchFileService.getInstance().scratchesMapping.mappings.forEach { file, _ -> + runWriteAction { file.delete(this) } + } + } + + companion object { + private val INSTANCE_WITH_KOTLIN_TEST = object : KotlinWithJdkAndRuntimeLightProjectDescriptor( + arrayListOf( + ForTestCompileRuntime.runtimeJarForTests(), + PathUtil.kotlinPathsForDistDirectory.kotlinTestPath + ) + ) { + override fun getSdk() = PluginTestCaseBase.fullJdk() + } + + private val INSTANCE_WITHOUT_RUNTIME = object : KotlinLightProjectDescriptor() { + override fun getSdk() = PluginTestCaseBase.fullJdk() + } + } +} \ No newline at end of file