-
Notifications
You must be signed in to change notification settings - Fork 77
/
cobertura.go
178 lines (154 loc) · 5.09 KB
/
cobertura.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/xml"
)
type Coverage struct {
XMLName xml.Name `xml:"coverage"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Version string `xml:"version,attr"`
Timestamp int64 `xml:"timestamp,attr"`
LinesCovered int64 `xml:"lines-covered,attr"`
LinesValid int64 `xml:"lines-valid,attr"`
BranchesCovered int64 `xml:"branches-covered,attr"`
BranchesValid int64 `xml:"branches-valid,attr"`
Complexity float32 `xml:"complexity,attr"`
Sources []*Source `xml:"sources>source"`
Packages []*Package `xml:"packages>package"`
}
type Source struct {
Path string `xml:",chardata"`
}
type Package struct {
Name string `xml:"name,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Complexity float32 `xml:"complexity,attr"`
Classes []*Class `xml:"classes>class"`
}
type Class struct {
Name string `xml:"name,attr"`
Filename string `xml:"filename,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Complexity float32 `xml:"complexity,attr"`
Methods []*Method `xml:"methods>method"`
Lines Lines `xml:"lines>line"`
}
type Method struct {
Name string `xml:"name,attr"`
Signature string `xml:"signature,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Complexity float32 `xml:"complexity,attr"`
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() int64 {
return int64(len(lines))
}
// NumLinesWithHits returns the number of lines with a hit count > 0
func (lines Lines) NumLinesWithHits() (numLinesWithHits int64) {
for _, line := range lines {
if line.Hits > 0 {
numLinesWithHits++
}
}
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 {
return method.Lines.HitRate()
}
// NumLines returns the number of lines
func (method Method) NumLines() int64 {
return method.Lines.NumLines()
}
// NumLinesWithHits returns the number of lines with a hit count > 0
func (method Method) NumLinesWithHits() int64 {
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 int64) {
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 int64) {
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 int64) {
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 int64) {
for _, class := range pkg.Classes {
numLinesWithHits += class.NumLinesWithHits()
}
return numLinesWithHits
}
// HitRate returns a float32 from 0.0 to 1.0 representing what fraction of lines
// have hits
func (cov Coverage) HitRate() float32 {
return float32(cov.NumLinesWithHits()) / float32(cov.NumLines())
}
// NumLines returns the number of lines
func (cov Coverage) NumLines() (numLines int64) {
for _, pkg := range cov.Packages {
numLines += pkg.NumLines()
}
return numLines
}
// NumLinesWithHits returns the number of lines with a hit count > 0
func (cov Coverage) NumLinesWithHits() (numLinesWithHits int64) {
for _, pkg := range cov.Packages {
numLinesWithHits += pkg.NumLinesWithHits()
}
return numLinesWithHits
}