Documentation
¶
Overview ¶
Package assert provides common assertions to use with the standard testing package.
Index ¶
- func AsErr[T Parameter](t TB, err error, target any, formatAndArgs ...any)
- func Equal[T Parameter, V any](t TB, got, want V, formatAndArgs ...any)
- func IsErr[T Parameter](t TB, err, target error, formatAndArgs ...any)
- func NoErr[T Parameter](t TB, err error, formatAndArgs ...any)
- type E
- type F
- type Parameter
- type TB
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsErr ¶
AsErr asserts that errors.As(err, target) is true. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise AsErr panics.
Example ¶
err := errors.New("test")
assert.AsErr[E](t, err, new(*fs.PathError)) // prints "got *errors.errorString; want **fs.PathError"
assert.AsErr[F](t, err, new(*fs.PathError)) // prints "got *errors.errorString; want **fs.PathError" and stops the test
func Equal ¶
Equal asserts that got and want are equal. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise Equal panics.
Example ¶
assert.Equal[E](t, 1, 2) // prints "got 1; want 2" assert.Equal[F](t, 1, 2) // prints "got 1; want 2" and stops the test
func IsErr ¶
IsErr asserts that errors.Is(err, target) is true. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise IsErr panics.
Example ¶
err := errors.New("test")
assert.IsErr[E](t, err, fs.ErrNotExist) // prints "got test; want file does not exist"
assert.IsErr[F](t, err, fs.ErrNotExist) // prints "got test; want file does not exist" and stops the test
func NoErr ¶
NoErr asserts that err is nil. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise NoErr panics.
Example ¶
err := errors.New("test")
assert.NoErr[E](t, err) // prints "got test; want no error"
assert.NoErr[F](t, err) // prints "got test; want no error" and stops the test
Types ¶
type E ¶
type E struct{}
E is a Parameter that marks the test as having failed but continues its execution (similar to testing.T.Errorf).
type F ¶
type F struct{}
F is a Parameter that marks the test as having failed and stops its execution (similar to testing.T.Fatalf).