-
Notifications
You must be signed in to change notification settings - Fork 6
/
section.go
41 lines (34 loc) · 1.05 KB
/
section.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
package main
import (
"fmt"
"regexp"
"strings"
)
// Section represents a line as produced by `go test`
type Section struct {
path string
startLine int
startChar int
endLine int
endChar int
sortValue int
}
// NewSection parses a coverage line as produces by `go test`, for example "foo/bar.go:1.2,3.5 1 0"
func NewSection(line string) Section {
// parse which package was covered
fileAndLocation := strings.SplitN(line, ":", 2)
path := fileAndLocation[0]
location := fileAndLocation[1]
// parse where the coverage starts and ends
locations := regexp.MustCompile("[,. ]").Split(location, -1)
startLine := stringToInt(locations[0])
startChar := stringToInt(locations[1])
endLine := stringToInt(locations[2])
endChar := stringToInt(locations[3])
// allow sorting multiple sections from the same path
sortValue := startLine*100000 + startChar
return Section{path, startLine, startChar, endLine, endChar, sortValue}
}
func (s Section) Location() string {
return fmt.Sprintf("%v.%v,%v.%v", s.startLine, s.startChar, s.endLine, s.endChar)
}