use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class DeleteTest method testDeleteStatementInCase.
@Test
public void testDeleteStatementInCase() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod method = adobada.getMethod("m3");
final CtCase aCase = method.getElements(new TypeFilter<>(CtCase.class)).get(0);
assertEquals(2, aCase.getStatements().size());
final CtStatement statement = aCase.getStatements().get(1);
statement.delete();
assertEquals(1, aCase.getStatements().size());
assertFalse(aCase.getStatements().contains(statement));
}
use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class DeleteTest method testDeleteACaseOfASwitch.
@Test
public void testDeleteACaseOfASwitch() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod method = adobada.getMethod("m3");
final CtSwitch aSwitch = method.getElements(new TypeFilter<>(CtSwitch.class)).get(0);
final CtCase aCase = (CtCase) aSwitch.getCases().get(1);
assertEquals(2, aSwitch.getCases().size());
aCase.delete();
assertEquals(1, aSwitch.getCases().size());
assertFalse(aSwitch.getCases().contains(aCase));
}
use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class SwitchCaseTest method insertAfterStatementInSwitchCaseWithoutException.
@Test
public void insertAfterStatementInSwitchCaseWithoutException() throws Exception {
String packageName = "spoon.test.ctCase";
String className = "ClassWithSwitchExample";
Factory factory = factoryFor(packageName, className);
List<CtCase> elements = elementsOfType(CtCase.class, factory);
assertEquals(3, elements.size());
CtCase firstCase = elements.get(0);
List<CtStatement> statements = firstCase.getStatements();
assertEquals(2, statements.size());
CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
statements.get(0).insertAfter(newStatement);
statements = firstCase.getStatements();
assertEquals(3, statements.size());
assertTrue(statements.get(1) == newStatement);
}
use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class TestCtBlock method testAddStatementInCase.
@Test
public void testAddStatementInCase() {
// contract: we can add a statement at a specific index
// the statements with a higher index are pushed after
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
spoon.buildModel();
CtClass<?> toto = spoon.getFactory().Class().get(Toto.class);
CtMethod bar = toto.getMethodsByName("bar").get(0);
CtSwitch<?> ctSwitch = (CtSwitch) bar.getBody().getStatement(0);
CtCase firstCase = ctSwitch.getCases().get(0);
CtCase originalFirstCase = firstCase.clone();
CtCase secondCase = ctSwitch.getCases().get(1);
CtCase originalSecondCase = secondCase.clone();
assertEquals(3, firstCase.getStatements().size());
firstCase.addStatement(3, spoon.getFactory().createBreak());
assertEquals(4, firstCase.getStatements().size());
for (int i = 0; i < 3; i++) {
assertEquals(originalFirstCase.getStatements().get(i), firstCase.getStatements().get(i));
}
assertEquals(2, secondCase.getStatements().size());
secondCase.addStatement(1, (CtStatement) spoon.getFactory().createInvocation().setExecutable(bar.getReference()));
assertEquals(3, secondCase.getStatements().size());
assertEquals(originalSecondCase.getStatements().get(0), secondCase.getStatements().get(0));
assertEquals(originalSecondCase.getStatements().get(1), secondCase.getStatements().get(2));
}
use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class SwitchCaseTest method insertBeforeStatementInSwitchCaseWithoutException.
@Test
public void insertBeforeStatementInSwitchCaseWithoutException() throws Exception {
String packageName = "spoon.test.ctCase";
String className = "ClassWithSwitchExample";
Factory factory = factoryFor(packageName, className);
List<CtCase> elements = elementsOfType(CtCase.class, factory);
assertEquals(3, elements.size());
CtCase firstCase = elements.get(0);
List<CtStatement> statements = firstCase.getStatements();
assertEquals(2, statements.size());
CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
statements.get(0).insertBefore(newStatement);
statements = firstCase.getStatements();
assertEquals(3, statements.size());
assertTrue(statements.get(0) == newStatement);
}
Aggregations