Documentation ¶
Overview ¶
Package testerr provides some helpers to check errors from calls in unit tests.
Especially if the errors are not the focus of the test, it is useful to also check these errors - especially those accompanying other return values - without distracting too much from the actual test.
Test functions in testerr that begin with "Should" (Should, Should1) call the error function of a test if the test fails. Test functions that begin with "Shall" (Shall, Shall1) call the fatal function of the test if the test fails.
Example ¶
var t *testing.T testedFunc := func() (int, error) { return 4711, nil } // In plain Go: n, err := testedFunc() if err != nil { t.Fatalf("illegal error: %s", err) } if n < 0 || n > 10000 { t.Errorf("%d out of range", n) } // Short, but ignoring error may yield misleading results: n, _ = testedFunc() if n < 0 || n > 10000 { t.Errorf("%d out of range", n) } // Using testerr: n = Shall1(testedFunc()).BeNil(t) // Calls t.Fatal() if err is not nil if n < 0 || n > 10000 { t.Errorf("%d out of range", n) }
Output:
Index ¶
- Variables
- func MsgString(msg ...any) string
- type CheckFunc
- type Checker
- func Is(target error) Checker
- func IsNil(msg ...any) Checker
- func Msg(msg ...any) Checker
- func MsgContains(msg ...any) Checker
- func MsgMatch(expr *regexp.Regexp) Checker
- func MsgMatchRegexp(expr string) Checker
- func MsgPrefix(msg ...any) Checker
- func MsgSuffix(msg ...any) Checker
- func NotNil(msg ...any) Checker
- type Ret0Error
- func (r Ret0Error) All(t Test, checks ...Checker) error
- func (r Ret0Error) Any(t Test, checks ...Checker) error
- func (r Ret0Error) Be(t Test, target error) error
- func (r Ret0Error) BeNil(t Test, msg ...any) error
- func (r Ret0Error) Check(t Test, check Checker) error
- func (r Ret0Error) NotNil(t Test, msg ...any) error
- type Ret0Fatal
- func (r Ret0Fatal) All(t Test, checks ...Checker) error
- func (r Ret0Fatal) Any(t Test, checks ...Checker) error
- func (r Ret0Fatal) Be(t Test, target error) error
- func (r Ret0Fatal) BeNil(t Test, msg ...any)
- func (r Ret0Fatal) Check(t Test, check Checker) error
- func (r Ret0Fatal) NotNil(t Test, msg ...any) error
- type Ret1Error
- func (r Ret1Error[R]) All(t Test, checks ...Checker) R
- func (r Ret1Error[R]) Any(t Test, checks ...Checker) R
- func (r Ret1Error[R]) Be(t Test, target error) R
- func (r Ret1Error[R]) BeNil(t Test, msg ...any) R
- func (r Ret1Error[R]) Check(t Test, check Checker) R
- func (r Ret1Error[R]) NotNil(t Test, msg ...any) R
- type Ret1Fatal
- func (r Ret1Fatal[R]) All(t Test, checks ...Checker) R
- func (r Ret1Fatal[R]) Any(t Test, checks ...Checker) R
- func (r Ret1Fatal[R]) Be(t Test, target error) R
- func (r Ret1Fatal[R]) BeNil(t Test, msg ...any) R
- func (r Ret1Fatal[R]) Check(t Test, check Checker) R
- func (r Ret1Fatal[R]) NotNil(t Test, msg ...any) R
- type Test
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Ln lnIndicator Fmt fmtIndicator )
Functions ¶
func MsgString ¶
MsgString is used to format strings throughout testerr using the fmt.Print functions without requiring the …ln() and …f() variants for all functions suppoting messages. MsgString will call fmt.Sprintln() if msg[0]==Ln and fmt.Sprintf if msg[0]==Fmt. Otherwise MsgString calls fmt.Sprint.
Example ¶
fmt.Print("fmt", 4711, true) fmt.Println() io.WriteString(os.Stdout, MsgString("fmt", 4711, true)) fmt.Println() fmt.Println("fmt", 4711, true) io.WriteString(os.Stdout, MsgString(Ln, "fmt", 4711, true)) fmt.Printf("fmt %d & %t", 4711, true) fmt.Println() io.WriteString(os.Stdout, MsgString(Fmt, "fmt %d & %t", 4711, true)) fmt.Println()
Output: fmt4711 true fmt4711 true fmt 4711 true fmt 4711 true fmt 4711 & true fmt 4711 & true
Types ¶
type Checker ¶
A Checker examines the error value passed to the Check method for some condition and return nil if the condition is fulfilled. Otherwise, Check returns an error that describes why the condition is not fulfilled. Basic checkers are already integrated into the RetN(Error|Fatal) types for easy use.
func IsNil ¶
IsNil returns a checker that checks whether an error is nil. With msg you can add an information message in case the check fails (see MsgString).
func Msg ¶
Msg returns a checker that requires err != nil and checks the error string to be MsgString(msg).
func MsgContains ¶
Msg returns a checker that requires err != nil and checks the error string to contain MsgString(msg).
func MsgMatch ¶
Msg returns a checker that requires err != nil and checks the error string to match the regular expression expr.
func MsgMatchRegexp ¶
Msg returns a checker that requires err != nil and checks the error string to match the regular expression regexp.Compile(expr).
func MsgPrefix ¶
Msg returns a checker that requires err != nil and checks the error string to start with MsgString(msg).
type Ret0Error ¶
type Ret0Error struct {
// contains filtered or unexported fields
}
func Should ¶
Should records the passed error, usually from a function call, in order to react to an unexpected error with t.Error(). The error can be analysed using the methods of Ret0Error.
Example ¶
testedFunction := func() error { return errors.New("this is an error") } var t *testing.T // Usually passed in by the test framework Should(testedFunction()).BeNil(t) // will call t.Error()
Output:
type Ret0Fatal ¶
type Ret0Fatal struct {
// contains filtered or unexported fields
}
func Shall ¶
Shall records the passed error, usually from a function call, in order to react to an unexpected error with t.Fatal(). The error can be analysed using the methods of Ret0Fatal.
Example ¶
testedFunction := func() error { return errors.New("this is an error") } var t *testing.T // Usually passed in by the test framework Shall(testedFunction()).BeNil(t) // will call t.Fatal()
Output:
type Ret1Error ¶
type Ret1Error[R any] struct { // contains filtered or unexported fields }
func Should1 ¶
Should1 records the passed values, usually from a function call, in order to react to an unexpected error with t.Error(). The error is evaluated using the Ret1Error methods and the remaining values are returned.
Example ¶
testedFunction := func() (int, error) { return 4711, errors.New("this is an error") } var t *testing.T // Usually passed in by the test framework n := Should1(testedFunction()).BeNil(t) // will call t.Error() if n != 4711 { t.Errorf("unexpected n == %d", n) }
Output:
type Ret1Fatal ¶
type Ret1Fatal[R any] struct { // contains filtered or unexported fields }
func Shall1 ¶
Shall1 records the passed values, usually from a function call, in order to react to an unexpected error with t.Fatal(). The error is evaluated using the Ret1Fatal methods and the remaining values are returned.
Example ¶
testedFunction := func() (int, error) { return 4711, errors.New("this is an error") } var t *testing.T // Usually passed in by the test framework n := Shall1(testedFunction()).BeNil(t) // will call t.Fatal() if n != 4711 { t.Errorf("unexpected n == %d", n) }
Output: