[kotlin] test paths and reuse common api for highlighters/line markers

GitOrigin-RevId: 9845c2d9301f17331e8a83ce392d6ba66d850e6d
This commit is contained in:
Anna Kozlova
2022-12-01 15:29:29 +01:00
committed by intellij-monorepo-bot
parent bc3fe7188f
commit c47006ce3b
7 changed files with 45 additions and 46 deletions

View File

@@ -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)
}
}

View File

@@ -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)

View File

@@ -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

View File

@@ -1,16 +1,16 @@
class A {
var mutable = 0
set(value) {
mutable<lineMarker text="Recursive call">++</lineMarker>
mutable<lineMarker text="Recursive call">+=</lineMarker>1
mutable <lineMarker text="Recursive call">=</lineMarker> value
<lineMarker descr="Recursive call">mutable</lineMarker><lineMarker text="Recursive call">++</lineMarker>
<lineMarker descr="Recursive call">mutable</lineMarker><lineMarker text="Recursive call">+=</lineMarker>1
<lineMarker descr="Recursive call">mutable</lineMarker> <lineMarker text="Recursive call">=</lineMarker> value
}
var immutable = 0
get() {
println("$<lineMarker text="Recursive call">immutable</lineMarker>")
immutable<lineMarker text="Recursive call">++</lineMarker>
immutable <lineMarker text="Recursive call">+=</lineMarker> 1
<lineMarker descr="Recursive call">immutable</lineMarker><lineMarker text="Recursive call">++</lineMarker>
<lineMarker descr="Recursive call">immutable</lineMarker> <lineMarker text="Recursive call">+=</lineMarker> 1
return <lineMarker text="Recursive call">immutable</lineMarker>
}

View File

@@ -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<out KotlinTemplateContextType>) {
myFixture.configureByMainPath()
myFixture.configureByDefaultFile()
val allContexts = TemplateContextTypes.getAllContextTypes().filterIsInstance<KotlinTemplateContextType>()
val enabledContexts = allContexts.filter { it.isInContext(myFixture.file, myFixture.caretOffset) }.map { it::class.java }
UsefulTestCase.assertSameElements(enabledContexts, *expectedContexts)

View File

@@ -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")

View File

@@ -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
}