Introduce variable tests fixed for multi-catch type

This commit is contained in:
Roman Shevchenko
2011-03-22 19:32:43 +01:00
parent 2270811fba
commit 00292e4696
4 changed files with 28 additions and 18 deletions

View File

@@ -13,14 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Created by IntelliJ IDEA.
* User: dsl
* Date: Nov 15, 2002
* Time: 5:21:33 PM
* To change this template use Options | File Templates.
*/
package com.intellij.refactoring.introduceVariable;
import com.intellij.codeInsight.CodeInsightUtil;
@@ -72,6 +64,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* @author dsl
* Date: Nov 15, 2002
*/
public abstract class IntroduceVariableBase extends IntroduceHandlerBase implements RefactoringActionHandler {
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceVariable.IntroduceVariableBase");
@NonNls private static final String PREFER_STATEMENTS_OPTION = "introduce.variable.prefer.statements";
@@ -848,6 +844,4 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme
conflicts.putValue(occurence, RefactoringBundle.message("introducing.variable.may.break.code.logic"));
}
}
}

View File

@@ -6,7 +6,7 @@ class C {
void m() {
try { }
catch (E1 | E2 ex) {
final B<? extends Number> b = ex;
final Exception b = ex;
}
}
}

View File

@@ -222,7 +222,7 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
}
public void testSiblingInnerClassType() throws Exception {
doTest(new MockIntroduceVariableHandler("vari", true, false, false, "A.B"){
doTest(new MockIntroduceVariableHandler("vari", true, false, false, "A.B") {
@Override
public IntroduceVariableSettings getSettings(Project project, Editor editor,
PsiExpression expr, PsiExpression[] occurrences,
@@ -249,11 +249,11 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
}
public void testMultiCatchSimple() throws Exception {
doTest(new MockIntroduceVariableHandler("e", true, true, false, "C.E1 | C.E2"));
doTest(new MockIntroduceVariableHandler("e", true, true, false, "java.lang.Exception", true));
}
public void testMultiCatchTyped() throws Exception {
doTest(new MockIntroduceVariableHandler("b", true, true, false, "C.E1 | C.E2"));
doTest(new MockIntroduceVariableHandler("b", true, true, false, "java.lang.Exception", true));
}
private void doTest(IntroduceVariableBase testMe) throws Exception {

View File

@@ -21,19 +21,27 @@ class MockIntroduceVariableHandler extends IntroduceVariableBase {
private final boolean myDeclareFinal;
private final boolean myReplaceLValues;
private final String myExpectedTypeCanonicalName;
private final boolean myLookForType;
public MockIntroduceVariableHandler(@NonNls final String name, final boolean replaceAll,
final boolean declareFinal, final boolean replaceLValues,
@NonNls final String expectedTypeCanonicalName) {
final boolean declareFinal, final boolean replaceLValues,
@NonNls final String expectedTypeCanonicalName) {
this(name, replaceAll, declareFinal, replaceLValues, expectedTypeCanonicalName, false);
}
public MockIntroduceVariableHandler(@NonNls final String name, final boolean replaceAll,
final boolean declareFinal, final boolean replaceLValues,
@NonNls final String expectedTypeCanonicalName, boolean lookForType) {
myName = name;
myReplaceAll = replaceAll;
myDeclareFinal = declareFinal;
myReplaceLValues = replaceLValues;
myExpectedTypeCanonicalName = expectedTypeCanonicalName;
myLookForType = lookForType;
}
@Override
public IntroduceVariableSettings getSettings(Project project, Editor editor,
PsiExpression expr, final PsiExpression[] occurrences,
@@ -42,7 +50,8 @@ class MockIntroduceVariableHandler extends IntroduceVariableBase {
boolean anyAssignmentLHS,
InputValidator validator,
final OccurrencesChooser.ReplaceChoice replaceChoice) {
final PsiType type = typeSelectorManager.getDefaultType();
final PsiType type = myLookForType ? findType(typeSelectorManager.getTypesForAll(), typeSelectorManager.getDefaultType())
: typeSelectorManager.getDefaultType();
Assert.assertTrue(type.getCanonicalText(), type.equalsToText(myExpectedTypeCanonicalName));
IntroduceVariableSettings introduceVariableSettings = new IntroduceVariableSettings() {
@Override
@@ -93,4 +102,11 @@ class MockIntroduceVariableHandler extends IntroduceVariableBase {
protected boolean reportConflicts(final MultiMap<PsiElement,String> conflicts, final Project project, IntroduceVariableSettings dialog) {
return false;
}
private PsiType findType(final PsiType[] candidates, PsiType defaultType) {
for (PsiType candidate : candidates) {
if (candidate.equalsToText(myExpectedTypeCanonicalName)) return candidate;
}
return defaultType;
}
}