use of spoon.Launcher in project spoon by INRIA.
the class TestCtBlock method testRemoveStatement.
@Test
public void testRemoveStatement() {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
spoon.buildModel();
List<CtMethod> methods = spoon.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "foo"));
assertEquals(1, methods.size());
CtMethod foo = methods.get(0);
CtBlock block = foo.getBody();
CtStatement lastStatement = block.getLastStatement();
assertEquals("i++", lastStatement.toString());
block.removeStatement(lastStatement);
CtStatement newLastStatement = block.getLastStatement();
assertTrue(newLastStatement != lastStatement);
assertTrue(newLastStatement instanceof CtIf);
}
use of spoon.Launcher in project spoon by INRIA.
the class CUFilterTest method testSingleExcludeWithFilter.
@Test
public void testSingleExcludeWithFilter() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/noclasspath/same-package");
launcher.getModelBuilder().addCompilationUnitFilter(new CompilationUnitFilter() {
@Override
public boolean exclude(final String path) {
return path.endsWith("B.java");
}
});
launcher.buildModel();
final CtModel model = launcher.getModel();
assertEquals(1, model.getAllTypes().size());
// make sure `B` is not available in `model.getAllTypes`
assertEquals("A", model.getAllTypes().iterator().next().getSimpleName());
// make sure declaration of `B` is known in `model`
final CtReturn ctReturn = model.getAllTypes().iterator().next().getMethod("createB").getBody().getStatement(0);
final CtConstructorCall ctConstructorCall = (CtConstructorCall) ctReturn.getReturnedExpression();
assertEquals("spoon.test.same.B", ctConstructorCall.getType().getQualifiedName());
}
use of spoon.Launcher 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.Launcher in project spoon by INRIA.
the class FilterTest method testQueryWithOptionalNumberOfInputs.
@Test
public void testQueryWithOptionalNumberOfInputs() throws Exception {
// contract: QueryFactory allows to create query with an optional number of inputs
// the input can be provided as Array or Iterable
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);
CtClass<?> cls3 = launcher.getFactory().Class().get(Antojito.class);
// here is the query
CtQuery q1 = launcher.getFactory().Query().createQuery(cls, cls2).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada" }, q1.list().toArray());
CtQuery q1b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada" }, q1b.list().toArray());
CtQuery q2 = launcher.getFactory().Query().createQuery(cls, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Antojito" }, q2.list().toArray());
CtQuery q2b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls3)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Antojito" }, q2b.list().toArray());
CtQuery q3 = launcher.getFactory().Query().createQuery(cls, cls2, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada", "Antojito" }, q3.list().toArray());
CtQuery q3b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2, cls3)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada", "Antojito" }, q3b.list().toArray());
}
use of spoon.Launcher 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);
}
Aggregations