Documentation ¶
Overview ¶
Package assert provides utilities to assert conditions in tests.
Assertion functions return a boolean value indicating whether the assertion succeeded.
By default, assertion failures are reported using testing.TB.Fatal. It can be customized with the Report() option.
Index ¶
- Variables
- func AllocsPerRun(tb testing.TB, runs int, f func(), allocs float64, opts ...Option) bool
- func BytesEqual(tb testing.TB, b1, b2 []byte, opts ...Option) bool
- func BytesNotEqual(tb testing.TB, b1, b2 []byte, opts ...Option) bool
- func ChanEmpty[T any](tb testing.TB, c chan T, opts ...Option) bool
- func ChanLen[T any](tb testing.TB, c chan T, l int, opts ...Option) bool
- func ChanNotEmpty[T any](tb testing.TB, c chan T, opts ...Option) bool
- func Condition(tb testing.TB, f func() bool, opts ...Option) bool
- func DeepEqual[T any](tb testing.TB, v1, v2 T, opts ...Option) bool
- func Equal[T comparable](tb testing.TB, v1, v2 T, opts ...Option) bool
- func Error(tb testing.TB, err error, opts ...Option) bool
- func ErrorAs(tb testing.TB, err error, target any, opts ...Option) bool
- func ErrorContains(tb testing.TB, err error, substr string, opts ...Option) bool
- func ErrorEqual(tb testing.TB, err error, message string, opts ...Option) bool
- func ErrorIs(tb testing.TB, err, target error, opts ...Option) bool
- func ErrorNotIs(tb testing.TB, err, target error, opts ...Option) bool
- func Fail(tb testing.TB, name string, msg string, opts ...Option)
- func False(tb testing.TB, v bool, opts ...Option) bool
- func Greater[T cmp.Ordered](tb testing.TB, v1, v2 T, opts ...Option) bool
- func GreaterOrEqual[T cmp.Ordered](tb testing.TB, v1, v2 T, opts ...Option) bool
- func Less[T cmp.Ordered](tb testing.TB, v1, v2 T, opts ...Option) bool
- func LessOrEqual[T cmp.Ordered](tb testing.TB, v1, v2 T, opts ...Option) bool
- func MapEmpty[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool
- func MapEqual[M1, M2 ~map[K]V, K, V comparable](tb testing.TB, m1 M1, m2 M2, opts ...Option) bool
- func MapLen[M ~map[K]V, K comparable, V any](tb testing.TB, m M, l int, opts ...Option) bool
- func MapNil[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool
- func MapNotEmpty[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool
- func MapNotEqual[M1, M2 ~map[K]V, K, V comparable](tb testing.TB, m1 M1, m2 M2, opts ...Option) bool
- func MapNotNil[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool
- func Negative[T SignedAndFloat](tb testing.TB, v T, opts ...Option) bool
- func NewDeepEqualerWithComparator(cr *compare.Comparator) func(v1, v2 any) (string, bool)
- func NoError(tb testing.TB, err error, opts ...Option) bool
- func NotDeepEqual[T any](tb testing.TB, v1, v2 T, opts ...Option) bool
- func NotEqual[T comparable](tb testing.TB, v1, v2 T, opts ...Option) bool
- func NotPanics(tb testing.TB, f func(), opts ...Option) (ok bool)
- func NotZero[T comparable](tb testing.TB, v T, opts ...Option) bool
- func Panics(tb testing.TB, f func(), opts ...Option) (rec any, ok bool)
- func Positive[T SignedAndFloat](tb testing.TB, v T, opts ...Option) bool
- func RegexpMatch[RS RegexpString](tb testing.TB, rs RS, s string, opts ...Option) bool
- func RegexpNotMatch[RS RegexpString](tb testing.TB, rs RS, s string, opts ...Option) bool
- func SliceContains[S ~[]E, E comparable](tb testing.TB, s S, v E, opts ...Option) bool
- func SliceContainsAll[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
- func SliceEmpty[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool
- func SliceEqual[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
- func SliceLen[S ~[]E, E any](tb testing.TB, s S, l int, opts ...Option) bool
- func SliceNil[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool
- func SliceNotContains[S ~[]E, E comparable](tb testing.TB, s S, v E, opts ...Option) bool
- func SliceNotContainsAll[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
- func SliceNotEmpty[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool
- func SliceNotEqual[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
- func SliceNotNil[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool
- func StringContains(tb testing.TB, s, substr string, opts ...Option) bool
- func StringEmpty(tb testing.TB, s string, opts ...Option) bool
- func StringEqualFold(tb testing.TB, s1, s2 string, opts ...Option) bool
- func StringHasPrefix(tb testing.TB, s, prefix string, opts ...Option) bool
- func StringHasSuffix(tb testing.TB, s, suffix string, opts ...Option) bool
- func StringLen(tb testing.TB, s string, l int, opts ...Option) bool
- func StringNotContains(tb testing.TB, s, substr string, opts ...Option) bool
- func StringNotEmpty(tb testing.TB, s string, opts ...Option) bool
- func True(tb testing.TB, v bool, opts ...Option) bool
- func Type[T any](tb testing.TB, v any, opts ...Option) (T, bool)
- func Zero[T comparable](tb testing.TB, v T, opts ...Option) bool
- type Option
- func Lazy(f func() Option) Option
- func Message(msg string) Option
- func MessageTransform(f func(msg string) string) Option
- func MessageWrap(msg string) Option
- func MessageWrapf(format string, args ...any) Option
- func Messagef(format string, args ...any) Option
- func Options(opts ...Option) Option
- func Report(f ReportFunc) Option
- type RegexpString
- type ReportFunc
- type SignedAndFloat
Constants ¶
This section is empty.
Variables ¶
var DeepEqualer = NewDeepEqualerWithComparator(compare.DefaultComparator)
DeepEqualer is a function that checks if two values are deep equal.
It can be customized to provide a better comparison.
By default it uses compare.DefaultComparator.
var ErrorStringer func(error) string = func(err error) string { if err == nil { return "<nil>" } return fmt.Sprintf("%q", err) }
ErrorStringer is a function that returns a string representation of an error.
It can be customized to provide a better error message.
var ValueStringer func(any) string = prettyString
ValueStringer is a function that returns the string representation of a value.
It can be customized to provide a better string representation.
By default it uses pretty.String.
Functions ¶
func AllocsPerRun ¶ added in v0.1.2
AllocsPerRun asserts that a function allocates a certain number of times per run.
func BytesEqual ¶
BytesEqual asserts that b1 and b2 are equal. It uses bytes.Equal to compare the two byte slices.
func BytesNotEqual ¶
BytesNotEqual asserts that b1 and b2 are not equal. It uses bytes.Equal to compare the two byte slices.
func ChanNotEmpty ¶
ChanNotEmpty asserts that c is not empty.
func DeepEqual ¶
DeepEqual asserts that v1 and v2 are deep equal according to DeepEqualer.
func Equal ¶
func Equal[T comparable](tb testing.TB, v1, v2 T, opts ...Option) bool
Equal asserts that v1 == v2.
func ErrorContains ¶
ErrorContains asserts that the result of [error.Error] contains substr.
func ErrorEqual ¶
ErrorEqual asserts that the result of [error.Error] is equal to message.
func ErrorNotIs ¶
ErrorNotIs asserts that errors.Is returns false.
func GreaterOrEqual ¶
GreaterOrEqual asserts that v1 >= v2.
func LessOrEqual ¶
LessOrEqual asserts that v1 <= v2.
func MapEqual ¶
func MapEqual[M1, M2 ~map[K]V, K, V comparable](tb testing.TB, m1 M1, m2 M2, opts ...Option) bool
MapEqual asserts that m1 and m2 are equal.
func MapNotEmpty ¶
MapNotEmpty asserts that m is not empty.
func MapNotEqual ¶
func MapNotEqual[M1, M2 ~map[K]V, K, V comparable](tb testing.TB, m1 M1, m2 M2, opts ...Option) bool
MapNotEqual asserts that m1 and m2 are not equal.
func Negative ¶
func Negative[T SignedAndFloat](tb testing.TB, v T, opts ...Option) bool
Negative asserts that the value is negative.
func NewDeepEqualerWithComparator ¶ added in v0.6.0
func NewDeepEqualerWithComparator(cr *compare.Comparator) func(v1, v2 any) (string, bool)
NewDeepEqualerWithComparator creates a new DeepEqualer with a custom compare.Comparator.
func NotDeepEqual ¶
NotDeepEqual asserts that v1 and v2 are not deep equal according to DeepEqualer.
func NotEqual ¶
func NotEqual[T comparable](tb testing.TB, v1, v2 T, opts ...Option) bool
NotEqual asserts that v1 != v2.
func NotZero ¶
func NotZero[T comparable](tb testing.TB, v T, opts ...Option) bool
NotZero asserts that v != zero.
func Panics ¶
Panics asserts that the code inside the function f panics.
It returns the recovered value.
func Positive ¶
func Positive[T SignedAndFloat](tb testing.TB, v T, opts ...Option) bool
Positive asserts that the value is positive.
func RegexpMatch ¶
RegexpMatch asserts that rs matches s.
func RegexpNotMatch ¶
RegexpNotMatch asserts that rs doesn't match s.
func SliceContains ¶
func SliceContains[S ~[]E, E comparable](tb testing.TB, s S, v E, opts ...Option) bool
SliceContains asserts that s contains v.
func SliceContainsAll ¶
func SliceContainsAll[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
SliceContainsAll asserts that s1 contains all elements in s2.
func SliceEmpty ¶
SliceEmpty asserts that s is empty.
func SliceEqual ¶
func SliceEqual[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
SliceEqual asserts that s1 and s2 are equal.
func SliceNotContains ¶
func SliceNotContains[S ~[]E, E comparable](tb testing.TB, s S, v E, opts ...Option) bool
SliceNotContains asserts that s does not contain v.
func SliceNotContainsAll ¶
func SliceNotContainsAll[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
SliceNotContainsAll asserts that s1 does not contain all elements in s2.
func SliceNotEmpty ¶
SliceNotEmpty asserts that s is not empty.
func SliceNotEqual ¶
func SliceNotEqual[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool
SliceNotEqual asserts that s1 and s2 are not equal.
func SliceNotNil ¶
SliceNotNil asserts that s is not nil.
func StringContains ¶
StringContains asserts that s contains substr.
func StringEmpty ¶
StringEmpty asserts that s is empty.
func StringEqualFold ¶
StringEqualFold asserts that s1 and s2 are equal, ignoring case.
func StringHasPrefix ¶
StringHasPrefix asserts that s begins with prefix.
func StringHasSuffix ¶
StringHasSuffix asserts that s ends with suffix.
func StringNotContains ¶
StringNotContains asserts that s does not contain substr.
func StringNotEmpty ¶
StringNotEmpty asserts that s is not empty.
Types ¶
type Option ¶
type Option func(*options)
Option is an option for an assertion.
func Lazy ¶ added in v0.5.0
Lazy returns an Option that defers the evaluation of the option.
It helps to reduce allocations when the option is not used.
func MessageTransform ¶
MessageTransform returns an Option that adds a message transform function. The function is called before the ReportFunc. If several function are added, they're called in order.
func MessageWrap ¶
MessageWrap returns an Option that wraps the message. The final message is "<msg>: <original message>".
func MessageWrapf ¶
MessageWrapf returns an Option that wraps the message. The final message is "<format msg>: <original message>".
func Report ¶
func Report(f ReportFunc) Option
Report returns an Option that sets the report function.
type RegexpString ¶
RegexpString is a type that can be either a *regexp.Regexp or a string.
If it's a string, it's automatically compiled to a *regexp.Regexp.
type ReportFunc ¶
type ReportFunc func(args ...any)
ReportFunc is a function that is called when an assertion fails.
It is implemented by [testing.TB.Fatal]|[testing.TB.Error]|[testing.TB.Skip]|[testing.TB.Log].
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package assertauto provides helpers to automatically update the expected values of assertions.
|
Package assertauto provides helpers to automatically update the expected values of assertions. |
Package asserttest provides utilities to test assertions.
|
Package asserttest provides utilities to test assertions. |