Search in sources :

Example 16 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class PrinterTest method testChangeAutoImportModeWorks.

@Test
public void testChangeAutoImportModeWorks() throws Exception {
    Launcher spoon = new Launcher();
    spoon.getEnvironment().setAutoImports(false);
    PrettyPrinter printer = spoon.createPrettyPrinter();
    spoon.addInputResource("./src/test/java/spoon/test/prettyprinter/testclasses/AClass.java");
    spoon.buildModel();
    CtType element = spoon.getFactory().Class().getAll().get(0);
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String result = printer.getResult();
    assertTrue("The result should not contain imports: " + result, !result.contains("import java.util.List;"));
    // recreating an auto-immport  printer
    spoon.getEnvironment().setAutoImports(true);
    printer = spoon.createPrettyPrinter();
    printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
    result = printer.getResult();
    assertTrue("The result should now contain imports: " + result, result.contains("import java.util.List;"));
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 17 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class PrinterTest method testFQNModeWriteFQNConstructorInCtVisitor.

@Test
public void testFQNModeWriteFQNConstructorInCtVisitor() {
    Launcher spoon = new Launcher();
    PrettyPrinter printer = spoon.createPrettyPrinter();
    spoon.getEnvironment().setAutoImports(false);
    spoon.addInputResource("./src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java");
    spoon.buildModel();
    CtType element = spoon.getFactory().Class().getAll().get(0);
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String result = printer.getResult();
    assertTrue("The result should contain FQN for constructor: " + result, result.contains("new spoon.support.visitor.replace.ReplacementVisitor("));
    assertTrue("The result should not contain reduced constructors: " + result, !result.contains("new ReplacementVisitor("));
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 18 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class PrinterTest method testAutoimportModeDontImportUselessStatic.

@Test
public void testAutoimportModeDontImportUselessStatic() {
    Launcher spoon = new Launcher();
    spoon.getEnvironment().setAutoImports(true);
    PrettyPrinter printer = spoon.createPrettyPrinter();
    spoon.addInputResource("./src/test/java/spoon/test/prettyprinter/testclasses/ImportStatic.java");
    spoon.buildModel();
    CtType element = spoon.getFactory().Class().getAll().get(0);
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String result = printer.getResult();
    assertTrue("The result should not contain import static: ", !result.contains("import static spoon.test.prettyprinter.testclasses.sub.Constants.READY"));
    assertTrue("The result should contain import type: ", result.contains("import spoon.test.prettyprinter.testclasses.sub.Constants"));
    assertTrue("The result should contain import static assertTrue: ", result.contains("import static org.junit.Assert.assertTrue;"));
    assertTrue("The result should contain assertTrue(...): ", result.contains("assertTrue(\"blabla\".equals(\"toto\"));"));
    assertTrue("The result should use System.out.println(Constants.READY): " + result, result.contains("System.out.println(Constants.READY);"));
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 19 with PrettyPrinter

use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.

the class PrinterTest method testRuleCanBeBuild.

@Test
public void testRuleCanBeBuild() {
    Launcher spoon = new Launcher();
    PrettyPrinter printer = spoon.createPrettyPrinter();
    spoon.getEnvironment().setAutoImports(true);
    String output = "./target/spoon-rule/";
    spoon.addInputResource("./src/test/java/spoon/test/prettyprinter/testclasses/Rule.java");
    spoon.setSourceOutputDirectory(output);
    spoon.run();
    CtType element = spoon.getFactory().Class().getAll().get(0);
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String result = printer.getResult();
    assertTrue("The result should contain direct this accessor for field: " + result, !result.contains("Rule.Phoneme.this.phonemeText"));
    canBeBuilt(output, 7);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 Launcher (spoon.Launcher)18 Test (org.junit.Test)17 CtType (spoon.reflect.declaration.CtType)17 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)17 ArrayList (java.util.ArrayList)16 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 StringTokenizer (java.util.StringTokenizer)2 CtClass (spoon.reflect.declaration.CtClass)2 CtPackage (spoon.reflect.declaration.CtPackage)2 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)1 Environment (spoon.compiler.Environment)1 MainTest (spoon.test.main.MainTest)1