[kotlin] apply .k2.kt.after postfix for inspection tests

GitOrigin-RevId: 12097426763100bef908255bbc2cde2794c99857
This commit is contained in:
Vladislav.Koshkin
2023-11-13 19:49:56 +00:00
committed by intellij-monorepo-bot
parent 6913216c6a
commit 2517f9319b
20 changed files with 20 additions and 33 deletions
@@ -10,6 +10,9 @@ import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.test.utils.IgnoreTests
import java.io.File
import java.nio.file.Path
import kotlin.io.path.div
import kotlin.io.path.exists
abstract class AbstractK2LocalInspectionTest : AbstractLocalInspectionTest() {
override fun isFirPlugin() = true
@@ -29,6 +32,14 @@ abstract class AbstractK2LocalInspectionTest : AbstractLocalInspectionTest() {
)
}
override fun getAfterTestDataAbsolutePath(mainFileName: String): Path {
val k2FileName = mainFileName.removeSuffix(".kt") + ".k2.kt.after"
val k2FilePath = testDataDirectory.toPath() / k2FileName
if (k2FilePath.exists()) return k2FilePath
return super.getAfterTestDataAbsolutePath(mainFileName)
}
override fun doTestFor(mainFile: File, inspection: LocalInspectionTool, fileText: String) {
IgnoreTests.runTestIfNotDisabledByFileDirective(mainFile.toPath(), IgnoreTests.DIRECTIVES.IGNORE_K2, "after") {
doTestForInternal(mainFile, inspection, fileText)
@@ -38,6 +38,8 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
protected open val inspectionFileName: String
get() = ".inspection"
private val afterFileNameSuffix: String = ".after"
private val expectedProblemDirectiveName: String = "PROBLEM"
protected val expectedProblemHighlightType: String = "HIGHLIGHT"
@@ -265,6 +267,9 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
}
}
protected open fun getAfterTestDataAbsolutePath(mainFileName: String) =
testDataDirectory.toPath() / (mainFileName + afterFileNameSuffix)
protected fun doTestForInternal(mainFile: File, inspection: LocalInspectionTool, fileText: String) {
val mainFileName = mainFile.name
val expectedProblemString = InTextDirectivesUtils.findStringWithPrefixes(
@@ -278,9 +283,7 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
)
val inspectionSettings = loadInspectionSettings(mainFile)
val afterFileName = mainFileName +
if (isDifferentExpectedDataForK2(inspection, fileText)) ".after.k2" else ".after"
val afterFileAbsolutePath = testDataDirectory.toPath() / afterFileName
val afterFileAbsolutePath = getAfterTestDataAbsolutePath(mainFileName)
if (!runInspectionWithFixesAndCheck(
inspection,
@@ -290,17 +293,17 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
inspectionSettings
)
) {
assertFalse("$afterFileName should not exist as no action could be applied", Files.exists(afterFileAbsolutePath))
assertFalse("${afterFileAbsolutePath.fileName} should not exist as no action could be applied", Files.exists(afterFileAbsolutePath))
return
}
createAfterFileIfItDoesNotExist(afterFileAbsolutePath)
dispatchAllEventsInIdeEventQueue()
try {
myFixture.checkResultByFile(afterFileName)
myFixture.checkResultByFile("${afterFileAbsolutePath.fileName}")
} catch (e: ComparisonFailure) {
KotlinTestUtils.assertEqualsToFile(
File(testDataDirectory, afterFileName),
File(testDataDirectory, "${afterFileAbsolutePath.fileName}"),
editor.document.text
)
}
@@ -308,15 +311,6 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
checkForUnexpectedErrors()
}
private fun isDifferentExpectedDataForK2(inspection: LocalInspectionTool, fileText: String): Boolean {
val withK2Data = InTextDirectivesUtils.isDirectiveDefined(
fileText, "// WITH_DIFFERENT_DATA_FOR_K2"
)
return inspection.javaClass.`package`.name == "org.jetbrains.kotlin.idea.k2.codeinsight.inspections"
&& withK2Data
}
private fun createAfterFileIfItDoesNotExist(path: Path) {
if (!Files.exists(path)) {
path.createFile().write(editor.document.text)
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(p: List<String?>): Int {
val v = p[0] ?: // return -1 if null
return -1
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(p: List<String?>): Int {
val v = p[0]
<caret>if (v == null) {
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(p: List<String?>): Int {
val v = p[0]
?: // return -1 if null
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(p: List<String?>): Int {
val v = p[0] ?: // v is null
// we should do something with it
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(p: List<String?>): Int {
val v = p[0]
<caret>if (v == null) { // v is null
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(p: List<String?>): Int {
val v = p[0]
?: // v is null
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx =
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 24
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 24
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx =
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
@@ -1,4 +1,3 @@
// WITH_DIFFERENT_DATA_FOR_K2
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
@@ -1,5 +1,4 @@
// IGNORE_FE10_BINDING_BY_FIR
// WITH_DIFFERENT_DATA_FOR_K2
fun test(foo: Int?, bar: Int): Int? {
var i = foo ?: return bar
return i
@@ -1,5 +1,4 @@
// IGNORE_FE10_BINDING_BY_FIR
// WITH_DIFFERENT_DATA_FOR_K2
fun test(foo: Int?, bar: Int): Int? {
var i = foo
<caret>if (i == null) {
@@ -1,5 +1,4 @@
// IGNORE_FE10_BINDING_BY_FIR
// WITH_DIFFERENT_DATA_FOR_K2
fun test(foo: Int?, bar: Int): Int? {
var i: Int? = foo ?: return bar
return i
@@ -1,5 +1,4 @@
// IGNORE_FE10_BINDING_BY_FIR
// WITH_DIFFERENT_DATA_FOR_K2
fun test(foo: Int?, bar: Int): Int {
var i = foo ?: return bar
return baz(i)
@@ -1,5 +1,4 @@
// IGNORE_FE10_BINDING_BY_FIR
// WITH_DIFFERENT_DATA_FOR_K2
fun test(foo: Int?, bar: Int): Int {
var i = foo
<caret>if (i == null) {
@@ -1,5 +1,4 @@
// IGNORE_FE10_BINDING_BY_FIR
// WITH_DIFFERENT_DATA_FOR_K2
fun test(foo: Int?, bar: Int): Int {
var i: Int? = foo ?: return bar
return baz(i)