use of spoon.reflect.visitor.Query in project spoon by INRIA.
the class FilterTest method testFilterChildrenWithoutFilterQueryStep.
@Test
public void testFilterChildrenWithoutFilterQueryStep() 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();
// Contract: the filterChildren(null) without Filter returns same results like filter which returns true for each input.
List<CtElement> list = launcher.getFactory().Package().getRootPackage().filterChildren(null).list();
Iterator<CtElement> iter = list.iterator();
launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
return true;
}).forEach(real -> {
// the elements produced by each query are same
CtElement expected = iter.next();
if (real != expected) {
assertEquals(expected, real);
}
});
assertTrue(list.size() > 0);
assertTrue(iter.hasNext() == false);
}
use of spoon.reflect.visitor.Query in project spoon by INRIA.
the class FilterTest method testEarlyTerminatingQuery.
@Test
public void testEarlyTerminatingQuery() throws Exception {
// contract: a method first evaluates query until first element is found and then terminates the query
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 {
boolean wasTerminated = false;
void failIfTerminated(String place) {
assertTrue("The " + place + " is called after query was terminated.", wasTerminated == false);
}
}
Context context = new Context();
CtMethod firstMethod = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
context.failIfTerminated("Filter#match of filterChildren");
return true;
}).map((CtElement e) -> {
context.failIfTerminated("Array returning CtFunction#apply of map");
// send result twice to check that second item is skipped
return new CtElement[] { e, e };
}).map((CtElement e) -> {
context.failIfTerminated("List returning CtFunction#apply of map");
// send result twice to check that second item is skipped
return Arrays.asList(new CtElement[] { e, e });
}).map((CtElement e, CtConsumer<Object> out) -> {
context.failIfTerminated("CtConsumableFunction#apply of map");
if (e instanceof CtMethod) {
// this call should pass and cause termination of the query
out.accept(e);
context.wasTerminated = true;
// let it call out.accept(e); once more to check that next step is not called after query is terminated
}
out.accept(e);
}).map(e -> {
context.failIfTerminated("CtFunction#apply of map after CtConsumableFunction");
return e;
}).first(CtMethod.class);
assertTrue(firstMethod != null);
assertTrue(context.wasTerminated);
}
use of spoon.reflect.visitor.Query in project spoon by INRIA.
the class FilterTest method testEmptyQuery.
@Test
public void testEmptyQuery() throws Exception {
// contract: unbound or empty query
final Launcher launcher = new Launcher();
// contract: empty query returns no element
assertEquals(0, launcher.getFactory().createQuery().list().size());
assertEquals(0, launcher.getFactory().createQuery((Object) null).list().size());
// contract: empty query returns no element
launcher.getFactory().createQuery().forEach(x -> fail());
launcher.getFactory().createQuery((Object) null).forEach(x -> fail());
// contract: empty query calls no mapping
assertEquals(0, launcher.getFactory().createQuery().map(x -> {
fail();
return true;
}).list().size());
assertEquals(0, launcher.getFactory().createQuery((Object) null).map(x -> {
fail();
return true;
}).list().size());
// contract: empty query calls no filterChildren
assertEquals(0, launcher.getFactory().createQuery().filterChildren(x -> {
fail();
return true;
}).list().size());
assertEquals(0, launcher.getFactory().createQuery((Object) null).filterChildren(x -> {
fail();
return true;
}).list().size());
}
Aggregations