Search in sources :

Example 1 with CtPathStringBuilder

use of spoon.reflect.path.CtPathStringBuilder in project spoon by INRIA.

the class PathTest method exceptionTest.

@Test
public void exceptionTest() {
    try {
        new CtPathStringBuilder().fromString("/CtClassss");
        fail();
    } catch (CtPathException e) {
        assertEquals("Unable to locate element with type CtClassss in Spoon model", e.getMessage());
    }
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtPathException(spoon.reflect.path.CtPathException) Test(org.junit.Test)

Example 2 with CtPathStringBuilder

use of spoon.reflect.path.CtPathStringBuilder in project spoon by INRIA.

the class PathTest method testMultiPathFromString.

@Test
public void testMultiPathFromString() throws Exception {
    // When role match a list but no index is provided, all of them must be returned
    Collection<CtElement> results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.foo#body#statement").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 3);
    // When role match a set but no name is provided, all of them must be returned
    results = new CtPathStringBuilder().fromString("#subPackage").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 1);
    // When role match a map but no key is provided, all of them must be returned
    results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.bar##annotation[index=0]#value").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 1);
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtElement(spoon.reflect.declaration.CtElement) Test(org.junit.Test)

Example 3 with CtPathStringBuilder

use of spoon.reflect.path.CtPathStringBuilder in project spoon by INRIA.

the class PathTest method testGetPathFromNonParent.

@Test
public void testGetPathFromNonParent() throws Exception {
    CtMethod fooMethod = (CtMethod) new CtPathStringBuilder().fromString(".spoon.test.path.Foo.foo").evaluateOn(factory.getModel().getRootPackage()).iterator().next();
    CtMethod barMethod = (CtMethod) new CtPathStringBuilder().fromString(".spoon.test.path.Foo.bar").evaluateOn(factory.getModel().getRootPackage()).iterator().next();
    try {
        new CtElementPathBuilder().fromElement(fooMethod, barMethod);
        fail("No path should be found to .spoon.test.path.Foo.foo from .spoon.test.path.Foo.bar");
    } catch (CtPathException e) {
    }
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtMethod(spoon.reflect.declaration.CtMethod) CtElementPathBuilder(spoon.reflect.path.CtElementPathBuilder) CtPathException(spoon.reflect.path.CtPathException) Test(org.junit.Test)

Example 4 with CtPathStringBuilder

use of spoon.reflect.path.CtPathStringBuilder in project spoon by INRIA.

the class PathTest method testBuilderMethod.

@Test
public void testBuilderMethod() throws Exception {
    equalsSet(new CtPathBuilder().name("spoon").name("test").name("path").name("Foo").type(CtMethod.class).build(), factory.Type().get("spoon.test.path.Foo").getMethods());
    equalsSet(new CtPathStringBuilder().fromString(".spoon.test.path.Foo/CtMethod"), factory.Type().get("spoon.test.path.Foo").getMethods());
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtPathBuilder(spoon.reflect.path.CtPathBuilder) Test(org.junit.Test)

Example 5 with CtPathStringBuilder

use of spoon.reflect.path.CtPathStringBuilder in project spoon by INRIA.

the class PathTest method testIncorrectPathFromString.

@Test
public void testIncorrectPathFromString() throws Exception {
    // match the else part of the if in Foo.bar() method which does not exist (Test non existing unique element)
    Collection<CtElement> results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.bar#body#statement[index=2]#else").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 0);
    // match the third statement of Foo.foo() method which does not exist (Test non existing element of a list)
    results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.foo#body#statement[index=3]").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 0);
    // match an non existing package (Test non existing element of a set)
    results = new CtPathStringBuilder().fromString("#subPackage[name=nonExistingPackage]").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 0);
    // match a non existing field of an annotation (Test non existing element of a map)
    results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.bar##annotation[index=0]#value[key=misspelled]").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 0);
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtElement(spoon.reflect.declaration.CtElement) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 CtPathStringBuilder (spoon.reflect.path.CtPathStringBuilder)5 CtElement (spoon.reflect.declaration.CtElement)2 CtPathException (spoon.reflect.path.CtPathException)2 CtMethod (spoon.reflect.declaration.CtMethod)1 CtElementPathBuilder (spoon.reflect.path.CtElementPathBuilder)1 CtPathBuilder (spoon.reflect.path.CtPathBuilder)1