Search in sources :

Example 61 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class ParentTest method testParentOfCtExecutableReference.

@Test
public void testParentOfCtExecutableReference() throws Exception {
    // contract: parent of a executable reference is the element which call getExecutable().
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    final CtExecutableReference oldExecutable = inv.getExecutable();
    assertNotNull(oldExecutable.getParent());
    assertEquals(inv, oldExecutable.getParent());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 62 with CtInvocation

use of spoon.reflect.code.CtInvocation in project spoon by INRIA.

the class QualifiedThisRefTest method testPrintCtFieldAccessWorkEvenWhenParentNotInitialized.

@Test
public void testPrintCtFieldAccessWorkEvenWhenParentNotInitialized() throws Exception {
    CtClass zeclass = factory.Class().get(QualifiedThisRef.class);
    List<CtMethod> methods = zeclass.getMethodsByName("bla");
    assertEquals(1, methods.size());
    CtStatement invocation = methods.get(0).getBody().getStatement(0);
    assertTrue(invocation instanceof CtInvocation);
    CtInvocation<?> arg0 = (CtInvocation) invocation;
    CtExpression param = arg0.getArguments().get(0);
    CtExecutableReference execref = factory.Core().createExecutableReference();
    execref.setDeclaringType(factory.Type().createReference("java.util.Map"));
    execref.setSimpleName("exorcise");
    execref.setStatic(true);
    CtTypeReference tmp = param.getType();
    CtExpression arg = null;
    CtFieldReference ctfe = factory.createFieldReference();
    ctfe.setSimpleName("class");
    ctfe.setDeclaringType(tmp.box());
    arg = factory.Core().createFieldRead();
    ((CtFieldAccessImpl) arg).setVariable(ctfe);
    CtLiteral location = factory.Core().createLiteral();
    location.setType(factory.Type().createReference(String.class));
    CtTypeReference tmpref = factory.Core().clone(tmp);
    CtInvocation invoc = factory.Core().createInvocation();
    invoc.setExecutable(execref);
    invoc.setArguments(Arrays.asList(new CtExpression[] { param, arg, location }));
    execref.setActualTypeArguments(Arrays.asList(new CtTypeReference<?>[] { tmpref }));
    // succeeds
    arg0.getArguments().set(0, invoc);
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(factory.getEnvironment());
    printer.visitCtClass(zeclass);
    assertFalse(printer.getResult().isEmpty());
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtFieldReference(spoon.reflect.reference.CtFieldReference) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtStatement(spoon.reflect.code.CtStatement) CtFieldAccessImpl(spoon.support.reflect.code.CtFieldAccessImpl) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 63 with CtInvocation

use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.

the class StatementAddTest method testStatementAdd.

@Test
public void testStatementAdd() throws Exception {
    /*
            Test the StatementAdd amplifier. It reuse existing object to add method call of accessible method.
            It can reuse return value to add method call. It results here with 7 new test cases.
         */
    final String packageName = "fr.inria.statementadd";
    InputProgram inputProgram = Utils.getInputProgram();
    final Factory factory = inputProgram.getFactory();
    inputProgram.setFactory(factory);
    AmplificationHelper.setSeedRandom(42L);
    StatementAdd amplifier = new StatementAdd(packageName);
    amplifier.reset(factory.Class().get(packageName + ".ClassTargetAmplify"));
    CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
    List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
    System.out.println(amplifiedMethods);
    assertEquals(6, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("method", "methodWithDomainParameter", "methodWithPrimitifParameters", "methodWithPrimitifParameters", "methodWithReturn", "method1");
    assertTrue(amplifiedMethods.stream().allMatch(amplifiedMethod -> amplifiedMethod.filterChildren(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return expectedCalledMethod.contains(element.getExecutable().getSimpleName());
        }
    }).first() != null));
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Arrays(java.util.Arrays) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) List(java.util.List) Utils(fr.inria.Utils) CtClass(spoon.reflect.declaration.CtClass) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractTest(fr.inria.AbstractTest) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) CtInvocation(spoon.reflect.code.CtInvocation) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 64 with CtInvocation

use of spoon.reflect.code.CtInvocation in project dspot by STAMP-project.

the class ChangeMinimizerTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws Exception {
    /*
            ChangeMinimizer keeps only the assertions that trigger the failure on the second version
         */
    final CtClass testClass = Utils.findClass("example.TestSuiteExample");
    final String configurationPath = Utils.getInputConfiguration().getProperty("configPath");
    final String pathToFolder = Utils.getInputConfiguration().getProperty("folderPath");
    InputConfiguration inputConfiguration = new InputConfiguration(configurationPath);
    InputProgram inputProgram = InputConfiguration.initInputProgram(inputConfiguration);
    inputConfiguration.setInputProgram(inputProgram);
    String pathToChangedVersionOfProgram = pathToFolder + DSpotUtils.shouldAddSeparator.apply(pathToFolder) + (inputConfiguration.getProperty("targetModule") != null ? inputConfiguration.getProperty("targetModule") + DSpotUtils.shouldAddSeparator.apply(pathToFolder) : "");
    inputProgram.setProgramDir(pathToChangedVersionOfProgram);
    Initializer.initialize(Utils.getInputConfiguration(), inputProgram);
    final HashMap<CtMethod<?>, Failure> failurePerAmplifiedTest = new HashMap<>();
    final CtMethod<?> test2 = Utils.findMethod(testClass, "test2");
    failurePerAmplifiedTest.put(test2, new Failure(Description.EMPTY, new StringIndexOutOfBoundsException(-1)));
    final ChangeMinimizer changeMinimizer = new ChangeMinimizer(testClass, inputConfiguration, inputProgram, pathToChangedVersionOfProgram, failurePerAmplifiedTest);
    final CtInvocation assertion = test2.getElements(new TypeFilter<CtInvocation>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation element) {
            return AmplificationChecker.isAssert(element);
        }
    }).get(0);
    final CtInvocation clone = assertion.clone();
    clone.getElements(new TypeFilter<CtLiteral>(CtLiteral.class) {

        @Override
        public boolean matches(CtLiteral element) {
            return element.getValue() instanceof Integer && super.matches(element);
        }
    }).get(0).setValue(-1);
    test2.getBody().insertEnd(clone);
    assertEquals(3, test2.getBody().getStatements().size());
    final CtMethod<?> minimize = changeMinimizer.minimize(test2);
    assertEquals(2, minimize.getBody().getStatements().size());
}
Also used : HashMap(java.util.HashMap) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtMethod(spoon.reflect.declaration.CtMethod) Failure(org.junit.runner.notification.Failure) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Aggregations

CtInvocation (spoon.reflect.code.CtInvocation)64 Test (org.junit.Test)49 Factory (spoon.reflect.factory.Factory)30 Launcher (spoon.Launcher)27 CtMethod (spoon.reflect.declaration.CtMethod)25 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)25 CtClass (spoon.reflect.declaration.CtClass)18 CtTypeReference (spoon.reflect.reference.CtTypeReference)11 List (java.util.List)9 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)9 CtStatement (spoon.reflect.code.CtStatement)8 CtBlock (spoon.reflect.code.CtBlock)7 CtElement (spoon.reflect.declaration.CtElement)7 CtIf (spoon.reflect.code.CtIf)6 CtLiteral (spoon.reflect.code.CtLiteral)6 AbstractTest (fr.inria.AbstractTest)5 InputProgram (fr.inria.diversify.utils.sosiefier.InputProgram)5 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)5