Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bump spoon version #886

Merged
merged 10 commits into from
Mar 5, 2020
Prev Previous commit
Next Next commit
refactor: clean code (scout) and remove deprecated code
danglotb committed Mar 4, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 68ba3a1be3489dd45349f097b69a1b1867fbcf08
Original file line number Diff line number Diff line change
@@ -165,19 +165,20 @@ private static void addPrimitiveMap(List<CtStatement> invocations,CtMethod<?> te
// TODO we need maybe limit assertion on a limited number of elements
@SuppressWarnings("unchecked")
private static List<CtInvocation<?>> buildSnippetAssertCollection(Factory factory, CtMethod<?> testMethod, String expression, Collection value) {
final CtVariableAccess variableRead = factory.createVariableRead(
factory.createLocalVariableReference().setSimpleName(expression),
false
);
final CtExpression variableRead = new Translator(factory).translate(expression);

final CtExecutableReference contains = factory.Type().get(Collection.class).getMethodsByName("contains").get(0).getReference();
return (List<CtInvocation<?>>) value.stream()
.limit(Math.min(value.size(), MAX_NUMBER_OF_CHECKED_ELEMENT_IN_LIST))
.map(factory::createLiteral)
.map(o ->
TestFramework.get().buildInvocationToAssertion(
testMethod, AssertEnum.ASSERT_TRUE,
Collections.singletonList(factory.createInvocation(variableRead,
contains, (CtLiteral) o
Collections.singletonList(
factory.createInvocation(
variableRead,
contains,
(CtLiteral) o
)
)
)
@@ -188,10 +189,7 @@ private static List<CtInvocation<?>> buildSnippetAssertCollection(Factory factor
// TODO we need maybe limit assertion on a limited number of elements
@SuppressWarnings("unchecked")
private static List<CtInvocation<?>> buildSnippetAssertMap(Factory factory, CtMethod<?> testMethod, String expression, Map value) {
final CtVariableAccess variableRead = factory.createVariableRead(
factory.createLocalVariableReference().setSimpleName(expression),
false
);
final CtExpression variableRead = new Translator(factory).translate(expression);
final CtExecutableReference containsKey = factory.Type().get(Map.class).getMethodsByName("containsKey").get(0).getReference();
final CtExecutableReference get = factory.Type().get(Map.class).getMethodsByName("get").get(0).getReference();
return (List<CtInvocation<?>>) value.keySet().stream()
Original file line number Diff line number Diff line change
@@ -138,7 +138,7 @@ public TestResult compileAndRun(CtType<?> testClass,
throw new AmplificationException(e);
}
}
}else {
} else {
//Delete junit-platform.properties if exits
if (TestFramework.isJUnit5(testsToRun.get(0))) {
String rootPath = classPath.split(":")[0];
@@ -173,8 +173,8 @@ public TestResult compileAndRun(CtType<?> testClass,
* This Exception is not thrown when the compilation fails but rather when the arguments are wrong.
*/
public List<CtMethod<?>> compileAndDiscardUncompilableMethods(DSpotCompiler compiler,
CtType<?> testClassToBeCompiled,
List<CtMethod<?>> testsToRun) throws AmplificationException {
CtType<?> testClassToBeCompiled,
List<CtMethod<?>> testsToRun) throws AmplificationException {
final List<CtMethod<?>> uncompilableMethod = compileAndDiscardUncompilableMethods(compiler, testClassToBeCompiled, 0);
testsToRun.removeAll(uncompilableMethod);
uncompilableMethod.forEach(testClassToBeCompiled::removeMethod);
@@ -260,13 +260,17 @@ private static CtClass<?> getNewModelCtClass(String pathToSrcFolder, String full

// output the .java of the test class to be compiled
// this method delete also the old .class, i.e. the old compiled file of the same test class, if exists
private static void printJavaFileAndDeleteClassFile(DSpotCompiler compiler, CtType classTest) {
private static void printJavaFileAndDeleteClassFile(DSpotCompiler compiler, CtType<?> classTest) {
try {
// final String pathToJavaFile =
// compiler.getSourceOutputDirectory().getAbsolutePath() + "/"
// + classTest.getQualifiedName().replaceAll("\\.", "/") + ".java";
// forceDelete(pathToJavaFile);
DSpotUtils.printCtTypeToGivenDirectory(classTest, compiler.getSourceOutputDirectory());
} catch (Exception e) {
throw new RuntimeException(e);
}
String pathToDotClass =
final String pathToDotClass =
compiler.getBinaryOutputDirectory().getAbsolutePath() + "/"
+ classTest.getQualifiedName().replaceAll("\\.", "/") + ".class";
try {
Original file line number Diff line number Diff line change
@@ -8,13 +8,10 @@
import org.slf4j.LoggerFactory;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.ImportScanner;
import spoon.reflect.visitor.ImportScannerImpl;
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.TypeFilter;

@@ -89,7 +86,6 @@ public static CtType<?> createAmplifiedTest(List<CtMethod<?>> ampTest, CtType<?>
if (!shouldKeepOriginalTestMethods) {
classTest.getMethods().stream()
.filter(TestFramework.get()::isTest)
//.filter(AmplificationChecker::isTest)
.forEach(currentTestClass::removeMethod);
}
return currentTestClass;
@@ -138,10 +134,6 @@ public static CtMethod removeAmpTestParent(CtMethod amplifiedTest) {
return ampTestToParent.remove(amplifiedTest);
}

public static int getAmpTestParentSize() {
return ampTestToParent.size();
}

public static void addTestBindingToOriginal(CtMethod clonedTest, CtMethod fromTest) {
CtMethod originalTest = fromTest;
if (originalTestBindings.containsKey(fromTest)) {
@@ -150,22 +142,10 @@ public static void addTestBindingToOriginal(CtMethod clonedTest, CtMethod fromTe
originalTestBindings.put(clonedTest, originalTest);
}

public static void removeTestBindingToOriginal(CtMethod clonedTest) {
originalTestBindings.remove(clonedTest);
}

public static CtMethod getOriginalTestMethod(CtMethod clonedTest) {
return originalTestBindings.get(clonedTest);
}

public static int getTestBindingToOriginalSize() {
return originalTestBindings.size();
}

public static void resetTestBindingToOriginal() {
originalTestBindings.clear();
}

@Deprecated
public static Set<CtType> computeClassProvider(CtType testClass) {
List<CtType> types = Query.getElements(testClass.getParent(CtPackage.class), new TypeFilter(CtType.class));
@@ -179,35 +159,9 @@ public static Set<CtType> computeClassProvider(CtType testClass) {
types.add(testClass.getParent(CtType.class));
}

types.addAll(types.stream()
.flatMap(type -> getImport(type).stream())
.collect(Collectors.toSet()));


return new HashSet<>(types);
}

@Deprecated
public static Set<CtType> getImport(CtType type) {
if (!AmplificationHelper.importByClass.containsKey(type)) {
ImportScanner importScanner = new ImportScannerImpl();
try {
importScanner.computeImports(type);
Set<CtType> set = importScanner.getAllImports()
.stream()
.map(CtImport::getReference)
.filter(Objects::nonNull)
.filter(ctElement -> ctElement instanceof CtType)
.map(ctElement -> (CtType) ctElement)
.collect(Collectors.toSet());
AmplificationHelper.importByClass.put(type, set);
} catch (Exception e) {
AmplificationHelper.importByClass.put(type, new HashSet<>(0));
}
}
return AmplificationHelper.importByClass.get(type);
}

@Deprecated
public static List<CtMethod<?>> getPassingTests(List<CtMethod<?>> newTests, TestResult result) {
final List<String> passingTests = result.getPassingTests();
@@ -228,13 +182,4 @@ public static CtMethod<?> addOriginInComment(CtMethod<?> amplifiedTest, CtMethod
return amplifiedTest;
}

@Deprecated
public static CtMethod getTopParent(CtMethod test) {
CtMethod topParent;
CtMethod currentTest = test;
while ((topParent = getAmpTestParent(currentTest)) != null) {
currentTest = topParent;
}
return currentTest;
}
}
Original file line number Diff line number Diff line change
@@ -44,6 +44,10 @@ public String getReportForCollector() {

@Override
public String output(CtType<?> testClass, String outputDirectory) {
final File outputDirectoryFile = new File(DSpotUtils.shouldAddSeparator.apply(outputDirectory));
if (!outputDirectoryFile.exists()) {
outputDirectoryFile.mkdirs();
}
// 1 output the specific JSON file for the test class
final File outputJsonFile = new File(
DSpotUtils.shouldAddSeparator.apply(outputDirectory) +