use of spoon.test.filters.testclasses.Tacos in project spoon by INRIA.
the class FilterTest method testInvocationFilterWithExecutableInLibrary.
@Test
public void testInvocationFilterWithExecutableInLibrary() throws Exception {
// contract: When we have an invocation of an executable declared in a library,
// we can filter it and get the executable of the invocation.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtInvocation<?> invSize = aTacos.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
if (element.getExecutable() == null) {
return false;
}
return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
final List<CtInvocation<?>> invocations = aTacos.getElements(new InvocationFilter(invSize.getExecutable()));
assertEquals(1, invocations.size());
final CtInvocation<?> expectedInv = invocations.get(0);
assertNotNull(expectedInv);
final CtExecutableReference<?> expectedExecutable = expectedInv.getExecutable();
assertNotNull(expectedExecutable);
assertEquals("size", expectedExecutable.getSimpleName());
assertNull(expectedExecutable.getDeclaration());
CtExecutable<?> exec = expectedExecutable.getExecutableDeclaration();
assertEquals("size", exec.getSimpleName());
assertEquals("ArrayList", ((CtClass) exec.getParent()).getSimpleName());
final CtExecutable<?> declaration = expectedExecutable.getExecutableDeclaration();
assertNotNull(declaration);
assertEquals("size", declaration.getSimpleName());
}
use of spoon.test.filters.testclasses.Tacos in project spoon by INRIA.
the class FilterTest method testReflectionBasedTypeFilter.
@Test
public void testReflectionBasedTypeFilter() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
// First collect all classes using tested TypeFilter
List<CtClass<?>> allClasses = launcher.getFactory().Package().getRootPackage().getElements(new TypeFilter<CtClass<?>>(CtClass.class));
assertTrue(allClasses.size() > 0);
allClasses.forEach(result -> {
assertTrue(result instanceof CtClass);
});
// then do it using Filter whose type is computed by reflection
List<CtClass<?>> allClasses2 = launcher.getFactory().Package().getRootPackage().getElements(new Filter<CtClass<?>>() {
@Override
public boolean matches(CtClass<?> element) {
return true;
}
});
assertArrayEquals(allClasses.toArray(), allClasses2.toArray());
// then do it using Filter implemented by lambda expression
List<CtClass<?>> allClasses3 = launcher.getFactory().Package().getRootPackage().getElements((CtClass<?> element) -> true);
assertArrayEquals(allClasses.toArray(), allClasses3.toArray());
// last try AbstractFilter constructor without class parameter
final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtInvocation<?> invSize = aTacos.getElements(new AbstractFilter<CtInvocation<?>>() {
/*no class is needed here*/
@Override
public boolean matches(CtInvocation<?> element) {
if (element.getExecutable() == null) {
return false;
}
return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
assertNotNull(invSize);
}
Aggregations