Added tests for LOC

This commit is contained in:
oowekyala
2017-06-24 22:20:52 +02:00
parent 3ecc9be86f
commit 2346176fe5
12 changed files with 177 additions and 13 deletions

View File

@ -61,7 +61,7 @@ public class ASTClassOrInterfaceDeclaration extends AbstractJavaAccessTypeNode i
}
@Override
public TypeKind getTypeKind(){
public TypeKind getTypeKind() {
return isInterface() ? TypeKind.INTERFACE : TypeKind.CLASS;
}
}

View File

@ -43,7 +43,7 @@ public class ASTEnumDeclaration extends AbstractJavaAccessTypeNode implements AS
}
@Override
public TypeKind getTypeKind(){
public TypeKind getTypeKind() {
return TypeKind.ENUM;
}
}

View File

@ -29,8 +29,8 @@ import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature;
/**
* Statistics about a class, enum, interface, or annotation. Gathers information about the contained members and their
* signatures. This class does not provide methods to operate directly on its nested classes, but only on itself. To
* operate on a nested class, retrieve the correct ClassStats with {@link PackageStats#getClassStats(QualifiedName,
* boolean)} then use the methods of ClassStats.
* operate on a nested class, retrieve the correct ClassStats with
* {@link PackageStats#getClassStats(QualifiedName, boolean)} then use the methods of ClassStats.
*
* <p>Note that at this level, entities of the DS do not manipulate QualifiedNames anymore, only Strings.
*

View File

@ -9,7 +9,6 @@ import java.util.Map;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.oom.api.MemoKey;
import net.sourceforge.pmd.lang.java.oom.api.Metric;
import net.sourceforge.pmd.lang.java.oom.api.Metric.Version;
import net.sourceforge.pmd.lang.java.oom.api.MetricVersion;
import net.sourceforge.pmd.lang.java.oom.api.OperationMetric;

View File

@ -7,9 +7,7 @@ package net.sourceforge.pmd.lang.java.oom.metrics;
import java.util.List;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.AccessNode;
import net.sourceforge.pmd.lang.java.ast.QualifiedName;
import net.sourceforge.pmd.lang.java.oom.AbstractMetric;
import net.sourceforge.pmd.lang.java.oom.api.ClassMetric;

View File

@ -35,16 +35,13 @@ public final class LocMetric extends AbstractMetric implements ClassMetric, Oper
@Override
public double computeFor(ASTAnyTypeDeclaration node, MetricVersion version) {
return node.getEndLine() - node.getBeginLine();
return 1 + node.getEndLine() - node.getBeginLine();
}
@Override
public double computeFor(ASTMethodOrConstructorDeclaration node, MetricVersion version) {
if (node.isAbstract()) {
return 1;
}
return node.getEndLine() - node.getBeginLine();
return 1 + node.getEndLine() - node.getBeginLine();
}
}

View File

@ -44,7 +44,8 @@ import net.sourceforge.pmd.lang.java.oom.api.OperationMetric;
/**
* Non Commenting Source Statements. Similar to LOC but only counts statements, which is roughly equivalent
* to counting the number of semicolons and opening braces in the program. The precise rules for counting
* statements comply with <a href="http://www.kclee.de/clemens/java/javancss/">JavaNCSS rules</a>.
* statements comply with <a href="http://www.kclee.de/clemens/java/javancss/">JavaNCSS rules</a>. The only
* difference is that import and package statements are not counted.
*
* @author Clément Fournier
* @see LocMetric

View File

@ -15,4 +15,8 @@ public class MetricsForceHook {
Metrics.setForce(b);
}
private MetricsForceHook() {
}
}

View File

@ -18,6 +18,7 @@ public class AllMetricsTest extends SimpleAggregatorTst {
addRule(RULESET, "CycloTest");
addRule(RULESET, "NcssTest");
addRule(RULESET, "WmcTest");
addRule(RULESET, "LocTest");
}
}

View File

@ -0,0 +1,24 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.oom.metrics;
import net.sourceforge.pmd.lang.java.oom.api.ClassMetricKey;
import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
/**
* @author Clément Fournier
*/
public class LocTestRule extends AbstractMetricTestRule {
@Override
protected ClassMetricKey getClassKey() {
return ClassMetricKey.LOC;
}
@Override
protected OperationMetricKey getOpKey() {
return OperationMetricKey.LOC;
}
}

View File

@ -0,0 +1,134 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<code-fragment id="full-example">
<![CDATA[
public class Foo {
int y, z, t;
String q;
{
bar();
int x;
switch (x) {
case 1: foo(); break;
case 2:
case 3: bar(); break;
default: break;
}
}
public void foo() {}
public static void main(String args[]) {
String k, l, m;
bar();
do {
x++;
} while (x < 2);
while (x < 4) {
x++;
}
for (int i = 1; x < 6; ) {
x += i;
}
{
x++;;;;;
}
int p =
2 + 4 + 5;
try {
x++;
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} finally {
// Do nothing
}
bar();
x = (int) bar();
/*
* This is
* a comment
*/
x.foobar();
}
}
]]>
</code-fragment>
<test-code>
<description>Full example</description>
<expected-problems>3</expected-problems>
<expected-messages>
<message>'.Foo' has value 58 highest 40.</message>
<message>'.Foo#foo()' has value 1.</message>
<message>'.Foo#main(String)' has value 40.</message>
</expected-messages>
<code-ref id="full-example"/>
</test-code>
<test-code>
<description>Empty method</description>
<expected-problems>2</expected-problems>
<expected-messages>
<message>'.Foo' has value 5 highest 3.</message>
<message>'.Foo#foo()' has value 3.</message>
</expected-messages>
<code>
<![CDATA[
public class Foo {
void foo() {
// Comment is counted
}
}
]]>
</code>
</test-code>
<test-code>
<description>Empty class</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>'.Foo' has value 2 highest 0.</message>
</expected-messages>
<code>
<![CDATA[
public class Foo {
}
]]>
</code>
</test-code>
<test-code>
<description>Switch</description>
<expected-problems>2</expected-problems>
<expected-messages>
<message>'.Foo' has value 12 highest 10.</message>
<message>'.Foo#foo()' has value 10.</message>
</expected-messages>
<code>
<![CDATA[
public class Foo {
void foo() {
int x;
switch (x) {
case 1:
case 2:
case 3:
case 4: foo(); break;
default: break;
}
}
}
]]>
</code>
</test-code>
</test-data>

View File

@ -27,4 +27,10 @@
metrics="true">
</rule>
<rule name="LocTest"
message = "''{0}'' has value {1}."
class="net.sourceforge.pmd.lang.java.oom.metrics.LocTestRule"
metrics="true">
</rule>
</ruleset>