Finished the tests

This commit is contained in:
oowekyala
2017-07-27 00:47:26 +02:00
parent d06cb63d38
commit 9eedf85395
5 changed files with 227 additions and 87 deletions

View File

@ -13,6 +13,8 @@ import net.sourceforge.pmd.lang.java.rule.AbstractJavaMetricsRule;
import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
/**
* Simple n-path complexity rule.
*
* @author Clément Fournier
*/
public class NPathComplexityRule extends AbstractJavaMetricsRule {
@ -48,5 +50,4 @@ public class NPathComplexityRule extends AbstractJavaMetricsRule {
return data;
}
}

View File

@ -66,7 +66,6 @@ public class Foo { // This has a Cyclomatic Complexity = 12
</example>
</rule>
<!-- TODO -->
<rule name="NcssCount"
message="The {0} ''{1}'' has a NCSS line count of {2}."
since="3.9"
@ -82,16 +81,15 @@ public class Foo { // This has a Cyclomatic Complexity = 12
<example>
<![CDATA[
public class Foo extends Bar {
public Foo() {
//this class only has 4 NCSS lines
super();
public Foo() { //this class only has 4 NCSS lines
super();
super.foo();
}
super.foo();
}
}
]]>
</example>
@ -99,7 +97,7 @@ public class Foo extends Bar {
<rule name="NPathComplexity"
since="3.9"
message="The {0} {1} has an NPath complexity of {2}"
message="The {0} ''{1}'' has an NPath complexity of {2}"
class="net.sourceforge.pmd.lang.java.metrics.rule.NPathComplexityRule"
metrics="true"
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#NPathComplexity">
@ -111,8 +109,8 @@ public class Foo extends Bar {
<priority>3</priority>
<example>
<![CDATA[
void bar() { // this is something more complex than it needs to be,
if (y) { // it should be broken down into smaller methods or functions
void bar() { // This is something more complex than it needs to be,
if (y) { // it should be broken down into smaller methods or functions
for (j = 0; j < m; j++) {
if (j > r) {
doSomething();
@ -134,9 +132,9 @@ void bar() { // this is something more complex than it needs to be,
doSomethingDangerous();
} catch (Exception ex) {
makeAmends();
} finally {
dontDoItAgain();
}
} finally {
dontDoItAgain();
}
}
}

View File

@ -11,7 +11,6 @@ import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
/**
* @author Clément Fournier
*/
public class MetricsRulesTest extends SimpleAggregatorTst {
private static final String RULESET = "java-metrics";
@ -26,8 +25,8 @@ public class MetricsRulesTest extends SimpleAggregatorTst {
@Override
public void setUp() {
// addRule(RULESET, "CyclomaticComplexity");
// addRule(RULESET, "NcssCount");
addRule(RULESET, "CyclomaticComplexity");
addRule(RULESET, "NcssCount");
addRule(RULESET, "NPathComplexity");
}
}

View File

@ -5,7 +5,7 @@
public class Foo {
public static void bar() {
boolean a, b = true;
try { // 2 * 2 + 2
try { // total 6
if (true) { // 2
List buz = new ArrayList();
}
@ -210,6 +210,29 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description>Ensure switch has same complexity as equivalent ifs</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>'.Foo#bar()' has value 7.</message>
</expected-messages>
<code><![CDATA[
class Foo {
void bar() {
boolean a, b;
int j = 23;
if (j == 1 || j == 2) {
} else if (j == 3) {
j = 5;
} else if (j == 4) {
if (b && a) { bar(); }
}
}
}
]]></code>
</test-code>
<test-code>
<description>Boolean operators</description>
<expected-problems>1</expected-problems>
@ -227,8 +250,7 @@ public class Foo {
<code-fragment id="bug3484404"><![CDATA[
class Bar
{
class Bar {
public void x(boolean x, boolean y) {
z((x ? 1 : 2), (y ? 3 : 4));
}
@ -254,4 +276,28 @@ class Bar
<code-ref id="bug3484404"/>
</test-code>
<test-code>
<description>NPath supports constructors</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>'.Foo#Foo()' has value 7.</message>
</expected-messages>
<code><![CDATA[
class Foo {
Foo() {
boolean a, b;
int j = 23;
switch(j) {
case 1:
case 2: break;
case 3: j = 5; break;
case 4: if (b && a) { bar(); } break;
default: break;
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -1,100 +1,196 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description>ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
<code-fragment id="full-example"><![CDATA[
public class Foo {
public static void bar() {
if (true) {List buz = new ArrayList();}
}
public static void bar() {
boolean a, b = true;
try { // 2 * 2 + 2
if (true) { // 2
List buz = new ArrayList();
}
for(int i = 0; i < 19; i++) { // 2
List buz = new ArrayList();
}
} catch(Exception e) {
if (true) { // 2
e.printStackTrace();
}
}
int j = 0;
if (true || a && b) { // 4
j = 10;
return;
}
while (j++ < 20) { // 2
List buz = new ArrayList();
}
switch(j) { // 7
case 1:
case 2: break;
case 3: j = 5; break;
case 4: if (b && a) { bar(); } break;
default: break;
}
do { // 3
List buz = new ArrayList();
} while (a && j++ < 30);
if (b) { // 2
return;
}
}
}
]]></code>
</test-code>
]]></code-fragment>
<test-code>
<description>fail, with minimum</description>
<rule-property name="reportLevel">1</rule-property>
<description>Full example</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>The method bar() has an NPath complexity of 2</message>
<message>The method 'bar()' has an NPath complexity of 2016</message>
</expected-messages>
<code-ref id="full-example"/>
</test-code>
<test-code>
<description>Test default report level - report 200</description>
<rule-property name="reportLevel">0</rule-property>
<expected-problems>1</expected-problems>
<expected-messages>
<message>The method 'bar()' has an NPath complexity of 200</message>
</expected-messages>
<code><![CDATA[
public class Foo {
public static void bar() {
if (true) {List buz = new ArrayList();}
}
}
]]></code>
public class Foo {
public static void bar() {
boolean a, b = true;
int j = 0;
switch (j) { // 5
case 0:
case 1:
case 3: if (a || b) {} break;
}
switch (j) { // * 5
case 0:
case 1:
case 3: if (a || b) {} break;
}
if (true || a && b); // * 4
while (j++ < 20); // * 2
}
}
]]></code>
</test-code>
<test-code>
<description>Failure case</description>
<expected-problems>1</expected-problems>
<description>Test default report level - ignore 199</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public static int bar() {
try{
if (true) {List buz = new ArrayList();}
for(int i = 0; i < 19; i++) {List buz = new ArrayList();}
int j = 0;
if (true) {j = 10;}
while (j++ < 20) {List buz = new ArrayList();}
if (true) {j = 21;}
if(false) {j = 0;}
do {List buz = new ArrayList();} while (j++ < 30);
} catch(Exception e){
if (true) {e.printStackTrace();}
}
if (true) {return 1;}
else {return 2;}
}
}
]]></code>
class Foo {
void bar() {
boolean a, b;
try {
switch(j) { // 7
case 1:
case 2: break;
case 3: j = 5; break;
case 4: if (b && a) { bar(); } break;
default: break;
}
switch(j) { // * 7
case 1:
case 2: break;
case 3: j = 5; break;
case 4: if (b && a) { bar(); } break;
default: break;
}
if (true || a || b); // * 4
} catch (ScaryException e) {
if (true || a); // + 3
}
}
}
]]></code>
</test-code>
<test-code>
<description>Boolean operators</description>
<rule-property name="reportLevel">2</rule-property>
<expected-problems>1</expected-problems>
<expected-messages>
<message>The method 'bar()' has an NPath complexity of 4</message>
</expected-messages>
<code><![CDATA[
class Foo {
void bar() {
if (a && b || c);
}
}
]]></code>
</test-code>
<code-fragment id="bug3484404"><![CDATA[
class Bar
{
public void x(boolean x, boolean y)
{
z(
(x ? 1 : 2),
(y ? 3 : 4)
);
}
class Bar {
public void x(boolean x, boolean y) {
z((x ? 1 : 2), (y ? 3 : 4));
}
public int y(boolean x, boolean y)
{
return z(
(x ? 1 : 2),
(y ? 3 : 4)
);
}
public int y(boolean x, boolean y) {
return z((x ? 1 : 2), (y ? 3 : 4));
}
public int z(int x, int y)
{
return x + y;
}
public int z(int x, int y) {
return x + y;
}
}
]]></code-fragment>
<test-code>
<description>test case for bug 3484404 (Invalid NPath calculation in return statement)</description>
<expected-problems>0</expected-problems>
<code-ref id="bug3484404"/>
</test-code>
<test-code>
<description>test case for bug 3484404 (Invalid NPath calculation in return statement) with minimum 25</description>
<rule-property name="minimum">25.0</rule-property>
<rule-property name="reportLevel">5</rule-property>
<expected-problems>2</expected-problems>
<expected-messages>
<message>The method x() has an NPath complexity of 25</message>
<message>The method y() has an NPath complexity of 25</message>
<message>The method 'x(boolean, boolean)' has an NPath complexity of 25</message>
<message>The method 'y(boolean, boolean)' has an NPath complexity of 25</message>
</expected-messages>
<code-ref id="bug3484404"/>
</test-code>
<test-code>
<description>NPath supports constructors</description>
<rule-property name="reportLevel">6</rule-property>
<expected-problems>1</expected-problems>
<expected-messages>
<message>The constructor 'Foo()' has an NPath complexity of 7</message>
</expected-messages>
<code><![CDATA[
class Foo {
Foo() {
boolean a, b;
int j = 23;
switch(j) {
case 1:
case 2: break;
case 3: j = 5; break;
case 4: if (b && a) { bar(); } break;
default: break;
}
}
}
]]></code>
</test-code>
</test-data>