coverage of switch case

79 views
Skip to first unread message

Manlio Perillo

unread,
Nov 24, 2015, 9:45:32 AM11/24/15
to golang-nuts
Hi.

There is a small issue with the go cover tool, when using multiple expressions in a case expression.
As an example, when testing the following function:

func foo(x int) {
    switch x {
    case 1, 2, 3, 5, 7:
        ...
    case 2:
        ... 
    }
}

A call to foo(1) will cause the entire line of the `case 1, 2, 3, 5, 7`expression to be highlighted, in the HTML page generated by go cover.
This is bad, since I can not tell if all the cases are covered.
This is not an issue with switch case, but an issue when multiple statements are on the same line.
Is this a limitation of the UI or a limitation of the coverage algorithm?

In general multi line statements are allowed by the grammar, but reformatted by go fmt, as an example:

func foo() {
if true { fmt.Println("true") } else { fmt.Println("false") }
}

So, if this is only an issue with the UI, I hope that a special case can be made to handle the switch statement.


Thanks  Manlio

Ian Lance Taylor

unread,
Nov 24, 2015, 10:08:18 AM11/24/15
to Manlio Perillo, golang-nuts
On Tue, Nov 24, 2015 at 6:45 AM, Manlio Perillo
<manlio....@gmail.com> wrote:
>
> There is a small issue with the go cover tool, when using multiple
> expressions in a case expression.
> As an example, when testing the following function:
>
> func foo(x int) {
> switch x {
> case 1, 2, 3, 5, 7:
> ...
> case 2:
> ...
> }
> }
>
> A call to foo(1) will cause the entire line of the `case 1, 2, 3, 5,
> 7`expression to be highlighted, in the HTML page generated by go cover.
> This is bad, since I can not tell if all the cases are covered.
> This is not an issue with switch case, but an issue when multiple statements
> are on the same line.
> Is this a limitation of the UI or a limitation of the coverage algorithm?

My understanding is that it is a limitation of the coverage tool,
which simply rewrites the program line by line.

Ian

Rob Pike

unread,
Nov 24, 2015, 10:11:53 AM11/24/15
to Manlio Perillo, golang-nuts
Working as designed. The cover tool works at block level, not expression level, so it cannot tell which of the case expressions was matched. See go tool cover -help.

-rob


On Tue, Nov 24, 2015 at 6:45 AM, Manlio Perillo <manlio....@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Pike

unread,
Nov 24, 2015, 10:12:31 AM11/24/15
to Ian Lance Taylor, Manlio Perillo, golang-nuts
Not line by line but block by block, but yes, this is a limitation.

-rob


Reply all
Reply to author
Forward
0 new messages