Skip to content

Commit

Permalink
Calculate LineRate for pkg, class, method
Browse files Browse the repository at this point in the history
  • Loading branch information
msabramo committed Feb 14, 2018
1 parent 42de488 commit 3196d40
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
88 changes: 86 additions & 2 deletions cobertura.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Class struct {
BranchRate float32 `xml:"branch-rate,attr,omitempty"`
Complexity float32 `xml:"complexity,attr,omitempty"`
Methods []*Method `xml:"methods>method"`
Lines []*Line `xml:"lines>line"`
Lines Lines `xml:"lines>line"`
}

type Method struct {
Expand All @@ -47,10 +47,94 @@ type Method struct {
LineRate float32 `xml:"line-rate,attr,omitempty"`
BranchRate float32 `xml:"branch-rate,attr,omitempty"`
Complexity float32 `xml:"complexity,attr,omitempty"`
Lines []*Line `xml:"lines>line"`
Lines Lines `xml:"lines>line"`
}

type Line struct {
Number int `xml:"number,attr"`
Hits int64 `xml:"hits,attr"`
}

// Lines is a slice of Line pointers, with some convenience methods
type Lines []*Line

// HitRate returns a float32 from 0.0 to 1.0 representing what fraction of lines
// have hits
func (lines Lines) HitRate() (hitRate float32) {
return float32(lines.NumLinesWithHits()) / float32(len(lines))
}

// NumLines returns the number of lines
func (lines Lines) NumLines() int {
return len(lines)
}

// NumLinesWithHits returns the number of lines with a hit count > 0
func (lines Lines) NumLinesWithHits() (numLinesWithHits int) {
for _, line := range lines {
if line.Hits > 0 {
numLinesWithHits++
}
}
return numLinesWithHits
}

// HitRate returns a float32 from 0.0 to 1.0 representing what fraction of lines
// have hits
func (method Method) HitRate() float32 {
return method.Lines.HitRate()
}

// NumLines returns the number of lines
func (method Method) NumLines() int {
return method.Lines.NumLines()
}

// NumLinesWithHits returns the number of lines with a hit count > 0
func (method Method) NumLinesWithHits() int {
return method.Lines.NumLinesWithHits()
}

// HitRate returns a float32 from 0.0 to 1.0 representing what fraction of lines
// have hits
func (class Class) HitRate() float32 {
return float32(class.NumLinesWithHits()) / float32(class.NumLines())
}

// NumLines returns the number of lines
func (class Class) NumLines() (numLines int) {
for _, method := range class.Methods {
numLines += method.NumLines()
}
return numLines
}

// NumLinesWithHits returns the number of lines with a hit count > 0
func (class Class) NumLinesWithHits() (numLinesWithHits int) {
for _, method := range class.Methods {
numLinesWithHits += method.NumLinesWithHits()
}
return numLinesWithHits
}

// HitRate returns a float32 from 0.0 to 1.0 representing what fraction of lines
// have hits
func (pkg Package) HitRate() float32 {
return float32(pkg.NumLinesWithHits()) / float32(pkg.NumLines())
}

// NumLines returns the number of lines
func (pkg Package) NumLines() (numLines int) {
for _, class := range pkg.Classes {
numLines += class.NumLines()
}
return numLines
}

// NumLinesWithHits returns the number of lines with a hit count > 0
func (pkg Package) NumLinesWithHits() (numLinesWithHits int) {
for _, class := range pkg.Classes {
numLinesWithHits += class.NumLinesWithHits()
}
return numLinesWithHits
}
8 changes: 3 additions & 5 deletions gocover-cobertura.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (cov *Coverage) parseProfile(profile *Profile) error {
profile: profile,
}
ast.Walk(visitor, parsed)
pkg.LineRate = pkg.HitRate()
return nil
}

Expand All @@ -112,10 +113,12 @@ func (v *fileVisitor) Visit(node ast.Node) ast.Visitor {
case *ast.FuncDecl:
class := v.class(n)
method := v.method(n)
method.LineRate = method.Lines.HitRate()
class.Methods = append(class.Methods, method)
for _, line := range method.Lines {
class.Lines = append(class.Lines, line)
}
class.LineRate = class.Lines.HitRate()
}
return v
}
Expand All @@ -130,7 +133,6 @@ func (v *fileVisitor) method(n *ast.FuncDecl) *Method {
startCol := start.Column
endLine := end.Line
endCol := end.Column
numLinesCovered := 0
// The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block.
for _, b := range v.profile.Blocks {
if b.StartLine > endLine || (b.StartLine == endLine && b.StartCol >= endCol) {
Expand All @@ -143,12 +145,8 @@ func (v *fileVisitor) method(n *ast.FuncDecl) *Method {
}
for i := b.StartLine; i <= b.EndLine; i++ {
method.Lines = append(method.Lines, &Line{Number: i, Hits: int64(b.Count)})
if b.Count > 0 {
numLinesCovered++
}
}
}
method.LineRate = float32(numLinesCovered) / float32(len(method.Lines))
return method
}

Expand Down

0 comments on commit 3196d40

Please sign in to comment.