Added some tests for CyclomaticComplexityRule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1408 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-02-07 18:50:06 +00:00
parent 56319fa659
commit dd82e9730e
4 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.rules.CyclomaticComplexityRule;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleViolation;
import java.util.Iterator;
public class CyclomaticComplexityRuleTest extends RuleTst {
private CyclomaticComplexityRule rule = new CyclomaticComplexityRule();
public void setUp() {
rule.setMessage("The {0} ''{1}'' has a Cyclomatic Complexity of {2}.");
}
public void testOneMethod() throws Throwable {
rule.addProperty("reportLevel", "1");
Report report = process("CyclomaticComplexity1.java", rule);
Iterator i = report.iterator();
RuleViolation rv = (RuleViolation)i.next();
assertTrue(rv.getDescription().indexOf("Highest = 1") != -1);
}
public void testNastyComplicatedMethod() throws Throwable {
rule.addProperty("reportLevel", "10");
Report report = process("CyclomaticComplexity2.java", rule);
Iterator i = report.iterator();
RuleViolation rv = (RuleViolation)i.next();
assertTrue(rv.getDescription().indexOf("Highest = 12") != -1);
}
public void testConstructor() throws Throwable {
rule.addProperty("reportLevel", "1");
Report report = process("CyclomaticComplexity3.java", rule);
Iterator i = report.iterator();
RuleViolation rv = (RuleViolation)i.next();
assertTrue(rv.getDescription().indexOf("Highest = 1") != -1);
}
public void testLessComplicatedThanReportLevel() throws Throwable {
rule.addProperty("reportLevel", "10");
Report report = process("CyclomaticComplexity1.java", rule);
assertEquals(0, report.size());
}
}

View File

@ -0,0 +1,3 @@
public class CyclomaticComplexity1 {
public void foo() {}
}

View File

@ -0,0 +1,64 @@
public class CyclomaticComplexity2 {
public void example() {
int x = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int a1 = 0;
int a2 = 0;
int b1 = 0;
int b2 = 0;
int z = 0;
int h = 0;
int e = 0;
int f = 0;
if (a == b) {
if (a1 == b1) {
x=2;
} else if (a2 == b2) {
x=2;
}
else
{
x=2;
}
}
else if (c == d)
{
while (c == d)
{
x=2;
}
}
else if (e == f)
{
for (int n = 0; n < h; n++)
{
x=2;
}
}
else
{
switch (z)
{
case 1:
x=2;
break;
case 2:
x=2;
break;
case 3:
x=2;
break;
default:
x=2;
break;
}
}
}
}

View File

@ -0,0 +1,3 @@
public class CyclomaticComplexity3 {
public CyclomaticComplexity3() {}
}