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());
}
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));
}
Aggregations