From c47006ce3b7103ceb7b01e06293998a998d2e9ba Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 1 Dec 2022 15:29:29 +0100 Subject: [PATCH] [kotlin] test paths and reuse common api for highlighters/line markers GitOrigin-RevId: 9845c2d9301f17331e8a83ce392d6ba66d850e6d --- ...ewLightKotlinCodeInsightFixtureTestCase.kt | 47 +++++++++---------- ...tKotlinGoToSuperDeclarationsHandlerTest.kt | 2 +- .../test/AbstractLineMarkerTest.kt | 17 ++++--- .../testData/recursive/propertyAccessors.kt | 10 ++-- .../liveTemplates/LiveTemplatesContextTest.kt | 6 +-- .../idea/liveTemplates/LiveTemplatesTest.kt | 4 +- .../test/AbstractKotlinPostfixTemplateTest.kt | 5 +- 7 files changed, 45 insertions(+), 46 deletions(-) diff --git a/plugins/kotlin/base/test/test/org/jetbrains/kotlin/idea/base/test/NewLightKotlinCodeInsightFixtureTestCase.kt b/plugins/kotlin/base/test/test/org/jetbrains/kotlin/idea/base/test/NewLightKotlinCodeInsightFixtureTestCase.kt index ff61c614d551..022f80ced142 100644 --- a/plugins/kotlin/base/test/test/org/jetbrains/kotlin/idea/base/test/NewLightKotlinCodeInsightFixtureTestCase.kt +++ b/plugins/kotlin/base/test/test/org/jetbrains/kotlin/idea/base/test/NewLightKotlinCodeInsightFixtureTestCase.kt @@ -7,7 +7,6 @@ import com.intellij.psi.PsiFile import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase import com.intellij.util.io.exists -import com.intellij.util.io.readText import junit.framework.TestCase import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginKind import org.jetbrains.kotlin.idea.base.plugin.checkKotlinPluginKind @@ -22,17 +21,22 @@ import java.lang.reflect.Modifier import java.nio.file.Path import java.nio.file.Paths import kotlin.io.path.absolutePathString -import kotlin.io.path.name +import kotlin.io.path.extension +import kotlin.io.path.nameWithoutExtension import kotlin.io.path.writeText abstract class NewLightKotlinCodeInsightFixtureTestCase : LightJavaCodeInsightFixtureTestCase() { protected abstract val pluginKind: KotlinPluginKind - val testRoot: Path by lazy { KotlinTestHelpers.getTestRootPath(javaClass) } + val testRoot: String by lazy { + val testClassPath = javaClass.getAnnotation(TestMetadata::class.java)?.value + ?: error("@${TestMetadata::class.java} annotation not found on class '${javaClass.name}'") + val pathString = KotlinTestHelpers.getTestRootPath(javaClass).resolve(testClassPath).absolutePathString() + if (pathString.endsWith(File.separatorChar)) pathString else pathString + File.separatorChar + } override fun getTestDataPath(): String { - val pathString = testRoot.absolutePathString() - return if (pathString.endsWith(File.separatorChar)) pathString else pathString + File.separatorChar + return testRoot } protected open val directivesContainer: DirectivesContainer @@ -46,7 +50,7 @@ abstract class NewLightKotlinCodeInsightFixtureTestCase : LightJavaCodeInsightFi directiveParser.build() } - protected val mainPath: Path by lazy { + protected val testMethodPath: Path by lazy { val testName = this.name val testClass = javaClass val testMethod = testClass.methods @@ -58,18 +62,11 @@ abstract class NewLightKotlinCodeInsightFixtureTestCase : LightJavaCodeInsightFi && method.returnType == Void.TYPE } - val testClassPath = testClass.getAnnotation(TestMetadata::class.java)?.value - ?: error("@${TestMetadata::class.java} annotation not found on class '${testClass.name}'") - val testMethodPath = testMethod.getAnnotation(TestMetadata::class.java)?.value ?: error("@${TestMetadata::class.java} annotation not found on method '${testMethod.name}'") - - Paths.get(testClassPath, testMethodPath) + Paths.get(testMethodPath) } - protected val mainPathAbsolute: Path - get() = Paths.get(testDataPath).resolve(mainPath) - override fun setUp() { val isK2Plugin = pluginKind == KotlinPluginKind.FIR_PLUGIN System.setProperty("idea.kotlin.plugin.use.k2", isK2Plugin.toString()) @@ -83,23 +80,18 @@ abstract class NewLightKotlinCodeInsightFixtureTestCase : LightJavaCodeInsightFi } fun checkTextByExpectedPath(expectedSuffix: String, actual: String) { - val expectedPath = KotlinTestHelpers.getExpectedPath(mainPath, expectedSuffix) + val expectedPath = Paths.get(testDataPath, getExpectedPath(expectedSuffix)) KotlinTestHelpers.assertEqualsToPath(expectedPath, actual) } - fun JavaCodeInsightTestFixture.configureByMainPath(): PsiFile { - return configureByFile(mainPath.toString()) - } - - fun JavaCodeInsightTestFixture.configureByMainPathStrippingTags(vararg tagNames: String): PsiFile { - val text = mainPath.readText() - return configureByText(mainPath.name, KotlinTestHelpers.stripTags(text, *tagNames)) + fun JavaCodeInsightTestFixture.configureByDefaultFile(): PsiFile { + return configureByFile(testMethodPath.toString()) } fun JavaCodeInsightTestFixture.checkContentByExpectedPath(expectedSuffix: String) { - val expectedPath = KotlinTestHelpers.getExpectedPath(mainPath, expectedSuffix) + val expectedPath = getExpectedPath(expectedSuffix) try { - checkResultByFile(expectedPath.toString(), /* ignoreTrailingWhitespaces = */ true) + checkResultByFile(expectedPath, /* ignoreTrailingWhitespaces = */ true) } catch (e: RuntimeException) { if (e.cause is FileNotFoundException) { val absoluteExpectedPath = Paths.get(testDataPath).resolve(expectedPath) @@ -122,4 +114,11 @@ abstract class NewLightKotlinCodeInsightFixtureTestCase : LightJavaCodeInsightFi } } } + + protected fun getExpectedPath(expectedSuffix: String): String = buildString { + append(testMethodPath.nameWithoutExtension) + append(expectedSuffix) + append(".") + append(testMethodPath.extension) + } } \ No newline at end of file diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/structureView/AbstractKotlinGoToSuperDeclarationsHandlerTest.kt b/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/structureView/AbstractKotlinGoToSuperDeclarationsHandlerTest.kt index 2aa428b78a24..93f6b2ef1f10 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/structureView/AbstractKotlinGoToSuperDeclarationsHandlerTest.kt +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/structureView/AbstractKotlinGoToSuperDeclarationsHandlerTest.kt @@ -16,7 +16,7 @@ abstract class AbstractKotlinGoToSuperDeclarationsHandlerTest : NewLightKotlinCo get() = KotlinPluginKind.FIR_PLUGIN protected fun performTest() { - val file = myFixture.configureByMainPath() as KtFile + val file = myFixture.configureByDefaultFile() as KtFile val superDeclarations = KotlinGoToSuperDeclarationsHandler.findSuperDeclarations(file, editor.caretModel.offset) val actualText = render(superDeclarations?.items ?: emptyList()) checkTextByExpectedPath(".expected", actualText) diff --git a/plugins/kotlin/code-insight/line-markers/test/org/jetbrains/kotlin/idea/k2/codeInsight/lineMarkers/test/AbstractLineMarkerTest.kt b/plugins/kotlin/code-insight/line-markers/test/org/jetbrains/kotlin/idea/k2/codeInsight/lineMarkers/test/AbstractLineMarkerTest.kt index f0dde1116d92..371b3cec9e3b 100644 --- a/plugins/kotlin/code-insight/line-markers/test/org/jetbrains/kotlin/idea/k2/codeInsight/lineMarkers/test/AbstractLineMarkerTest.kt +++ b/plugins/kotlin/code-insight/line-markers/test/org/jetbrains/kotlin/idea/k2/codeInsight/lineMarkers/test/AbstractLineMarkerTest.kt @@ -2,28 +2,27 @@ package org.jetbrains.kotlin.idea.k2.codeInsight.lineMarkers.test import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl +import com.intellij.testFramework.ExpectedHighlightingData import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginKind import org.jetbrains.kotlin.idea.base.test.KotlinJvmLightProjectDescriptor -import org.jetbrains.kotlin.idea.base.test.KotlinTestHelpers import org.jetbrains.kotlin.idea.base.test.NewLightKotlinCodeInsightFixtureTestCase -import org.jetbrains.kotlin.idea.base.test.Tag abstract class AbstractLineMarkerTest : NewLightKotlinCodeInsightFixtureTestCase() { override val pluginKind: KotlinPluginKind get() = KotlinPluginKind.FIR_PLUGIN protected fun performTest() { - myFixture.configureByMainPathStrippingTags("lineMarker") - myFixture.doHighlighting() + myFixture.configureByDefaultFile() val document = editor.document + val data = ExpectedHighlightingData(document) + data.init() + + myFixture.doHighlighting() + val lineMarkers = DaemonCodeAnalyzerImpl.getLineMarkers(document, project) - .map { Tag(it.startOffset, it.endOffset, "lineMarker", "text" to it.lineMarkerTooltip) } - - val expectedText = KotlinTestHelpers.insertTags(document.text, lineMarkers) - - KotlinTestHelpers.assertEqualsToPath(mainPath, expectedText) + data.checkLineMarkers(myFixture.file, lineMarkers, document.text) } override fun getProjectDescriptor() = KotlinJvmLightProjectDescriptor.DEFAULT diff --git a/plugins/kotlin/code-insight/line-markers/testData/recursive/propertyAccessors.kt b/plugins/kotlin/code-insight/line-markers/testData/recursive/propertyAccessors.kt index 5ac18b1be151..e8ce5b134e81 100644 --- a/plugins/kotlin/code-insight/line-markers/testData/recursive/propertyAccessors.kt +++ b/plugins/kotlin/code-insight/line-markers/testData/recursive/propertyAccessors.kt @@ -1,16 +1,16 @@ class A { var mutable = 0 set(value) { - mutable++ - mutable+=1 - mutable = value + mutable++ + mutable+=1 + mutable = value } var immutable = 0 get() { println("$immutable") - immutable++ - immutable += 1 + immutable++ + immutable += 1 return immutable } diff --git a/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesContextTest.kt b/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesContextTest.kt index 2436491eed41..f75ce1e686bb 100644 --- a/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesContextTest.kt +++ b/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesContextTest.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.kotlin.idea.liveTemplates @@ -6,8 +6,8 @@ import com.intellij.codeInsight.template.impl.TemplateContextTypes import com.intellij.testFramework.UsefulTestCase import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginKind import org.jetbrains.kotlin.idea.base.test.NewLightKotlinCodeInsightFixtureTestCase -import org.jetbrains.kotlin.idea.liveTemplates.KotlinTemplateContextType.* import org.jetbrains.kotlin.idea.base.test.TestRoot +import org.jetbrains.kotlin.idea.liveTemplates.KotlinTemplateContextType.* import org.jetbrains.kotlin.test.TestMetadata import org.junit.internal.runners.JUnit38ClassRunner import org.junit.runner.RunWith @@ -100,7 +100,7 @@ class LiveTemplatesContextTest : NewLightKotlinCodeInsightFixtureTestCase() { } private fun assertInContexts(vararg expectedContexts: java.lang.Class) { - myFixture.configureByMainPath() + myFixture.configureByDefaultFile() val allContexts = TemplateContextTypes.getAllContextTypes().filterIsInstance() val enabledContexts = allContexts.filter { it.isInContext(myFixture.file, myFixture.caretOffset) }.map { it::class.java } UsefulTestCase.assertSameElements(enabledContexts, *expectedContexts) diff --git a/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt b/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt index 2fbb152833d6..bd3f109d3d3d 100644 --- a/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt +++ b/plugins/kotlin/code-insight/live-templates-k1/test/org/jetbrains/kotlin/idea/liveTemplates/LiveTemplatesTest.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.kotlin.idea.liveTemplates @@ -282,7 +282,7 @@ class LiveTemplatesTest : NewLightKotlinCodeInsightFixtureTestCase() { } private fun start() { - myFixture.configureByMainPath() + myFixture.configureByDefaultFile() myFixture.type(templateName) doAction("ExpandLiveTemplateByTab") diff --git a/plugins/kotlin/code-insight/postfix-templates/test/org/jetbrains/kotlin/idea/codeInsight/postfix/test/AbstractKotlinPostfixTemplateTest.kt b/plugins/kotlin/code-insight/postfix-templates/test/org/jetbrains/kotlin/idea/codeInsight/postfix/test/AbstractKotlinPostfixTemplateTest.kt index e6228a494f3a..292ce2ecb7a3 100644 --- a/plugins/kotlin/code-insight/postfix-templates/test/org/jetbrains/kotlin/idea/codeInsight/postfix/test/AbstractKotlinPostfixTemplateTest.kt +++ b/plugins/kotlin/code-insight/postfix-templates/test/org/jetbrains/kotlin/idea/codeInsight/postfix/test/AbstractKotlinPostfixTemplateTest.kt @@ -6,6 +6,7 @@ import com.intellij.testFramework.LightProjectDescriptor import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginKind import org.jetbrains.kotlin.idea.base.test.KotlinJvmLightProjectDescriptor import org.jetbrains.kotlin.idea.base.test.NewLightKotlinCodeInsightFixtureTestCase +import java.nio.file.Paths import kotlin.io.path.name abstract class AbstractKotlinPostfixTemplateTest : NewLightKotlinCodeInsightFixtureTestCase() { @@ -22,11 +23,11 @@ abstract class AbstractKotlinPostfixTemplateTest : NewLightKotlinCodeInsightFixt } protected fun performTest() { - myFixture.configureByMainPath() + myFixture.configureByDefaultFile() myFixture.type(".$templateName\t") myFixture.checkContentByExpectedPath(".after") } private val templateName: String - get() = mainPath.parent.name + get() = Paths.get(testDataPath).name } \ No newline at end of file