Skip to content

Commit

Permalink
test: add test for template substitution of return (#1391)
Browse files Browse the repository at this point in the history
* test SubstitionVisitor replace of return

* fix contract description
  • Loading branch information
pvojtechovsky authored and monperrus committed Jun 12, 2017
1 parent ccaa903 commit cbb9448
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/test/java/spoon/test/template/TemplateReplaceReturnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package spoon.test.template;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.Test;

import spoon.Launcher;
import spoon.OutputType;
import spoon.reflect.code.CtBlock;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.factory.Factory;
import spoon.support.compiler.FileSystemFile;
import spoon.test.template.testclasses.ReturnReplaceTemplate;
import spoon.testing.utils.ModelUtils;

public class TemplateReplaceReturnTest {

@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\"; } }}", resultKlass.getMethod("method").getBody().toString().replaceAll("[\\r\\n\\t]+", "").replaceAll("\\s{2,}", " "));
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/ReturnReplaceResult.java"), 8);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package spoon.test.template.testclasses;

import spoon.reflect.code.CtBlock;
import spoon.template.ExtensionTemplate;
import spoon.template.Local;
import spoon.template.TemplateParameter;

public class ReturnReplaceTemplate extends ExtensionTemplate {

public String method() throws Throwable {
return _statement_.S();
}

TemplateParameter<String> _statement_;

@Local
public ReturnReplaceTemplate(CtBlock<String> statement) {
this._statement_ = statement;
}

@Local
String sample() {
if(System.currentTimeMillis()%2L==0) {
return "Panna";
} else {
return "Orel";
}
}
}

0 comments on commit cbb9448

Please sign in to comment.