package archtest import ( "go/ast" "testing" ) // maxFunctionLines keeps one function from collecting every step of a feature, // where two changes to unrelated steps collide. Split it into named steps. const maxFunctionLines = 300 func TestFunctionLength(t *testing.T) { for _, src := range sources(t) { for _, decl := range src.file.Decls { fn, ok := decl.(*ast.FuncDecl) if !ok { continue } start, end := fileSet.Position(fn.Pos()).Line, fileSet.Position(fn.End()).Line lines := end - start + 1 // Embedded scripts and templates are text, not steps. ast.Inspect(fn, func(n ast.Node) bool { if lit, ok := n.(*ast.BasicLit); ok { lines -= fileSet.Position(lit.End()).Line - fileSet.Position(lit.Pos()).Line } return true }) if lines > maxFunctionLines { t.Errorf("%s:%d: %s has %d lines, the limit is %d", src.rel, start, fn.Name.Name, lines, maxFunctionLines) } } } }