Search in sources :

Example 1 with AClass

use of spoon.test.prettyprinter.testclasses.AClass in project spoon by INRIA.

the class DefaultPrettyPrinterTest method testPrintAMethodWithImports.

@Test
public void testPrintAMethodWithImports() throws Exception {
    final Launcher launcher = new Launcher();
    final Factory factory = launcher.getFactory();
    factory.getEnvironment().setAutoImports(true);
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/"));
    compiler.build();
    final String expected = "public List<?> aMethod() {" + nl + "    return new ArrayList<>();" + nl + "}";
    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    assertEquals(expected, aClass.getMethodsByName("aMethod").get(0).toString());
    final CtConstructorCall<?> constructorCall = aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> ctTypeReference = constructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(ctTypeReference.isImplicit());
    assertEquals("Object", ctTypeReference.getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AClass(spoon.test.prettyprinter.testclasses.AClass) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) File(java.io.File) Test(org.junit.Test)

Example 2 with AClass

use of spoon.test.prettyprinter.testclasses.AClass in project spoon by INRIA.

the class DefaultPrettyPrinterTest method testPrintAClassWithImports.

@Test
public void testPrintAClassWithImports() throws Exception {
    final Launcher launcher = new Launcher();
    final Factory factory = launcher.getFactory();
    factory.getEnvironment().setAutoImports(true);
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/"));
    compiler.build();
    final String expected = "public class AClass {" + nl + "    public List<?> aMethod() {" + nl + "        return new ArrayList<>();" + nl + "    }" + nl + "" + nl + "    public List<? extends ArrayList> aMethodWithGeneric() {" + nl + "        return new ArrayList<>();" + nl + "    }" + nl + "}";
    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    assertEquals(expected, aClass.toString());
    final CtConstructorCall<?> constructorCall = aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> ctTypeReference = constructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(ctTypeReference.isImplicit());
    assertEquals("Object", ctTypeReference.getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AClass(spoon.test.prettyprinter.testclasses.AClass) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) File(java.io.File) Test(org.junit.Test)

Example 3 with AClass

use of spoon.test.prettyprinter.testclasses.AClass in project spoon by INRIA.

the class DefaultPrettyPrinterTest method useFullyQualifiedNamesInCtElementImpl_toString.

@Test
public void useFullyQualifiedNamesInCtElementImpl_toString() throws Exception {
    Factory factory = build(AClass.class);
    factory.getEnvironment().setAutoImports(false);
    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    String computed = aClass.getMethodsByName("aMethod").get(0).toString();
    final String expected = "public java.util.List<?> aMethod() {" + nl + "    return new java.util.ArrayList<>();" + nl + "}";
    assertEquals("the toString method of CtElementImpl should not shorten type names as it has no context or import statements", expected, computed);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Factory(spoon.reflect.factory.Factory) AClass(spoon.test.prettyprinter.testclasses.AClass) Test(org.junit.Test)

Example 4 with AClass

use of spoon.test.prettyprinter.testclasses.AClass in project spoon by INRIA.

the class DefaultPrettyPrinterTest method testPrintAMethodWithGeneric.

@Test
public void testPrintAMethodWithGeneric() throws Exception {
    final Launcher launcher = new Launcher();
    final Factory factory = launcher.getFactory();
    factory.getEnvironment().setAutoImports(true);
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/"));
    compiler.build();
    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    final String expected = "public List<? extends ArrayList> aMethodWithGeneric() {" + System.lineSeparator() + "    return new ArrayList<>();" + System.lineSeparator() + "}";
    assertEquals(expected, aClass.getMethodsByName("aMethodWithGeneric").get(0).toString());
    final CtConstructorCall<?> constructorCall = aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> ctTypeReference = constructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(ctTypeReference.isImplicit());
    assertEquals("Object", ctTypeReference.getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AClass(spoon.test.prettyprinter.testclasses.AClass) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 CtClass (spoon.reflect.declaration.CtClass)4 Factory (spoon.reflect.factory.Factory)4 AClass (spoon.test.prettyprinter.testclasses.AClass)4 File (java.io.File)3 Launcher (spoon.Launcher)3 SpoonModelBuilder (spoon.SpoonModelBuilder)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3