Search in sources :

Example 1 with FooMethod

use of spoon.test.position.testclasses.FooMethod in project spoon by INRIA.

the class PositionTest method testPositionMethod.

@Test
public void testPositionMethod() throws Exception {
    final Factory build = build(FooMethod.class);
    final CtClass<FooMethod> foo = build.Class().get(FooMethod.class);
    String classContent = getClassContent(foo);
    CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
    BodyHolderSourcePosition position1 = (BodyHolderSourcePosition) method1.getPosition();
    assertEquals(5, position1.getLine());
    assertEquals(7, position1.getEndLine());
    assertEquals(69, position1.getSourceStart());
    assertEquals(114, position1.getSourceEnd());
    assertEquals("public static void m(int parm1) {\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, position1));
    assertEquals("m", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
    assertEquals("public static", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
    // contract: body contains starting and ending brackets {}
    assertEquals("{\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, position1.getBodyStart(), position1.getBodyEnd()));
    DeclarationSourcePosition positionParam1 = (DeclarationSourcePosition) method1.getParameters().get(0).getPosition();
    assertEquals(5, positionParam1.getLine());
    assertEquals(5, positionParam1.getEndLine());
    assertEquals(90, positionParam1.getSourceStart());
    assertEquals(98, positionParam1.getSourceEnd());
    assertEquals("int parm1", contentAtPosition(classContent, positionParam1));
    assertEquals("parm1", contentAtPosition(classContent, positionParam1.getNameStart(), positionParam1.getNameEnd()));
    assertEquals("", contentAtPosition(classContent, positionParam1.getModifierSourceStart(), positionParam1.getModifierSourceEnd()));
    CtMethod method2 = foo.getMethodsByName("mWithDoc").get(0);
    BodyHolderSourcePosition position2 = (BodyHolderSourcePosition) method2.getPosition();
    assertEquals(13, position2.getLine());
    assertEquals(15, position2.getEndLine());
    assertEquals("/**\n" + "\t * Mathod with javadoc\n" + "\t * @param parm1 the parameter\n" + "\t */\n" + "\tint mWithDoc(int parm1) {\n" + "\t\treturn parm1;\n" + "\t}", contentAtPosition(classContent, position2));
    assertEquals("mWithDoc", contentAtPosition(classContent, position2.getNameStart(), position2.getNameEnd()));
    assertEquals("", contentAtPosition(classContent, position2.getModifierSourceStart(), position2.getModifierSourceEnd()));
    CtConstructor<FooMethod> constructor = foo.getConstructor(build.Type().integerPrimitiveType());
    SourcePosition position3 = constructor.getPosition();
    contentAtPosition(classContent, position3);
    CtMethod mWithLine = foo.getMethod("mWithLine", build.Type().integerPrimitiveType());
    SourcePosition position4 = mWithLine.getPosition();
    contentAtPosition(classContent, position4);
}
Also used : FooMethod(spoon.test.position.testclasses.FooMethod) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) SourcePosition(spoon.reflect.cu.SourcePosition) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) Test(org.junit.Test)

Example 2 with FooMethod

use of spoon.test.position.testclasses.FooMethod in project spoon by INRIA.

the class PositionTest method testPositionAbstractMethod.

@Test
public void testPositionAbstractMethod() throws Exception {
    final Factory build = build(FooAbstractMethod.class);
    final CtClass<FooMethod> foo = build.Class().get(FooAbstractMethod.class);
    String classContent = getClassContent(foo);
    CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
    BodyHolderSourcePosition position1 = (BodyHolderSourcePosition) method1.getPosition();
    assertEquals(5, position1.getLine());
    assertEquals(5, position1.getEndLine());
    assertEquals(86, position1.getSourceStart());
    assertEquals(125, position1.getSourceEnd());
    assertEquals("public abstract void m(final int parm1);", contentAtPosition(classContent, position1));
    assertEquals("m", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
    assertEquals("public abstract", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
    // contract: body of abstract method is empty
    assertEquals("", contentAtPosition(classContent, position1.getBodyStart(), position1.getBodyEnd()));
    DeclarationSourcePosition positionParam1 = (DeclarationSourcePosition) method1.getParameters().get(0).getPosition();
    assertEquals(5, positionParam1.getLine());
    assertEquals(5, positionParam1.getEndLine());
    assertEquals(109, positionParam1.getSourceStart());
    assertEquals(123, positionParam1.getSourceEnd());
    assertEquals("final int parm1", contentAtPosition(classContent, positionParam1));
    assertEquals("parm1", contentAtPosition(classContent, positionParam1.getNameStart(), positionParam1.getNameEnd()));
    assertEquals("final", contentAtPosition(classContent, positionParam1.getModifierSourceStart(), positionParam1.getModifierSourceEnd()));
}
Also used : FooMethod(spoon.test.position.testclasses.FooMethod) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) Factory(spoon.reflect.factory.Factory) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 BodyHolderSourcePosition (spoon.reflect.cu.position.BodyHolderSourcePosition)2 DeclarationSourcePosition (spoon.reflect.cu.position.DeclarationSourcePosition)2 Factory (spoon.reflect.factory.Factory)2 FooMethod (spoon.test.position.testclasses.FooMethod)2 SourcePosition (spoon.reflect.cu.SourcePosition)1 CtMethod (spoon.reflect.declaration.CtMethod)1