125 lines
2.3 KiB
Go
125 lines
2.3 KiB
Go
package metrieke
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestJavaLines(t *testing.T) {
|
|
file1 := `
|
|
/** class
|
|
* documentation
|
|
**/
|
|
public class JavaTest {
|
|
// single line comment
|
|
string field1;
|
|
|
|
/** Method Documentation **/
|
|
public static void main(String[] args){
|
|
/*multiline
|
|
comment*/
|
|
|
|
if(arg[0] == "narf") {
|
|
System.out.println("whoop");
|
|
}
|
|
}
|
|
}
|
|
`
|
|
pf := NewJavaFileFromContents("JavaTest.java", []byte(file1))
|
|
pf.Parse()
|
|
|
|
wantLoc := 19
|
|
wantLloc := 8
|
|
wantDloc := 4
|
|
wantCloc := 3
|
|
|
|
haveLoc := pf.Metrics.Loc
|
|
haveLloc := pf.Metrics.Lloc
|
|
haveDloc := pf.Metrics.Dloc
|
|
haveCloc := pf.Metrics.Cloc
|
|
|
|
if wantLoc != haveLoc {
|
|
t.Fatalf("expected loc %v but got %v\n", wantLoc, haveLoc)
|
|
}
|
|
|
|
if wantLloc != haveLloc {
|
|
t.Fatalf("expected lloc %v but got %v\n", wantLloc, haveLloc)
|
|
}
|
|
|
|
if wantDloc != haveDloc {
|
|
t.Fatalf("expected dloc %v but got %v\n", wantDloc, haveDloc)
|
|
}
|
|
|
|
if wantCloc != haveCloc {
|
|
t.Fatalf("expected cloc %v but got %v\n", wantCloc, haveCloc)
|
|
}
|
|
}
|
|
|
|
func TestJavaNesting(t *testing.T) {
|
|
file1 := `
|
|
public class Java {
|
|
public static void nesting4(String[] args){
|
|
if(arg[0] == "narf") {
|
|
while(True) {
|
|
for(int i=0; i<args.length(); i++) {
|
|
if(i==0) {
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public static void nesting0() {
|
|
}
|
|
|
|
}
|
|
`
|
|
pf := NewJavaFileFromContents("JavaTest.py", []byte(file1))
|
|
pf.Parse()
|
|
|
|
wantSum := 4
|
|
wantAvg := float64(2)
|
|
haveSum := pf.Metrics.SumNesting
|
|
haveAvg := pf.Metrics.AvgNesting
|
|
|
|
if wantSum != haveSum {
|
|
t.Fatalf("expected nesting sum %v but got %v\n", wantSum, haveSum)
|
|
}
|
|
|
|
if wantAvg != haveAvg {
|
|
t.Fatalf("expected nesting avg %v but got %v\n", wantAvg, haveAvg)
|
|
}
|
|
}
|
|
|
|
func testJavaComplexity(t *testing.T) {
|
|
file1 := `
|
|
public class Java {
|
|
public static void nesting4(String[] args){
|
|
if(arg[0] == "narf") {
|
|
while(True) {
|
|
for(int i=0; i<args.length(); i++) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public static void nesting0() {
|
|
}
|
|
}
|
|
`
|
|
|
|
pf := NewJavaFileFromContents("test.py", []byte(file1))
|
|
pf.Parse()
|
|
wantAvg := 1.5
|
|
wantSum := 3
|
|
|
|
haveSum := pf.Metrics.SumComplexity
|
|
haveAvg := pf.Metrics.AvgComplexity
|
|
|
|
if wantSum != haveSum {
|
|
t.Fatalf("expected mccc sum %v but got %v\n", wantSum, haveSum)
|
|
}
|
|
|
|
if wantAvg != haveAvg {
|
|
t.Fatalf("expected mccc sum %v but got %v\n", wantAvg, haveAvg)
|
|
}
|
|
}
|