use of spoon.refactoring.CtRenameLocalVariableRefactoring in project spoon by INRIA.
the class CtRenameLocalVariableRefactoringTest method testRefactorWrongUsage.
@Test
public void testRefactorWrongUsage() throws Exception {
CtType varRenameClass = ModelUtils.buildClass(CtRenameLocalVariableRefactoringTestSubject.class);
CtLocalVariable<?> local1Var = varRenameClass.filterChildren((CtLocalVariable<?> var) -> var.getSimpleName().equals("local1")).first();
// contract: a target variable is not defined. Throw SpoonException
CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
refactor.setNewName("local1");
try {
refactor.refactor();
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to empty string. Throw SpoonException
refactor.setTarget(local1Var);
try {
refactor.setNewName("");
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to variable name which contains space. Throw SpoonException
try {
refactor.setNewName("x ");
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to variable name which contains space. Throw SpoonException
try {
refactor.setNewName("x y");
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to variable name which contains character which is not allowed in variable name. Throw SpoonException
try {
refactor.setNewName("x(");
fail();
} catch (SpoonException e) {
// should fail - OK
}
}
use of spoon.refactoring.CtRenameLocalVariableRefactoring in project spoon by INRIA.
the class CtRenameLocalVariableRefactoringTest method testRenameLocalVariableToSameName.
@Test
public void testRenameLocalVariableToSameName() throws Exception {
CtType varRenameClass = ModelUtils.buildClass(CtRenameLocalVariableRefactoringTestSubject.class);
CtLocalVariable<?> local1Var = varRenameClass.filterChildren((CtLocalVariable<?> var) -> var.getSimpleName().equals("local1")).first();
CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
refactor.setTarget(local1Var);
refactor.setNewName("local1");
refactor.refactor();
assertEquals("local1", local1Var.getSimpleName());
}
use of spoon.refactoring.CtRenameLocalVariableRefactoring in project spoon by INRIA.
the class CtRenameLocalVariableRefactoringTest method checkLocalVariableRename.
protected void checkLocalVariableRename(Launcher launcher, CtLocalVariable<?> targetVariable, String newName, boolean renameShouldPass) {
String originName = targetVariable.getSimpleName();
CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
refactor.setTarget(targetVariable);
refactor.setNewName(newName);
if (renameShouldPass) {
try {
refactor.refactor();
} catch (SpoonException e) {
throw new AssertionError(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" should NOT fail when trying rename to \"" + newName + "\"\n" + targetVariable.toString(), e);
}
assertEquals(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" to \"" + newName + "\" passed, but the name of variable was not changed", newName, targetVariable.getSimpleName());
assertCorrectModel(launcher, getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" to \"" + newName + "\"");
} else {
try {
refactor.refactor();
fail(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" should fail when trying rename to \"" + newName + "\"");
} catch (SpoonException e) {
}
assertEquals(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" failed when trying rename to \"" + newName + "\" but the name of variable should not be changed", originName, targetVariable.getSimpleName());
}
if (renameShouldPass) {
rollback(targetVariable, originName);
}
assertEquals(originName, targetVariable.getSimpleName());
}
use of spoon.refactoring.CtRenameLocalVariableRefactoring in project spoon by INRIA.
the class CtRenameLocalVariableRefactoringTest method rollback.
private void rollback(CtLocalVariable<?> targetVariable, String originName) {
String newName = targetVariable.getSimpleName();
CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
refactor.setTarget(targetVariable);
// rollback changes
refactor.setNewName(originName);
try {
refactor.refactor();
} catch (SpoonException e) {
throw new AssertionError(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" to \"" + newName + "\" passed, but rename back to \"" + originName + "\" failed", e);
}
}
Aggregations