Search in sources :

Example 26 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateTest method testFieldAccessNameSubstitution.

@Test
public void testFieldAccessNameSubstitution() throws Exception {
    // contract: the substitution of name of whole field is possible
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/FieldAccessTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new FieldAccessTemplate("value").apply(factory.Class().create("x.X"));
        assertEquals("int value;", result.getField("value").toString());
        assertEquals("value = 7", result.getMethodsByName("m").get(0).getBody().getStatement(0).toString());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AnotherFieldAccessTemplate(spoon.test.template.testclasses.AnotherFieldAccessTemplate) FieldAccessTemplate(spoon.test.template.testclasses.FieldAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 27 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateArrayAccessTest method testArrayAccess.

@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", null, "b" }).apply(resultKlass);
    assertEquals("new java.lang.String[]{ \"a\", null, \"b\" }.toString()", result.toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) SubstituteArrayAccessTemplate(spoon.test.template.testclasses.SubstituteArrayAccessTemplate) Test(org.junit.Test)

Example 28 with FileSystemFile

use of spoon.support.compiler.FileSystemFile in project spoon by INRIA.

the class TemplateArrayAccessTest method testArrayLengthAccess.

@Test
public void testArrayLengthAccess() throws Exception {
    // contract: the template engine replaces length of collection of parameter values by number
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SubstituteArrayLengthTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtStatement result = new SubstituteArrayLengthTemplate(new String[] { "a", null, "b" }).apply(resultKlass);
    assertEquals("if (3 > 0);", result.toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) SubstituteArrayLengthTemplate(spoon.test.template.testclasses.SubstituteArrayLengthTemplate) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 FileSystemFile (spoon.support.compiler.FileSystemFile)28 Launcher (spoon.Launcher)26 Factory (spoon.reflect.factory.Factory)25 CtClass (spoon.reflect.declaration.CtClass)8 CtMethod (spoon.reflect.declaration.CtMethod)8 CtStatement (spoon.reflect.code.CtStatement)6 File (java.io.File)5 CtBlock (spoon.reflect.code.CtBlock)4 CtIf (spoon.reflect.code.CtIf)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtTry (spoon.reflect.code.CtTry)2 CtElement (spoon.reflect.declaration.CtElement)2 TemplateMatcher (spoon.template.TemplateMatcher)2 AnotherFieldAccessTemplate (spoon.test.template.testclasses.AnotherFieldAccessTemplate)2 FieldAccessOfInnerClassTemplate (spoon.test.template.testclasses.FieldAccessOfInnerClassTemplate)2 InvocationSubstitutionByExpressionTemplate (spoon.test.template.testclasses.InvocationSubstitutionByExpressionTemplate)2 ReturnReplaceTemplate (spoon.test.template.testclasses.ReturnReplaceTemplate)2 ModelUtils.getOptimizedString (spoon.testing.utils.ModelUtils.getOptimizedString)2 HashMap (java.util.HashMap)1