Skip to content

Instantly share code, notes, and snippets.

@tarilabs
Last active September 22, 2023 08:48
Show Gist options
  • Save tarilabs/6b60e2babb6ef69864140595cb5c6c99 to your computer and use it in GitHub Desktop.
Save tarilabs/6b60e2babb6ef69864140595cb5c6c99 to your computer and use it in GitHub Desktop.
closureforloop_test.go
package closureforloop
import (
"strings"
"testing"
)
// exercising on: https://go.dev/blog/loopvar-preview
func TestHappens(t *testing.T) {
tt := []struct {
name string
lower string
upper string
}{
{"test1", "a", "A"},
{"test2", "z", "Z"},
}
{
for testID, test := range tt {
tf := func(t *testing.T) {
t.Parallel()
t.Logf("Test: %d name: %s for %s and %s", testID, test.name, test.lower, test.upper)
if strings.ToUpper(test.lower) == test.upper {
t.Logf("Should expect %s once uppercased is %s", test.lower, test.upper)
} else {
t.Errorf("Should expect %s once uppercased is %s", test.lower, test.upper)
}
}
t.Run(test.name, tf)
}
}
// We can notice is testing the second testtable row, twice:
//
// === NAME TestHappens/test1
// closureforloop_test.go:21: Test: 1 name: test2 for z and Z
// closureforloop_test.go:24: Should expect z once uppercased is Z
// === NAME TestHappens/test2
// closureforloop_test.go:21: Test: 1 name: test2 for z and Z
// closureforloop_test.go:24: Should expect z once uppercased is Z
// --- PASS: TestHappens (0.00s)
// --- PASS: TestHappens/test1 (0.00s)
// --- PASS: TestHappens/test2 (0.00s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment