[inplace extract function] KTIJ-21682 KTIJ-21681: test inplace mode

GitOrigin-RevId: b195ff183305395820f33bd947623878dc21f146
This commit is contained in:
Alexandr Suhinin
2022-05-05 11:14:37 +00:00
committed by intellij-monorepo-bot
parent dffe7781bf
commit b2c836a40c
11 changed files with 118 additions and 0 deletions
@@ -0,0 +1,68 @@
// 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.codeInsight.template.impl.TemplateState
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.testFramework.TestDataPath
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.JUnit3RunnerWithInners
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.TestRoot
import org.jetbrains.kotlin.test.TestMetadata
import org.junit.runner.RunWith
@TestRoot("idea/tests")
@TestDataPath("\$CONTENT_ROOT")
@TestMetadata("testData/refactoring/extractFunction/inplace")
@RunWith(JUnit3RunnerWithInners::class)
class InplaceExtractFunctionTest : KotlinLightCodeInsightFixtureTestCase() {
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)
val template = getActiveTemplate()
require(template != null) { "Failed to start refactoring" }
if (changedName != null) {
renameTemplate(template, changedName)
}
template.gotoEnd(false)
UIUtil.dispatchAllInvocationEvents()
if (checkResult) {
myFixture.checkResultByFile("${getTestName(false)}.after.kt")
}
}
private fun getActiveTemplate() = TemplateManagerImpl.getTemplateState(editor)
private fun renameTemplate(templateState: TemplateState, name: String) {
WriteCommandAction.runWriteCommandAction(project) {
val range = templateState.currentVariableRange!!
editor.document.replaceString(range.startOffset, range.endOffset, name)
}
}
}
@@ -0,0 +1,3 @@
fun sample(){
<selection>println("42")</selection>
}
@@ -0,0 +1,6 @@
fun sample(){
<selection>println("42")</selection>
}
fun conflict(){
}
@@ -0,0 +1,8 @@
fun sample(){
renamed<caret>()
renamed()
}
private fun renamed() {
println("duplicate")
}
@@ -0,0 +1,4 @@
fun sample(){
<selection>println("duplicate")</selection>
println("duplicate")
}
@@ -0,0 +1,11 @@
fun sample(){
val x = getX<caret>()
println(x)
}
private fun getX(): Int {
val x = 42
println()
return x
}
@@ -0,0 +1,6 @@
fun sample(){
<selection>val x = 42
println()</selection>
println(x)
}
@@ -0,0 +1,3 @@
val example = "STRING_${extracted<caret>()}_BIG"
private fun extracted() = "SAMPLE"
@@ -0,0 +1 @@
val example = "STRING_<selection>SAMPLE</selection>_BIG"
@@ -0,0 +1,5 @@
val example = "STRING_${substring<caret>()}_BIG"
private fun substring() = "SAMPLE"
fun extracted(): String = "42"
@@ -0,0 +1,3 @@
val example = "STRING_<selection>SAMPLE</selection>_BIG"
fun extracted(): String = "42"