use of spoon.reflect.visitor.chain.CtConsumableFunction 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.chain.CtConsumableFunction in project spoon by INRIA.
the class FilterTest method testClassCastExceptionOnForEach.
@Test
public void testClassCastExceptionOnForEach() throws Exception {
// contract: bound query, without any mapping
// This test could fail with a version of JDK <= 8.0.40.
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();
// contract: if the query produces elements which cannot be cast to forEach consumer, then they are ignored
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
context.count++;
});
assertTrue(context.count > 0);
}
{
Context context = new Context();
// contract: if the for each implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
context.count++;
throw new ClassCastException("TEST");
});
fail("It must fail, because body of forEach should be called and thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the for each implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach(new CtConsumer<CtType>() {
@Override
public void accept(CtType t) {
context.count++;
throw new ClassCastException("TEST");
}
});
fail("It must fail, because body of forEach should be called and thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the select implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).select(new Filter<CtType>() {
@Override
public boolean matches(CtType element) {
context.count++;
throw new ClassCastException("TEST");
}
}).list();
fail("It must fail, because body of select thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the select implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).select((CtType element) -> {
context.count++;
throw new ClassCastException("TEST");
}).list();
fail("It must fail, because body of select thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtFunction) implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtFunction<CtType, Object>() {
@Override
public Object apply(CtType input) {
context.count++;
throw new ClassCastException("TEST");
}
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtFunction) implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input) -> {
context.count++;
throw new ClassCastException("TEST");
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtConsumableFunction) implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtConsumableFunction<CtType>() {
@Override
public void apply(CtType input, CtConsumer<Object> outputConsumer) {
context.count++;
throw new ClassCastException("TEST");
}
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtConsumableFunction) implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input, CtConsumer<Object> outputConsumer) -> {
context.count++;
throw new ClassCastException("TEST");
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
}
use of spoon.reflect.visitor.chain.CtConsumableFunction in project spoon by INRIA.
the class FilterTest method testElementMapConsumableFunction.
// now testing map(CtConsumableFunction)
@Test
public void testElementMapConsumableFunction() throws Exception {
// contract: a method map(CtConsumableFunction) is provided
// a simple consumer.accept() is equivalent to a single return in a CtFunction
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);
// long version
class aFunction implements CtConsumableFunction<CtClass> {
@Override
public void apply(CtClass c, CtConsumer out) {
// equivalent to a single return
out.accept(c.getParent());
}
}
assertEquals(cls.getParent(), cls.map(new aFunction()).list().get(0));
// now the same with Java8 one-liner
assertEquals(cls.getParent(), cls.map((CtClass<?> c, CtConsumer<Object> out) -> out.accept(c.getParent())).list().get(0));
}
Aggregations