Search in sources :

Example 1 with Panini

use of spoon.test.fieldaccesses.testclasses.Panini in project spoon by INRIA.

the class FieldAccessTest method testIncrementationOnAVarIsAUnaryOperator.

@Test
public void testIncrementationOnAVarIsAUnaryOperator() throws Exception {
    // contract: When we use var++, the variable is a read access with an unary operator.
    final CtType<Panini> aMole = buildClass(Panini.class);
    final CtMethod<?> make = aMole.getMethodsByName("make").get(0);
    final List<CtUnaryOperator<?>> unaryOperators = make.getElements(new TypeFilter<CtUnaryOperator<?>>(CtUnaryOperator.class));
    final CtFieldWrite<Object> fieldRead = aMole.getFactory().Core().createFieldWrite();
    fieldRead.setTarget(aMole.getFactory().Code().createThisAccess(aMole.getReference(), true));
    final CtFieldReference fieldReference = aMole.getField("i").getReference();
    fieldRead.setVariable(fieldReference);
    assertEquals(2, unaryOperators.size());
    final CtUnaryOperator<?> first = unaryOperators.get(0);
    assertEquals(UnaryOperatorKind.POSTINC, first.getKind());
    assertEquals(fieldRead, first.getOperand());
    assertEquals("(i)++", first.toString());
    final CtUnaryOperator<?> second = unaryOperators.get(1);
    assertEquals(UnaryOperatorKind.PREINC, second.getKind());
    assertEquals(fieldRead, second.getOperand());
    assertEquals("++(i)", second.toString());
}
Also used : Panini(spoon.test.fieldaccesses.testclasses.Panini) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtFieldReference(spoon.reflect.reference.CtFieldReference) Test(org.junit.Test)

Example 2 with Panini

use of spoon.test.fieldaccesses.testclasses.Panini in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessInAnonymousClass.

@Test
public void testFieldAccessInAnonymousClass() throws Exception {
    final Factory factory = build(Panini.class);
    final CtType<Panini> panini = factory.Type().get(Panini.class);
    final CtFieldRead fieldInAnonymous = panini.getElements(new TypeFilter<>(CtFieldRead.class)).get(0);
    assertEquals("ingredient", fieldInAnonymous.getTarget().toString());
    assertEquals("next", fieldInAnonymous.getVariable().getSimpleName());
    assertEquals("ingredient.next", fieldInAnonymous.toString());
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) Factory(spoon.reflect.factory.Factory) Panini(spoon.test.fieldaccesses.testclasses.Panini) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Panini (spoon.test.fieldaccesses.testclasses.Panini)2 CtFieldRead (spoon.reflect.code.CtFieldRead)1 CtUnaryOperator (spoon.reflect.code.CtUnaryOperator)1 Factory (spoon.reflect.factory.Factory)1 CtFieldReference (spoon.reflect.reference.CtFieldReference)1 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)1