Search in sources :

Example 1 with CtLoop

use of spoon.reflect.code.CtLoop in project spoon by INRIA.

the class FilterTest method testLineFilter.

@Test
public void testLineFilter() throws Exception {
    CtType<FooLine> foo = ModelUtils.buildClass(FooLine.class);
    CtMethod method = foo.getMethod("simple");
    List<CtStatement> expressions = method.getElements(new LineFilter());
    assertEquals(3, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    method = foo.getMethod("loopBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtLoop);
    method = foo.getMethod("loopNoBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtLoop);
    method = foo.getMethod("ifBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtIf);
    method = foo.getMethod("ifNoBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(2, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtIf);
    method = foo.getMethod("switchBlock");
    expressions = method.getElements(new LineFilter());
    assertEquals(3, expressions.size());
    assertNull(expressions.get(0).getParent(new LineFilter()));
    assertTrue(expressions.get(1).getParent(new LineFilter()) instanceof CtSwitch);
    assertTrue(expressions.get(2).getParent(new LineFilter()) instanceof CtSwitch);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtSwitch(spoon.reflect.code.CtSwitch) CtMethod(spoon.reflect.declaration.CtMethod) CtLoop(spoon.reflect.code.CtLoop) CtIf(spoon.reflect.code.CtIf) LineFilter(spoon.reflect.visitor.filter.LineFilter) Test(org.junit.Test)

Example 2 with CtLoop

use of spoon.reflect.code.CtLoop 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());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) Join(spoon.test.loop.testclasses.Join) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLoop(spoon.reflect.code.CtLoop) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 CtLoop (spoon.reflect.code.CtLoop)2 CtBlock (spoon.reflect.code.CtBlock)1 CtIf (spoon.reflect.code.CtIf)1 CtStatement (spoon.reflect.code.CtStatement)1 CtSwitch (spoon.reflect.code.CtSwitch)1 CtMethod (spoon.reflect.declaration.CtMethod)1 LineFilter (spoon.reflect.visitor.filter.LineFilter)1 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)1 Join (spoon.test.loop.testclasses.Join)1