Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add tests.
  • Loading branch information
ryaneberly committed Dec 10, 2016
1 parent a69595e commit d3f0dce
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/cflint/BugInfo.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.cflint;

import java.io.ObjectInputStream.GetField;
import java.util.List;

import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginParameter;

import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.script.CFParsedStatement;
import javolution.context.Context;
import net.htmlparser.jericho.Element;

public class BugInfo implements Comparable<BugInfo> {
Expand Down Expand Up @@ -167,6 +169,8 @@ private void doMessageText(final Element elem) {
String message = notNull(bugInfo.getMessage());
final String variable = notNull(bugInfo.getVariable());
message = message.replace("${variable}", variable);
message = message.replace("${functionName}", bugInfo.function);

if (message.contains("{tag}") && elem != null) {
message = message.replaceAll("\\$\\{tag\\}", notNull(elem.getName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void expression(final CFScriptStatement expression, final Context context
complexity = 0;
alreadyTooComplex = false;
} else if (expression == null) {
bugs.add(new BugInfo.BugInfoBuilder().setLine(context.startLine()).setMessageCode("PARSE_NOTHING")
.setSeverity("WARNING").setFilename(context.getFilename()).setFunction(context.getFunctionName())
.setMessage("Nothing to parse").build());
// bugs.add(new BugInfo.BugInfoBuilder().setLine(context.startLine()).setMessageCode("PARSE_NOTHING")
// .setSeverity("WARNING").setFilename(context.getFilename()).setFunction(context.getFunctionName())
// .setMessage("Nothing to parse").build());
}
// Not using instanceof to avoid double counting
else if (expression.getClass().equals(CFIfStatement.class) || expression.getClass().equals(CFForStatement.class)
Expand Down Expand Up @@ -90,11 +90,7 @@ protected void checkComplexity(final String name, final int lineNo, final Contex
if (!alreadyTooComplex && complexity > threshold) {
alreadyTooComplex = true;

bugs.add(new BugInfo.BugInfoBuilder().setLine(lineNo).setMessageCode("FUNCTION_TOO_COMPLEX")
.setSeverity(severity)
.setFilename(context.getFilename()).setFunction(context.getFunctionName()).setMessage("Function "
+ name + " is too complex. Consider breaking the function into smaller functions.")
.build());
context.addMessage("FUNCTION_TOO_COMPLEX", null);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/cflint.definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
"className": "SimpleComplexityChecker",
"message": [
{
"messageText": "Function ${functionName} is too complex. Consider breaking the function into smaller functions.",
"code": "FUNCTION_TOO_COMPLEX",
"severity": "WARNING"
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/cflint.definition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
</ruleImpl>
<ruleImpl name="SimpleComplexityChecker" className="SimpleComplexityChecker">
<message code="FUNCTION_TOO_COMPLEX">
<messageText>Function ${functionName} is too complex. Consider breaking the function into smaller functions.</messageText>
<severity>WARNING</severity>
</message>
<parameter name="maximum" value="10" />
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/com/cflint/tests/Complexity/.cflintrc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<config>
<includes code="PARSE_NOTHING"/>
<includes code="FUNCTION_TOO_COMPLEX"/>
</config>
9 changes: 9 additions & 0 deletions src/test/resources/com/cflint/tests/Complexity/ifelse.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component {
public void function foo() {
if (something) {
doSomething();
} else {
doSomethingElse();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
29 changes: 29 additions & 0 deletions src/test/resources/com/cflint/tests/Complexity/ifelseComplex.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
component {
public void function foo() {
if (something) {
doSomething();
} else {
if (something) {
if (something) {
if (something) {
if (something) {
if (something) {
if (something) {
if (something) {
if (something) {
if (something) {
if (something) {
doSomethingElse();
}
}
}
}
}
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[ {
"severity" : "WARNING",
"id" : "FUNCTION_TOO_COMPLEX",
"message" : "FUNCTION_TOO_COMPLEX",
"category" : "CFLINT",
"abbrev" : "FT",
"locations" : [ {
"file" : "src/test/resources/com/cflint/tests/Complexity/ifelseComplex.cfc",
"fileName" : "ifelseComplex.cfc",
"function" : "foo",
"column" : "21",
"line" : "15",
"message" : "Function foo is too complex. Consider breaking the function into smaller functions.",
"variable" : "",
"expression" : "if(something ) {/ndoSomethingElse();/n/n }"
} ]
} ]

0 comments on commit d3f0dce

Please sign in to comment.