Skip to content

Commit

Permalink
Disallow line appearing more than once
Browse files Browse the repository at this point in the history
I'm not an expert on Cobertura, but it seems confusing to have stuff
like:

```
<line number="5" hits="1"></line>
<line number="5" hits="0"></line>
```

I decided to treat this situation in a pessimistic way - i.e.: if the
second entry for a line number is lower than the previous one, then we
update the hit count down; but we never update it up. In other words, if
we ever see a 0 for a line, we mark the line uncovered, even if we
previously saw a 1.
  • Loading branch information
msabramo committed Feb 15, 2018
1 parent 93e82da commit c42e4b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
16 changes: 16 additions & 0 deletions cobertura.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ func (lines Lines) NumLinesWithHits() (numLinesWithHits int64) {
return numLinesWithHits
}

// AddOrUpdateLine adds a line if it is a different line than the last line recorded.
// If it's the same line as the last line recorded then we update the hits down
// if the new hits is less; otherwise just leave it as-is
func (lines *Lines) AddOrUpdateLine(lineNumber int, hits int64) {
if len(*lines) > 0 {
lastLine := (*lines)[len(*lines)-1]
if lineNumber == lastLine.Number {
if hits < lastLine.Hits {
lastLine.Hits = hits
}
return
}
}
*lines = append(*lines, &Line{Number: lineNumber, Hits: hits})
}

// HitRate returns a float32 from 0.0 to 1.0 representing what fraction of lines
// have hits
func (method Method) HitRate() float32 {
Expand Down
2 changes: 1 addition & 1 deletion gocover-cobertura.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (v *fileVisitor) method(n *ast.FuncDecl) *Method {
continue
}
for i := b.StartLine; i <= b.EndLine; i++ {
method.Lines = append(method.Lines, &Line{Number: i, Hits: int64(b.Count)})
method.Lines.AddOrUpdateLine(i, int64(b.Count))
}
}
return method
Expand Down
23 changes: 10 additions & 13 deletions gocover-cobertura_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,45 +180,42 @@ func TestConvertSetMode(t *testing.T) {
if c.Methods == nil || len(c.Methods) != 1 {
t.Fatal()
}
if c.Lines == nil || len(c.Lines) != 5 { // Why 5 lines? hmm...
t.Fatal()
if c.Lines == nil || len(c.Lines) != 4 {
t.Errorf("Expected 4 lines but got %d", len(c.Lines))
}

m := c.Methods[0]
if m.Name != "Func1" {
t.Error()
}
if m.Lines == nil || len(m.Lines) != 5 {
t.Fatal()
if c.Lines == nil || len(c.Lines) != 4 {
t.Errorf("Expected 4 lines but got %d", len(c.Lines))
}

var l *Line
if l = m.Lines[0]; l.Number != 4 || l.Hits != 1 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}
if l = m.Lines[1]; l.Number != 5 || l.Hits != 1 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}
if l = m.Lines[2]; l.Number != 5 || l.Hits != 0 {
if l = m.Lines[1]; l.Number != 5 || l.Hits != 0 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}
if l = m.Lines[3]; l.Number != 6 || l.Hits != 0 {
if l = m.Lines[2]; l.Number != 6 || l.Hits != 0 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}
if l = m.Lines[4]; l.Number != 7 || l.Hits != 0 {
if l = m.Lines[3]; l.Number != 7 || l.Hits != 0 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}

if l = c.Lines[0]; l.Number != 4 || l.Hits != 1 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}
if l = c.Lines[1]; l.Number != 5 || l.Hits != 1 {
if l = c.Lines[1]; l.Number != 5 || l.Hits != 0 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}
if l = c.Lines[2]; l.Number != 5 || l.Hits != 0 {
if l = c.Lines[2]; l.Number != 6 || l.Hits != 0 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}
if l = c.Lines[3]; l.Number != 6 || l.Hits != 0 {
if l = c.Lines[3]; l.Number != 7 || l.Hits != 0 {
t.Errorf("unmatched line: Number:%d, Hits:%d", l.Number, l.Hits)
}

Expand Down

0 comments on commit c42e4b7

Please sign in to comment.