-
-
Notifications
You must be signed in to change notification settings - Fork 355
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 enum values (#1389)
* test SubstitutionVisitor enum value replace * fix SubstitutionVisitor enum value replace * check generated code * fix variable name and create enum class reference in tmpl constructor
- Loading branch information
1 parent
42cf54e
commit 526bf88
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
src/test/java/spoon/test/template/TemplateEnumAccessTest.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,37 @@ | ||
package spoon.test.template; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.lang.annotation.ElementType; | ||
|
||
import org.junit.Test; | ||
|
||
import spoon.Launcher; | ||
import spoon.OutputType; | ||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.factory.Factory; | ||
import spoon.support.compiler.FileSystemFile; | ||
import spoon.test.template.testclasses.EnumAccessTemplate; | ||
import spoon.testing.utils.ModelUtils; | ||
|
||
public class TemplateEnumAccessTest { | ||
|
||
@Test | ||
public void testEnumAccessTest() throws Exception { | ||
//contract: the template engine supports enum value access substitution | ||
Launcher launcher = new Launcher(); | ||
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/EnumAccessTemplate.java")); | ||
|
||
launcher.buildModel(); | ||
Factory factory = launcher.getFactory(); | ||
|
||
CtClass<?> resultKlass = factory.Class().create(factory.Package().getOrCreate("spoon.test.template"), "EnumAccessResult"); | ||
new EnumAccessTemplate(ElementType.FIELD, launcher.getFactory()).apply(resultKlass); | ||
assertEquals("java.lang.annotation.ElementType.FIELD.name()", resultKlass.getMethod("method").getBody().getStatement(0).toString()); | ||
launcher.setSourceOutputDirectory(new File("./target/spooned/")); | ||
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES); | ||
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/EnumAccessResult.java"), 8); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/spoon/test/template/testclasses/EnumAccessTemplate.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,32 @@ | ||
package spoon.test.template.testclasses; | ||
|
||
import spoon.reflect.factory.Factory; | ||
import spoon.reflect.reference.CtTypeReference; | ||
import spoon.template.ExtensionTemplate; | ||
import spoon.template.Local; | ||
import spoon.template.Parameter; | ||
|
||
public class EnumAccessTemplate extends ExtensionTemplate { | ||
|
||
public void method() throws Throwable { | ||
_AnEnum_._value_.name(); | ||
} | ||
|
||
@Parameter("_AnEnum_") | ||
CtTypeReference<?> anEnum; | ||
|
||
@Parameter | ||
Enum _value_; | ||
|
||
@Local | ||
public EnumAccessTemplate(Enum enumValue, Factory factory) { | ||
this._value_ = enumValue; | ||
this.anEnum = factory.Type().createReference(enumValue.getClass()); | ||
} | ||
|
||
@Local | ||
enum _AnEnum_ { | ||
@Parameter | ||
_value_ | ||
} | ||
} |