use of spoon.test.template.testclasses.ReturnReplaceTemplate in project spoon by INRIA.
the class TemplateReplaceReturnTest method testNoReturnReplaceTemplate.
@Test
public void testNoReturnReplaceTemplate() throws Exception {
// contract: the template engine supports replace of return expression by `<CtExpression>`
Launcher launcher = new Launcher();
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ReturnReplaceTemplate.java"));
launcher.buildModel();
Factory factory = launcher.getFactory();
CtExpression<String> model = factory.createLiteral("AStringLiteral");
CtClass<?> resultKlass = factory.Class().create(factory.Package().getOrCreate("spoon.test.template"), "ReturnReplaceResult");
new ReturnReplaceTemplate(model).apply(resultKlass);
assertEquals("{ return \"AStringLiteral\";}", getOptimizedString(resultKlass.getMethod("method").getBody()));
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/ReturnReplaceResult.java"), 8);
}
use of spoon.test.template.testclasses.ReturnReplaceTemplate in project spoon by INRIA.
the class TemplateReplaceReturnTest method testReturnReplaceTemplate.
@Test
public void testReturnReplaceTemplate() throws Exception {
// contract: the template engine supports replace of `return _param_.S()` by `<CtBlock>`
Launcher launcher = new Launcher();
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ReturnReplaceTemplate.java"));
launcher.buildModel();
Factory factory = launcher.getFactory();
CtBlock<String> model = (CtBlock) factory.Class().get(ReturnReplaceTemplate.class).getMethod("sample").getBody();
CtClass<?> resultKlass = factory.Class().create(factory.Package().getOrCreate("spoon.test.template"), "ReturnReplaceResult");
new ReturnReplaceTemplate(model).apply(resultKlass);
assertEquals("{ if (((java.lang.System.currentTimeMillis()) % 2L) == 0) { return \"Panna\"; }else { return \"Orel\"; }}", getOptimizedString(resultKlass.getMethod("method").getBody()));
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/ReturnReplaceResult.java"), 8);
}
Aggregations