Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #79 from Gman98ish/master
Browse files Browse the repository at this point in the history
Prints out totalled report coverage
  • Loading branch information
axw authored Mar 22, 2017
2 parents c77561c + ac809b6 commit 3a69a0d
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions gocov/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 3a69a0d

Please sign in to comment.