diff --git a/gocov/report.go b/gocov/report.go index 6f633d4..261feeb 100644 --- a/gocov/report.go +++ b/gocov/report.go @@ -101,6 +101,45 @@ func (r *report) clear() { r.packages = nil } +// functionReports returns the packages functions as an array of +// reportFunction objects with the statements reached calculated +func functionReports(pkg *gocov.Package) reportFunctionList { + functions := make(reportFunctionList, len(pkg.Functions)) + for i, fn := range pkg.Functions { + reached := 0 + for _, stmt := range fn.Statements { + if stmt.Reached > 0 { + reached++ + } + } + functions[i] = reportFunction{fn, reached} + } + + return functions + +} + +// printTotalCoverage outputs the combined coverage for each +// package +func (r *report) printTotalCoverage(w io.Writer) { + var totalStatements, totalReached int + + for _, pkg := range r.packages { + functions := functionReports(pkg) + sort.Sort(reverse{functions}) + + for _, fn := range functions { + reached := fn.statementsReached + totalStatements += len(fn.Statements) + totalReached += reached + } + } + + coveragePercentage := float64(totalReached) / float64(totalStatements) * 100 + fmt.Fprintf(w, "Total Coverage: %.2f%% (%d/%d)", coveragePercentage, totalReached, totalStatements) + fmt.Fprintln(w) +} + // PrintReport prints a coverage report to the given writer. func printReport(w io.Writer, r *report) { w = tabwriter.NewWriter(w, 0, 8, 0, '\t', 0) @@ -110,19 +149,11 @@ func printReport(w io.Writer, r *report) { printPackage(w, pkg) fmt.Fprintln(w) } + r.printTotalCoverage(w) } func printPackage(w io.Writer, pkg *gocov.Package) { - functions := make(reportFunctionList, len(pkg.Functions)) - for i, fn := range pkg.Functions { - reached := 0 - for _, stmt := range fn.Statements { - if stmt.Reached > 0 { - reached++ - } - } - functions[i] = reportFunction{fn, reached} - } + functions := functionReports(pkg) sort.Sort(reverse{functions}) var longestFunctionName int