diff --git a/src/test/java/spoon/test/template/TemplateArrayAccessTest.java b/src/test/java/spoon/test/template/TemplateArrayAccessTest.java new file mode 100644 index 00000000000..da02658da3a --- /dev/null +++ b/src/test/java/spoon/test/template/TemplateArrayAccessTest.java @@ -0,0 +1,30 @@ +package spoon.test.template; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import spoon.Launcher; +import spoon.reflect.code.CtStatement; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.factory.Factory; +import spoon.support.compiler.FileSystemFile; +import spoon.test.template.testclasses.SubstituteArrayAccessTemplate; + +public class TemplateArrayAccessTest { + + @Test + public void testArrayAccess() throws Exception { + //contract: the template engine supports variable access, typed as Array substitution + Launcher spoon = new Launcher(); + spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstituteArrayAccessTemplate.java")); + + spoon.buildModel(); + Factory factory = spoon.getFactory(); + + CtClass resultKlass = factory.Class().create("Result"); + CtStatement result = new SubstituteArrayAccessTemplate(new String[]{"a","b"}).apply(resultKlass); + assertEquals("new java.lang.String[]{ \"a\" , \"b\" }.toString()", result.toString()); + } + +} diff --git a/src/test/java/spoon/test/template/testclasses/SubstituteArrayAccessTemplate.java b/src/test/java/spoon/test/template/testclasses/SubstituteArrayAccessTemplate.java new file mode 100644 index 00000000000..91920ee6700 --- /dev/null +++ b/src/test/java/spoon/test/template/testclasses/SubstituteArrayAccessTemplate.java @@ -0,0 +1,21 @@ +package spoon.test.template.testclasses; + +import spoon.template.Local; +import spoon.template.Parameter; +import spoon.template.StatementTemplate; + +public class SubstituteArrayAccessTemplate extends StatementTemplate { + + @Override + public void statement() throws Throwable { + anArray.toString(); + } + + @Parameter + String[] anArray; + + @Local + public SubstituteArrayAccessTemplate(String[] anArray) { + this.anArray = anArray; + } +} \ No newline at end of file