This is a question about organizing table tests.

I believe the most common idiom for writing table tests is to store test 
cases in a slice of structs like in the example from 
https://github.com/golang/go/wiki/TableDrivenTests 
<https://github.com/golang/go/wiki/TableDrivenTests#example-of-a-table-driven-test>
:

var flagtests = []struct {
in  string
out string
}{
{"%a", "[%a]"},
// ...
{"%-1.2abc", "[%-1.2a]bc"},
}

I've also seen map[string]struct{...} used when one wants to give test 
cases a name (described in detail in 
https://dave.cheney.net/2019/05/07/prefer-table-driven-tests).

What got me curious, however, are cases where instead of a slice literal, 
the author has chosen to use an array literal. Example from the Go source 
tree:

src/net/http/cookiejar/jar_test.go:var hasDotSuffixTests = [...]struct {
src/net/http/cookiejar/jar_test.go-     s, suffix string
src/net/http/cookiejar/jar_test.go-}{
src/net/http/cookiejar/jar_test.go-     {"", ""},
src/net/http/cookiejar/jar_test.go-     {"", "."},
src/net/http/cookiejar/jar_test.go-     {"", "x"},


To my surprise, the array literal pattern appears more often than maps in 
tests in the Go tree:

$ git grep -F '[]struct {' -- '*_test.go' | wc -l 
742 
$ git grep -F '[...]struct {' -- '*_test.go' | wc -l  
38 
$ git grep -F 'map[string]struct {' -- '*_test.go' | wc -l  
11 


Why and when would one put test cases in an array literal? What is the 
point?


Thank you and cheers,

Rodolfo

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/799a78ea-c9ac-40c4-9a59-163f8f3c5e58%40googlegroups.com.

Reply via email to