[extract method with object] add tests

GitOrigin-RevId: 0e2d72f613e0b8d1f396bd3a28ca32dd834893c7
This commit is contained in:
Alexandr Suhinin
2023-01-30 21:35:35 +00:00
committed by intellij-monorepo-bot
parent 17a785e6b6
commit 8a3a0b1d0b
17 changed files with 389 additions and 26 deletions
@@ -0,0 +1,12 @@
public class Test {
public static int main(boolean param) {
<selection>int x = 0;
int y = 0;
if (param) return -1;
if (Math.random() > 0.5) return -1;
System.out.println();</selection>
System.out.println("Point(" + x + ", " + y+ ")");
return 0;
}
}
@@ -0,0 +1,25 @@
import org.jetbrains.annotations.Nullable;
public class Test {
public static int main(boolean param) {
Result result = getResult(param);
if (result == null) return -1;
System.out.println("Point(" + result.x() + ", " + result.y() + ")");
return 0;
}
@Nullable
private static Result getResult(boolean param) {
int x = 0;
int y = 0;
if (param) return null;
if (Math.random() > 0.5) return null;
System.out.println();
Result result = new Result(x, y);
return result;
}
private record Result(int x, int y) {
}
}
@@ -0,0 +1,11 @@
public class Test {
public static void main(String[] args) {
<selection>int x = 42;
int y = 0;
System.out.println();
System.out.println();</selection>
x = (x + y)/2;
System.out.println("Point(" + x + ", " + y + ")");
}
}
@@ -0,0 +1,24 @@
import org.jetbrains.annotations.NotNull;
public class Test {
public static void main(String[] args) {
Result result = getResult();
int x;
x = (result.x() + result.y())/2;
System.out.println("Point(" + x + ", " + result.y() + ")");
}
@NotNull
private static Result getResult() {
int x = 42;
int y = 0;
System.out.println();
System.out.println();
Result result = new Result(x, y);
return result;
}
private record Result(int x, int y) {
}
}
@@ -0,0 +1,14 @@
public class Test<R> {
private R getR() {
return null;
}
public <T extends CharSequence> void main(String[] args, T param) {
<selection>T t = param;
R r = getR();
System.out.println();</selection>
System.out.println("Custom(" + t + ", " + r + ")");
}
}
@@ -0,0 +1,26 @@
import org.jetbrains.annotations.NotNull;
public class Test<R> {
private R getR() {
return null;
}
public <T extends CharSequence> void main(String[] args, T param) {
MyResult<T, R> myVariable = getTrMyResult(param);
System.out.println("Custom(" + myVariable.t() + ", " + myVariable.r() + ")");
}
@NotNull
private <T extends CharSequence> MyResult<T, R> getTrMyResult(T param) {
T t = param;
R r = getR();
System.out.println();
MyResult<T, R> myVariable = new MyResult<>(t, r);
return myVariable;
}
private record MyResult<T extends CharSequence, R>(T t, R r) {
}
}
@@ -0,0 +1,14 @@
public class Test<R> {
private R getR() {
return null;
}
public <T extends CharSequence> void main(String[] args, T param) {
<selection>T t = param;
R r = getR();
System.out.println();</selection>
System.out.println("Custom(" + t + ", " + r + ")");
}
}
@@ -0,0 +1,26 @@
import org.jetbrains.annotations.NotNull;
public class Test<R> {
private R getR() {
return null;
}
public <T extends CharSequence> void main(String[] args, T param) {
Result<T, R> result = getTrResult(param);
System.out.println("Custom(" + result.t() + ", " + result.r() + ")");
}
@NotNull
private <T extends CharSequence> Result<T, R> getTrResult(T param) {
T t = param;
R r = getR();
System.out.println();
Result<T, R> result = new Result<>(t, r);
return result;
}
private record Result<T extends CharSequence, R>(T t, R r) {
}
}
@@ -0,0 +1,10 @@
public class Test {
void test() {
<selection>int x = 0;
int y = 0;
System.out.println();</selection>
System.out.println("Point(" + x + ", " + y + ")");
}
}
@@ -0,0 +1,12 @@
public class Test {
void test() {
<selection>int x = 0;
int y = 0;
System.out.println();</selection>
System.out.println("Point(" + x + ", " + y + ")");
}
record Conflict(int x) {}
}
@@ -0,0 +1,10 @@
public class Test {
void test() {
<selection>int x = 0;
int y = 0;
System.out.println();</selection>
System.out.println("Point(" + x + ", " + y + ")");
}
}
@@ -0,0 +1,11 @@
public class Test {
void test() {
int conflict = 42;
<selection>int x = 0;
int y = 0;
System.out.println();</selection>
System.out.println("Point(" + x + ", " + y + ")");
}
}
@@ -0,0 +1,10 @@
public class Test {
void test() {
<selection>int x = 0;
int y = 0;
System.out.println();</selection>
System.out.println("Point(" + x + ", " + y + ")");
}
}
@@ -0,0 +1,29 @@
import org.jetbrains.annotations.NotNull;
public class Test {
void test() {
Result result = getResult();
System.out.println("Point(" + result.x + ", " + result.y + ")");
}
@NotNull
private static Result getResult() {
int x = 0;
int y = 0;
System.out.println();
Result result = new Result(x, y);
return result;
}
private static class Result {
public final int x;
public final int y;
public Result(int x, int y) {
this.x = x;
this.y = y;
}
}
}
@@ -0,0 +1,10 @@
public class Test {
void test() {
<selection>int x = 0;
int y = 0;
System.out.println();</selection>
System.out.println("Point(" + x + ", " + y + ")");
}
}
@@ -0,0 +1,22 @@
import org.jetbrains.annotations.NotNull;
public class Test {
void test() {
Result result = getResult();
System.out.println("Point(" + result.x() + ", " + result.y() + ")");
}
@NotNull
private static Result getResult() {
int x = 0;
int y = 0;
System.out.println();
Result result = new Result(x, y);
return result;
}
private record Result(int x, int y) {
}
}
@@ -44,18 +44,26 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
}
fun testInvalidRename(){
doTest(changedName = "invalid! name", checkResults = false)
doTest {
renameTemplate("invalid! name")
nextTemplateVariable()
}
require(getActiveTemplate() != null)
}
fun testConflictRename(){
doTest(changedName = "conflict", checkResults = false)
doTest {
renameTemplate("conflict")
nextTemplateVariable()
}
require(getActiveTemplate() != null)
}
fun testValidRename(){
doTest(changedName = "valid")
require(getActiveTemplate() == null)
doTest {
renameTemplate("valid")
nextTemplateVariable()
}
}
fun testGeneratedDefault(){
@@ -63,11 +71,17 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
}
fun testRenamedExactDuplicate(){
doTest(changedName = "renamed")
doTest {
renameTemplate("renamed")
nextTemplateVariable()
}
}
fun testRenamedParametrizedDuplicate(){
doTest(changedName = "averageWithOffset")
doTest {
renameTemplate("averageWithOffset")
nextTemplateVariable()
}
}
fun testStaticMustBePlaced(){
@@ -81,7 +95,10 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
}
fun testThreeDuplicates(){
doTest(changedName = "sayHello")
doTest {
renameTemplate("sayHello")
nextTemplateVariable()
}
}
fun testParameterGrouping(){
@@ -217,7 +234,10 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
}
fun testTemplateRenamesInsertedCallOnly(){
doTest(changedName = "renamed")
doTest {
renameTemplate("renamed")
nextTemplateVariable()
}
}
fun testSignatureChangeIsNotAvoided() {
@@ -271,6 +291,78 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
}
}
fun testIntroduceSimpleObject(){
IdeaTestUtil.withLevel(module, LanguageLevel.JDK_1_8) {
doTest()
}
}
fun testIntroduceSimpleRecord(){
doTest()
}
fun testIntroduceNullableObject(){
doTest()
}
fun testIntroduceObjectWithAssignments(){
doTest()
}
fun testIntroduceObjectWithTypeParameters(){
doTest()
}
fun testIntroduceObjectWithRename(){
doTest {
renameTemplate("MyResult")
nextTemplateVariable()
renameTemplate("myVariable")
nextTemplateVariable()
nextTemplateVariable()
}
}
fun testIntroduceObjectWithWrongClassname1(){
doTest {
renameTemplate("Wrong !")
nextTemplateVariable()
nextTemplateVariable()
nextTemplateVariable()
}
require(getActiveTemplate() != null)
}
fun testIntroduceObjectWithWrongClassname2(){
doTest {
renameTemplate("Conflict")
nextTemplateVariable()
nextTemplateVariable()
nextTemplateVariable()
}
require(getActiveTemplate() != null)
}
fun testIntroduceObjectWithWrongVariableName1(){
doTest {
nextTemplateVariable()
renameTemplate("wrong !")
nextTemplateVariable()
nextTemplateVariable()
}
require(getActiveTemplate() != null)
}
fun testIntroduceObjectWithWrongVariableName2(){
doTest {
nextTemplateVariable()
renameTemplate("conflict")
nextTemplateVariable()
nextTemplateVariable()
}
require(getActiveTemplate() != null)
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")
@@ -288,27 +380,30 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
override fun conflictsDetected(refactoringId: String, conflictsData: RefactoringEventData) = Unit
override fun undoRefactoring(refactoringId: String) = Unit
})
val template = startRefactoring(editor)
startRefactoring(editor)
require(startReceived)
finishTemplate(template)
nextTemplateVariable()
require(doneReceived)
}
}
private fun doTest(checkResults: Boolean = true, changedName: String? = null){
private fun doTest(runnable: (TemplateState) -> Unit = { finishTemplate() }){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")
val template = startRefactoring(editor)
if (changedName != null) {
renameTemplate(template, changedName)
}
finishTemplate(template)
if (checkResults) {
runnable.invoke(template)
if (getActiveTemplate() == null) {
checkResultByFile("$BASE_PATH/${getTestName(false)}_after.java")
}
}
}
private fun finishTemplate(){
do {
val isVariableSwitched = nextTemplateVariable()
} while (isVariableSwitched)
}
private inline fun runAndRevertSettings(action: () -> Unit) {
val settings = JavaRefactoringSettings.getInstance()
val defaultStatic = settings.EXTRACT_STATIC_METHOD
@@ -329,6 +424,7 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
private fun startRefactoring(editor: Editor): TemplateState {
val selection = with(editor.selectionModel) { TextRange(selectionStart, selectionEnd) }
MethodExtractor().doExtract(file, selection)
UIUtil.dispatchAllInvocationEvents()
val templateState = getActiveTemplate()
require(templateState != null) { "Failed to start refactoring" }
return templateState
@@ -336,18 +432,19 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
private fun getActiveTemplate() = TemplateManagerImpl.getTemplateState(editor)
private fun finishTemplate(templateState: TemplateState){
try {
LookupManager.getActiveLookup(templateState.editor)?.hideLookup(true)
val dataContext = DataManager.getInstance().getDataContext(editor.component)
val event = AnActionEvent.createFromDataContext(ActionPlaces.UNKNOWN, Presentation(), dataContext)
ActionManager.getInstance().getAction(IdeActions.ACTION_EDITOR_NEXT_TEMPLATE_VARIABLE).actionPerformed(event)
UIUtil.dispatchAllInvocationEvents()
} catch (ignore: RefactoringErrorHintException) {
}
private fun nextTemplateVariable(): Boolean {
val templateState = getActiveTemplate() ?: return false
val previousRange = templateState.currentVariableRange
LookupManager.getActiveLookup(templateState.editor)?.hideLookup(true)
val dataContext = DataManager.getInstance().getDataContext(editor.component)
val event = AnActionEvent.createFromDataContext(ActionPlaces.UNKNOWN, Presentation(), dataContext)
ActionManager.getInstance().getAction(IdeActions.ACTION_EDITOR_NEXT_TEMPLATE_VARIABLE).actionPerformed(event)
UIUtil.dispatchAllInvocationEvents()
return templateState.isFinished || templateState.currentVariableRange != previousRange
}
private fun renameTemplate(templateState: TemplateState, name: String) {
private fun renameTemplate(name: String) {
val templateState = getActiveTemplate() ?: return
WriteCommandAction.runWriteCommandAction(project) {
val range = templateState.currentVariableRange!!
editor.document.replaceString(range.startOffset, range.endOffset, name)