Skip to content

Commit

Permalink
Support coverage on switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
aopat committed Nov 12, 2019
1 parent 4dcdc6b commit bf844d6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func CheckCoverage(filename, tag string) error {
if block.Count > 0 {
continue
}
node := findNode(block.StartLine, block.StartCol, fs, f)
node := findNode(block.StartLine, fs, f)
if node == nil {
// nocover
return fmt.Errorf("Block not found for %s:%d. Has the source been modified since coverage was generated?",
Expand Down Expand Up @@ -62,13 +62,13 @@ func printBlock(fs *token.FileSet, file *ast.File, node ast.Node) error {
return nil
}

func findNode(startLine, startCol int, fs *token.FileSet, file *ast.File) ast.Node {
func findNode(startLine int, fs *token.FileSet, file *ast.File) ast.Node {
var node ast.Node
for _, d := range file.Decls {
ast.Inspect(d, func(n ast.Node) bool {
if n != nil {
pos := fs.Position(n.Pos())
if pos.Line == startLine && pos.Column == startCol {
if pos.Line == startLine {
node = n
return false
}
Expand Down
11 changes: 11 additions & 0 deletions cover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ func TestCheckCoverage_Mixed(t *testing.T) {

err := main.CheckCoverage("testdata/coverage.out", "nocover")
assert.Error(t, err)
assert.Contains(t, err.Error(), "Non-tagged code exists without coverage")
}

func TestCheckCoverage_Switch(t *testing.T) {
os.Remove("testdata/coverage.out")
cmd := exec.Command("go", "test", "-coverprofile=coverage.out", "github.com/digio/covermate/testdata/switcher")
cmd.Dir = "testdata"
assert.NoError(t, cmd.Run())

err := main.CheckCoverage("testdata/coverage.out", "nocover")
assert.NoError(t, err)
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
10 changes: 10 additions & 0 deletions testdata/switcher/switch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package switcher

func switcher(in string) string {
switch in {
case "one":
return "ok"
default: // nocover
return "unexpected"
}
}
7 changes: 7 additions & 0 deletions testdata/switcher/switch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package switcher

import "testing"

func TestSwitch(t *testing.T) {
switcher("one")
}

0 comments on commit bf844d6

Please sign in to comment.