Search in sources :

Example 11 with FileSystemFile

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

the class TemplateTest method testTemplateArrayAccess.

@Test
public void testTemplateArrayAccess() throws Exception {
    // contract: the template engine supports substitution of arrays of parameters.
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/ArrayAccessTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> resultKlass = factory.Class().create("Result");
    CtClass<?> templateClass = factory.Class().get(ArrayAccessTemplate.class);
    // create array of template parameters, which contains CtBlocks
    TemplateParameter[] params = templateClass.getMethod("sampleBlocks").getBody().getStatements().toArray(new TemplateParameter[0]);
    new ArrayAccessTemplate(params).apply(resultKlass);
    CtMethod<?> m = resultKlass.getMethod("method");
    // check that both TemplateParameter usages were replaced by appropriate parameter value and that substitution which miss the value is silently removed
    assertEquals(2, m.getBody().getStatements().size());
    assertTrue(m.getBody().getStatements().get(0) instanceof CtBlock);
    assertEquals("int i = 0", ((CtBlock) m.getBody().getStatements().get(0)).getStatement(0).toString());
    assertTrue(m.getBody().getStatements().get(1) instanceof CtBlock);
    assertEquals("java.lang.String s = \"Spoon is cool!\"", ((CtBlock) m.getBody().getStatements().get(1)).getStatement(0).toString());
    // check that both @Parameter usage was replaced by appropriate parameter value
    CtMethod<?> m2 = resultKlass.getMethod("method2");
    assertEquals("java.lang.System.out.println(\"second\")", m2.getBody().getStatement(0).toString());
    // check that substitution by missing value correctly produces empty expression
    assertEquals("java.lang.System.out.println(null)", m2.getBody().getStatement(1).toString());
}
Also used : TemplateParameter(spoon.template.TemplateParameter) CtBlock(spoon.reflect.code.CtBlock) ArrayAccessTemplate(spoon.test.template.testclasses.ArrayAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 12 with FileSystemFile

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

the class TemplateTest method testSubstitutionInsertAllNtoN.

@Test
public void testSubstitutionInsertAllNtoN() {
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/NtonCodeTemplate.java"));
    spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/C.java");
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> cclass = factory.Class().get("spoon.test.template.testclasses.C");
    new NtonCodeTemplate(cclass.getReference(), 5).apply(cclass);
    Set<CtMethod<?>> listMethods = cclass.getMethods();
    assertEquals(0, cclass.getMethodsByName("apply").size());
    assertEquals(4, listMethods.size());
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) NtonCodeTemplate(spoon.test.template.testclasses.NtonCodeTemplate) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 13 with FileSystemFile

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

the class TemplateTest method testTemplateInvocationSubstitution.

@Test
public void testTemplateInvocationSubstitution() throws Exception {
    // contract: the template engine supports substitution of method names in method calls.
    Launcher spoon = new Launcher();
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/InvocationTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> resultKlass = factory.Class().create("Result");
    new InvocationTemplate(factory.Type().OBJECT, "hashCode").apply(resultKlass);
    CtMethod<?> templateMethod = (CtMethod<?>) resultKlass.getElements(new NamedElementFilter<>(CtMethod.class, "invoke")).get(0);
    CtStatement templateRoot = (CtStatement) templateMethod.getBody().getStatement(0);
    // iface.$method$() becomes iface.hashCode()
    assertEquals("iface.hashCode()", templateRoot.toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) InvocationTemplate(spoon.test.template.testclasses.InvocationTemplate) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 14 with FileSystemFile

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

the class TemplateTest method testAnotherFieldAccessNameSubstitution.

@Test
public void testAnotherFieldAccessNameSubstitution() 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/AnotherFieldAccessTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    {
        // contract: String value is substituted in String literal
        final CtClass<?> result = (CtClass<?>) new AnotherFieldAccessTemplate().apply(factory.Class().create("x.X"));
        assertEquals("int x;", result.getField("x").toString());
        assertEquals("int m_x;", result.getField("m_x").toString());
        assertEquals("java.lang.System.out.println(((x) + (m_x)))", result.getAnonymousExecutables().get(0).getBody().getStatement(0).toString());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AnotherFieldAccessTemplate(spoon.test.template.testclasses.AnotherFieldAccessTemplate) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) Test(org.junit.Test)

Example 15 with FileSystemFile

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

the class ResourceTest method testEqualsFileSystemFile.

@Test
public void testEqualsFileSystemFile() throws Exception {
    // two files with equivalent paths are equals
    String entry = "src/test/resources/spoon/test/api/Foo.java";
    assertTrue(new FileSystemFile(new File(entry)).equals(new FileSystemFile(new File("./" + entry))));
}
Also used : FileSystemFile(spoon.support.compiler.FileSystemFile) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) 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