Documentation
¶
Overview ¶
Package errorspec provides helpers for asserting error values in tests
var testCases = []struct {
input string
output string
errspec errorspec.Assertion
}{
{
input: "hello world",
output: "OK",
// default errspec equivalent to errorspec.NoError
},
{
input: "hello, w",
errspec: errorspec.Is(io.ErrUnexpectedEOF),
},
{
input: "おはよう",
errspec: errorspec.As(func(t testing.TB, err MyError) bool {
return assert.Equal(t, "jp", err.LangCode)
})
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out, err := MyFunc(tc.input)
assert.Equal(t, tc.output, out)
errorspec.CheckError(t, tc.errspec, err)
})
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckError ¶
CheckError applies the given assertion to err and returns the result. Table tests should use this function to evaluate the test case assertion against the error returned by the function under test.
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out, err := MyFunc(tc.input)
assert.Equal(t, tc.output, out)
errorspec.CheckError(t, tc.errspec, err)
})
}
Types ¶
type Assertion ¶
Assertion describes an error value in a test. A nil assertion is treated the same as NoError. Table test structs will typically will include an Assertion field.
type testCase struct {
name string
input string
output any
errspec errorspec.Assertion
}
func AllOf ¶
AllOf returns an assertion that checks all of the given assertions until one fails. If all assertions pass, the returned assertion passes.
Click to show internal directories.
Click to hide internal directories.