Search in sources :

Example 11 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class StatementAddTest method testStatementAddOnArrayObjects.

@Test
public void testStatementAddOnArrayObjects() throws Exception {
    final String packageName = "fr.inria.statementaddarray";
    InputProgram inputProgram = Utils.getInputProgram();
    final Factory factory = inputProgram.getFactory();
    inputProgram.setFactory(factory);
    AmplificationHelper.setSeedRandom(32L);
    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(5, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("methodWithArrayParatemeter", "methodWithArrayParatemeterFromDomain", "methodWithDomainParameter", "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 12 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project dspot by STAMP-project.

the class StatementAddTest method testStatementAddOnUnderTest.

@Test
public void testStatementAddOnUnderTest() throws Exception {
    Factory factory = Utils.getFactory();
    CtClass<Object> ctClass = factory.Class().get("fr.inria.mutation.ClassUnderTestTest");
    AmplificationHelper.setSeedRandom(23L);
    StatementAdd amplificator = new StatementAdd();
    amplificator.reset(ctClass);
    CtMethod originalMethod = Utils.findMethod(ctClass, "testLit");
    List<CtMethod> amplifiedMethods = amplificator.apply(originalMethod);
    System.out.println(amplifiedMethods);
    assertEquals(2, amplifiedMethods.size());
    List<String> expectedCalledMethod = Arrays.asList("plusOne", "minusOne");
    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) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 13 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class CommentTest method testCommentsInComment1And2.

@Test
public void testCommentsInComment1And2() {
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(Comment1.class);
    List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
    assertEquals(4, comments.size());
    type = (CtClass<?>) f.Type().get(Comment2.class);
    comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
    assertEquals(2, comments.size());
    CtComment commentD = comments.get(1);
    assertEquals("D", commentD.getContent());
}
Also used : Comment1(spoon.test.comment.testclasses.Comment1) CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 14 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class CtTypeTest method testIsSubTypeOfonTypeReferences.

@Test
public void testIsSubTypeOfonTypeReferences() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-c" });
    launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
    CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
    O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
        nameToTypeRef.put(var.getSimpleName(), var.getType());
    });
    int[] count = new int[1];
    O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
        for (CtComment comment : ass.getComments()) {
            checkIsNotSubtype(comment, nameToTypeRef);
            count[0]++;
        }
        ;
        count[0]++;
        checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
    });
    assertTrue(count[0] > (9 * 8));
}
Also used : CtComment(spoon.reflect.code.CtComment) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtAssignment(spoon.reflect.code.CtAssignment) HashMap(java.util.HashMap) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 15 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class DeleteTest method testDeleteChainOfAssignment.

@Test
public void testDeleteChainOfAssignment() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m4", factory.Type().INTEGER_PRIMITIVE, factory.Type().FLOAT_PRIMITIVE, factory.Type().STRING);
    final CtAssignment chainOfAssignment = method.getElements(new TypeFilter<>(CtAssignment.class)).get(0);
    assertNotNull(chainOfAssignment.getAssignment());
    chainOfAssignment.getAssignment().delete();
    assertNull(chainOfAssignment.getAssignment());
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

TypeFilter (spoon.reflect.visitor.filter.TypeFilter)132 Test (org.junit.Test)119 Launcher (spoon.Launcher)54 Factory (spoon.reflect.factory.Factory)52 CtMethod (spoon.reflect.declaration.CtMethod)43 CtInvocation (spoon.reflect.code.CtInvocation)27 CtClass (spoon.reflect.declaration.CtClass)24 CtLiteral (spoon.reflect.code.CtLiteral)18 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)18 AbstractTest (fr.inria.AbstractTest)17 List (java.util.List)17 CtTypeReference (spoon.reflect.reference.CtTypeReference)17 ArrayList (java.util.ArrayList)14 CtBlock (spoon.reflect.code.CtBlock)10 CtConstructorCall (spoon.reflect.code.CtConstructorCall)10 CtIf (spoon.reflect.code.CtIf)10 CtStatement (spoon.reflect.code.CtStatement)10 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)10 File (java.io.File)9 CtElement (spoon.reflect.declaration.CtElement)8