use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class AccessFullyQualifiedFieldTest method testStaticImportWithAutoImport.
@Test
public void testStaticImportWithAutoImport() throws Exception {
String output = "target/spooned-" + this.getClass().getSimpleName() + "-MultiAutoImport/";
String pathResource = "src/test/java/spoon/test/variable/testclasses/MultiBurritos.java";
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--with-imports" });
spoon.addInputResource(pathResource);
spoon.setSourceOutputDirectory(output);
spoon.run();
PrettyPrinter prettyPrinter = spoon.createPrettyPrinter();
CtType element = spoon.getFactory().Class().getAll().get(0);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = prettyPrinter.getResult();
assertTrue("The result should contain a static import for spoon.Launcher.SPOONED_CLASSES", result.contains("import static spoon.Launcher.SPOONED_CLASSES;"));
assertTrue("The variable x should be assigned with only SPOONED_CLASSES", result.contains("Object x = SPOONED_CLASSES;"));
assertTrue("The result should not contain a static import for spoon.test.variable.testclasses.ForStaticVariables.foo as it is in the same package", !result.contains("import static spoon.test.variable.testclasses.ForStaticVariables.foo;"));
assertTrue("The result should not contain a import static for spoon.test.variable.testclasses.MultiBurritos.toto as it is in the same class", !result.contains("import static spoon.test.variable.testclasses.MultiBurritos.toto;"));
assertTrue("The result should not contain a FQN for toto", !result.contains("spoon.test.variable.testclasses.MultiBurritos.toto();"));
assertTrue("The result should not contain a FQN for spoon access", !result.contains("spoon.test.variable.testclasses.MultiBurritos.spoon = \"truc\";"));
assertTrue("The result should not contain a FQN for foo", !result.contains("spoon.test.variable.testclasses.ForStaticVariables.foo();"));
canBeBuilt(output, 7);
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class ImportTest method testStaticMethodWithDifferentClassSameNameJava7NoCollision.
@Test
public void testStaticMethodWithDifferentClassSameNameJava7NoCollision() {
// contract: when there is a collision between class names when using static method, we should create a static import for the method
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned-staticmethod";
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticmethod/");
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enums/");
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enum2/");
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/LangTestSuite.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.getEnvironment().setComplianceLevel(7);
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.apachetestsuite.staticmethod.AllLangTestSuiteStaticMethod");
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
assertTrue("The file should contain a static import ", output.contains("import static spoon.test.imports.testclasses2.apachetestsuite.enums.EnumTestSuite.suite;"));
assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(suite());"));
canBeBuilt(outputDir, 7);
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class ImportTest method testSortingOfImports.
@Test
public void testSortingOfImports() {
// contract: imports are sorted alphabetically
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned";
launcher.addInputResource("./src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get(DefaultJavaPrettyPrinter.class);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
StringTokenizer st = new StringTokenizer(output, System.getProperty("line.separator"));
String lastImport = null;
int countOfImports = 0;
while (st.hasMoreTokens()) {
String line = st.nextToken();
if (line.startsWith("import")) {
countOfImports++;
if (lastImport != null) {
// check that next import is alphabetically higher then last import
assertTrue(lastImport + " should be before " + line, lastImport.compareTo(line) < 0);
}
lastImport = line;
} else {
if (lastImport != null) {
// there are no more imports. Finish
break;
}
// no import found yet. Continue with next line
}
}
assertTrue(countOfImports > 10);
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class ImportTest method testSortImportPutStaticImportAfterTypeImport.
@Test
public void testSortImportPutStaticImportAfterTypeImport() {
// contract: static import should be after import
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setShouldCompile(true);
String outputDir = "./target/spoon-sort-import";
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/StaticNoOrdered.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get(StaticNoOrdered.class);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
StringTokenizer st = new StringTokenizer(output, System.getProperty("line.separator"));
int countImports = 0;
int nbStaticImports = 2;
int nbStandardImports = 4;
boolean startStatic = false;
while (st.hasMoreTokens()) {
String line = st.nextToken();
if (line.startsWith("import static")) {
if (!startStatic) {
assertEquals("Static import should start after exactly " + nbStandardImports + " standard imports", nbStandardImports, countImports);
} else {
assertTrue("It will normally have only " + nbStaticImports + " static imports", countImports <= nbStandardImports + nbStaticImports);
}
startStatic = true;
assertTrue("Static import should be after normal import", countImports >= nbStandardImports);
}
if (line.startsWith("import")) {
countImports++;
}
}
assertEquals("Exactly " + nbStandardImports + nbStaticImports + " should have been counted.", (nbStandardImports + nbStaticImports), countImports);
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class ImportTest method testAccessPath.
@Test
public void testAccessPath() {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/TransportIndicesShardStoresAction.java");
String outputDir = "./target/spooned-accessPath";
launcher.setSourceOutputDirectory(outputDir);
launcher.run();
CtType element = launcher.getFactory().Class().getAll().get(0);
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
canBeBuilt(outputDir, 7);
}
Aggregations