Search in sources :

Example 1 with ParentFunction

use of spoon.reflect.visitor.filter.ParentFunction in project spoon by INRIA.

the class FilterTest method testParentFunction.

@Test
public void testParentFunction() throws Exception {
    // contract: a mapping function which returns all parents of CtElement
    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);
    CtLocalVariable<?> varStrings = cls.filterChildren(new NamedElementFilter<>(CtLocalVariable.class, "strings")).first();
    class Context {

        CtElement expectedParent;
    }
    Context context = new Context();
    context.expectedParent = varStrings;
    varStrings.map(new ParentFunction()).forEach((parent) -> {
        context.expectedParent = context.expectedParent.getParent();
        assertSame(context.expectedParent, parent);
    });
    // context.expectedParent is last visited element
    // Check that last visited element was root package
    assertSame(launcher.getFactory().getModel().getUnnamedModule(), context.expectedParent);
    // contract: if includingSelf(false), then parent of input element is first element
    assertSame(varStrings.getParent(), varStrings.map(new ParentFunction().includingSelf(false)).first());
    // contract: if includingSelf(true), then input element is first element
    assertSame(varStrings, varStrings.map(new ParentFunction().includingSelf(true)).first());
    // contract: do not fail on unitialized parent
    assertNull(factory.Type().createReference("p.T").map(new ParentFunction()).first());
}
Also used : CtElement(spoon.reflect.declaration.CtElement) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 Launcher (spoon.Launcher)1 CtElement (spoon.reflect.declaration.CtElement)1 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)1 ParentFunction (spoon.reflect.visitor.filter.ParentFunction)1