Documentation
¶
Overview ¶
Package assert provides assertion functions.
Index ¶
- func After(t tester.T, mark, date time.Time, opts ...any) bool
- func AfterOrEqual(t tester.T, mark, date any, opts ...any) bool
- func Before(t tester.T, mark, date any, opts ...any) bool
- func BeforeOrEqual(t tester.T, mark, date time.Time, opts ...any) bool
- func Cap(t tester.T, want int, have any, opts ...any) bool
- func ChannelWillClose[C any](t tester.T, within any, c <-chan C, opts ...any) bool
- func Contain(t tester.T, want, have string, opts ...any) bool
- func Count(t tester.T, count int, what, where any, opts ...any) bool
- func Decreasing[T constraints.Ordered](t tester.T, seq []T, opts ...any) bool
- func Delta[T, E constraints.Number](t tester.T, want T, delta E, have T, opts ...any) bool
- func DeltaSlice[T, E constraints.Number](t tester.T, want []T, delta E, have []T, opts ...any) bool
- func DirExist(t tester.T, pth string, opts ...any) bool
- func Duration(t tester.T, want, have any, opts ...any) bool
- func Empty(t tester.T, have any, opts ...any) bool
- func Epsilon[T, E constraints.Number](t tester.T, want T, epsilon E, have T, opts ...any) bool
- func EpsilonSlice[T, E constraints.Number](t tester.T, want []T, epsilon E, have []T, opts ...any) bool
- func Equal(t tester.T, want, have any, opts ...any) bool
- func Error(t tester.T, err error, opts ...any) bool
- func ErrorAs(t tester.T, want any, err error, opts ...any) bool
- func ErrorContain(t tester.T, want string, err error, opts ...any) bool
- func ErrorEqual(t tester.T, want string, err error, opts ...any) bool
- func ErrorIs(t tester.T, want, err error, opts ...any) bool
- func ErrorRegexp(t tester.T, want string, err error, opts ...any) bool
- func Exact(t tester.T, want, have any, opts ...any) bool
- func ExitCode(t tester.T, want int, err error, opts ...any) bool
- func False(t tester.T, have bool, opts ...any) bool
- func Fields(t tester.T, want int, s any, opts ...any) bool
- func FileContain[T check.Content](t tester.T, want T, pth string, opts ...any) bool
- func FileExist(t tester.T, pth string, opts ...any) bool
- func Greater[T constraints.Ordered](t tester.T, want, have T, opts ...any) bool
- func GreaterOrEqual[T constraints.Ordered](t tester.T, want, have T, opts ...any) bool
- func Has[T comparable](t tester.T, want T, bag []T, opts ...any) bool
- func HasKey[K comparable, V any](t tester.T, key K, set map[K]V, opts ...any) (V, bool)
- func HasKeyValue[K, V comparable](t tester.T, key K, want V, set map[K]V, opts ...any) bool
- func HasNo[T comparable](t tester.T, want T, bag []T, opts ...any) bool
- func HasNoKey[K comparable, V any](t tester.T, key K, set map[K]V, opts ...any) bool
- func Increasing[T constraints.Ordered](t tester.T, seq []T, opts ...any) bool
- func JSON(t tester.T, want, have string, opts ...any) bool
- func Len(t tester.T, want int, have any, opts ...any) bool
- func MapSubset[K cmp.Ordered, V any](t tester.T, want, have map[K]V, opts ...any) bool
- func MapsSubset[K cmp.Ordered, V any](t tester.T, want, have []map[K]V, opts ...any) bool
- func Nil(t tester.T, have any, opts ...any) bool
- func NoDirExist(t tester.T, pth string, opts ...any) bool
- func NoError(t tester.T, err error, opts ...any) bool
- func NoFileExist(t tester.T, pth string, opts ...any) bool
- func NoPanic(t tester.T, fn check.TestFunc, opts ...any) bool
- func NotContain(t tester.T, want, have string, opts ...any) bool
- func NotDecreasing[T constraints.Ordered](t tester.T, seq []T, opts ...any) bool
- func NotEmpty(t tester.T, have any, opts ...any) bool
- func NotEqual(t tester.T, want, have any, opts ...any) bool
- func NotIncreasing[T constraints.Ordered](t tester.T, seq []T, opts ...any) bool
- func NotNil(t tester.T, have any, opts ...any) bool
- func NotSame(t tester.T, want, have any, opts ...any) bool
- func NotSameType(t tester.T, want, have any, opts ...any) bool
- func NotZero(t tester.T, have any, opts ...any) bool
- func Panic(t tester.T, fn check.TestFunc, opts ...any) bool
- func PanicContain(t tester.T, want string, fn check.TestFunc, opts ...any) bool
- func PanicMsg(t tester.T, fn check.TestFunc, opts ...any) *string
- func Recent(t tester.T, have any, opts ...any) bool
- func Regexp(t tester.T, want, have any, opts ...any) bool
- func Same(t tester.T, want, have any, opts ...any) bool
- func SameType(t tester.T, want, have any, opts ...any) bool
- func SliceSubset[T comparable](t tester.T, want, have []T, opts ...any) bool
- func Smaller[T constraints.Ordered](t tester.T, want, have T, opts ...any) bool
- func SmallerOrEqual[T constraints.Ordered](t tester.T, want, have T, opts ...any) bool
- func Time(t tester.T, want, have any, opts ...any) bool
- func True(t tester.T, have bool, opts ...any) bool
- func Type(t tester.T, want, have any, opts ...any) bool
- func Wait(t tester.T, timeout string, fn func() bool, opts ...any) bool
- func Within(t tester.T, want, within, have any, opts ...any) bool
- func Zero(t tester.T, have any, opts ...any) bool
- func Zone(t tester.T, want, have *time.Location, opts ...any) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After asserts "date" is after the "mark" date. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "date" and "mark" might be date representations in the form of string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
func AfterOrEqual ¶
AfterOrEqual asserts "date" is equal or after "mark". Returns true if it's, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "date" and "mark" might be date representations in the form of a string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
func Before ¶
Before asserts "date" is before the "mark" date. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "date" and "mark" might be date representations in the form of string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
func BeforeOrEqual ¶
BeforeOrEqual asserts "date" is equal or before the "mark" date. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "date" and "mark" might be date representations in the form of a string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
func Cap ¶ added in v0.28.0
Cap asserts "have" has "want" capacity. Returns true if it is, otherwise it marks the test as failed, writes an error message to the test log and returns false.
func ChannelWillClose ¶
ChannelWillClose asserts the channel will be closed "within" a given time duration. Returns true if it was, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "within" may represent duration in the form of a string, int, int64 or time.Duration.
func Contain ¶
Contain asserts "want" is a substring of "have". Returns true if it's, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Count ¶
Count asserts there is "count" occurrences of "what" in "where". Returns true if the count matches, otherwise marks the test as failed, writes an error message to the test log and returns false.
Currently, only strings are supported.
func Decreasing ¶ added in v0.22.0
Decreasing checks if the given sequence has values in the decreasing order. You may use the check.WithDecreasingSoft option to allow consecutive values to be equal. It returns true if the sequence is decreasing otherwise, marks the test as failed, writes an error message to the test log and returns false.
func Delta ¶ added in v0.23.0
Delta asserts both values are within the given delta. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
|w-h|/|w| <= delta
func DeltaSlice ¶ added in v0.23.0
func DeltaSlice[T, E constraints.Number]( t tester.T, want []T, delta E, have []T, opts ...any, ) bool
DeltaSlice asserts values are within the given delta for all respective slice indexes. It returns true if all differences are within the delta; otherwise, marks the test as failed, writes an error message to the test log and returns false.
|w[i]-h[i]| <= delta
func DirExist ¶
DirExist asserts "pth" points to an existing directory. It fails if the path points to a filesystem entry, which is not a directory, or there is an error when trying to check the path. Returns true on success, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Duration ¶
Duration asserts "want" and "have" durations are equal. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "want" and "have" might be duration representation in the form of string, int, int64 or time.Duration.
func Empty ¶
Empty asserts "have" is empty. Returns true if it's, otherwise marks the test as failed, writes an error message to the test log and returns false.
See check.Empty for the list of values which are considered empty.
func Epsilon ¶
Epsilon asserts the relative error is less than epsilon. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
|w-h|/|w| <= epsilon
func EpsilonSlice ¶ added in v0.21.0
func EpsilonSlice[T, E constraints.Number]( t tester.T, want []T, epsilon E, have []T, opts ...any, ) bool
EpsilonSlice asserts the relative error is less than epsilon for all respective values in the provided slices. It returns true if all differences are within the delta; otherwise, marks the test as failed, writes an error message to the test log and returns false.
|w[i]-h[i]|/|w[i]| <= epsilon
func Equal ¶
Equal asserts both values are equal. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Error ¶
Error asserts "err" is not nil. Returns true if it's, otherwise marks the test as failed, writes an error message to the test log and returns false.
func ErrorAs ¶
ErrorAs finds the first error in "err" tree that matches the "want" target, and if one is found, sets a target to that error. Returns true if it does, otherwise marks the test as failed, writes an error message to the test log and returns false.
func ErrorContain ¶
ErrorContain asserts "err" is not nil and its message contains "want". Returns true if it does, otherwise marks the test as failed, writes an error message to the test log and returns false.
func ErrorEqual ¶
ErrorEqual asserts "err" is not nil and its message equals to "want". Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
func ErrorIs ¶
ErrorIs asserts whether any error in "err" tree matches the "want" target. Returns true if it does, otherwise marks the test as failed, writes an error message to the test log and returns false.
func ErrorRegexp ¶
ErrorRegexp asserts "err" is not nil and its message matches the "want" regex. Returns true if it does, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "want" can be either a regular expression string or instance of regexp.Regexp. The fmt.Sprint is used to get string representation of have argument.
func Exact ¶
Exact asserts "want" and "have" dates are equal and are in the same timezone. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "want" and "have" might be date representations in the form of a string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
func ExitCode ¶
ExitCode asserts "err" is a pointer to [exec.ExitError] with exit code equal to "want". Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
func False ¶
False asserts "have" is false. Returns true if it's, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Fields ¶
Fields asserts struct or pointer to a struct "s" has "want" number of fields. Returns true if it does, otherwise marks the test as failed, writes an error message to the test log and returns false.
func FileContain ¶
FileContain asserts a file at "pth" can be read and its string content contains "want". It fails if the path points to a filesystem entry, which is not a file, or there is an error reading the file. The file is read in full, then Contain assertion is used to check it contains the "want" string. Returns true on success, otherwise marks the test as failed, writes an error message to the test log and returns false.
func FileExist ¶
FileExist asserts "pth" points to an existing file. It fails if the path points to a filesystem entry, which is not a file, or there is an error when trying to check the path. Returns true on success, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Greater ¶ added in v0.22.0
Greater checks the "have" value is greater than the "want" value. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
func GreaterOrEqual ¶ added in v0.22.0
GreaterOrEqual checks the "have" value is greater or equal than the "want" value. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Has ¶
func Has[T comparable](t tester.T, want T, bag []T, opts ...any) bool
Has asserts the slice has "want" value. Returns true if it does, otherwise marks the test as failed, writes an error message to the test log and returns false.
func HasKey ¶
HasKey asserts the map has a key. Returns true if it does, otherwise marks the test as failed, writes an error message to the test log and returns false.
func HasKeyValue ¶
func HasKeyValue[K, V comparable]( t tester.T, key K, want V, set map[K]V, opts ...any, ) bool
HasKeyValue asserts the map has a key with the given value. Returns true if it doesn't, otherwise marks the test as failed, writes an error message to the test log and returns false.
func HasNo ¶
func HasNo[T comparable](t tester.T, want T, bag []T, opts ...any) bool
HasNo asserts slice does not have a "want" value. Returns true if it does not, otherwise marks the test as failed, writes an error message to the test log and returns false.
func HasNoKey ¶
HasNoKey asserts the map has no key. Returns true if it doesn't, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Increasing ¶ added in v0.22.0
Increasing checks if the given sequence has values in the increasing order. You may use the check.WithIncreasingSoft option to allow consecutive values to be equal. It returns true if the sequence is increasing otherwise, marks the test as failed, writes an error message to the test log and returns false.
func JSON ¶
JSON asserts that two JSON strings are equivalent. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
Example:
assert.JSON(t, `{"hello": "world"}`, `{"foo": "bar"}`)
func Len ¶
Len asserts "have" has "want" length. Returns true if it is, otherwise it marks the test as failed, writes an error message to the test log and returns false.
func MapSubset ¶
MapSubset asserts the "want" is a subset "have". In other words, all keys and their corresponding values in the "want" map must be in the "have" map. It is not an error when the "have" map has some other keys. Returns true if "want is a subset of "have", otherwise marks the test as failed, writes an error message to the test log and returns false.
func MapsSubset ¶
MapsSubset asserts all the "want" maps are subsets of corresponding "have" maps using MapSubset. Returns true if all "want" maps are subset of corresponding "have" maps, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Nil ¶
Nil asserts "have" is nil. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NoDirExist ¶
NoDirExist asserts "pth" points to not existing directory. It fails if the path points to an existing filesystem entry. Returns true on success, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NoError ¶
NoError asserts "err" is nil. Returns true if it is not, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NoFileExist ¶
NoFileExist asserts "pth" points to a not existing file. It fails if the path points to an existing filesystem entry. Returns true on success, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NoPanic ¶
NoPanic asserts "fn" does not panic. Returns true if it did not panic, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NotContain ¶
NotContain asserts "want" is not a substring of "have". Returns true if it's not, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NotDecreasing ¶ added in v0.22.0
NotDecreasing is inverse of Decreasing.
func NotEmpty ¶
NotEmpty asserts "have" is not empty. Returns true if it is not, otherwise marks the test as failed, writes an error message to the test log and returns false.
See check.Empty for the list of values which are considered empty.
func NotEqual ¶
NotEqual asserts both values are not equal. Returns true if they are not, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NotIncreasing ¶ added in v0.22.0
NotIncreasing is inverse of Increasing.
func NotNil ¶
NotNil asserts "have" is not nil. Returns true if it is not, otherwise marks the test as failed, writes an error message to the test log and returns false.
func NotSame ¶
NotSame asserts "want" and "have" are generic pointers and that both do not reference the same object. Returns true if they are not, otherwise marks the test as failed, writes an error message to the test log and returns false.
Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.
func NotSameType ¶ added in v0.38.0
NotSameType asserts that the arguments are not of the same type. Returns true if they are not, otherwise marks the test as failed, writes an error message to the test log and returns false.
Assertion uses reflect.TypeOf equality to determine the type.
func NotZero ¶
NotZero asserts "have" is not the zero value for its type. Returns true if it is not, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Panic ¶
Panic asserts "fn" panics. Returns true if it panicked, otherwise marks the test as failed, writes an error message to the test log and returns false.
func PanicContain ¶
PanicContain asserts "fn" panics, and the recovered panic value represented as a string contains "want". Returns true if it panics and does contain the wanted string, otherwise marks the test as failed, writes an error message to the test log and returns false.
func PanicMsg ¶
PanicMsg asserts the "fn" panics and returns the recovered panic value represented as a string. If the function did not panic, it marks the test as failed and writes an error message to the test log.
func Recent ¶
Recent asserts "have" is within check.Options.Recent from time.Now. Returns nil if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "have" may represent date in the form of a string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
func Regexp ¶
Regexp asserts that "want" regexp matches a string representation of "have. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "want" can be either a regular expression string or instance of regexp.Regexp. The fmt.Sprint s used to get string representation of have argument.
func Same ¶
Same asserts "want" and "have" are generic pointers and that both reference the same object. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.
func SameType ¶ added in v0.37.0
SameType asserts that both arguments are of the same type. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
Assertion uses reflect.TypeOf equality to determine the type.
func SliceSubset ¶
func SliceSubset[T comparable](t tester.T, want, have []T, opts ...any) bool
SliceSubset checks the "want" is a subset "have". In other words, all values in the "want" slice must be in the "have" slice. Returns nil if they are, otherwise returns an error with a message indicating the expected and actual values.
func Smaller ¶ added in v0.22.0
Smaller checks the "have" value is smaller than the "want" value. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
func SmallerOrEqual ¶ added in v0.22.0
SmallerOrEqual checks the "have" value is smaller or equal than the "want" value. Returns true if it is, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Time ¶
Time asserts "want" and "have" dates are equal. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "want" and "have" might be date representations in the form of a string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
func True ¶
True asserts "have" is true. Returns true if it's, otherwise marks the test as failed, writes an error message to the test log and returns false.
func Type ¶
Type asserts that the "src" can be type assigned to the pointer of the "target" (same as target, ok := src.(target)). Returns true if it can be done, otherwise marks the test as failed, writes an error message to the test log and returns false.
Example:
var target int var src any = 42 assert.Type(t, &target, src)
func Wait ¶ added in v0.32.0
Wait waits for "fn" to return true but no longer then given timeout. By default, calls to "fn" are throttled with a default throttle set in check.Options.WaitThrottle - use check.WithWaitThrottle option to change it. Returns true when the function returns true within given timeout, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "timeout" may represent duration in the form of a string, int, int64 or time.Duration.
func Within ¶
Within asserts "want" and "have" dates are equal "within" given duration. Returns true if they are, otherwise marks the test as failed, writes an error message to the test log and returns false.
The "want" and "have" might be date representations in the form of a string, int, int64 or time.Time. For string representations the check.Options.TimeFormat is used during parsing and the returned date is always in UTC. The int and int64 types are interpreted as Unix Timestamp, and the date returned is also in UTC.
Types ¶
This section is empty.