inplace introduce api: inplace introduce parameter: tests

This commit is contained in:
anna
2011-05-26 16:08:20 +04:00
parent 49281d78ec
commit 6f3a5a0578
11 changed files with 242 additions and 43 deletions
@@ -0,0 +1,6 @@
class Test {
void simpleMethod() {
System.out.println("<caret>");
System.out.println("");
}
}
@@ -0,0 +1,5 @@
class Test {
void simpleMethod() {
boolean b<caret>b;
}
}
@@ -0,0 +1,5 @@
class Test {
void simpleMethod() {
boolean <caret>bb;
}
}
@@ -0,0 +1,6 @@
class Test {
void simpleMethod() {
System.out.println(<caret>"");
System.out.println("");
}
}
@@ -0,0 +1,6 @@
class Test {
void foo() {
System.out.println("hello");
System.out.println("hel<caret>lo");
}
}
@@ -0,0 +1,6 @@
class Test {
void foo(String hello) {
System.out.println(hello);
System.out.println(<caret>hello);
}
}
@@ -0,0 +1,135 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.refactoring;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pass;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.introduceParameter.InplaceIntroduceParameterPopup;
import com.intellij.refactoring.introduceParameter.IntroduceParameterHandler;
import com.intellij.testFramework.LightCodeInsightTestCase;
import org.jetbrains.annotations.NotNull;
/**
* User: anna
* Date: 3/16/11
*/
public class InplaceIntroduceParameterTest extends LightCodeInsightTestCase {
private static final String BASE_PATH = "/refactoring/inplaceIntroduceParameter/";
public void testReplaceAll() throws Exception {
doTest(new Pass<InplaceIntroduceParameterPopup>() {
@Override
public void pass(InplaceIntroduceParameterPopup inplaceIntroduceFieldPopup) {
inplaceIntroduceFieldPopup.setReplaceAllOccurrences(true);
}
});
}
private void doTest(final Pass<InplaceIntroduceParameterPopup> pass) throws Exception {
String name = getTestName(true);
configureByFile(BASE_PATH + name + ".java");
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject());
try {
templateManager.setTemplateTesting(true);
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
final MyIntroduceParameterHandler introduceParameterHandler = new MyIntroduceParameterHandler();
final PsiExpression expression =
PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiExpression.class);
if (expression != null) {
introduceParameterHandler.invokeImpl(getProject(), expression, getEditor());
} else {
final PsiLocalVariable localVariable =
PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiLocalVariable.class);
assertNotNull(localVariable);
introduceParameterHandler.invokeImpl(getProject(), localVariable, getEditor());
}
pass.pass(introduceParameterHandler.getInplaceIntroduceParameterPopup());
TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
assert state != null;
state.gotoEnd(false);
checkResultByFile(BASE_PATH + name + "_after.java");
}
finally {
myEditor.getSettings().setVariableInplaceRenameEnabled(enabled);
templateManager.setTemplateTesting(false);
}
}
public void testEscapePosition() throws Exception {
doTestEscape();
}
public void testEscapePositionOnLocal() throws Exception {
doTestEscape();
}
private void doTestEscape() throws Exception {
String name = getTestName(true);
configureByFile(BASE_PATH + name + ".java");
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject());
try {
templateManager.setTemplateTesting(true);
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
final MyIntroduceParameterHandler introduceParameterHandler = new MyIntroduceParameterHandler();
final PsiExpression expression =
PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiExpression.class);
if (expression != null) {
introduceParameterHandler.invokeImpl(getProject(), expression, getEditor());
} else {
final PsiLocalVariable localVariable =
PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiLocalVariable.class);
assertNotNull(localVariable);
introduceParameterHandler.invokeImpl(getProject(), localVariable, getEditor());
}
TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
assert state != null;
state.gotoEnd(true);
checkResultByFile(BASE_PATH + name + "_after.java");
}
finally {
myEditor.getSettings().setVariableInplaceRenameEnabled(enabled);
templateManager.setTemplateTesting(false);
}
}
private static class MyIntroduceParameterHandler extends IntroduceParameterHandler {
@Override
public boolean invokeImpl(Project project, @NotNull PsiExpression selectedExpr, Editor editor) {
return super.invokeImpl(project, selectedExpr, editor);
}
@Override
public boolean invokeImpl(Project project, PsiLocalVariable localVariable, Editor editor) {
return super.invokeImpl(project, localVariable, editor);
}
}
}
@@ -11,8 +11,6 @@ package com.intellij.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLocalVariable;
@@ -44,8 +42,11 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
private void doTest(int replaceFieldsWithGetters, boolean removeUnusedParameters, boolean searchForSuper, boolean declareFinal, final boolean generateDelegate,
String conflict) throws Exception {
boolean enabled = true;
try {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
enabled = myEditor.getSettings().isVariableInplaceRenameEnabled();
myEditor.getSettings().setVariableInplaceRenameEnabled(false);
perform(true, replaceFieldsWithGetters, "anObject", searchForSuper, declareFinal, removeUnusedParameters, generateDelegate);
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
if (conflict != null) {
@@ -57,6 +58,8 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
throw e;
}
assertEquals(conflict, e.getMessage());
} finally {
myEditor.getSettings().setVariableInplaceRenameEnabled(enabled);
}
}
@@ -270,14 +273,23 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
private void doTestThroughHandler() throws Exception {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
new IntroduceParameterHandler().invoke(getProject(), myEditor, myFile, new DataContext() {
@Override
@Nullable
public Object getData(@NonNls final String dataId) {
return null;
}
});
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
boolean enabled = true;
try {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
enabled = myEditor.getSettings().isVariableInplaceRenameEnabled();
myEditor.getSettings().setVariableInplaceRenameEnabled(false);
new IntroduceParameterHandler().invoke(getProject(), myEditor, myFile, new DataContext() {
@Override
@Nullable
public Object getData(@NonNls final String dataId) {
return null;
}
});
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
}
finally {
myEditor.getSettings().setVariableInplaceRenameEnabled(enabled);
}
}
public void testEnclosingWithParamDeletion() throws Exception {