Skip to content

Commit

Permalink
test CtParameterRemoveRefactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed May 21, 2017
1 parent 1b409e4 commit d9d55c4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.junit.Test;

import spoon.Launcher;
import spoon.OutputType;
import spoon.refactoring.CtParameterRemoveRefactoring;
import spoon.refactoring.RefactoringException;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtLambda;
import spoon.reflect.declaration.CtClass;
Expand Down Expand Up @@ -59,6 +62,7 @@ public void testSubInheritanceHierarchyFunction() {
"spoon.test.refactoring.parameter.testclasses.TypeB",
"spoon.test.refactoring.parameter.testclasses.TypeB$1",
"spoon.test.refactoring.parameter.testclasses.TypeB$1Local",
"spoon.test.refactoring.parameter.testclasses.TypeB$2",
"spoon.test.refactoring.parameter.testclasses.TypeC",
"spoon.test.refactoring.parameter.testclasses.IFaceL",
"spoon.test.refactoring.parameter.testclasses.TypeL",
Expand Down Expand Up @@ -202,4 +206,56 @@ private void forEachMethodOfHierarchy(Factory factory, final String hierarchyNam
return false;
}).forEach(consumer);
}

@Test
public void testCtParameterRemoveRefactoring() {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
Factory factory = ModelUtils.build(new File("./src/test/java/"+testPackagePath));
Launcher launcher = ModelUtils.getLastLauncher();

CtType<?> typeA = factory.Class().get(TypeA.class);

CtMethod<?> methodTypeA_method1 = typeA.getMethodsByName("method1").get(0);
CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring().setTarget(methodTypeA_method1.getParameters().get(0));
refactor.setTarget(methodTypeA_method1.getParameters().get(0));
//check that expected methods are targets of refactoring
List<CtExecutable<?>> execs = refactor.getTargetExecutables();
execs.forEach(exec->{
//check that each to be modified method has one parameter
assertEquals(1, exec.getParameters().size());
});
refactor.refactor();
execs.forEach(exec->{
//check that each to be modified method has no parameter after refactoring
assertEquals(0, exec.getParameters().size());
});
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt("./target/spooned/"+testPackagePath, 8);
}
@Test
public void testCtParameterRemoveRefactoringValidationCheck() {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
Factory factory = ModelUtils.build(new File("./src/test/java/"+testPackagePath));
Launcher launcher = ModelUtils.getLastLauncher();

CtType<?> typeR = factory.Class().get(TypeR.class);

CtMethod<?> methodTypeR_method1 = typeR.getMethodsByName("method1").get(0);
CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring().setTarget(methodTypeR_method1.getParameters().get(0));
refactor.setTarget(methodTypeR_method1.getParameters().get(0));
//check that each to be refactored method has one parameter
List<CtExecutable<?>> execs = refactor.getTargetExecutables();
execs.forEach(exec->{
//check that each to be modified method has one parameter
assertEquals(1, exec.getParameters().size());
});
//try refactor
try {
refactor.refactor();
fail();
} catch (RefactoringException e) {
this.getClass();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ public void method1(Double p1) {
}
}
}
private void anMethodWithNullParameterValue() {
IFaceB<String> ifaceB = new IFaceB<String>() {
@Override
@TestHierarchy("A_method1")
public void method1(String p1) {
}
};
ifaceB.method1(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ private void methodWithLambdaOf_R() {
IFaceT ifaceT = /*R_method1*/ p->{};
ifaceT.method1(1.0);
}

private void methodWithComplexExpression_R() {
IFaceT ifaceT = /*R_method1*/ p->{};
/*
* the refactoring should by default report calling of method as problem, because it cannot know,
* whether that method call has side effects. Such method call remove might cause malfunction.
*/
ifaceT.method1(Math.abs(1.0));
}
}

0 comments on commit d9d55c4

Please sign in to comment.