Skip to content

Commit

Permalink
test CtParameterRemoveRefactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed May 30, 2017
1 parent cf055ff commit 8206654
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
74 changes: 69 additions & 5 deletions src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -12,9 +13,14 @@

import org.junit.Test;

import spoon.Launcher;
import spoon.OutputType;
import spoon.SpoonModelBuilder;
import spoon.compiler.SpoonResourceHelper;
import spoon.refactoring.CtParameterRemoveRefactoring;
import spoon.refactoring.RefactoringException;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
Expand All @@ -24,7 +30,6 @@
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction;
import spoon.reflect.visitor.filter.ExecutableReferenceFilter;
import spoon.reflect.visitor.filter.NameFilter;
import spoon.reflect.visitor.filter.SubInheritanceHierarchyFunction;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.refactoring.parameter.testclasses.IFaceB;
Expand All @@ -34,9 +39,7 @@
import spoon.test.refactoring.parameter.testclasses.TypeA;
import spoon.test.refactoring.parameter.testclasses.TypeB;
import spoon.test.refactoring.parameter.testclasses.TypeC;
import spoon.test.refactoring.parameter.testclasses.TypeL;
import spoon.test.refactoring.parameter.testclasses.TypeR;
import spoon.test.refactoring.parameter.testclasses.TypeS;
import spoon.testing.utils.ModelUtils;

public class MethodsRefactoringTest {
Expand Down Expand Up @@ -64,6 +67,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 @@ -300,4 +304,64 @@ private boolean removeSame(Collection list, Object item) {
}
return false;
}
}

@Test
public void testCtParameterRemoveRefactoring() throws FileNotFoundException {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/"+testPackagePath)));
comp.build();
Factory factory = comp.getFactory();

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

CtMethod<?> methodTypeA_method1 = typeA.getMethodsByName("method1").get(0);
CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring();
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() throws FileNotFoundException {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/"+testPackagePath)));
comp.build();
Factory factory = comp.getFactory();

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 @@ -39,4 +39,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 @@ -21,4 +21,16 @@ private void methodWithLambdaOf_R() {
};
ifaceT.method1(1.0);
}

private void methodWithComplexExpression_R() {
IFaceT ifaceT = p->{
@TestHierarchy("R_method1")
int x;
};
/*
* 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 8206654

Please sign in to comment.