From b24ce04dea5beba5a1e56b36530ba1bf4c0b3488 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 22 Mar 2017 18:56:05 +0100 Subject: [PATCH] Convert IntentionTestGenerated to light fixture test case --- .../KotlinLightCodeInsightFixtureTestCase.kt | 8 +- .../intentions/importMember/TopLevelFun.kt | 2 +- .../importMember/TopLevelFun.kt.after | 2 +- .../kotlin/asJava/KtFileLightClassTest.kt | 2 +- .../kotlin/asJava/LightClassFromTextTest.kt | 2 +- .../AbstractKotlinCoverageOutputFilesTest.kt | 8 +- .../debugger/AbstractSmartStepIntoTest.kt | 2 +- .../idea/intentions/AbstractIntentionTest.kt | 92 ++++--------------- 8 files changed, 34 insertions(+), 84 deletions(-) diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt index 89139c92e6a..f8c807a3972 100644 --- a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt +++ b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt @@ -104,9 +104,7 @@ abstract class KotlinLightCodeInsightFixtureTestCase : KotlinLightCodeInsightFix protected fun getProjectDescriptorFromFileDirective(): LightProjectDescriptor { if (!isAllFilesPresentInTest()) { try { - val metadata = this::class.findAnnotation() - val basePath = metadata?.value ?: testDataPath - val fileText = FileUtil.loadFile(File(basePath, fileName()), true) + val fileText = FileUtil.loadFile(File(testDataPath, fileName()), true) val withLibraryDirective = InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileText, "WITH_LIBRARY:") if (!withLibraryDirective.isEmpty()) { @@ -157,4 +155,8 @@ abstract class KotlinLightCodeInsightFixtureTestCase : KotlinLightCodeInsightFix managerEx.fireAfterActionPerformed(action, dataContext, event) return true } + + override fun getTestDataPath(): String { + return this::class.findAnnotation()?.value ?: super.getTestDataPath() + } } diff --git a/idea/testData/intentions/importMember/TopLevelFun.kt b/idea/testData/intentions/importMember/TopLevelFun.kt index 7cc0f6d88fe..de6acefccb9 100644 --- a/idea/testData/intentions/importMember/TopLevelFun.kt +++ b/idea/testData/intentions/importMember/TopLevelFun.kt @@ -1,5 +1,5 @@ // INTENTION_TEXT: "Add import for 'kotlin.test.assertFailsWith'" -// WITH_RUNTIME +// RUNTIME_WITH_KOTLIN_TEST fun foo() { kotlin.test.assertFailsWith("", {}) diff --git a/idea/testData/intentions/importMember/TopLevelFun.kt.after b/idea/testData/intentions/importMember/TopLevelFun.kt.after index 0d7deda6bda..8746b613709 100644 --- a/idea/testData/intentions/importMember/TopLevelFun.kt.after +++ b/idea/testData/intentions/importMember/TopLevelFun.kt.after @@ -1,7 +1,7 @@ import kotlin.test.assertFailsWith // INTENTION_TEXT: "Add import for 'kotlin.test.assertFailsWith'" -// WITH_RUNTIME +// RUNTIME_WITH_KOTLIN_TEST fun foo() { assertFailsWith("", {}) diff --git a/idea/tests/org/jetbrains/kotlin/asJava/KtFileLightClassTest.kt b/idea/tests/org/jetbrains/kotlin/asJava/KtFileLightClassTest.kt index 7414ea170bd..1cefa55580e 100644 --- a/idea/tests/org/jetbrains/kotlin/asJava/KtFileLightClassTest.kt +++ b/idea/tests/org/jetbrains/kotlin/asJava/KtFileLightClassTest.kt @@ -69,7 +69,7 @@ class KtFileLightClassTest : KotlinLightCodeInsightFixtureTestCase() { assertEquals(0, facadeFiles.size) } - override fun getTestDataPath(): String? { + override fun getTestDataPath(): String { return PluginTestCaseBase.getTestDataPathBase() + "/asJava/fileLightClass/" } } diff --git a/idea/tests/org/jetbrains/kotlin/asJava/LightClassFromTextTest.kt b/idea/tests/org/jetbrains/kotlin/asJava/LightClassFromTextTest.kt index 58923948af5..90865e55892 100644 --- a/idea/tests/org/jetbrains/kotlin/asJava/LightClassFromTextTest.kt +++ b/idea/tests/org/jetbrains/kotlin/asJava/LightClassFromTextTest.kt @@ -110,7 +110,7 @@ class LightClassFromTextTest : KotlinLightCodeInsightFixtureTestCase() { return classes } - override fun getTestDataPath(): String? { + override fun getTestDataPath(): String { return PluginTestCaseBase.getTestDataPathBase() + "/asJava/fileLightClass/" } } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/coverage/AbstractKotlinCoverageOutputFilesTest.kt b/idea/tests/org/jetbrains/kotlin/idea/coverage/AbstractKotlinCoverageOutputFilesTest.kt index 10e7266e7ba..df66eb7f131 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/coverage/AbstractKotlinCoverageOutputFilesTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/coverage/AbstractKotlinCoverageOutputFilesTest.kt @@ -16,19 +16,19 @@ package org.jetbrains.kotlin.idea.coverage +import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.vfs.VirtualFile import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.test.KotlinTestUtils import java.io.File -import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.vfs.VirtualFile -import org.jetbrains.kotlin.idea.util.application.runWriteAction abstract class AbstractKotlinCoverageOutputFilesTest(): KotlinLightCodeInsightFixtureTestCase() { private val TEST_DATA_PATH = PluginTestCaseBase.getTestDataPathBase() + "/coverage/outputFiles" - override fun getTestDataPath(): String? = TEST_DATA_PATH + override fun getTestDataPath(): String = TEST_DATA_PATH fun doTest(path: String) { val kotlinFile = myFixture.configureByFile(path) as KtFile diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/AbstractSmartStepIntoTest.kt b/idea/tests/org/jetbrains/kotlin/idea/debugger/AbstractSmartStepIntoTest.kt index 0fff365b6da..4d6f641bbc4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/debugger/AbstractSmartStepIntoTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/AbstractSmartStepIntoTest.kt @@ -78,7 +78,7 @@ abstract class AbstractSmartStepIntoTest : KotlinLightCodeInsightFixtureTestCase return sb.toString() } - override fun getTestDataPath(): String? { + override fun getTestDataPath(): String { return PluginTestCaseBase.getTestDataPathBase() + "/debugger/smartStepInto" } diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.kt index c4ed18a7255..f430f2f6b47 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.kt @@ -18,9 +18,7 @@ package org.jetbrains.kotlin.idea.intentions import com.google.common.collect.Lists import com.intellij.codeInsight.intention.IntentionAction -import com.intellij.ide.startup.impl.StartupManagerImpl import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.startup.StartupManager import com.intellij.openapi.util.Computable import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.util.io.FileUtil @@ -28,16 +26,10 @@ import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiFile import com.intellij.refactoring.BaseRefactoringProcessor import com.intellij.refactoring.util.CommonRefactoringUtil -import com.intellij.testFramework.PlatformTestUtil -import com.intellij.util.PathUtil -import com.intellij.util.containers.ContainerUtil -import com.intellij.util.containers.Convertor import junit.framework.ComparisonFailure import junit.framework.TestCase -import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils -import org.jetbrains.kotlin.idea.test.KotlinCodeInsightTestCase -import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.test.InTextDirectivesUtils @@ -46,20 +38,14 @@ import org.junit.Assert import java.io.File import java.util.* -abstract class AbstractIntentionTest : KotlinCodeInsightTestCase() { - protected open fun intentionFileName(): String { - return ".intention" - } +abstract class AbstractIntentionTest : KotlinLightCodeInsightFixtureTestCase() { + protected open fun intentionFileName(): String = ".intention" - protected open fun afterFileNameSuffix(): String { - return ".after" - } + protected open fun afterFileNameSuffix(): String = ".after" protected open fun isApplicableDirectiveName(): String = "IS_APPLICABLE" - protected open fun intentionTextDirectiveName(): String { - return "INTENTION_TEXT" - } + protected open fun intentionTextDirectiveName(): String = "INTENTION_TEXT" @Throws(Exception::class) private fun createIntention(testDataFile: File): IntentionAction { @@ -95,32 +81,21 @@ abstract class AbstractIntentionTest : KotlinCodeInsightTestCase() { val sourceFilePaths = ArrayList() val parentDir = mainFile.parentFile var i = 1 + sourceFilePaths.add(mainFile.name) extraFileLoop@ while (true) { for (extension in EXTENSIONS) { val extraFile = File(parentDir, mainFileName + "." + i + extension) if (extraFile.exists()) { - sourceFilePaths.add(extraFile.path) + sourceFilePaths.add(extraFile.name) i++ continue@extraFileLoop } } break } - sourceFilePaths.add(path) - val pathToFiles = ContainerUtil.newMapFromKeys( - sourceFilePaths.iterator(), - Convertor { path -> - try { - configureByFile(path) - } - catch (e: Exception) { - throw RuntimeException(e) - } - - myFile - } - ) + val psiFiles = myFixture.configureByFiles(*sourceFilePaths.toTypedArray()) + val pathToFiles = mapOf(*(sourceFilePaths zip psiFiles).toTypedArray()) val fileText = FileUtil.loadFile(mainFile, true) @@ -129,30 +104,14 @@ abstract class AbstractIntentionTest : KotlinCodeInsightTestCase() { val minJavaVersion = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// MIN_JAVA_VERSION: ") if (minJavaVersion != null && !SystemInfo.isJavaVersionAtLeast(minJavaVersion)) return - val isWithRuntime = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// WITH_RUNTIME") != null - - try { - if (isWithRuntime) { - ConfigLibraryUtil.configureKotlinRuntimeAndSdk(module, PluginTestCaseBase.mockJdk()) - } - - ConfigLibraryUtil.configureLibrariesByDirective(module, PlatformTestUtil.getCommunityPath(), fileText) - - if (file is KtFile && !InTextDirectivesUtils.isDirectiveDefined(fileText, "// SKIP_ERRORS_BEFORE")) { - DirectiveBasedActionUtils.checkForUnexpectedErrors(file as KtFile) - } - - doTestFor(path, pathToFiles, intentionAction, fileText) - - if (file is KtFile && !InTextDirectivesUtils.isDirectiveDefined(fileText, "// SKIP_ERRORS_AFTER")) { - DirectiveBasedActionUtils.checkForUnexpectedErrors(file as KtFile) - } + if (file is KtFile && !InTextDirectivesUtils.isDirectiveDefined(fileText, "// SKIP_ERRORS_BEFORE")) { + DirectiveBasedActionUtils.checkForUnexpectedErrors(file as KtFile) } - finally { - ConfigLibraryUtil.unconfigureLibrariesByDirective(myModule, fileText) - if (isWithRuntime) { - ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(module, testProjectJdk) - } + + doTestFor(mainFile.name, pathToFiles, intentionAction, fileText) + + if (file is KtFile && !InTextDirectivesUtils.isDirectiveDefined(fileText, "// SKIP_ERRORS_AFTER")) { + DirectiveBasedActionUtils.checkForUnexpectedErrors(file as KtFile) } } @@ -190,19 +149,19 @@ abstract class AbstractIntentionTest : KotlinCodeInsightTestCase() { // Don't bother checking if it should have failed. if (shouldFailString.isEmpty()) { for ((filePath, value) in pathToFiles) { - val canonicalPathToExpectedFile = PathUtil.getCanonicalPath(filePath + afterFileNameSuffix()) + val canonicalPathToExpectedFile = filePath + afterFileNameSuffix() if (filePath == mainFilePath) { try { - checkResultByFile(canonicalPathToExpectedFile) + myFixture.checkResultByFile(canonicalPathToExpectedFile) } catch (e: ComparisonFailure) { KotlinTestUtils - .assertEqualsToFile(File(canonicalPathToExpectedFile), editor.document.text) + .assertEqualsToFile(File(testDataPath, canonicalPathToExpectedFile), editor.document.text) } } else { - KotlinTestUtils.assertEqualsToFile(File(canonicalPathToExpectedFile), value.text) + KotlinTestUtils.assertEqualsToFile(File(testDataPath, canonicalPathToExpectedFile), value.text) } } } @@ -215,17 +174,6 @@ abstract class AbstractIntentionTest : KotlinCodeInsightTestCase() { catch (e: CommonRefactoringUtil.RefactoringErrorHintException) { TestCase.assertEquals("Failure message mismatch.", shouldFailString, e.message?.replace('\n', ' ')) } - - } - - @Throws(Exception::class) - override fun setUp() { - super.setUp() - (StartupManager.getInstance(project) as StartupManagerImpl).runPostStartupActivities() - } - - override fun getTestDataPath(): String { - return "" } companion object {