Documentation
¶
Overview ¶
Package assert provides assertion functions.
Index ¶
- func After(t tester.T, date, mark time.Time, opts ...check.Option) bool
- func AfterOrEqual(t tester.T, date, mark any, opts ...check.Option) bool
- func Before(t tester.T, date, mark any, opts ...check.Option) bool
- func BeforeOrEqual(t tester.T, date, mark time.Time, opts ...check.Option) bool
- func ChannelWillClose[C any](t tester.T, within any, c <-chan C, opts ...check.Option) bool
- func Contain(t tester.T, want, have string, opts ...check.Option) bool
- func Count(t tester.T, count int, what, where any, opts ...check.Option) bool
- func DirExist(t tester.T, pth string, opts ...check.Option) bool
- func Duration(t tester.T, want, have any, opts ...check.Option) bool
- func Empty(t tester.T, have any, opts ...check.Option) bool
- func Epsilon[T constraints.Number](t tester.T, want, delta, have T, opts ...check.Option) bool
- func Equal(t tester.T, want, have any, opts ...check.Option) bool
- func Error(t tester.T, err error, opts ...check.Option) bool
- func ErrorAs(t tester.T, err error, target any, opts ...check.Option) bool
- func ErrorContain(t tester.T, want string, err error, opts ...check.Option) bool
- func ErrorEqual(t tester.T, want string, err error, opts ...check.Option) bool
- func ErrorIs(t tester.T, err, target error, opts ...check.Option) bool
- func ErrorRegexp(t tester.T, want string, err error, opts ...check.Option) bool
- func Exact(t tester.T, want, have any, opts ...check.Option) bool
- func ExitCode(t tester.T, want int, err error, opts ...check.Option) bool
- func False(t tester.T, have bool, opts ...check.Option) bool
- func Fields(t tester.T, want int, s any, opts ...check.Option) bool
- func FileContain[T check.Content](t tester.T, want T, pth string, opts ...check.Option) bool
- func FileExist(t tester.T, pth string, opts ...check.Option) bool
- func Has[T comparable](t tester.T, want T, bag []T, opts ...check.Option) bool
- func HasKey[K comparable, V any](t tester.T, key K, set map[K]V, opts ...check.Option) (V, bool)
- func HasKeyValue[K, V comparable](t tester.T, key K, want V, set map[K]V, opts ...check.Option) bool
- func HasNo[T comparable](t tester.T, want T, bag []T, opts ...check.Option) bool
- func HasNoKey[K comparable, V any](t tester.T, key K, set map[K]V, opts ...check.Option) bool
- func JSON(t tester.T, want, have string, opts ...check.Option) bool
- func Len(t tester.T, want int, have any, opts ...check.Option) bool
- func MapSubset[K cmp.Ordered, V any](t tester.T, want, have map[K]V, opts ...check.Option) bool
- func MapsSubset[K cmp.Ordered, V any](t tester.T, want, have []map[K]V, opts ...check.Option) bool
- func Nil(t tester.T, have any, opts ...check.Option) bool
- func NoDirExist(t tester.T, pth string, opts ...check.Option) bool
- func NoError(t tester.T, err error, opts ...check.Option) bool
- func NoFileExist(t tester.T, pth string, opts ...check.Option) bool
- func NoPanic(t tester.T, fn check.TestFunc, opts ...check.Option) bool
- func NotContain(t tester.T, want, have string, opts ...check.Option) bool
- func NotEmpty(t tester.T, have any, opts ...check.Option) bool
- func NotEqual(t tester.T, want, have any, opts ...check.Option) bool
- func NotNil(t tester.T, have any, opts ...check.Option) bool
- func NotSame(t tester.T, want, have any, opts ...check.Option) bool
- func NotZero(t tester.T, have any, opts ...check.Option) bool
- func Panic(t tester.T, fn check.TestFunc, opts ...check.Option) bool
- func PanicContain(t tester.T, want string, fn check.TestFunc, opts ...check.Option) bool
- func PanicMsg(t tester.T, fn check.TestFunc, opts ...check.Option) *string
- func Recent(t tester.T, have any, opts ...check.Option) bool
- func Regexp(t tester.T, want, have any, opts ...check.Option) bool
- func Same(t tester.T, want, have any, opts ...check.Option) bool
- func SliceSubset[T comparable](t tester.T, want, have []T, opts ...check.Option) bool
- func Time(t tester.T, want, have any, opts ...check.Option) bool
- func True(t tester.T, have bool, opts ...check.Option) bool
- func Type(t tester.T, want, have any, opts ...check.Option) bool
- func Within(t tester.T, want, within, have any, opts ...check.Option) bool
- func Zero(t tester.T, have any, opts ...check.Option) bool
- func Zone(t tester.T, want, have *time.Location, opts ...check.Option) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After asserts "date" is after "mark". Returns true if it's, otherwise marks the test as failed, writes error message to test log and returns false.
The "date" and "mark" might be date representations in 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 error message to test log and returns false.
The "date" and "mark" might be date representations in 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 Before ¶
Before asserts "date" is before "mark". Returns true if it's, otherwise marks the test as failed, writes error message to test log and returns false.
The "date" and "mark" might be date representations in 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 "mark". Returns true if it's, otherwise marks the test as failed, writes error message to test log and returns false.
The "date" and "mark" might be date representations in 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 ChannelWillClose ¶
ChannelWillClose asserts channel will be closed "within" given time duration. Returns true if it was, otherwise marks the test as failed, writes error message to test log and returns false.
The "within" may represent duration in 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 error message to 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 error message to test log and returns false.
Currently only strings are supported.
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 error message to 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 error message to test log and returns false.
The "want" and "have" might be duration representation in 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 error message to test log and returns false.
See check.Empty for list of values which are considered empty.
func Epsilon ¶
Epsilon asserts the difference between two numbers is within a given delta. Returns true if it is, otherwise marks the test as failed, writes error message to test log and returns false.
func Equal ¶
Equal asserts both values are equal. Returns true if they are, otherwise marks the test as failed, writes error message to 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 error message to test log and returns false.
func ErrorAs ¶
ErrorAs finds the first error in "err" tree that matches target, and if one is found, sets target to that error. Returns true if it does, otherwise marks the test as failed, writes error message to 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 error message to test log and returns false.
func ErrorEqual ¶
ErrorEqual asserts "err" is not nil and its message equals to "want". Returns true if it's, otherwise marks the test as failed, writes error message to test log and returns false.
func ErrorIs ¶
ErrorIs asserts whether any error in "err" tree matches target. Returns true if it does, otherwise marks the test as failed, writes error message to 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 error message to test log and returns false.
The "want" can be either 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 error message to test log and returns false.
The "want" and "have" might be date representations in form of string, int, int64 or time.Time. For string representations the [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 pointer to [exec.ExitError] with exit code equal to "want". Returns true if it is, otherwise marks the test as failed, writes error message to test log and returns false.
func False ¶
False asserts "have" is false. Returns true if it's, otherwise marks the test as failed, writes error message to 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 error message to test log and returns false.
func FileContain ¶
FileContain asserts 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 "want" string. Returns true on success, otherwise marks the test as failed, writes error message to 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 error message to test log and returns false.
func Has ¶
Has asserts slice has "want" value. Returns true if it does, otherwise marks the test as failed, writes error message to test log and returns false.
func HasKey ¶
HasKey asserts map has a key. Returns true if it does, otherwise marks the test as failed, writes error message to test log and returns false.
func HasKeyValue ¶
func HasKeyValue[K, V comparable]( t tester.T, key K, want V, set map[K]V, opts ...check.Option, ) bool
HasKeyValue asserts map has a key with given value. Returns true if it doesn't, otherwise marks the test as failed, writes error message to test log and returns false.
func HasNo ¶
HasNo asserts slice does not have "want" value. Returns true if it does not, otherwise marks the test as failed, writes error message to test log and returns false.
func HasNoKey ¶
HasNoKey asserts map has no key. Returns true if it doesn't, otherwise marks the test as failed, writes error message to 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 error message to test log and returns false.
assert.JSON(t, `{"hello": "world"}`, `{"foo": "bar"}`)
func Len ¶
Len asserts "have" has "want" elements. Returns true if it is, otherwise it marks the test as failed, writes error message to 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 "want" map must be in "have" map. It is not an error when "have" map has some other keys. Returns true if "want is a subset of "have", otherwise marks the test as failed, writes error message to test log and returns false.
func MapsSubset ¶
func MapsSubset[K cmp.Ordered, V any]( t tester.T, want, have []map[K]V, opts ...check.Option, ) bool
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 error message to test log and returns false.
func Nil ¶
Nil asserts "have" is nil. Returns true if it is, otherwise marks the test as failed, writes error message to 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 error message to 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 error message to test log and returns false.
func NoFileExist ¶
NoFileExist asserts "pth" points to 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 error message to 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 error message to 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 error message to test log and returns false.
func NotEmpty ¶
NotEmpty asserts "have" is not empty. Returns true if it is not, otherwise marks the test as failed, writes error message to test log and returns false.
See check.Empty for 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 error message to test log and returns false.
func NotNil ¶
NotNil asserts "have" is not nil. Returns true if it is not, otherwise marks the test as failed, writes error message to 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 error message to 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 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 error message to test log and returns false.
func Panic ¶
Panic asserts "fn" panics. Returns true if it panicked, otherwise marks the test as failed, writes error message to 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 wanted string, otherwise marks the test as failed, writes error message to test log and returns false.
func PanicMsg ¶
PanicMsg asserts "fn" panics, and returns the recovered panic value represented as a string. If function did not panic it marks the test as failed and writes error message to 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 error message to test log and returns false.
The "have" may represent date in 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 error message to test log and returns false.
The "want" can be either 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 error message to 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 SliceSubset ¶
SliceSubset checks the "want" is a subset "have". In other words all values in "want" slice must be in "have" slice. Returns nil if they are, otherwise returns an error with a message indicating the expected and actual values.
func Time ¶
Time asserts "want" and "have" dates are equal. Returns true if they are, otherwise marks the test as failed, writes error message to test log and returns false.
The "want" and "have" might be date representations in form of string, int, int64 or time.Time. For string representations the [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 error message to test log and returns false.
func Type ¶
Type asserts that both arguments are of the same type. Returns true if they are, otherwise marks the test as failed, writes error message to test log and returns false.
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 error message to test log and returns false.
The "want" and "have" might be date representations in form of string, int, int64 or time.Time. For string representations the [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.
The "within" might be duration representation in form of string, int, int64 or time.Duration.
Types ¶
This section is empty.