use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class AnonymousExecutableTest method testStatements.
@Test
public void testStatements() throws Exception {
CtType<?> type = build("spoon.test.model", "AnonymousExecutableClass");
CtAnonymousExecutable anonexec = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
List<CtStatement> stats = anonexec.getBody().getStatements();
assertEquals(1, stats.size());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class BlockTest method testAddEmptyBlock.
@Test
public void testAddEmptyBlock() {
// specifies a bug found by Benoit Cornu on August 7 2014
Factory factory = createFactory();
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " " + "}};").compile();
CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
// empty block (immutable EMPTY_LIST())
CtBlock<?> body = foo.getBody();
CtCodeSnippetStatement snippet = factory.Core().createCodeSnippetStatement();
List<CtStatement> statements = body.getStatements();
statements.add(snippet);
assertEquals(snippet, body.getStatement(0));
// plus implicit assertion: no exception
CtCodeSnippetStatement snippet2 = factory.Core().createCodeSnippetStatement();
body.getStatements().add(snippet2);
assertEquals(snippet2, body.getStatement(1));
assertEquals(2, body.getStatements().size());
CtCodeSnippetStatement snippet3 = factory.Core().createCodeSnippetStatement();
statements.add(snippet3);
assertEquals(snippet3, body.getStatement(2));
assertEquals(3, body.getStatements().size());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class BlockTest method testIterationStatements.
@Test
public void testIterationStatements() {
Factory factory = createFactory();
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " int x=0;int y=0;" + "}};").compile();
CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
CtBlock<?> body = foo.getBody();
assertEquals(2, body.getStatements().size());
List<CtStatement> l = new ArrayList<CtStatement>();
// this compiles (thanks to the new CtBlock extends CtStatementList)
for (CtStatement s : body) {
l.add(s);
}
assertTrue(body.getStatements().equals(l));
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class SwitchCaseTest method testIterationStatements.
@Test
public void testIterationStatements() {
Factory factory = createFactory();
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " int x=0;" + "switch(x) {" + "case 0: x=x+1;break;" + "case 1: x=0;" + "default: x=-1;" + "}" + "}};").compile();
CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
CtSwitch<?> sw = foo.getElements(new TypeFilter<CtSwitch<?>>(CtSwitch.class)).get(0);
assertEquals(3, sw.getCases().size());
CtCase<?> c = (CtCase<?>) sw.getCases().get(0);
assertEquals(0, ((CtLiteral<?>) c.getCaseExpression()).getValue());
assertEquals(2, c.getStatements().size());
List<CtStatement> l = new ArrayList<CtStatement>();
// this compiles (thanks to the new CtCase extends CtStatementList)
for (CtStatement s : c) {
l.add(s);
}
assertTrue(c.getStatements().equals(l));
}
Aggregations