Documentation
¶
Overview ¶
Package errassert provides set of simple error assertions for table driven tests.
Example ¶
package main import ( "strconv" "testing" "github.com/zoido/errassert" ) func main() { t := testing.T{} // Provided by the testing package. type testCase struct { in string errassert errassert.ErrorAssertion } run := func(t *testing.T, tc testCase) { _, err := strconv.Atoi(tc.in) tc.errassert.Require(t, err) } testCases := map[string]testCase{ "ok": { in: "42", errassert: errassert.NilError(), }, "invalid input fails": { in: "invalid", errassert: errassert.SomeError(), }, "empty input fails": { in: "", errassert: errassert.ErrorEndsWith("invalid syntax"), }, } for name, tc := range testCases { t.Run(name, func(t *testing.T) { run(t, tc) }) } }
Output:
Index ¶
- type ErrorAssertion
- func Error(msg string) ErrorAssertion
- func ErrorAs(target interface{}) ErrorAssertion
- func ErrorContains(substring string) ErrorAssertion
- func ErrorEndsWith(suffix string) ErrorAssertion
- func ErrorIs(expected error) ErrorAssertion
- func ErrorStartsWith(prefix string) ErrorAssertion
- func NilError() ErrorAssertion
- func SomeError() ErrorAssertion
- func Want(assertions ...ErrorAssertion) ErrorAssertion
- type TestingT
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorAssertion ¶
ErrorAssertion represents a single instance of the error assertion. If the error is not as expected, call returns an error.
Zero value (nil) is valid and Assert nor Require will never fail. It can be looked at as "I don't care about the error" assertion.
Example (Custom) ¶
package main import ( "errors" "fmt" "strconv" "testing" "github.com/zoido/errassert" ) func main() { t := testing.T{} // Provided by the testing package. type testCase struct { in string errassert errassert.ErrorAssertion } run := func(t *testing.T, tc testCase) { _, err := strconv.Atoi(tc.in) tc.errassert.Require(t, err) } testCases := map[string]testCase{ "empty input fails": { in: "very specific error input", errassert: func(err error) error { if err == nil { return errors.New("expected error, got nil") } if err.Error() != "very specific error" { return fmt.Errorf("expected very specific error, got: '%v'", err.Error()) } return nil }, }, } for name, tc := range testCases { t.Run(name, func(t *testing.T) { run(t, tc) }) } }
Output:
func Error ¶ added in v0.4.0
func Error(msg string) ErrorAssertion
Error returns an assertion that checks if the error is equal to the error with the given message.
func ErrorAs ¶
func ErrorAs(target interface{}) ErrorAssertion
ErrorAs returns an assertion that checks if the error passes errors.As check.
func ErrorContains ¶
func ErrorContains(substring string) ErrorAssertion
ErrorContains returns an assertion that checks if the error contains the given substring.
func ErrorEndsWith ¶
func ErrorEndsWith(suffix string) ErrorAssertion
ErrorEndsWith returns an assertion that checks if the error ends with the given suffix.
func ErrorIs ¶
func ErrorIs(expected error) ErrorAssertion
ErrorIs returns an assertion that checks if the error passes errors.Is check.
func ErrorStartsWith ¶
func ErrorStartsWith(prefix string) ErrorAssertion
ErrorStartsWith returns an assertion that checks if the error starts with the given prefix.
func NilError ¶
func NilError() ErrorAssertion
NilError returns an assertion that checks if the error is nil.
func SomeError ¶
func SomeError() ErrorAssertion
SomeError returns an assertion that checks if the error is not nil.
func Want ¶
func Want(assertions ...ErrorAssertion) ErrorAssertion
Want combines multiple error assertions into a single assertion. All assertions must pass for the combined assertion to pass. If any of the assertions fails, the combined assertion fails and returns the first error encountered.
Example ¶
package main import ( "strconv" "testing" "github.com/zoido/errassert" ) func main() { t := testing.T{} // Provided by the testing package. type testCase struct { in string errassert errassert.ErrorAssertion } run := func(t *testing.T, tc testCase) { _, err := strconv.Atoi(tc.in) tc.errassert.Require(t, err) } testCases := map[string]testCase{ "ok": { in: "42", errassert: errassert.NilError(), }, "invalid input": { in: "input", errassert: errassert.Want( errassert.ErrorContains("\"input\""), errassert.ErrorEndsWith("invalid syntax"), ), }, } for name, tc := range testCases { t.Run(name, func(t *testing.T) { run(t, tc) }) } }
Output:
func (ErrorAssertion) Assert ¶
func (assertion ErrorAssertion) Assert(t TestingT, err error)
Assert checks if the error is as expected and calls t.Fail if not.
func (ErrorAssertion) Require ¶
func (assertion ErrorAssertion) Require(t TestingT, err error)
Require checks if the error is as expected and calls t.FailNow if not.
Directories
¶
Path | Synopsis |
---|---|
Package grpcerrassert provides assertions for gRPC status errors.
|
Package grpcerrassert provides assertions for gRPC status errors. |