Skip to content

Instantly share code, notes, and snippets.

@namsral
Created October 5, 2016 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namsral/04ee7c39064e2e53d1000c1a56d07d6f to your computer and use it in GitHub Desktop.
Save namsral/04ee7c39064e2e53d1000c1a56d07d6f to your computer and use it in GitHub Desktop.
Go table-driven tests snippets for vim ultisnips
snippet tt "table-driven tests"
func Test${1:Func}(t *testing.T) {
testCases := []struct {
${2:input} ${3:Type}
want ${4:Type}
}{
{$2: ${5:""}, want: ${6:""}},${0: // comment}
}
for _, tc := range testCases {
if got := $1(tc.$2); got != tc.want {
t.Errorf("$1(%s) = %s; want %s", tc.$2, got, tc.want)
}
}
}
endsnippet
snippet tts "table-drive tests using subtests"
func Test${1:Func}(t *testing.T) {
testCases := []struct {
${2:input} ${3:Type}
want ${4:Type}
}{
{$2: ${5:""}, want: ${6:""}},${0: // comment}
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%q", tc.$2), func(t *testing.T) {
if got := $1(tc.$2); got != tc.want {
t.Errorf("got %s; want %s", got, tc.want)
}
})
}
}
endsnippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment