Search in sources :

Example 1 with FooStatement

use of spoon.test.position.testclasses.FooStatement 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));
}
Also used : CtBlock(spoon.reflect.code.CtBlock) FooStatement(spoon.test.position.testclasses.FooStatement) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) SourcePosition(spoon.reflect.cu.SourcePosition) Factory(spoon.reflect.factory.Factory) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 CtBlock (spoon.reflect.code.CtBlock)1 CtIf (spoon.reflect.code.CtIf)1 SourcePosition (spoon.reflect.cu.SourcePosition)1 BodyHolderSourcePosition (spoon.reflect.cu.position.BodyHolderSourcePosition)1 DeclarationSourcePosition (spoon.reflect.cu.position.DeclarationSourcePosition)1 Factory (spoon.reflect.factory.Factory)1 FooStatement (spoon.test.position.testclasses.FooStatement)1