Search in sources :

Example 1 with ReturnReplaceTemplate

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);
}
Also used : ReturnReplaceTemplate(spoon.test.template.testclasses.ReturnReplaceTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) ModelUtils.getOptimizedString(spoon.testing.utils.ModelUtils.getOptimizedString) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 2 with ReturnReplaceTemplate

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);
}
Also used : CtBlock(spoon.reflect.code.CtBlock) ReturnReplaceTemplate(spoon.test.template.testclasses.ReturnReplaceTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) ModelUtils.getOptimizedString(spoon.testing.utils.ModelUtils.getOptimizedString) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Aggregations

File (java.io.File)2 Test (org.junit.Test)2 Launcher (spoon.Launcher)2 Factory (spoon.reflect.factory.Factory)2 FileSystemFile (spoon.support.compiler.FileSystemFile)2 ReturnReplaceTemplate (spoon.test.template.testclasses.ReturnReplaceTemplate)2 ModelUtils.getOptimizedString (spoon.testing.utils.ModelUtils.getOptimizedString)2 CtBlock (spoon.reflect.code.CtBlock)1