metrieke/python_test.go

112 lines
2.1 KiB
Go

package metrieke
import (
"testing"
)
func TestPythonLines(t *testing.T) {
file1 := `"""Module documentation"""
from util.tracking import GlobalState
import numpy as np
import os.path
import pprint
def main():
"""function documentation
:param: none
"""
# normaler comment
g = GlobalState()
g.add_author()
sums = np.sum()
`
pf := NewPythonFileFromContents("test.py", []byte(file1))
pf.Parse()
wantLoc := 15
wantLloc := 8
wantDloc := 4
wantCloc := 1
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 TestPythonNesting(t *testing.T) {
file1 := `
def nesting3(a):
print(a)
if a == b:
while True:
for c in d:
print('narf')
elif a> b:
print('not counting')
def nesting1():
print('b')
if a > b:
print('c')
`
pf := NewPythonFileFromContents("test.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 testPythonComplexity(t *testing.T) {
file1 := `
def mccc2(a):
print('a')
if a == b:
print('c')
def mccc1():
print('c')
`
pf := NewPythonFileFromContents("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)
}
}