Documentation
¶
Index ¶
- Constants
- Variables
- func Errors(e *Exp) bool
- func FailOn(err error, t *stdTesting.T, label string)
- func Match(s string, args ...string) bool
- func Message(e *Exp) string
- func MockOSArg(t *testing.T, index int, value string)
- func MockStdErr(t *stdTesting.T) (content func() string)
- func MockStdout(t *testing.T) func() []byte
- func PncNotImplemented()
- func PncTestingT(prefix string, t *stdTesting.T)
- func RandString(length int) string
- func WaitFor(cond func() bool, timeout ...time.Duration) bool
- type Exp
- func Contains(t T, value, sub StringRepresentation) *Exp
- func ContainsNot(t T, value, sub StringRepresentation) *Exp
- func Eq(t T, a, b any) *Exp
- func EqNot(t T, a, b any) *Exp
- func Err(t T) *Exp
- func ErrIs(t T, err, target error) *Exp
- func Eventually(t T, cond func(*ExpBool) bool, timeout ...time.Duration) *Exp
- func Matches(t T, value StringRepresentation, ss ...string) *Exp
- func MatchesNot(t T, value StringRepresentation, ss ...string) *Exp
- func Msg(t T, args ...any) *Exp
- func Msgf(t T, format string, args ...any) *Exp
- func Must(t T, value bool) *Exp
- func MustNot(t T, value bool) *Exp
- func Panics(t T, value func()) *Exp
- func (e *Exp) Contains(value, sub StringRepresentation, args ...any)
- func (e *Exp) ContainsNot(value, sub StringRepresentation, args ...any)
- func (e *Exp) Eq(a, b any)
- func (e *Exp) EqNot(a, b any)
- func (e *Exp) Err() *Exp
- func (e *Exp) ErrIs(err, target error)
- func (e *Exp) Eventually(cond func(exp *ExpBool) bool, timeout ...time.Duration)
- func (e *Exp) Matches(value StringRepresentation, ss ...string)
- func (e *Exp) MatchesNot(value StringRepresentation, ss ...string)
- func (e *Exp) Msg(args ...any) *Exp
- func (e *Exp) Msgf(fmtMessage string, args ...any) *Exp
- func (e *Exp) Must(truthy bool)
- func (e *Exp) MustNot(falsy bool)
- func (e *Exp) Panics(f func()) (pncValue any)
- type ExpBool
- func (e *ExpBool) Contains(value, sub StringRepresentation) bool
- func (e *ExpBool) ContainsNot(value, sub StringRepresentation) bool
- func (e *ExpBool) Eq(a, b any) bool
- func (e *ExpBool) EqNot(a, b any) bool
- func (e *ExpBool) ErrIs(err, target error) bool
- func (e *ExpBool) Matches(value StringRepresentation, ss ...string) bool
- func (e *ExpBool) MatchesNot(value StringRepresentation, ss ...string) bool
- func (e *ExpBool) Must(value bool) bool
- func (e *ExpBool) MustNot(value bool) bool
- func (e *ExpBool) Panics(f func()) (exp bool)
- type StringRepresentation
- type T
Constants ¶
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" )
const NotImplemented = "not implemented yet"
const TInstanceZeroPnc = "expect testing.T instance not to be zero"
Variables ¶
var ErrMock = errors.New("mocked test-error")
ErrMock is just an error to be used if one is needed without particularities.
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.
var EventuallyTimeoutDflt = 500 * time.Millisecond
EventuallyTimeoutDflt is the default duration the Exp.Eventually assertion waits for its condition to evaluate to true
Functions ¶
func 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.
func Match ¶
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.
func MockOSArg ¶
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.
func 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.
func MockStdout ¶
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.
func PncNotImplemented ¶
func PncNotImplemented()
PncNotImplemented raises an according panic useful for methods/functions which are not implemented yet.
func 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.
func RandString ¶
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.
Types ¶
type 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.
func 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.
func 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.
func Eq ¶
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).
func EqNot ¶
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).
func Err ¶
Err returns a new Exp-instance having the error-flag set (instead of failing a test fatal).
func ErrIs ¶
ErrIs returns a new Exp-instance having given error err already asserted, i.e. it fatales the test if err does not match target.
func Eventually ¶
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.
func 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).
func 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).
func Msg ¶
Msg return a new Exp-instance having given message msg set as the failure message for the next assertion.
func Msgf ¶
Msg return a new Exp-instance having given message msg set as the failure message for the next assertion.
func Must ¶
Must returns a new Exp-instance having given value already asserted, i.e. it fatales the test if given value is not true.
func MustNot ¶
MustNot returns a new Exp-instance having given value already asserted, i.e. it fatales the test if given value is not false.
func Panics ¶
MustNot returns a new Exp-instance having given value already asserted, i.e. it fatales the test if given value does not panic.
func (*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...).
func (*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...).
func (*Exp) Eq ¶
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)
func (*Exp) EqNot ¶
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
func (*Exp) Err ¶
Err fails the next assertion with an error instead of fatal, i.e. doesn't stop the test execution on failing.
func (*Exp) ErrIs ¶
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.
func (*Exp) Eventually ¶
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).
func (*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.
func (*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.
type 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.
func (*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.
func (*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
func (*ExpBool) Eq ¶
Eq returns true if given values a and b are considered equal; otherwise false (see Exp.Eq).
func (*ExpBool) EqNot ¶
EqNot returns true if given values a and b are considered not equal; otherwise false (see Exp.Eq).
func (*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]).
func (*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]).
type 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.