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);
}
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));
}
Aggregations