Search in sources :

Example 1 with CtRenameLocalVariableRefactoring

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
    }
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) CtType(spoon.reflect.declaration.CtType) SpoonException(spoon.SpoonException) Test(org.junit.Test)

Example 2 with CtRenameLocalVariableRefactoring

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());
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) CtType(spoon.reflect.declaration.CtType) Test(org.junit.Test)

Example 3 with CtRenameLocalVariableRefactoring

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());
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) SpoonException(spoon.SpoonException)

Example 4 with CtRenameLocalVariableRefactoring

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);
    }
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) SpoonException(spoon.SpoonException)

Aggregations

CtRenameLocalVariableRefactoring (spoon.refactoring.CtRenameLocalVariableRefactoring)4 SpoonException (spoon.SpoonException)3 Test (org.junit.Test)2 CtType (spoon.reflect.declaration.CtType)2