Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: measure coverage inside boolean expressions #8741

Open
josharian opened this issue Sep 15, 2014 · 1 comment
Open

testing: measure coverage inside boolean expressions #8741

josharian opened this issue Sep 15, 2014 · 1 comment
Milestone

Comments

@josharian
Copy link
Contributor

Test coverage works on basic blocks. It could be extended to short-circuiting boolean
expressions like f() && g().

Approach:

func GoCoverBool(ret bool, n int) bool {
  GoCover.Count[n] = 1
  return ret
}

Rewrite f() && g() to f() && GoCoverBool(true, 5) && g() and f()
|| g() to f() || GoCoverBool(false, 5) || g().

I don't know what adjustments to the downstream tools this might require.
@bradfitz bradfitz removed the new label Dec 18, 2014
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@JayNakrani
Copy link
Contributor

This looks possible. The only change needed would be instrumentation. Rest of the tool chain would simply work, because we already encode column info with each block.

$ cat -n haha.go 
     1  package haha
     2  
     3  func Hihi() {
     4    a := true
     5    b := true
     6  
     7    if a && b {
     8      _ = 42
     9    }
    10  
    11    if a || b {
    12      _ = 42
    13    }
    14  }

$ go tool cover -mode=count haha.go | cat -n
     1  package haha
     2  
     3  func Hihi() {
     4    GoCover.Count[0]++
     5    a := true
     6    b := true
     7  
     8    if a && b {
     9      GoCover.Count[2]++
    10      _ = 42
    11    }
    12    GoCover.Count[1]++
    13  
    14    if a || b {
    15      GoCover.Count[3]++
    16      _ = 42
    17    }
    18  }
    19  
    20  var GoCover = struct {
    21    Count     [4]uint32
    22    Pos       [3 * 4]uint32
    23    NumStmt   [4]uint16
    24  } {
    25    Pos: [3 * 4]uint32{
    26      3, 7, 0xc000d, // [0]
    27      11, 11, 0xc0002, // [1]
    28      7, 9, 0x3000c, // [2]
    29      11, 13, 0x3000c, // [3]
    30    },
    31    NumStmt: [4]uint16{
    32      3, // 0
    33      1, // 1
    34      1, // 2
    35      1, // 3
    36    },
    37  }

Here's how each block looks like from above instrumentation. A block is denoted with i[...].

package haha

func Hihi() 0[{
  a := true
  b := true

  if a && b] 2[{
    _ = 42
  ]}

  1[if a || b] 3[{
    _ = 42
  ]}
}

If we wanted to measure code coverage inside boolean expressions, new blocks would look like following.

package haha

func Hihi() 0[{
  a := true
  b := true

  if a] 1[&& b] 4[{
    _ = 42
  ]}

  2[if a] 3[|| b] 5[{
    _ = 42
  ]}
}

Now the question is, do we see enough value in doing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants