[kotlin] make inplace extract function test generated

GitOrigin-RevId: 7b008de0f8b04aec8222e70f25e1fd0a5d1555de
This commit is contained in:
Anna Kozlova
2024-05-14 08:00:57 +00:00
committed by intellij-monorepo-bot
parent 7b86c6013e
commit b8bf19419b
31 changed files with 300 additions and 168 deletions
@@ -0,0 +1,60 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.refactoring.introduce
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.java.refactoring.ExtractMethodAndDuplicatesInplaceTest.Companion.nextTemplateVariable
import com.intellij.java.refactoring.ExtractMethodAndDuplicatesInplaceTest.Companion.renameTemplate
import com.intellij.openapi.application.impl.NonBlockingReadActionImpl
import com.intellij.util.ui.UIUtil
import org.jetbrains.kotlin.idea.base.test.IgnoreTests
import org.jetbrains.kotlin.idea.base.test.InTextDirectivesUtils
import org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.AbstractExtractKotlinFunctionHandler
import org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractKotlinFunctionHandler
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import java.io.File
abstract class AbstractInplaceIntroduceFunctionTest : KotlinLightCodeInsightFixtureTestCase() {
fun doTest(unused: String) {
val disableTestDirective = if (isFirPlugin) IgnoreTests.DIRECTIVES.IGNORE_K2 else IgnoreTests.DIRECTIVES.IGNORE_K1
IgnoreTests.runTestIfNotDisabledByFileDirective(
dataFilePath(),
disableTestDirective,
directivePosition = IgnoreTests.DirectivePosition.LAST_LINE_IN_FILE
) { isTestEnabled -> doTestInternal() }
}
private fun doTestInternal() {
TemplateManagerImpl.setTemplateTesting(testRootDisposable)
val mainFile = File(testDataDirectory, fileName())
myFixture.testDataPath = mainFile.parent
val file = myFixture.configureByFile(mainFile.name)
getExtractFunctionHandler(getTestName(true).contains("Local")).invoke(myFixture.project, myFixture.editor, myFixture.file, null)
NonBlockingReadActionImpl.waitForAsyncTaskCompletion()
val template = getActiveTemplate()
require(template != null) { "Failed to start refactoring" }
val fileText = file.text
val changedName: String? = InTextDirectivesUtils.findListWithPrefixes(fileText, "// CHANGED_NAME: ").firstOrNull()
val checkResult: Boolean = !InTextDirectivesUtils.isDirectiveDefined(fileText, "// IGNORE_RESULT")
if (changedName != null) {
renameTemplate(template, changedName)
}
nextTemplateVariable(template)
UIUtil.dispatchAllInvocationEvents()
if (checkResult) {
val testName = getTestName(false)
var expectedFile = "$testName.kt.after"
if (isFirPlugin && File(testDataDirectory, "$testName.kt.fir.after").exists()) {
expectedFile = "$testName.kt.fir.after"
}
myFixture.checkResultByFile(expectedFile)
}
}
protected open fun getExtractFunctionHandler(allContainersEnabled: Boolean): AbstractExtractKotlinFunctionHandler =
ExtractKotlinFunctionHandler(allContainersEnabled)
private fun getActiveTemplate() = TemplateManagerImpl.getTemplateState(editor)
}
@@ -1,60 +0,0 @@
// 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.refactoring.introduce
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.java.refactoring.ExtractMethodAndDuplicatesInplaceTest.Companion.nextTemplateVariable
import com.intellij.java.refactoring.ExtractMethodAndDuplicatesInplaceTest.Companion.renameTemplate
import com.intellij.openapi.application.impl.NonBlockingReadActionImpl
import com.intellij.util.ui.UIUtil
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractKotlinFunctionHandler
import org.jetbrains.kotlin.idea.test.IDEA_TEST_DATA_DIR
import org.jetbrains.kotlin.idea.test.JUnit3RunnerWithInners
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.junit.runner.RunWith
@RunWith(JUnit3RunnerWithInners::class)
class InplaceExtractFunctionTest : KotlinLightCodeInsightFixtureTestCase() {
override val testDataDirectory = IDEA_TEST_DATA_DIR.resolve("refactoring/extractFunctionInplace")
fun testStringTemplate() {
doTest()
}
fun testStringTemplateWithNameConflict() {
doTest(changedName = "substring")
}
fun testConflictNameNotAccepted() {
doTest(changedName = "conflict", checkResult = false)
TestCase.assertTrue(getActiveTemplate() != null)
}
fun testConsecutiveDuplicates() {
doTest(changedName = "renamed")
}
fun testExposedAssignment() {
doTest(changedName = "getX")
}
fun doTest(changedName: String? = null, checkResult: Boolean = true) {
TemplateManagerImpl.setTemplateTesting(testRootDisposable)
myFixture.configureByFile("${getTestName(false)}.kt")
ExtractKotlinFunctionHandler().invoke(myFixture.project, myFixture.editor, myFixture.file, null)
NonBlockingReadActionImpl.waitForAsyncTaskCompletion()
val template = getActiveTemplate()
require(template != null) { "Failed to start refactoring" }
if (changedName != null) {
renameTemplate(template, changedName)
}
nextTemplateVariable(template)
UIUtil.dispatchAllInvocationEvents()
if (checkResult) {
myFixture.checkResultByFile("${getTestName(false)}.after.kt")
}
}
private fun getActiveTemplate() = TemplateManagerImpl.getTemplateState(editor)
}
@@ -0,0 +1,85 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.refactoring.introduce;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.idea.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.idea.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.jetbrains.kotlin.idea.base.test.TestRoot;
import org.junit.runner.RunWith;
/**
* This class is generated by {@link org.jetbrains.kotlin.testGenerator.generator.TestGenerator}.
* DO NOT MODIFY MANUALLY.
*/
@SuppressWarnings("all")
@TestRoot("idea/tests")
@TestDataPath("$CONTENT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/refactoring/extractFunctionInplace")
public class InplaceIntroduceFunctionTestGenerated extends AbstractInplaceIntroduceFunctionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("ConflictNameNotAccepted.kt")
public void testConflictNameNotAccepted() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/ConflictNameNotAccepted.kt");
}
@TestMetadata("ConsecutiveDuplicates.kt")
public void testConsecutiveDuplicates() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/ConsecutiveDuplicates.kt");
}
@TestMetadata("DontSkipWhitespacesBefore.kt")
public void testDontSkipWhitespacesBefore() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/DontSkipWhitespacesBefore.kt");
}
@TestMetadata("EmptySpaces.kt")
public void testEmptySpaces() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/EmptySpaces.kt");
}
@TestMetadata("ExposedAssignment.kt")
public void testExposedAssignment() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/ExposedAssignment.kt");
}
@TestMetadata("ExtractLocal.kt")
public void testExtractLocal() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/ExtractLocal.kt");
}
@TestMetadata("MoveLambdaOutsideParenthesis.kt")
public void testMoveLambdaOutsideParenthesis() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/MoveLambdaOutsideParenthesis.kt");
}
@TestMetadata("MoveLambdaOutsideParenthesis1.kt")
public void testMoveLambdaOutsideParenthesis1() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/MoveLambdaOutsideParenthesis1.kt");
}
@TestMetadata("SearchForCallAfter.kt")
public void testSearchForCallAfter() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/SearchForCallAfter.kt");
}
@TestMetadata("SkipCommentsBefore.kt")
public void testSkipCommentsBefore() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/SkipCommentsBefore.kt");
}
@TestMetadata("StringTemplate.kt")
public void testStringTemplate() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/StringTemplate.kt");
}
@TestMetadata("StringTemplateWithNameConflict.kt")
public void testStringTemplateWithNameConflict() throws Exception {
runTest("testData/refactoring/extractFunctionInplace/StringTemplateWithNameConflict.kt");
}
}
@@ -1,3 +1,5 @@
// CHANGED_NAME: conflict
// IGNORE_RESULT
fun sample(){
<selection>println("42")</selection>
}
@@ -1,3 +1,4 @@
// CHANGED_NAME: renamed
fun sample(){
<selection>println("duplicate")</selection>
println("duplicate")
@@ -10,4 +10,5 @@ fun foo() {
print(10)
}
}
// IGNORE_K1
@@ -1,3 +1,4 @@
// CHANGED_NAME: getX
fun sample(){
<selection>val x = 42
println()</selection>
@@ -0,0 +1,7 @@
fun interface FunctionalInterface {
fun paramOutput(name: String): String
}
fun getFunInterface(): FunctionalInterface = FunctionalInterface(function<caret>())
private fun function() = { paramName -> "ParamName is $paramName!" }
@@ -0,0 +1,6 @@
val lazyFun: Int by lazy(function<caret>())
private fun function() = {
print("i'm lazy")
1000
}
@@ -1,3 +0,0 @@
fun initList() = ints<caret>()
private fun ints(): List<Int> = listOf<Int>(1, 2, 3, 4)
@@ -1 +1,2 @@
fun initList() = <selection>listOf<Int>(1,2,3,4)</selection>
fun initList() = <selection>listOf<Int>(1,2,3,4)</selection>
// IGNORE_K1
@@ -0,0 +1,4 @@
fun initList() = ints<caret>()
private fun ints(): List<Int> = listOf<Int>(1, 2, 3, 4)
// IGNORE_K1
@@ -1,3 +1,4 @@
// CHANGED_NAME: substring
val example = "STRING_<selection>SAMPLE</selection>_BIG"
fun s(): String = "42"
@@ -1,3 +1,4 @@
// CHANGED_NAME: substring
val example = "STRING_${substring()}_BIG"
private fun substring(): String = "SAMPLE"
@@ -0,0 +1,26 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.k2.refactoring.introduce
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.kotlin.idea.fir.invalidateCaches
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.extractFunction.KotlinFirExtractFunctionHandler
import org.jetbrains.kotlin.idea.refactoring.introduce.AbstractInplaceIntroduceFunctionTest
import org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.AbstractExtractKotlinFunctionHandler
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.runAll
abstract class AbstractK2InplaceIntroduceFunctionTest : AbstractInplaceIntroduceFunctionTest() {
override fun isFirPlugin(): Boolean = true
override fun getExtractFunctionHandler(allContainersEnabled: Boolean): AbstractExtractKotlinFunctionHandler =
KotlinFirExtractFunctionHandler(allContainersEnabled)
override fun tearDown() {
runAll(
{ myFixture.project.invalidateCaches() },
{ super.tearDown() }
)
}
override fun getProjectDescriptor(): LightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.getInstance()
}
@@ -1,101 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.k2.refactoring.introduce
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.openapi.application.impl.NonBlockingReadActionImpl
import com.intellij.util.ui.UIUtil
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.extractFunction.KotlinFirExtractFunctionHandler
import org.jetbrains.kotlin.idea.test.IDEA_TEST_DATA_DIR
import org.jetbrains.kotlin.idea.test.JUnit3RunnerWithInners
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.junit.runner.RunWith
import com.intellij.java.refactoring.ExtractMethodAndDuplicatesInplaceTest.Companion.nextTemplateVariable
import com.intellij.java.refactoring.ExtractMethodAndDuplicatesInplaceTest.Companion.renameTemplate
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import java.io.File
@RunWith(JUnit3RunnerWithInners::class)
class FirInplaceExtractFunctionTest : KotlinLightCodeInsightFixtureTestCase() {
override fun isFirPlugin(): Boolean = true
override val testDataDirectory = IDEA_TEST_DATA_DIR.resolve("refactoring/extractFunctionInplace")
override fun getProjectDescriptor(): LightProjectDescriptor {
return KotlinWithJdkAndRuntimeLightProjectDescriptor.getInstance()
}
fun testStringTemplate() {
doTest()
}
fun testEmptySpaces() {
doTest()
}
fun testSearchForCallAfter() {
doTest()
}
fun testMoveLambdaOutsideParenthesis() {
doTest()
}
fun testMoveLambdaOutsideParenthesis1() {
doTest()
}
fun testSkipCommentsBefore() {
doTest()
}
fun testDontSkipWhitespacesBefore() {
doTest()
}
fun testExtractLocal() {
doTest()
}
fun testStringTemplateWithNameConflict() {
doTest(changedName = "substring")
}
fun testConflictNameNotAccepted() {
doTest(changedName = "conflict", checkResult = false)
assertTrue(getActiveTemplate() != null)
}
fun testConsecutiveDuplicates() {
doTest(changedName = "renamed")
}
fun testExposedAssignment() {
doTest(changedName = "getX")
}
fun doTest(changedName: String? = null, checkResult: Boolean = true) {
TemplateManagerImpl.setTemplateTesting(testRootDisposable)
myFixture.configureByFile("${getTestName(false)}.kt")
KotlinFirExtractFunctionHandler(getTestName(true).contains("Local")).invoke(myFixture.project, myFixture.editor, myFixture.file, null)
NonBlockingReadActionImpl.waitForAsyncTaskCompletion()
val template = getActiveTemplate()
require(template != null) { "Failed to start refactoring" }
if (changedName != null) {
renameTemplate(template, changedName)
}
nextTemplateVariable(template)
UIUtil.dispatchAllInvocationEvents()
if (checkResult) {
var expectedFile = "${getTestName(false)}.fir.after.kt"
if (!File(testDataDirectory, expectedFile).exists()) {
expectedFile = "${getTestName(false)}.after.kt"
}
myFixture.checkResultByFile(expectedFile)
}
}
private fun getActiveTemplate() = TemplateManagerImpl.getTemplateState(editor)
}
@@ -0,0 +1,85 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.k2.refactoring.introduce;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.idea.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.idea.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.jetbrains.kotlin.idea.base.test.TestRoot;
import org.junit.runner.RunWith;
/**
* This class is generated by {@link org.jetbrains.kotlin.testGenerator.generator.TestGenerator}.
* DO NOT MODIFY MANUALLY.
*/
@SuppressWarnings("all")
@TestRoot("refactorings/kotlin.refactorings.tests.k2")
@TestDataPath("$CONTENT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../idea/tests/testData/refactoring/extractFunctionInplace")
public class K2InplaceIntroduceFunctionTestGenerated extends AbstractK2InplaceIntroduceFunctionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("ConflictNameNotAccepted.kt")
public void testConflictNameNotAccepted() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/ConflictNameNotAccepted.kt");
}
@TestMetadata("ConsecutiveDuplicates.kt")
public void testConsecutiveDuplicates() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/ConsecutiveDuplicates.kt");
}
@TestMetadata("DontSkipWhitespacesBefore.kt")
public void testDontSkipWhitespacesBefore() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/DontSkipWhitespacesBefore.kt");
}
@TestMetadata("EmptySpaces.kt")
public void testEmptySpaces() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/EmptySpaces.kt");
}
@TestMetadata("ExposedAssignment.kt")
public void testExposedAssignment() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/ExposedAssignment.kt");
}
@TestMetadata("ExtractLocal.kt")
public void testExtractLocal() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/ExtractLocal.kt");
}
@TestMetadata("MoveLambdaOutsideParenthesis.kt")
public void testMoveLambdaOutsideParenthesis() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/MoveLambdaOutsideParenthesis.kt");
}
@TestMetadata("MoveLambdaOutsideParenthesis1.kt")
public void testMoveLambdaOutsideParenthesis1() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/MoveLambdaOutsideParenthesis1.kt");
}
@TestMetadata("SearchForCallAfter.kt")
public void testSearchForCallAfter() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/SearchForCallAfter.kt");
}
@TestMetadata("SkipCommentsBefore.kt")
public void testSkipCommentsBefore() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/SkipCommentsBefore.kt");
}
@TestMetadata("StringTemplate.kt")
public void testStringTemplate() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/StringTemplate.kt");
}
@TestMetadata("StringTemplateWithNameConflict.kt")
public void testStringTemplateWithNameConflict() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunctionInplace/StringTemplateWithNameConflict.kt");
}
}
@@ -107,6 +107,7 @@ import org.jetbrains.kotlin.idea.refactoring.inline.AbstractInlineMultiFileTest
import org.jetbrains.kotlin.idea.refactoring.inline.AbstractInlineTest
import org.jetbrains.kotlin.idea.refactoring.inline.AbstractInlineTestWithSomeDescriptors
import org.jetbrains.kotlin.idea.refactoring.introduce.AbstractExtractionTest
import org.jetbrains.kotlin.idea.refactoring.introduce.AbstractInplaceIntroduceFunctionTest
import org.jetbrains.kotlin.idea.refactoring.move.AbstractK1MultiModuleMoveTest
import org.jetbrains.kotlin.idea.refactoring.move.AbstractMoveTest
import org.jetbrains.kotlin.idea.refactoring.pullUp.AbstractPullUpTest
@@ -1150,7 +1151,7 @@ private fun assembleWorkspace(): TWorkspace = workspace {
testGroup("idea/tests", category = EXTRACT_REFACTORING) {
testClass<AbstractExtractionTest> {
model("refactoring/introduceVariable", pattern = KT_OR_KTS_WITHOUT_DOTS, testMethodName = "doIntroduceVariableTest")
model("refactoring/extractFunction", pattern = KT_OR_KTS, testMethodName = "doExtractFunctionTest", excludedDirectories = listOf("inplace"))
model("refactoring/extractFunction", pattern = KT_OR_KTS, testMethodName = "doExtractFunctionTest")
model("refactoring/introduceProperty", pattern = KT_OR_KTS, testMethodName = "doIntroducePropertyTest")
model("refactoring/introduceParameter", pattern = KT_OR_KTS, testMethodName = "doIntroduceSimpleParameterTest")
model("refactoring/introduceLambdaParameter", pattern = KT_OR_KTS, testMethodName = "doIntroduceLambdaParameterTest")
@@ -1161,6 +1162,9 @@ private fun assembleWorkspace(): TWorkspace = workspace {
model("refactoring/extractSuperclass", pattern = KT_OR_KTS_WITHOUT_DOTS, testMethodName = "doExtractSuperclassTest")
model("refactoring/extractInterface", pattern = KT_OR_KTS_WITHOUT_DOTS, testMethodName = "doExtractInterfaceTest")
}
testClass<AbstractInplaceIntroduceFunctionTest> {
model("refactoring/extractFunctionInplace")
}
}
testGroup("scripting-support", category = SCRIPTS) {
@@ -6,6 +6,7 @@ import org.jetbrains.kotlin.idea.k2.refactoring.bindToElement.AbstractK2BindToFq
import org.jetbrains.kotlin.idea.k2.refactoring.copy.AbstractK2CopyTest
import org.jetbrains.kotlin.idea.k2.refactoring.copy.AbstractK2MultiModuleCopyTest
import org.jetbrains.kotlin.idea.k2.refactoring.inline.AbstractKotlinFirInlineTest
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.AbstractK2InplaceIntroduceFunctionTest
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.AbstractK2IntroduceConstantTest
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.AbstractK2PsiUnifierTest
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.introduceVariable.AbstractK2IntroduceVariableTest
@@ -85,6 +86,10 @@ internal fun MutableTWorkspace.generateK2RefactoringsTests() {
model("refactoring/extractFunction", pattern = Patterns.KT_OR_KTS, testMethodName = "doExtractFunctionTest")
}
testClass<AbstractK2InplaceIntroduceFunctionTest> {
model("refactoring/extractFunctionInplace")
}
testClass<AbstractK2IntroduceParameterTest> {
model("refactoring/introduceParameter", pattern = Patterns.KT_OR_KTS, testMethodName = "doIntroduceParameterTest")
}