use of spoon.reflect.code.CtBlock in project spoon by INRIA.
the class PositionTest method testPositionStatement.
@Test
public void testPositionStatement() throws Exception {
final Factory build = build(FooStatement.class);
final CtType<FooStatement> foo = build.Type().get(FooStatement.class);
String classContent = getClassContent(foo);
CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
CtBlock<?> body = method1.getBody();
SourcePosition positionBody = body.getPosition();
assertEquals(7, positionBody.getLine());
assertEquals(23, positionBody.getEndLine());
assertEquals("{\n" + "\t\tint field2 = m2(parm1);\n" + "\t\tthis.field = m2(parm1);\n" + "\t\tif(parm1 > 2 && true) {\n" + "\t\t\tswitch (parm1) {\n" + "\t\t\tcase 1:\n" + "\t\t\t\treturn;\n" + "\t\t\tdefault:\n" + "\t\t\t\tparm1++;\n" + "\t\t\t}\n" + "\t\t\tint count = 0;\n" + "\t\t\tfor (int i =0; i< parm1; i++) {\n" + "\t\t\t\tcount ++;\n" + "\t\t\t}\n" + "\t\t}\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, positionBody));
SourcePosition positionLocalVariable = body.getStatement(0).getPosition();
assertEquals(8, positionLocalVariable.getLine());
assertEquals(8, positionLocalVariable.getEndLine());
assertEquals("int field2 = m2(parm1);", contentAtPosition(classContent, positionLocalVariable));
SourcePosition positionFieldWrite = body.getStatement(1).getPosition();
assertEquals(9, positionFieldWrite.getLine());
assertEquals(9, positionFieldWrite.getEndLine());
assertEquals("this.field = m2(parm1);", contentAtPosition(classContent, positionFieldWrite));
CtIf ctIf = body.getStatement(2);
SourcePosition positionIf = ctIf.getPosition();
assertEquals(10, positionIf.getLine());
assertEquals(21, positionIf.getEndLine());
assertEquals("if(parm1 > 2 && true) {\n" + "\t\t\tswitch (parm1) {\n" + "\t\t\tcase 1:\n" + "\t\t\t\treturn;\n" + "\t\t\tdefault:\n" + "\t\t\t\tparm1++;\n" + "\t\t\t}\n" + "\t\t\tint count = 0;\n" + "\t\t\tfor (int i =0; i< parm1; i++) {\n" + "\t\t\t\tcount ++;\n" + "\t\t\t}\n" + "\t\t}", contentAtPosition(classContent, positionIf));
SourcePosition positionSwitch = ((CtBlock) ctIf.getThenStatement()).getStatement(0).getPosition();
assertEquals(11, positionSwitch.getLine());
assertEquals(16, positionSwitch.getEndLine());
assertEquals("switch (parm1) {\n" + "\t\t\tcase 1:\n" + "\t\t\t\treturn;\n" + "\t\t\tdefault:\n" + "\t\t\t\tparm1++;\n" + "\t\t\t}", contentAtPosition(classContent, positionSwitch));
positionLocalVariable = ((CtBlock) ctIf.getThenStatement()).getStatement(1).getPosition();
assertEquals(17, positionLocalVariable.getLine());
assertEquals(17, positionLocalVariable.getEndLine());
assertEquals("int count = 0;", contentAtPosition(classContent, positionLocalVariable));
SourcePosition positionFor = ((CtBlock) ctIf.getThenStatement()).getStatement(2).getPosition();
assertEquals(18, positionFor.getLine());
assertEquals(20, positionFor.getEndLine());
assertEquals("for (int i =0; i< parm1; i++) {\n" + "\t\t\t\tcount ++;\n" + "\t\t\t}", contentAtPosition(classContent, positionFor));
SourcePosition positionReturn = method1.getBody().getStatement(3).getPosition();
assertEquals(22, positionReturn.getLine());
assertEquals(22, positionReturn.getEndLine());
assertEquals("return;", contentAtPosition(classContent, positionReturn));
}
use of spoon.reflect.code.CtBlock in project spoon by INRIA.
the class LoopTest method testForeachShouldHaveAlwaysABlockInItsBody.
@Test
public void testForeachShouldHaveAlwaysABlockInItsBody() throws Exception {
final CtClass<Join> aType = build(Join.class, Condition.class).Class().get(Join.class);
final CtConstructor<Join> joinCtConstructor = aType.getConstructors().stream().findFirst().get();
final CtLoop ctLoop = joinCtConstructor.getBody().getElements(new TypeFilter<>(CtLoop.class)).get(0);
assertTrue(ctLoop.getBody() instanceof CtBlock);
//
String expected = //
"for (spoon.test.loop.testclasses.Condition<? super T> condition : conditions)" + nl + " this.conditions.add(spoon.test.loop.testclasses.Join.notNull(condition));" + nl;
assertEquals(expected, ctLoop.toString());
}
Aggregations