testing
v0.0.0-20261107141710-3b2f04da4aca
Documentation
pkg.go.dev ↗Constants
FailMust (14 declarations)
const ( // FailMust is the generic failure message for the 'Must'-assertion FailMust = "expected given argument to be true" // FailMust is the generic failure message for the 'MustNot'-assertion FailMustNot = "expected given argument to be not true" // FailPanics is the generic failure message for the 'Panics'-assertion FailPanics = "expected given function to panic" // FailErrIs is the generic failure message for the 'ErrIs'-assertion FailErrIs = "expected given error of target '%v' got '%v'" // FailContains is the generic failure message for the // 'Contains'-assertion FailContains = "expected following string (representation)%s" + ">>>>>> to be in >>>>>>%s" // FailContainsNot is the generic failure message for the // 'ContainsNot'-assertion FailContainsNot = "expected following string (representation)%s" + ">>>>>> NOT to be in >>>>>>%s" // FailEqTypes is the generic failure message for the 'Eq'-assertion // in case of type mismatch FailEqTypes = "expected given values of same type; got %T != %T" // FailEqPointer is the generic failure message for the // 'Eq'-assertion in case of pointer inequality FailEqPointer = "expected equal pointers; got %p != %p" // FailEqDiff is the generic failure message for the 'Eq'-assertion // in case not-equal values/string representations FailEqDiff = "expected equal string representations; got\n%s" // FailEqNot is the generic failure message for the // 'EqNot'-assertion FailEqNot = "expected different values; got '%s' == '%s'" // FailMatches is the generic failure message for the // 'Matches'-assertion FailMatches = "Regexp%s>>>>>> doesn't match >>>>>>%s" // FailMatchesNot is the generic failure message for the // 'MatchesNot'-assertion FailMatchesNot = "Regexp%s>>>>>> matches >>>>>>%s" // FailEventually is the generic failure message for the // 'MatchesEventually'-assertion in case no ExpBool assertion was // used to fail. FailEventually = "expected eventually condition evaluate to true" // ZeroString replaces in the Contains failure message an zero value ZeroString = "ZERO-STRING" )
NotImplemented
const NotImplemented = "not implemented yet"
PncZeroTestingMsg
const PncZeroTestingMsg = "zero *testing.T instance given"
TInstanceZeroPnc
const TInstanceZeroPnc = "expect testing.T instance not to be zero"
Variables
ErrMock
var ErrMock = errors.New("mocked test-error")
ErrMock is just an error to be used if one is needed without particularities.
ErrNotImplemented
var ErrNotImplemented = errors.New(NotImplemented)
ErrNotImplemented provides an intermediate error for the use case to have a building function that may fail but is not implemented yet except for returning this error.
EventuallyTimeoutDflt
var EventuallyTimeoutDflt = 500 * time.Millisecond
EventuallyTimeoutDflt is the default duration the Exp.Eventually assertion waits for its condition to evaluate to true
Functions
Errors
func Errors(e *Exp) bool
Errors informs if given expectations instance should rather error than fatale.
FailOn
func FailOn(err error, t *stdTesting.T, label string)
FailOn fails given test t if given error err is not nil. The error message is optionally prefixed by given label.
Match
func Match(s string, args ...string) bool
Match returns true if given string is (partially) matched by the regexp created from given variadic strings args: each string in args is split at new-lines; each split has its spaces trimmed and every regex relevant special characters escaped; finally all the splits are joined around `.*?` and prefix by the `. matches newline`-flag.
Matcher
func Matcher(args ...string) *regexp.Regexp
Matcher compiles a regular expression from given arguments: each string in args is split at new-lines; each split has its spaces trimmed and every regex relevant special characters escaped; finally all the splits are joined around `.*?` and prefix by the `. matches newline`-flag.
Message
func Message(e *Exp) string
Message extracts of given expectations instance e a potentially set message.
MockOSArg
func MockOSArg(t *testing.T, index int, value string)
MockOSArg mocks the value at given index in os.Args with given value iff index is not bigger than os.Args length. The mocked argument is reset on cleaning up.
MockStdErr
func MockStdErr(t *stdTesting.T) (content func() string)
MockStdErr mock os.Stderr by a byte buffer whose content is reported calling returned function. MockStdErr resets os.Stderr to its initial value once the test is cleaned up.
MockStdout
func MockStdout(t *testing.T) func() []byte
MockStdout replaces os.Stdout with given test t specific temporary file which is closed and whose bytes are returned if returned function is called. Stdout is reset to its default at cleaning up.
PanicOnZeroTestingInstance
func PanicOnZeroTestingInstance(t *testing.T)
PncNotImplemented
func PncNotImplemented()
PncNotImplemented raises an according panic useful for methods/functions which are not implemented yet.
PncTestingT
func PncTestingT(prefix string, t *stdTesting.T)
PncTestingT panics with an according message prefixed by given prefix iff given testing.T instance t is zero.
RandString
func RandString(length int) string
RandomString returns a random string of given length. Taken from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go.
WaitFor
func WaitFor(cond func() bool, timeout ...time.Duration) bool
Types
Exp
type Exp struct {
T T
EventuallyTimeout time.Duration
LastPanic any
// contains filtered or unexported fields
}
Exp provides assertions for the test associated by set Exp.T implementation.
Contains
func Contains(t T, value, sub StringRepresentation) *Exp
Contains returns a new Exp-instance having given values already asserted, i.e. it fatales the test if value's string representation doesn't contain sub's string representation.
ContainsNot
func ContainsNot(t T, value, sub StringRepresentation) *Exp
ContainsNot returns a new Exp-instance having given values already asserted, i.e. it fatales the test if value's string representation contains sub's string representation.
Eq
func Eq(t T, a, b any) *Exp
Eq returns a new Exp-instance having given values already asserted, i.e. it fatales the test if a and b a are not considered equal (see Exp.Eq).
EqNot
func EqNot(t T, a, b any) *Exp
EqNot returns a new Exp-instance having given values already asserted, i.e. it fatales the test if a and b a are considered equal (see Exp.Eq).
Err
func Err(t T) *Exp
Err returns a new Exp-instance having the error-flag set (instead of failing a test fatal).
ErrIs
func ErrIs(t T, err, target error) *Exp
ErrIs returns a new Exp-instance having given error err already asserted, i.e. it fatales the test if err does not match target.
Eventually
func Eventually( t T, cond func(exp *ExpBool) bool, timeout ...time.Duration, ) *Exp
Eventually returns a new Exp-instance having given value already asserted, i.e. it fatales the test if given condition cond doesn't return true within set Exp.EventuallyTimeout.
Matches
func Matches(t T, value StringRepresentation, ss ...string) *Exp
Matches returns a new Exp-instance having given values already asserted, i.e. it fatales the test if value's string representation is not matched by the joined variadic `.*?`-regex (see Exp.Matches).
MatchesNot
func MatchesNot(t T, value StringRepresentation, ss ...string) *Exp
MatchesNot returns a new Exp-instance having given values already asserted, i.e. it fatales the test if value's string representation is matched by the joined variadic `.*?`-regex (see Exp.Matches).
Msg
func Msg(t T, args ...any) *Exp
Msg return a new Exp-instance having given message msg set as the failure message for the next assertion.
Msgf
func Msgf(t T, format string, args ...any) *Exp
Msg return a new Exp-instance having given message msg set as the failure message for the next assertion.
Must
func Must(t T, value bool) *Exp
Must returns a new Exp-instance having given value already asserted, i.e. it fatales the test if given value is not true.
MustNot
func MustNot(t T, value bool) *Exp
MustNot returns a new Exp-instance having given value already asserted, i.e. it fatales the test if given value is not false.
Panics
func Panics(t T, value func()) *Exp
MustNot returns a new Exp-instance having given value already asserted, i.e. it fatales the test if given value does not panic.
Exp.Contains
func (e *Exp) Contains(value, sub StringRepresentation, args ...any)
Contains fails the test if given value's string representation doesn't contain given sub-value's string representation. In case of given variadic arguments sub is interpreted as a format string, i.e.: sub = fmt.Sprintf(sub, args...).
Exp.ContainsNot
func (e *Exp) ContainsNot(value, sub StringRepresentation, args ...any)
Contains fails the test if given value's string representation contains given sub-value's string representation. In case of given variadic arguments sub is interpreted as a format string, i.e.: sub = fmt.Sprintf(sub, args...).
Exp.Eq
func (e *Exp) Eq(a, b any)
Eq fails associated test if given values a, b are not considered equal, i.e.: - if they are not of the same type and a && b are no stringers - if a != b in case of two pointers - if toString(a) != toString(b)
Exp.EqNot
func (e *Exp) EqNot(a, b any)
EqNot fails associated test if given values a, b are considered equal, i.e.: - if a == b in case of two pointers - if toString(a) == toString(b) otherwise
Exp.Err
func (e *Exp) Err() *Exp
Err fails the next assertion with an error instead of fatal, i.e. doesn't stop the test execution on failing.
Exp.ErrIs
func (e *Exp) ErrIs(err, target error)
ErrIs fails associated test if given error is not of target error. NOTE this assertion may also be used to ensure that no error occurred:
Exp.ErrIs(errors.New("err"), nil)
will fail the test.
Exp.Eventually
func (e *Exp) Eventually(cond func(exp *ExpBool) bool, timeout ...time.Duration)
Eventually fails associated test if given condition cond not returns true within set Exp.EventuallyTimeout. Note if Exp.EventuallyTimeout is not set it defaults to EventuallyTimeoutDflt which may be overwritten by providing an optional timeout argument. The condition is provided wit an ExpBool instance providing assertions that evaluate to true or false, i.e. may be called repeatedly and set on failing a generic fail message like the Exp assertions. The last such message set before Eventually fails is reported as failure message (if Eventually fails).
Exp.Matches
func (e *Exp) Matches(value StringRepresentation, ss ...string)
Matches escapes given variadic-strings before it joins them with the `.*?`-separator and matches the result against given value's string representation, e.g. a string str:
<p> some text </p>
would be matched by
exp.Matches(str, "p", "me", "x", "/p").
Matches fails associated test if the matching fails.
Exp.MatchesNot
func (e *Exp) MatchesNot(value StringRepresentation, ss ...string)
MatchesNot fails associated test if the regex build from given variadic strings ss matches given value.
Exp.Msg
func (e *Exp) Msg(args ...any) *Exp
Msg sets a user defined failure message for the next assertion.
Exp.Msgf
func (e *Exp) Msgf(fmtMessage string, args ...any) *Exp
Msgf sets a user defined failure message for the next assertion.
Exp.Must
func (e *Exp) Must(truthy bool)
Must fails associated test if given bool truthy is not true.
Exp.MustNot
func (e *Exp) MustNot(falsy bool)
MustNot fails associated test if given bool falsy is not false.
Exp.Panics
func (e *Exp) Panics(f func()) (pncValue any)
Panics fails associated test if given function f's execution doesn't panic.
ExpBool
type ExpBool struct {
// contains filtered or unexported fields
}
ExpBool provides the assertions as boolean functions which can be used inside the condition of an Eventually assertion which provides to the conditional-callback an ExpBool instance.
ExpBool.Contains
func (e *ExpBool) Contains(value, sub StringRepresentation) bool
Contains return true if value's string representation contains given sub string representation; otherwise false.
ExpBool.ContainsNot
func (e *ExpBool) ContainsNot(value, sub StringRepresentation) bool
ContainsNot return true if value's string representation contains not give sub string representation; otherwise false
ExpBool.Eq
func (e *ExpBool) Eq(a, b any) bool
Eq returns true if given values a and b are considered equal; otherwise false (see Exp.Eq).
ExpBool.EqNot
func (e *ExpBool) EqNot(a, b any) bool
EqNot returns true if given values a and b are considered not equal; otherwise false (see Exp.Eq).
ExpBool.ErrIs
func (e *ExpBool) ErrIs(err, target error) bool
ErrIs return true if given error err matches target; otherwise false.
ExpBool.Matches
func (e *ExpBool) Matches(value StringRepresentation, ss ...string) bool
Matches returns true if given values string representation is matched by the variadic `.*?`-regex joined from given strings ss; false otherwise (see [Exp.Match]).
ExpBool.MatchesNot
func (e *ExpBool) MatchesNot(value StringRepresentation, ss ...string) bool
Matches returns true if given values string representation is *not* matched by the variadic `.*?`-regex joined from given strings ss; false otherwise (see [Exp.Match]).
ExpBool.Must
func (e *ExpBool) Must(value bool) bool
Must returns true if given bool value is true; otherwise false.
ExpBool.MustNot
func (e *ExpBool) MustNot(value bool) bool
MustNot return true if given bool value is false; otherwise false.
ExpBool.Panics
func (e *ExpBool) Panics(f func()) (exp bool)
Panics returns true if given function f panics; otherwise false.
String
type String string
String.SplitTrimmed
func (s String) SplitTrimmed(split string) []string
StringRepresentation
type StringRepresentation interface{}
StringRepresentation documents what a string representation of any type is:
- the string if it is of type string,
- the return value of String if the Stringer interface is implemented,
- fmt.Sprintf("%v", value) in all other cases.
T
type T interface {
Fatal(args ...any)
Helper()
Error(args ...any)
}
T interface implementations associate a test with an Exp-instance whose *testing.T instance implements this interface.