Search in sources :

Example 1 with AllMethodsSameSignatureFunction

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

the class MethodsRefactoringTest method testAllMethodsSameSignatureFunction.

@Test
public void testAllMethodsSameSignatureFunction() {
    Factory factory = ModelUtils.build(new File("./src/test/java/spoon/test/refactoring/parameter/testclasses"));
    // each executable in test classes is marked with a annotation TestHierarchy,
    // which defines the name of the hierarchy where this executable belongs to.
    // collect all executables which are marked that they belong to hierarchy A_method1
    List<CtExecutable<?>> executablesOfHierarchyA = getExecutablesOfHierarchy(factory, "A_method1");
    // check executables of this hierarchy
    checkMethodHierarchies(executablesOfHierarchyA);
    // collect all executables which are marked that they belong to hierarchy R_method1
    List<CtExecutable<?>> executablesOfHierarchyR = getExecutablesOfHierarchy(factory, "R_method1");
    // check executables of this hierarchy
    checkMethodHierarchies(executablesOfHierarchyR);
    // contract: CtConstructor has no other same signature
    CtConstructor<?> constructorTypeA = factory.Class().get(TypeA.class).getConstructors().iterator().next();
    CtExecutable<?> exec = constructorTypeA.map(new AllMethodsSameSignatureFunction()).first();
    assertNull("Unexpected executable found by Constructor of TypeA " + exec, exec);
    CtConstructor<?> constructorTypeB = factory.Class().get(TypeB.class).getConstructors().iterator().next();
    exec = constructorTypeA.map(new AllMethodsSameSignatureFunction()).first();
    assertNull("Unexpected executable found by Constructor of TypeA " + exec, exec);
    // contract: constructor is returned if includingSelf == true
    assertSame(constructorTypeA, constructorTypeA.map(new AllMethodsSameSignatureFunction().includingSelf(true)).first());
}
Also used : TypeB(spoon.test.refactoring.parameter.testclasses.TypeB) Factory(spoon.reflect.factory.Factory) AllMethodsSameSignatureFunction(spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) TypeA(spoon.test.refactoring.parameter.testclasses.TypeA) Test(org.junit.Test)

Example 2 with AllMethodsSameSignatureFunction

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

the class MethodsRefactoringTest method checkMethodHierarchy.

private void checkMethodHierarchy(List<CtExecutable<?>> expectedExecutables, CtExecutable startExecutable) {
    // contract: check that by default it does not includes self
    // contract: check that by default it returns lambdas
    {
        final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction()).list();
        assertFalse("Unexpected start executable " + startExecutable, containsSame(executables, startExecutable));
        // check that some method was found
        assertTrue(executables.size() > 0);
        // check that expected methods were found and remove them
        expectedExecutables.forEach(m -> {
            boolean found = removeSame(executables, m);
            if (startExecutable == m) {
                // it is start method. It should not be there
                assertFalse("The signature " + getQSignature(m) + " was returned too", found);
            } else {
                assertTrue("The signature " + getQSignature(m) + " not found", found);
            }
        });
        // check that there is no unexpected executable
        assertTrue("Unexpected executables: " + executables, executables.isEmpty());
    }
    // contract: check that includingSelf(true) returns startMethod too
    // contract: check that by default it still returns lambdas
    {
        final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true)).list();
        assertTrue("Missing start executable " + startExecutable, containsSame(executables, startExecutable));
        // check that some method was found
        assertTrue(executables.size() > 0);
        // check that expected methods were found and remove them
        expectedExecutables.forEach(m -> {
            assertTrue("The signature " + getQSignature(m) + " not found", removeSame(executables, m));
        });
        // check that there is no unexpected executable
        assertTrue("Unexpected executables: " + executables, executables.isEmpty());
    }
    // contract: check that includingLambdas(false) returns no lambda expressions
    {
        final List<CtExecutable<?>> executables = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true).includingLambdas(false)).list();
        if (startExecutable instanceof CtLambda) {
            // lambda must not be returned even if it is first
            assertFalse("Unexpected start executable " + startExecutable, containsSame(executables, startExecutable));
        } else {
            assertTrue("Missing start executable " + startExecutable, containsSame(executables, startExecutable));
        }
        // check that some method was found
        assertTrue(executables.size() > 0);
        // check that expected methods were found and remove them
        expectedExecutables.forEach(m -> {
            if (m instanceof CtLambda) {
                // the lambdas are not expected. Do not ask for them
                return;
            }
            assertTrue("The signature " + getQSignature(m) + " not found", removeSame(executables, m));
        });
        // check that there is no unexpected executable or lambda
        assertTrue("Unexepcted executables " + executables, executables.isEmpty());
    }
    // contract: check early termination
    // contract: check that first returned element is the startExecutable itself if includingSelf == true
    CtExecutable<?> exec = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(true)).first();
    assertSame(startExecutable, exec);
    // contract: check that first returned element is not the startExecutable itself if includingSelf == false, but some other executable from the expected
    exec = startExecutable.map(new AllMethodsSameSignatureFunction().includingSelf(false)).first();
    assertNotSame(startExecutable, exec);
    assertTrue(containsSame(expectedExecutables, exec));
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) AllMethodsSameSignatureFunction(spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction) IFaceL(spoon.test.refactoring.parameter.testclasses.IFaceL) IFaceK(spoon.test.refactoring.parameter.testclasses.IFaceK) CtStatement(spoon.reflect.code.CtStatement) ArrayList(java.util.ArrayList) SpoonModelBuilder(spoon.SpoonModelBuilder) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) IFaceB(spoon.test.refactoring.parameter.testclasses.IFaceB) TypeA(spoon.test.refactoring.parameter.testclasses.TypeA) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) TypeC(spoon.test.refactoring.parameter.testclasses.TypeC) TypeB(spoon.test.refactoring.parameter.testclasses.TypeB) CtExecutable(spoon.reflect.declaration.CtExecutable) CtLambda(spoon.reflect.code.CtLambda) CtConstructor(spoon.reflect.declaration.CtConstructor) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Iterator(java.util.Iterator) RefactoringException(spoon.refactoring.RefactoringException) SpoonResourceHelper(spoon.compiler.SpoonResourceHelper) TypeR(spoon.test.refactoring.parameter.testclasses.TypeR) Collection(java.util.Collection) OutputType(spoon.OutputType) Test(org.junit.Test) CtParameterRemoveRefactoring(spoon.refactoring.CtParameterRemoveRefactoring) Factory(spoon.reflect.factory.Factory) ModelUtils(spoon.testing.utils.ModelUtils) TestHierarchy(spoon.test.refactoring.parameter.testclasses.TestHierarchy) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) SubInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SubInheritanceHierarchyFunction) Assert(org.junit.Assert) Collections(java.util.Collections) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter) CtMethod(spoon.reflect.declaration.CtMethod) CtLambda(spoon.reflect.code.CtLambda) ArrayList(java.util.ArrayList) List(java.util.List) AllMethodsSameSignatureFunction(spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction)

Aggregations

File (java.io.File)2 Test (org.junit.Test)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 Factory (spoon.reflect.factory.Factory)2 AllMethodsSameSignatureFunction (spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction)2 TypeA (spoon.test.refactoring.parameter.testclasses.TypeA)2 TypeB (spoon.test.refactoring.parameter.testclasses.TypeB)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Assert (org.junit.Assert)1 Launcher (spoon.Launcher)1 OutputType (spoon.OutputType)1 SpoonModelBuilder (spoon.SpoonModelBuilder)1 SpoonResourceHelper (spoon.compiler.SpoonResourceHelper)1 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)1