[kotlin k2] The "Introduce variable refactoring" inspection: fix tests

KTIJ-21135
IJ-CR-113137

GitOrigin-RevId: b91eef951615b0ddef4c23bb1cc934f76d722ec4
This commit is contained in:
Andrey Cherkasov
2023-10-18 11:38:02 +00:00
committed by intellij-monorepo-bot
parent d902a6af15
commit a1e61d1d6b
43 changed files with 132 additions and 41 deletions
@@ -59,6 +59,7 @@ import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.test.utils.IgnoreTests
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
import java.io.File
import java.util.*
@@ -70,15 +71,21 @@ abstract class AbstractExtractionTest : KotlinLightCodeInsightFixtureTestCase()
val fixture: JavaCodeInsightTestFixture get() = myFixture
protected open fun doIntroduceVariableTest(unused: String) {
doTest { file ->
file as KtFile
IgnoreTests.runTestIfNotDisabledByFileDirective(
dataFilePath(),
IgnoreTests.DIRECTIVES.IGNORE_K1,
directivePosition = IgnoreTests.DirectivePosition.LAST_LINE_IN_FILE
) {
doTest { file ->
file as KtFile
KotlinIntroduceVariableHandler.invoke(
fixture.project,
fixture.editor,
file,
DataManager.getInstance().getDataContext(fixture.editor.component)
)
KotlinIntroduceVariableHandler.invoke(
fixture.project,
fixture.editor,
file,
DataManager.getInstance().getDataContext(fixture.editor.component)
)
}
}
}
@@ -382,7 +389,7 @@ abstract class AbstractExtractionTest : KotlinLightCodeInsightFixtureTestCase()
try {
checkExtract(
ExtractTestFiles(mainFile.path, fixture.configureByFile(mainFileName), extraFilesToPsi),
ExtractTestFiles(mainFile.path, fixture.configureByFile(mainFileName), extraFilesToPsi, isFirPlugin),
checkAdditionalAfterdata,
action,
)
@@ -403,8 +410,21 @@ class ExtractTestFiles(
val conflictFile: File,
val extraFilesToPsi: Map<PsiFile, File> = emptyMap()
) {
constructor(path: String, mainFile: PsiFile, extraFilesToPsi: Map<PsiFile, File> = emptyMap()) :
this(mainFile, File("$path.after"), File("$path.conflicts"), extraFilesToPsi)
constructor(path: String, mainFile: PsiFile, extraFilesToPsi: Map<PsiFile, File> = emptyMap(), isFirPlugin: Boolean) :
this(mainFile, getAfterFile(path, isFirPlugin), File("$path.conflicts"), extraFilesToPsi)
}
private fun getAfterFile(path: String, isFirPlugin: Boolean): File {
var file = File("$path.after")
if (isFirPlugin) {
val firSpecific = File("$path.fir.after")
if (firSpecific.exists()) {
file = firSpecific
}
}
return file
}
fun checkExtract(files: ExtractTestFiles, checkAdditionalAfterdata: Boolean = false, action: (PsiFile) -> Unit) {
@@ -558,9 +558,14 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest {
runTest("testData/refactoring/introduceVariable/NameSuggestionCheckVisibility.kt");
}
@TestMetadata("noConflictWithInnerVariable.kt")
public void testNoConflictWithInnerVariable() throws Exception {
runTest("testData/refactoring/introduceVariable/noConflictWithInnerVariable.kt");
@TestMetadata("noConflictWithInnerVariableK1.kt")
public void testNoConflictWithInnerVariableK1() throws Exception {
runTest("testData/refactoring/introduceVariable/noConflictWithInnerVariableK1.kt");
}
@TestMetadata("noConflictWithInnerVariableK2.kt")
public void testNoConflictWithInnerVariableK2() throws Exception {
runTest("testData/refactoring/introduceVariable/noConflictWithInnerVariableK2.kt");
}
@TestMetadata("NoExplicitReceivers.kt")
@@ -1,4 +1,3 @@
// IGNORE_K2
fun a() {
if (<selection>true</selection>) 2
else 1
@@ -1,4 +1,3 @@
// IGNORE_K2
fun a() {
val b = true
if (b) 2
@@ -0,0 +1,5 @@
fun a() {
val bool = true
if (bool) 2
else 1
}
@@ -1,4 +1,4 @@
// IGNORE_K2
// WITH_STDLIB
fun a() {
for (a in <selection>1..2</selection>) {}
}
@@ -1,4 +1,4 @@
// IGNORE_K2
// WITH_STDLIB
fun a() {
val intRange = 1..2
for (a in intRange) {}
@@ -0,0 +1,5 @@
// WITH_STDLIB
fun a() {
val ints = 1..2
for (a in ints) {}
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun a() {
while (<selection>true</selection>) {}
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun a() {
val b = true
while (b) {}
@@ -0,0 +1,4 @@
fun a() {
val bool = true
while (bool) {}
}
@@ -1,4 +1,3 @@
// IGNORE_K2
// WITH_STDLIB
fun f() {
val v : List<Int> = <selection>(listOf())</selection>
@@ -1,4 +1,3 @@
// IGNORE_K2
// WITH_STDLIB
fun f() {
val listOf = listOf<Int>()
@@ -0,0 +1,5 @@
// WITH_STDLIB
fun f() {
val listOf = listOf<Int>()
val v : List<Int> = (listOf)
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) { }
fun test() {
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) { }
fun test() {
@@ -0,0 +1,6 @@
fun foo(f: () -> Int) { }
fun test() {
val i = 1 + 2
foo(fun() = (i) * 3)
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) { }
fun test() {
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) { }
fun test() {
@@ -0,0 +1,6 @@
fun foo(f: () -> Int) { }
fun test() {
val i = 1 + 2
foo(fun(): Int { return (i) * 3 })
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) { }
fun test() {
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) { }
fun test() {
@@ -0,0 +1,6 @@
fun foo(f: () -> Int) { }
fun test() {
val i = 1 + 2
foo { (i) * 3 }
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -0,0 +1,6 @@
fun foo(f: () -> Int) = f()
fun test() {
val i = 1 + 2
foo(fun() = foo(fun() = (i) * 3))
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -0,0 +1,6 @@
fun foo(f: () -> Int) = f()
fun test() {
val i = 1 + 2
foo { foo(fun() = (i) * 3) }
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -0,0 +1,6 @@
fun foo(f: () -> Int) = f()
fun test() {
val i = 1 + 2
foo { foo { (i) * 3 } }
}
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -1,4 +1,3 @@
// IGNORE_K2
fun foo(f: () -> Int) = f()
fun test() {
@@ -0,0 +1,6 @@
fun foo(f: () -> Int) = f()
fun test() {
val i = 1 + 2
foo(fun() = foo { (i) * 3 })
}
@@ -1,4 +1,3 @@
// IGNORE_K2
// WITH_STDLIB
fun test() {
(<selection>1 to 2</selection>).first
@@ -1,4 +1,3 @@
// IGNORE_K2
// WITH_STDLIB
fun test() {
val pair = 1 to 2
@@ -0,0 +1,5 @@
// WITH_STDLIB
fun test() {
val pair = 1 to 2
(pair).first
}
@@ -0,0 +1,8 @@
// IGNORE_K1
fun foo() {
val a = 1
if (<selection>a > 0</selection>) {
val bool = 0
println(bool)
}
}
@@ -0,0 +1,9 @@
// IGNORE_K1
fun foo() {
val a = 1
val bool1 = a > 0
if (bool1) {
val bool = 0
println(bool)
}
}
@@ -556,9 +556,14 @@ public abstract class K2IntroduceVariableTestGenerated extends AbstractK2Introdu
runTest("../../idea/tests/testData/refactoring/introduceVariable/NameSuggestionCheckVisibility.kt");
}
@TestMetadata("noConflictWithInnerVariable.kt")
public void testNoConflictWithInnerVariable() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceVariable/noConflictWithInnerVariable.kt");
@TestMetadata("noConflictWithInnerVariableK1.kt")
public void testNoConflictWithInnerVariableK1() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceVariable/noConflictWithInnerVariableK1.kt");
}
@TestMetadata("noConflictWithInnerVariableK2.kt")
public void testNoConflictWithInnerVariableK2() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceVariable/noConflictWithInnerVariableK2.kt");
}
@TestMetadata("NoExplicitReceivers.kt")