Search in sources :

Example 1 with CtQueryImpl

use of spoon.reflect.visitor.chain.CtQueryImpl in project spoon by INRIA.

the class FilterTest method testFunctionQueryStep.

@Test
public void testFunctionQueryStep() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        int count = 0;
    }
    Context context = new Context();
    CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
        return true;
    }).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
    ((CtQueryImpl) query).logging(true);
    query.forEach((CtInterface<?> c) -> {
        assertEquals("ITostada", c.getSimpleName());
        context.count++;
    });
    assertTrue(context.count > 0);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 2 with CtQueryImpl

use of spoon.reflect.visitor.chain.CtQueryImpl in project spoon by INRIA.

the class FilterTest method testReuseOfQuery.

@Test
public void testReuseOfQuery() throws Exception {
    // contract: a query created from an existing element can be reused on other inputs
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
    CtClass<?> cls2 = launcher.getFactory().Class().get(Tostada.class);
    // by default the query starts with "cls" as input
    CtQuery q = cls.map((CtClass c) -> c.getSimpleName());
    // high-level assert
    assertEquals(cls.getSimpleName(), q.list().get(0));
    // low-level assert on implementation
    assertEquals(1, ((CtQueryImpl) q).getInputs().size());
    assertSame(cls, ((CtQueryImpl) q).getInputs().get(0));
    // now changing the input of query to cls2
    q.setInput(cls2);
    // the input is still cls2
    assertEquals(cls2.getSimpleName(), q.list().get(0));
    assertEquals(1, ((CtQueryImpl) q).getInputs().size());
    assertSame(cls2, ((CtQueryImpl) q).getInputs().get(0));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Launcher (spoon.Launcher)2 CtQuery (spoon.reflect.visitor.chain.CtQuery)2 CtQueryImpl (spoon.reflect.visitor.chain.CtQueryImpl)2 CtClass (spoon.reflect.declaration.CtClass)1 CtInterface (spoon.reflect.declaration.CtInterface)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtType (spoon.reflect.declaration.CtType)1 CtTypeReference (spoon.reflect.reference.CtTypeReference)1