-
-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for template substitution of return (#1391)
* test SubstitionVisitor replace of return * fix contract description
- Loading branch information
1 parent
ccaa903
commit cbb9448
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/test/java/spoon/test/template/TemplateReplaceReturnTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/spoon/test/template/testclasses/ReturnReplaceTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |