127 lines
2.2 KiB
Plaintext
127 lines
2.2 KiB
Plaintext
# functions test
|
|
snippet /^test/ "Main function" r
|
|
func Test_${1:Func}(t *testing.T) {
|
|
${0:${VISUAL}}
|
|
}
|
|
endsnippet
|
|
|
|
snippet tt "table-driven tests"
|
|
func Test_${1:Func}(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
expected ${2}
|
|
${3}
|
|
}{
|
|
{
|
|
name: "happy case",
|
|
expected: ${4},
|
|
${5},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
actual := ${7}$1(${6:params})
|
|
|
|
if $8 {
|
|
t.Errorf("Expected %v, actual %v", tc.expected, actual)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
endsnippet
|
|
|
|
snippet tte "table-driven tests with errors"
|
|
func Test_${1:Func}(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
expected ${2}
|
|
${3}
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "happy case",
|
|
expected: ${4}
|
|
${5},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
actual, err := ${7}$1(${6:params})
|
|
if (err != nil) != tc.wantErr {
|
|
t.Errorf("Error '%v' even if wantErr is %t", err, tc.wantErr)
|
|
return
|
|
}
|
|
|
|
if tc.wantErr == false && $8 {
|
|
t.Errorf("Expected %v, actual %v", tc.expected, actual)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
endsnippet
|
|
|
|
snippet err "Basic error handling" b
|
|
if err != nil {
|
|
${1}
|
|
}
|
|
endsnippet
|
|
|
|
snippet errr "Basic error handling return err" b
|
|
if err != nil {
|
|
return err
|
|
}
|
|
${1}
|
|
endsnippet
|
|
|
|
snippet errr, "Basic error handling return err with another return" b
|
|
if err != nil {
|
|
return ${1:nil}, err
|
|
}
|
|
endsnippet
|
|
|
|
snippet errw "Return wrapped error" b
|
|
if err != nil {
|
|
return errors.Wrap(err, "${1}")
|
|
}
|
|
endsnippet
|
|
|
|
snippet errwf "Basic error handling with wrapping format" b
|
|
if err != nil {
|
|
return errors.Wrapf(err, "${1}", ${2})
|
|
}
|
|
endsnippet
|
|
|
|
snippet errab "Basic abort with error from Gin Context" b
|
|
if err != nil {
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
endsnippet
|
|
|
|
snippet errabwf "Basic abort with error from Gin Context" b
|
|
if err != nil {
|
|
err = errors.Wrapf(err, "${1}", ${2})
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
endsnippet
|
|
|
|
snippet /^package/ "Package declaration" b
|
|
// Package $1 provides ...
|
|
package ${1:main}
|
|
$2
|
|
endsnippet
|
|
|
|
snippet switcht "Switch type statement" b
|
|
switch ${1:expression}${1/(.+)/ /} := ${2:var}.(Type) {
|
|
case ${0:int}
|
|
}
|
|
endsnippet
|
|
|
|
snippet e: "Variable declaration := with error" b
|
|
${1:name}, err:= ${0:value}
|
|
endsnippet
|