rillig / gobco

Measure branch coverage of golang tests
62 stars 12 forks source link

Problems with assigments inside switch expressions #27

Closed emilioplatzer closed 1 year ago

emilioplatzer commented 1 year ago

I tried with the switch example in A Tour of Go. It seems that the assigment inside the switch is cousing problems (I tried moving it outside and it works).

    switch os := runtime.GOOS; os {
    case "darwin":
        fmt.Println("OS X.")

The output is:

PS C:\hecho\go\switch> gobco
# example/switch [example/switch.test]
.\switch.go:10:37: undefined: os      
FAIL    example/switch [build failed]
FAIL
exit status 2open C:\Users\abcd\AppData\Local\Temp\gobco-6ee1adac66cac296\gobco-counts.json: 

Complete code:

package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Print("Go runs on ")
    switch os := runtime.GOOS; os {
    case "darwin":
        fmt.Println("OS X.")
    case "linux":
        fmt.Println("Linux.")
    default:
        // freebsd, openbsd,
        // plan9, windows...
        fmt.Printf("%s.\n", os)
    }
}
rillig commented 1 year ago

Hello Emilio,

Thanks for your detailed bug report. I was able to reproduce the problem using gobco 1.0.1. About 3 weeks ago, I discovered the completely buggy instrumentation of the switch statement myself, wondered why nobody had reported it in the last few years, and fixed it.

Please try again with the master branch, that worked for me. It could be that there are a few edge cases left that don't work. I will add a comprehensive and systematic test suite later.

Best, Roland

rillig commented 1 year ago

Fixed in gobco 1.0.2, released yesterday.

emilioplatzer commented 1 year ago

Tested ok!. Thanks!