Documentation
¶
Overview ¶
Package verify contains the most useful type-safe fluent assertion functions.
It also provides the FailureMessage type which you can use for creating your own fluent assertions.
At last, you may embed a Fluent* type to extend it with additional assertion functions.
Deprecated: avoid using assertion libraries. Use the standard library and github.com/google/go-cmp/cmp instead. Follow the official Go Test Comments.
Index ¶
- type AssertionError
- type FailureMessage
- func And(assertions ...FailureMessage) FailureMessage
- func Eventually(timeout, interval time.Duration, fn func() FailureMessage) FailureMessage
- func EventuallyChan[TTimerPayload, TTickPayload any](timeout <-chan (TTimerPayload), ticker <-chan (TTickPayload), ...) FailureMessage
- func False[T ~bool](got T) FailureMessage
- func IsError(err error) FailureMessage
- func Nil(v any) FailureMessage
- func NoError(err error) FailureMessage
- func NotNil(v any) FailureMessage
- func NotPanics(fn func()) (msg FailureMessage)
- func Or(assertions ...FailureMessage) FailureMessage
- func Panics(fn func()) (msg FailureMessage)
- func True[T ~bool](got T) FailureMessage
- func (msg FailureMessage) Assert(t interface{ ... }, args ...any) bool
- func (msg FailureMessage) Assertf(t interface{ ... }, format string, args ...any) bool
- func (msg FailureMessage) Err() *AssertionError
- func (msg FailureMessage) Prefix(s string) FailureMessage
- func (msg FailureMessage) Require(t interface{ ... }, args ...any) bool
- func (msg FailureMessage) Requiref(t interface{ ... }, format string, args ...any) bool
- type FluentAny
- func (x FluentAny[T]) Check(fn func(got T) FailureMessage) FailureMessage
- func (x FluentAny[T]) DeepEqual(want T, opts ...cmp.Option) FailureMessage
- func (x FluentAny[T]) NotDeepEqual(obj T, opts ...cmp.Option) FailureMessage
- func (x FluentAny[T]) Should(pred func(got T) bool) FailureMessage
- func (x FluentAny[T]) ShouldNot(fn func(got T) bool) FailureMessage
- type FluentError
- type FluentMap
- func (x FluentMap[K, V]) All(predicate func(K, V) bool) FailureMessage
- func (x FluentMap[K, V]) Any(predicate func(K, V) bool) FailureMessage
- func (x FluentMap[K, V]) Contain(want map[K]V, opts ...cmp.Option) FailureMessage
- func (x FluentMap[K, V]) ContainPair(k K, v V, opts ...cmp.Option) FailureMessage
- func (x FluentMap[K, V]) Empty() FailureMessage
- func (x FluentMap[K, V]) Len(want int) FailureMessage
- func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage
- func (x FluentMap[K, V]) NotContain(want map[K]V, opts ...cmp.Option) FailureMessage
- func (x FluentMap[K, V]) NotContainPair(k K, v V, opts ...cmp.Option) FailureMessage
- func (x FluentMap[K, V]) NotEmpty() FailureMessage
- type FluentNumber
- type FluentObj
- type FluentOrdered
- type FluentSlice
- func (x FluentSlice[T]) All(predicate func(T) bool) FailureMessage
- func (x FluentSlice[T]) Any(predicate func(T) bool) FailureMessage
- func (x FluentSlice[T]) Contain(item T, opts ...cmp.Option) FailureMessage
- func (x FluentSlice[T]) Empty() FailureMessage
- func (x FluentSlice[T]) Equivalent(want []T, opts ...cmp.Option) FailureMessage
- func (x FluentSlice[T]) Len(want int) FailureMessage
- func (x FluentSlice[T]) None(predicate func(T) bool) FailureMessage
- func (x FluentSlice[T]) NotContain(item T, opts ...cmp.Option) FailureMessage
- func (x FluentSlice[T]) NotEmpty() FailureMessage
- func (x FluentSlice[T]) NotEquivalent(want []T, opts ...cmp.Option) FailureMessage
- type FluentString
- func (x FluentString[T]) Contain(substr string) FailureMessage
- func (x FluentString[T]) Empty() FailureMessage
- func (x FluentString[T]) EqualFold(want string) FailureMessage
- func (x FluentString[T]) Len(want int) FailureMessage
- func (x FluentString[T]) MatchRegex(regex *regexp.Regexp) FailureMessage
- func (x FluentString[T]) NoPrefix(prefix string) FailureMessage
- func (x FluentString[T]) NoSufix(sufix string) FailureMessage
- func (x FluentString[T]) NotContain(substr string) FailureMessage
- func (x FluentString[T]) NotEmpty() FailureMessage
- func (x FluentString[T]) NotEqualFold(want string) FailureMessage
- func (x FluentString[T]) NotMatchRegex(regex *regexp.Regexp) FailureMessage
- func (x FluentString[T]) Prefix(prefix string) FailureMessage
- func (x FluentString[T]) Sufix(sufix string) FailureMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AssertionError ¶ added in v1.2.0
type AssertionError struct {
Message FailureMessage
}
AssertionError is an error type used to represent failure messages from assertions. It is compatible with the error interface and can be used in instances where an error shall be returned instead of failing a test.
func (*AssertionError) Error ¶ added in v1.2.0
func (err *AssertionError) Error() string
Error returns the failure message as a string. It makes AssertionError compatible with the error interface.
type FailureMessage ¶
type FailureMessage string
FailureMessage encapsulates a failure message that can by emitted using objects compatible with the testing.TB interface.
func And ¶
func And(assertions ...FailureMessage) FailureMessage
And accumalates non-empty failure messages.
func Eventually ¶
func Eventually(timeout, interval time.Duration, fn func() FailureMessage) FailureMessage
Eventually executes the test function until it returns an empty FailureMessage or timeout elapses.
func EventuallyChan ¶
func EventuallyChan[TTimerPayload, TTickPayload any](timeout <-chan (TTimerPayload), ticker <-chan (TTickPayload), fn func() FailureMessage) FailureMessage
EventuallyChan executes the test function until it returns an empty FailureMessage or timeout elapses.
func Nil ¶
func Nil(v any) FailureMessage
Nil tests if provided interface value is nil. Use it only for interfaces. For structs and pointers use Obj(got).Zero().
func NotNil ¶
func NotNil(v any) FailureMessage
NotNil tests if provided interface is not nil. Use it only for interfaces. For structs and pointers use Obj(got).NonZero().
func NotPanics ¶
func NotPanics(fn func()) (msg FailureMessage)
NotPanics tests if the function does not panic when executed.
func Or ¶
func Or(assertions ...FailureMessage) FailureMessage
Or accumalates failure messages if all are not empty.
func Panics ¶
func Panics(fn func()) (msg FailureMessage)
Panics tests if the function panics when executed.
func (FailureMessage) Assert ¶
func (msg FailureMessage) Assert(t interface { Error(args ...any) }, args ...any, ) bool
Assert calls t.Error if the failure message is not empty. Calling Error on *testing.T marks the the function as having failed and continues its execution. Returns true when the failure message is empty.
func (FailureMessage) Assertf ¶
func (msg FailureMessage) Assertf(t interface { Errorf(format string, args ...any) }, format string, args ...any, ) bool
Assertf calls t.Errorf if the failure message is not empty. Calling Errorf on *testing.T marks the the function as having failed and continues its execution. Returns true when the failure message is empty
func (FailureMessage) Err ¶ added in v1.2.0
func (msg FailureMessage) Err() *AssertionError
Err returns the failure message as an error type, or nil if the message is empty.
func (FailureMessage) Prefix ¶
func (msg FailureMessage) Prefix(s string) FailureMessage
Prefix adds prefix if the failure message is not empty.
func (FailureMessage) Require ¶
func (msg FailureMessage) Require(t interface { Fatal(args ...any) }, args ...any, ) bool
Require calls t.Fatal if the failure message is not empty. Calling Fatal on *testing.T stops the test function execution. Returns true when the failure message is empty.
func (FailureMessage) Requiref ¶
func (msg FailureMessage) Requiref(t interface { Fatalf(format string, args ...any) }, format string, args ...any, ) bool
Requiref calls t.Fatalf if the failure message is not empty. Calling Fatalf on *testing.T stops the test function execution. Returns true when the failure message is empty.
type FluentAny ¶
type FluentAny[T any] struct { Got T }
FluentAny encapsulates assertions for any object.
func (FluentAny[T]) Check ¶
func (x FluentAny[T]) Check(fn func(got T) FailureMessage) FailureMessage
Check tests the object using the provided function.
func (FluentAny[T]) DeepEqual ¶
func (x FluentAny[T]) DeepEqual(want T, opts ...cmp.Option) FailureMessage
DeepEqual tests if the objects are deep equal using github.com/google/go-cmp/cmp.
func (FluentAny[T]) NotDeepEqual ¶
func (x FluentAny[T]) NotDeepEqual(obj T, opts ...cmp.Option) FailureMessage
NotDeepEqual tests if the objects are not deep equal using github.com/google/go-cmp/cmp.
func (FluentAny[T]) Should ¶
func (x FluentAny[T]) Should(pred func(got T) bool) FailureMessage
Should tests if the object meets the predicate criteria.
func (FluentAny[T]) ShouldNot ¶
func (x FluentAny[T]) ShouldNot(fn func(got T) bool) FailureMessage
ShouldNot tests if the object does not meet the predicate criteria.
type FluentError ¶
type FluentError struct { FluentAny[error] FluentString[string] }
FluentError encapsulates assertions for error object.
func (FluentError) As ¶
func (x FluentError) As(target any) FailureMessage
As finds the first error in err's chain that matches target, and if one is found, sets target to that error value. In such case it is a success..
func (FluentError) AsNot ¶
func (x FluentError) AsNot(target any) FailureMessage
AsNot finds the first error in err's chain that matches target, and if one is found, sets target to that error value. In such case it is a failure.
func (FluentError) Is ¶
func (x FluentError) Is(target error) FailureMessage
Is tests whether any error in err's chain matches target.
func (FluentError) IsNot ¶
func (x FluentError) IsNot(target error) FailureMessage
IsNot tests whether no error in err's chain matches target.
type FluentMap ¶
type FluentMap[K comparable, V any] struct { FluentAny[map[K]V] }
FluentMap encapsulates assertions for a map.
func Map ¶
func Map[K comparable, V any](got map[K]V) FluentMap[K, V]
Map is used for testing a map.
func (FluentMap[K, V]) All ¶
func (x FluentMap[K, V]) All(predicate func(K, V) bool) FailureMessage
All tests if all of the map's pairs meet the predicate criteria.
func (FluentMap[K, V]) Any ¶
func (x FluentMap[K, V]) Any(predicate func(K, V) bool) FailureMessage
Any tests if any of the map's pairs meets the predicate criteria.
func (FluentMap[K, V]) Contain ¶
func (x FluentMap[K, V]) Contain(want map[K]V, opts ...cmp.Option) FailureMessage
Contain tests if the map contains all pairs from want.
func (FluentMap[K, V]) ContainPair ¶
func (x FluentMap[K, V]) ContainPair(k K, v V, opts ...cmp.Option) FailureMessage
ContainPair tests if the map contains the pair.
func (FluentMap[K, V]) Empty ¶
func (x FluentMap[K, V]) Empty() FailureMessage
Empty tests if the slice is empty.
func (FluentMap[K, V]) Len ¶ added in v1.1.0
func (x FluentMap[K, V]) Len(want int) FailureMessage
Len tests the length of the map.
func (FluentMap[K, V]) None ¶
func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage
None tests if none of the map's pairs meets the predicate criteria.
func (FluentMap[K, V]) NotContain ¶
func (x FluentMap[K, V]) NotContain(want map[K]V, opts ...cmp.Option) FailureMessage
NotContain tests if the map does not contains all pairs from want.
func (FluentMap[K, V]) NotContainPair ¶
func (x FluentMap[K, V]) NotContainPair(k K, v V, opts ...cmp.Option) FailureMessage
NotContainPair tests if the map does not contain the pair.
func (FluentMap[K, V]) NotEmpty ¶
func (x FluentMap[K, V]) NotEmpty() FailureMessage
NotEmpty tests if the slice is not empty.
type FluentNumber ¶
type FluentNumber[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64] struct { FluentOrdered[T] }
FluentNumber encapsulates assertions for numbers that supports the operators < <= >= > + - * /.
func Number ¶
func Number[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64](got T) FluentNumber[T]
Number is used for testing numbers that supports the operators < <= >= > + - * /.
func (FluentNumber[T]) InDelta ¶
func (x FluentNumber[T]) InDelta(want T, delta float64) FailureMessage
InDelta tests that the numbers have an absolute error (distance) less or equal than delta.
func (FluentNumber[T]) InEpsilon ¶
func (x FluentNumber[T]) InEpsilon(want T, epsilon float64) FailureMessage
InEpsilon tests that the numbers have a relative error less or equal than epsilon.
func (FluentNumber[T]) NotInDelta ¶
func (x FluentNumber[T]) NotInDelta(want T, delta float64) FailureMessage
NotInDelta tests that the numbers have an absolute error (distance) greater than delta.
func (FluentNumber[T]) NotInEpsilon ¶
func (x FluentNumber[T]) NotInEpsilon(want T, epsilon float64) FailureMessage
NotInEpsilon tests that the numbers have a relative error greater than epsilon.
type FluentObj ¶
type FluentObj[T comparable] struct { FluentAny[T] }
FluentObj encapsulates assertions for comparable object.
func (FluentObj[T]) Equal ¶
func (x FluentObj[T]) Equal(want T) FailureMessage
Equal tests the objects using == operator.
func (FluentObj[T]) NonZero ¶
func (x FluentObj[T]) NonZero() FailureMessage
NonZero tests if the object is a non-zero value.
func (FluentObj[T]) NotEqual ¶
func (x FluentObj[T]) NotEqual(obj T) FailureMessage
NotEqual tests the objects using != operator.
func (FluentObj[T]) Zero ¶
func (x FluentObj[T]) Zero() FailureMessage
Zero tests if the object is a zero value.
type FluentOrdered ¶
type FluentOrdered[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string] struct { FluentObj[T] }
FluentOrdered encapsulates assertions for ordered object that supports the operators < <= >= >.
func Ordered ¶
func Ordered[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string](got T) FluentOrdered[T]
Ordered is used for testing a ordered object that supports the operators < <= >= >.
func (FluentOrdered[T]) Greater ¶
func (x FluentOrdered[T]) Greater(than T) FailureMessage
Greater tests the objects using > operator.
func (FluentOrdered[T]) GreaterOrEqual ¶
func (x FluentOrdered[T]) GreaterOrEqual(than T) FailureMessage
GreaterOrEqual tests the objects using >= operator.
func (FluentOrdered[T]) Lesser ¶
func (x FluentOrdered[T]) Lesser(than T) FailureMessage
Lesser tests the objects using < operator.
func (FluentOrdered[T]) LesserOrEqual ¶
func (x FluentOrdered[T]) LesserOrEqual(than T) FailureMessage
LesserOrEqual tests the objects using <= operator.
type FluentSlice ¶
FluentSlice encapsulates assertions for a slice.
func (FluentSlice[T]) All ¶
func (x FluentSlice[T]) All(predicate func(T) bool) FailureMessage
All tests if all of the slice's items meet the predicate criteria.
func (FluentSlice[T]) Any ¶
func (x FluentSlice[T]) Any(predicate func(T) bool) FailureMessage
Any tests if any of the slice's items meets the predicate criteria.
func (FluentSlice[T]) Contain ¶
func (x FluentSlice[T]) Contain(item T, opts ...cmp.Option) FailureMessage
Contain tests if the slice contains the item.
func (FluentSlice[T]) Empty ¶
func (x FluentSlice[T]) Empty() FailureMessage
Empty tests if the slice is empty.
func (FluentSlice[T]) Equivalent ¶
func (x FluentSlice[T]) Equivalent(want []T, opts ...cmp.Option) FailureMessage
Equivalent tests if the slice has the same items as want in any order.
func (FluentSlice[T]) Len ¶ added in v1.1.0
func (x FluentSlice[T]) Len(want int) FailureMessage
Len tests the length of the slice.
func (FluentSlice[T]) None ¶
func (x FluentSlice[T]) None(predicate func(T) bool) FailureMessage
None tests if none of the slice's items meets the predicate criteria.
func (FluentSlice[T]) NotContain ¶
func (x FluentSlice[T]) NotContain(item T, opts ...cmp.Option) FailureMessage
NotContain tests if the slice does not contain the item.
func (FluentSlice[T]) NotEmpty ¶
func (x FluentSlice[T]) NotEmpty() FailureMessage
NotEmpty tests if the slice is not empty.
func (FluentSlice[T]) NotEquivalent ¶
func (x FluentSlice[T]) NotEquivalent(want []T, opts ...cmp.Option) FailureMessage
NotEquivalent tests if the slice does not have the same items as want in any order.
type FluentString ¶
type FluentString[T ~string] struct { FluentOrdered[T] }
FluentString encapsulates assertions for string object.
func String ¶
func String[T ~string](got T) FluentString[T]
String is used for testing a string object.
func (FluentString[T]) Contain ¶
func (x FluentString[T]) Contain(substr string) FailureMessage
Contain tests if the string contains the substring.
func (FluentString[T]) Empty ¶
func (x FluentString[T]) Empty() FailureMessage
Empty tests if the string is not empty.
func (FluentString[T]) EqualFold ¶
func (x FluentString[T]) EqualFold(want string) FailureMessage
EqualFold tests if the values interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.
func (FluentString[T]) Len ¶ added in v1.1.0
func (x FluentString[T]) Len(want int) FailureMessage
Len tests the length of the string.
func (FluentString[T]) MatchRegex ¶
func (x FluentString[T]) MatchRegex(regex *regexp.Regexp) FailureMessage
MatchRegex tests if the string matches the regular expression.
func (FluentString[T]) NoPrefix ¶
func (x FluentString[T]) NoPrefix(prefix string) FailureMessage
NoPrefix tests if the string does not start with the prefix.
func (FluentString[T]) NoSufix ¶
func (x FluentString[T]) NoSufix(sufix string) FailureMessage
NoSufix tests if the string does not end with the sufix.
func (FluentString[T]) NotContain ¶
func (x FluentString[T]) NotContain(substr string) FailureMessage
NotContain tests if the string does not contain the substring.
func (FluentString[T]) NotEmpty ¶
func (x FluentString[T]) NotEmpty() FailureMessage
NotEmpty tests if the string is not empty.
func (FluentString[T]) NotEqualFold ¶
func (x FluentString[T]) NotEqualFold(want string) FailureMessage
NotEqualFold tests if the values interpreted as UTF-8 strings, are not equal under simple Unicode case-folding, which is a more general form of case-insensitivity.
func (FluentString[T]) NotMatchRegex ¶
func (x FluentString[T]) NotMatchRegex(regex *regexp.Regexp) FailureMessage
NotMatchRegex tests if the string does not match the regular expression.
func (FluentString[T]) Prefix ¶
func (x FluentString[T]) Prefix(prefix string) FailureMessage
Prefix tests if the string starts with the prefix.
func (FluentString[T]) Sufix ¶
func (x FluentString[T]) Sufix(sufix string) FailureMessage
Sufix tests if the string ends with the sufix.