Search in sources :

Example 1 with CheckBoundTemplate

use of spoon.test.template.testclasses.bounds.CheckBoundTemplate in project spoon by INRIA.

the class TemplateTest method testCheckBoundTemplate.

@Test
public void testCheckBoundTemplate() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/bounds/FooBound.java"), SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/bounds/CheckBoundTemplate.java")).build();
    CtClass<?> c = factory.Class().get(FooBound.class);
    CtMethod<?> method = c.getMethodsByName("method").get(0);
    assertEquals(1, Parameters.getAllTemplateParameterFields(CheckBoundTemplate.class).size());
    assertEquals(1, Parameters.getAllTemplateParameterFields(CheckBoundTemplate.class, factory).size());
    // creating a template instance
    CheckBoundTemplate t = new CheckBoundTemplate();
    assertTrue(t.isWellFormed());
    assertFalse(t.isValid());
    CtParameter<?> param = method.getParameters().get(0);
    t.setVariable(param);
    assertTrue(t.isValid());
    // getting the final AST
    CtStatement injectedCode = (t.apply(null));
    assertTrue(injectedCode instanceof CtIf);
    CtIf ifStmt = (CtIf) injectedCode;
    // contains the replaced code
    assertEquals("(l.size()) > 10", ifStmt.getCondition().toString());
    // adds the bound check at the beginning of a method
    method.getBody().insertBegin(injectedCode);
    assertEquals(injectedCode, method.getBody().getStatement(0));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CheckBoundTemplate(spoon.test.template.testclasses.bounds.CheckBoundTemplate) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 Launcher (spoon.Launcher)1 CtIf (spoon.reflect.code.CtIf)1 CtStatement (spoon.reflect.code.CtStatement)1 Factory (spoon.reflect.factory.Factory)1 CheckBoundTemplate (spoon.test.template.testclasses.bounds.CheckBoundTemplate)1