Skip to content

Commit

Permalink
fix of the magic number test computation
Browse files Browse the repository at this point in the history
  • Loading branch information
giograno committed Dec 10, 2020
1 parent 09d475c commit 45f334b
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions src/main/java/testsmell/smell/MagicNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import testsmell.*;
import testsmell.AbstractSmell;
import testsmell.TestMethod;
import testsmell.Util;
import thresholds.Thresholds;

import java.io.FileNotFoundException;
import java.util.List;

public class MagicNumberTest extends AbstractSmell {

private List<SmellyElement> smellyElementList;
public class MagicNumberTest extends AbstractSmell {

public MagicNumberTest(Thresholds thresholds) {
super(thresholds);
Expand All @@ -40,6 +39,7 @@ public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit

private class ClassVisitor extends VoidVisitorAdapter<Void> {
private MethodDeclaration currentMethod = null;
private MagicNumberTest magicNumberTest;
TestMethod testMethod;
private int magicCount = 0;

Expand All @@ -54,8 +54,7 @@ public void visit(MethodDeclaration n, Void arg) {

testMethod.setSmell(magicCount >= thresholds.getMagicNumberTest());
testMethod.addDataItem("MagicNumberCount", String.valueOf(magicCount));

smellyElementList.add(testMethod);
smellyElementsSet.add(testMethod);

//reset values for next method
currentMethod = null;
Expand All @@ -77,27 +76,27 @@ public void visit(MethodCallExpr n, Void arg) {
n.getNameAsString().equals("assertNotNull") ||
n.getNameAsString().equals("assertNull")) {
// checks all arguments of the assert method
for (Expression argument:n.getArguments()) {
for (Expression argument : n.getArguments()) {
// if the argument is a number
if(Util.isNumber(argument.toString())){
magicCount++;
}
// if the argument contains an ObjectCreationExpr (e.g. assertEquals(new Integer(2),...)
else if(argument instanceof ObjectCreationExpr){
for (Expression objectArguments:((ObjectCreationExpr) argument).getArguments()){
if(Util.isNumber(objectArguments.toString())){
magicCount++;
}
}
}
// if the argument contains an MethodCallExpr (e.g. assertEquals(someMethod(2),...)
else if(argument instanceof MethodCallExpr){
for (Expression objectArguments:((MethodCallExpr) argument).getArguments()){
if(Util.isNumber(objectArguments.toString())){
magicCount++;
}
}
}
if (Util.isNumber(argument.toString())) {
magicCount++;
}
// if the argument contains an ObjectCreationExpr (e.g. assertEquals(new Integer(2),...)
else if (argument instanceof ObjectCreationExpr) {
for (Expression objectArguments : ((ObjectCreationExpr) argument).getArguments()) {
if (Util.isNumber(objectArguments.toString())) {
magicCount++;
}
}
}
// if the argument contains an MethodCallExpr (e.g. assertEquals(someMethod(2),...)
else if (argument instanceof MethodCallExpr) {
for (Expression objectArguments : ((MethodCallExpr) argument).getArguments()) {
if (Util.isNumber(objectArguments.toString())) {
magicCount++;
}
}
}
}
}
}
Expand Down

0 comments on commit 45f334b

Please sign in to comment.