use of spoon.test.refactoring.testclasses.CtRenameLocalVariableRefactoringTestSubject in project spoon by INRIA.
the class CtRenameLocalVariableRefactoringTest method testRenameAllLocalVariablesOfRenameTestSubject.
/**
* The {@link CtRenameLocalVariableRefactoringTestSubject} class is loaded as spoon model. Then:
* - It looks for each CtVariable and it's CtAnnotation and tries to rename that variable to the name defined by annotation.
* - If the annotation name is prefixed with "-", then that refactoring should fail.
* - If the annotation name is not prefixed, then that refactoring should pass.
* If it behaves different then expected, then this test fails
*/
@Test
public void testRenameAllLocalVariablesOfRenameTestSubject() throws Exception {
final Launcher launcher = new Launcher();
final SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + CtRenameLocalVariableRefactoringTestSubject.class.getName().replace('.', '/') + ".java"));
comp.build();
final Factory factory = comp.getFactory();
CtClass<?> varRenameClass = (CtClass<?>) factory.Type().get(CtRenameLocalVariableRefactoringTestSubject.class);
CtTypeReference<TestTryRename> tryRename = varRenameClass.getFactory().createCtTypeReference(TestTryRename.class);
varRenameClass.getMethods().forEach(method -> {
// debugging support
if (DEBUG.length == 3 && DEBUG[0].equals(method.getSimpleName()) == false)
return;
method.filterChildren((CtVariable var) -> true).map((CtVariable var) -> var.getAnnotation(tryRename)).forEach((CtAnnotation<TestTryRename> annotation) -> {
String[] newNames = annotation.getActualAnnotation().value();
CtVariable<?> targetVariable = (CtVariable<?>) annotation.getAnnotatedElement();
for (String newName : newNames) {
boolean renameShouldPass = newName.startsWith("-") == false;
if (!renameShouldPass) {
newName = newName.substring(1);
}
if (targetVariable instanceof CtLocalVariable<?>) {
// debugging support
if (DEBUG.length == 3 && DEBUG[1].equals(targetVariable.getSimpleName()) && DEBUG[2].equals(newName)) {
// put breakpoint here and continue debugging of the buggy case
this.getClass();
}
checkLocalVariableRename(launcher, (CtLocalVariable<?>) targetVariable, newName, renameShouldPass);
} else {
// TODO test rename of other variables, e.g. parameters and catch... later
}
}
});
});
}
Aggregations