must

package
v1.12.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2025 License: MPL-2.0 Imports: 11 Imported by: 63

Documentation

Overview

Package test provides a modern generic testing assertions library.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ascending added in v0.2.5

func Ascending[O constraints.Ordered](t T, slice []O, settings ...Setting)

Ascending asserts slice[n] ≤ slice[n+1] for each element.

Example
Output:

func AscendingCmp added in v0.6.3

func AscendingCmp[A any](t T, slice []A, compare func(A, A) int, settings ...Setting)

AscendingCmp asserts slice[n] is less than slice[n+1] for each element using the cmp comparator.

Example
Output:

func AscendingFunc added in v0.2.5

func AscendingFunc[A any](t T, slice []A, less func(A, A) bool, settings ...Setting)

AscendingFunc asserts slice[n] is less than slice[n+1] for each element using the less comparator.

Example
Output:

func AscendingLess added in v0.2.5

func AscendingLess[L interfaces.LessFunc[L]](t T, slice []L, settings ...Setting)

AscendingLess asserts slice[n].Less(slice[n+1]) for each element.

Example
Output:

func Between added in v0.4.0

func Between[O constraints.Ordered](t T, lower, val, upper O, settings ...Setting)

Between asserts lower ≤ val ≤ upper.

Example
Output:

func BetweenExclusive added in v0.4.0

func BetweenExclusive[O constraints.Ordered](t T, lower, val, upper O, settings ...Setting)

BetweenExclusive asserts lower < val < upper.

Example
Output:

func Close added in v0.6.7

func Close(t T, c io.Closer)

Close asserts c.Close does not cause an error.

func Contains

func Contains[C any](t T, element C, container interfaces.ContainsFunc[C], settings ...Setting)

Contains asserts container.ContainsFunc(element) is true.

Example
Output:

func ContainsSubset added in v0.6.1

func ContainsSubset[C any](t T, elements []C, container interfaces.ContainsFunc[C], settings ...Setting)

ContainsSubset asserts each element in elements exists in container, in no particular order. There may be elements in container beyond what is present in elements.

Example
Output:

func Descending added in v0.2.5

func Descending[O constraints.Ordered](t T, slice []O, settings ...Setting)

Descending asserts slice[n] ≥ slice[n+1] for each element.

Example
Output:

func DescendingCmp added in v0.6.3

func DescendingCmp[A any](t T, slice []A, compare func(A, A) int, settings ...Setting)

DescendingCmp asserts slice[n+1] is ≤ slice[n] for each element.

Example
Output:

func DescendingFunc added in v0.2.5

func DescendingFunc[A any](t T, slice []A, less func(A, A) bool, settings ...Setting)

DescendingFunc asserts slice[n+1] is less than slice[n] for each element using the less comparator.

Example
Output:

func DescendingLess added in v0.2.5

func DescendingLess[L interfaces.LessFunc[L]](t T, slice []L, settings ...Setting)

DescendingLess asserts slice[n+1].Less(slice[n]) for each element.

Example
Output:

func DirExists added in v0.2.5

func DirExists(t T, directory string, settings ...Setting)

DirExists asserts directory exists on the OS filesystem.

Example
Output:

func DirExistsFS added in v0.4.0

func DirExistsFS(t T, system fs.FS, directory string, settings ...Setting)

DirExistsFS asserts directory exists on the fs.FS filesystem.

Example, DirExistsFS(t, os.DirFS("/usr/local"), "bin")

Example
Output:

func DirMode added in v1.8.0

func DirMode(t T, path string, permissions fs.FileMode, settings ...Setting)

DirMode asserts the directory at path on the OS filesystem has exactly the given permission bits.

func DirModeFS added in v1.8.0

func DirModeFS(t T, system fs.FS, path string, permissions fs.FileMode, settings ...Setting)

DirModeFS asserts the directory at path on fs.FS has exactly the given permission bits.

Example, DirModeFS(t, os.DirFS("/"), "bin", 0655)

func DirNotExists added in v0.2.5

func DirNotExists(t T, directory string, settings ...Setting)

DirNotExists asserts directory does not exist on the OS filesystem.

Example
Output:

func DirNotExistsFS added in v0.4.0

func DirNotExistsFS(t T, system fs.FS, directory string, settings ...Setting)

DirNotExistsFS asserts directory does not exist on the fs.FS filesystem.

Example, DirNotExistsFS(t, os.DirFS("/tmp"), "scratch")

Example
Output:

func Empty added in v0.2.5

func Empty(t T, e interfaces.EmptyFunc, settings ...Setting)

Empty asserts e.Empty() is true.

Example
Output:

func Eq

func Eq[A any](t T, exp, val A, settings ...Setting)

Eq asserts exp and val are equal using cmp.Equal.

Example
Output:

func EqError added in v0.2.2

func EqError(t T, err error, msg string, settings ...Setting)

EqError asserts err contains message msg.

Example
Output:

func EqFunc

func EqFunc[A any](t T, exp, val A, eq func(a, b A) bool, settings ...Setting)

EqFunc asserts exp and val are equal using eq.

Example
Output:

func EqJSON

func EqJSON(t T, exp, val string, settings ...Setting)

EqJSON asserts exp and val are equivalent JSON.

Example
Output:

func EqOp added in v0.2.4

func EqOp[C comparable](t T, exp, val C, settings ...Setting)

EqOp asserts exp == val.

Example
Output:

func Equal added in v0.4.0

func Equal[E interfaces.EqualFunc[E]](t T, exp, val E, settings ...Setting)

Equal asserts val.Equal(exp).

Example
Output:

func Error

func Error(t T, err error, settings ...Setting)

Error asserts err is a non-nil error.

Example
Output:

func ErrorAs added in v1.11.0

func ErrorAs[E error, Target *E](t T, err error, target Target, settings ...Setting)

ErrorAs asserts err's tree contains an error that matches target. If so, it sets target to the error value.

Example
Output:

e1

func ErrorContains added in v0.5.0

func ErrorContains(t T, err error, sub string, settings ...Setting)

ErrorContains asserts err contains sub.

Example
Output:

func ErrorIs

func ErrorIs(t T, err error, target error, settings ...Setting)

ErrorIs asserts err

Example
Output:

func False

func False(t T, condition bool, settings ...Setting)

False asserts condition is false.

Example
Output:

func FileContains added in v0.2.5

func FileContains(t T, file, content string, settings ...Setting)

FileContains asserts the file on the OS filesystem contains content as a substring.

Example
Output:

func FileContainsFS added in v0.4.0

func FileContainsFS(t T, system fs.FS, file, content string, settings ...Setting)

FileContainsFS asserts the file on fs.FS contains content as a substring.

Often os.DirFS is used to interact with the host filesystem. Example, FileContainsFS(t, os.DirFS("/etc"), "hosts", "localhost")

Example
Output:

func FileExists added in v0.2.5

func FileExists(t T, file string, settings ...Setting)

FileExists asserts file exists on the OS filesystem.

Example
Output:

func FileExistsFS added in v0.4.0

func FileExistsFS(t T, system fs.FS, file string, settings ...Setting)

FileExistsFS asserts file exists on the fs.FS filesystem.

Example, FileExistsFS(t, os.DirFS("/etc"), "hosts")

Example
Output:

func FileMode added in v0.2.5

func FileMode(t T, path string, permissions fs.FileMode, settings ...Setting)

FileMode asserts the file or directory at path on the OS filesystem has exactly the given permission bits.

Example
Output:

func FileModeFS added in v0.4.0

func FileModeFS(t T, system fs.FS, path string, permissions fs.FileMode, settings ...Setting)

FileModeFS asserts the file or directory at path on fs.FS has exactly the given permission bits.

Example, FileModeFS(t, os.DirFS("/bin"), "find", 0655)

Example
Output:

func FileNotExists added in v0.2.5

func FileNotExists(t T, file string, settings ...Setting)

FileNotExists asserts file does not exist on the OS filesystem.

Example
Output:

func FileNotExistsFS added in v0.4.0

func FileNotExistsFS(t T, system fs.FS, file string, settings ...Setting)

FileNotExistsFS asserts file does not exist on the fs.FS filesystem.

Example, FileNotExist(t, os.DirFS("/bin"), "exploit.exe")

Example
Output:

func FilePathValid added in v0.2.5

func FilePathValid(t T, path string, settings ...Setting)

FilePathValid asserts path is a valid file path.

Example
Output:

func Greater

func Greater[O constraints.Ordered](t T, exp, val O, settings ...Setting)

Greater asserts val > exp.

Example
Output:

func GreaterEq

func GreaterEq[O constraints.Ordered](t T, exp, val O, settings ...Setting)

GreaterEq asserts val ≥ exp.

Example
Output:

func InDelta

func InDelta[N interfaces.Number](t T, a, b, delta N, settings ...Setting)

InDelta asserts a and b are within delta of each other.

Example
Output:

func InDeltaSlice

func InDeltaSlice[N interfaces.Number](t T, a, b []N, delta N, settings ...Setting)

InDeltaSlice asserts each element a[n] is within delta of b[n].

Example
Output:

func Len added in v0.2.5

func Len[A any](t T, n int, slice []A, settings ...Setting)

Len asserts slice is of length n.

Shorthand function for SliceLen. For checking Len() of a struct, use the Length() assertion.

Example
Output:

func Length added in v0.4.0

func Length(t T, exp int, l interfaces.LengthFunc, settings ...Setting)

Length asserts l.Len() is equal to exp.

Example
Output:

func Less

func Less[O constraints.Ordered](t T, exp, val O, settings ...Setting)

Less asserts val < exp.

Example
Output:

func LessEq

func LessEq[O constraints.Ordered](t T, exp, val O, settings ...Setting)

LessEq asserts val ≤ exp.

Example
Output:

func Lesser

func Lesser[L interfaces.LessFunc[L]](t T, exp, val L, settings ...Setting)

Lesser asserts val.Less(exp).

Example
Output:

func MapContainsKey added in v0.6.0

func MapContainsKey[M ~map[K]V, K comparable, V any](t T, m M, key K, settings ...Setting)

MapContainsKey asserts m contains key.

Example
Output:

func MapContainsKeys added in v0.2.5

func MapContainsKeys[M ~map[K]V, K comparable, V any](t T, m M, keys []K, settings ...Setting)

MapContainsKeys asserts m contains each key in keys.

Example
Output:

func MapContainsValue added in v1.7.2

func MapContainsValue[M ~map[K]V, K comparable, V any](t T, m M, val V, settings ...Setting)

MapContainsValue asserts m contains val.

func MapContainsValueEqual added in v1.7.2

func MapContainsValueEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[V]](t T, m M, val V, settings ...Setting)

MapContainsValueEqual asserts m contains val using the V.Equal method.

func MapContainsValueFunc added in v1.7.2

func MapContainsValueFunc[M ~map[K]V, K comparable, V any](t T, m M, val V, eq func(V, V) bool, settings ...Setting)

MapContainsValueFunc asserts m contains val using the eq function.

func MapContainsValues added in v0.2.5

func MapContainsValues[M ~map[K]V, K comparable, V any](t T, m M, vals []V, settings ...Setting)

MapContainsValues asserts m contains each val in vals.

Example
Output:

func MapContainsValuesEqual added in v0.4.0

func MapContainsValuesEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[V]](t T, m M, vals []V, settings ...Setting)

MapContainsValuesEqual asserts m contains each val in vals using the V.Equal method.

Example
Output:

func MapContainsValuesFunc added in v0.2.5

func MapContainsValuesFunc[M ~map[K]V, K comparable, V any](t T, m M, vals []V, eq func(V, V) bool, settings ...Setting)

MapContainsValuesFunc asserts m contains each val in vals using the eq function.

Example
Output:

func MapEmpty

func MapEmpty[M ~map[K]V, K comparable, V any](t T, m M, settings ...Setting)

MapEmpty asserts map is empty.

Example
Output:

func MapEq

func MapEq[M1, M2 interfaces.Map[K, V], K comparable, V any](t T, exp M1, val M2, settings ...Setting)

MapEq asserts maps exp and val contain the same key/val pairs, using cmp.Equal function to compare vals.

Example
Output:

func MapEqFunc

func MapEqFunc[M1, M2 interfaces.Map[K, V], K comparable, V any](t T, exp M1, val M2, eq func(V, V) bool, settings ...Setting)

MapEqFunc asserts maps exp and val contain the same key/val pairs, using eq to compare vals.

Example
Output:

func MapEqOp added in v1.10.0

func MapEqOp[M interfaces.Map[K, V], K, V comparable](t T, exp M, val M, settings ...Setting)

MapEqOp asserts maps exp and val contain the same key/val pairs, using == to compare vals.

Example
Output:

func MapEqual added in v0.4.0

func MapEqual[M interfaces.MapEqualFunc[K, V], K comparable, V interfaces.EqualFunc[V]](t T, exp, val M, settings ...Setting)

MapEqual asserts maps exp and val contain the same key/val pairs, using Equal method to compare val

Example
Output:

func MapLen

func MapLen[M ~map[K]V, K comparable, V any](t T, n int, m M, settings ...Setting)

MapLen asserts map is of size n.

Example
Output:

func MapNotContainsKey added in v0.6.0

func MapNotContainsKey[M ~map[K]V, K comparable, V any](t T, m M, key K, settings ...Setting)

MapNotContainsKey asserts m does not contain key.

Example
Output:

func MapNotContainsKeys added in v0.4.6

func MapNotContainsKeys[M ~map[K]V, K comparable, V any](t T, m M, keys []K, settings ...Setting)

MapNotContainsKeys asserts m does not contain any key in keys.

Example
Output:

func MapNotContainsValue added in v1.7.2

func MapNotContainsValue[M ~map[K]V, K comparable, V any](t T, m M, val V, settings ...Setting)

MapNotContainsValue asserts m does not contain val.

func MapNotContainsValueEqual added in v1.7.2

func MapNotContainsValueEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[V]](t T, m M, val V, settings ...Setting)

MapNotContainsValueEqual asserts m does not contain val using the V.Equal method.

func MapNotContainsValueFunc added in v1.7.2

func MapNotContainsValueFunc[M ~map[K]V, K comparable, V any](t T, m M, val V, eq func(V, V) bool, settings ...Setting)

MapNotContainsValueFunc asserts m does not contain val using the eq function.

func MapNotContainsValues added in v0.4.6

func MapNotContainsValues[M ~map[K]V, K comparable, V any](t T, m M, vals []V, settings ...Setting)

MapNotContainsValues asserts m does not contain any value in vals.

Example
Output:

func MapNotContainsValuesEqual added in v0.4.6

func MapNotContainsValuesEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[V]](t T, m M, vals []V, settings ...Setting)

MapNotContainsValuesEqual asserts m does not contain any value in vals using the V.Equal method.

Example
Output:

func MapNotContainsValuesFunc added in v0.4.6

func MapNotContainsValuesFunc[M ~map[K]V, K comparable, V any](t T, m M, vals []V, eq func(V, V) bool, settings ...Setting)

MapNotContainsValuesFunc asserts m does not contain any value in vals using the eq function.

Example
Output:

func MapNotEmpty added in v0.4.0

func MapNotEmpty[M ~map[K]V, K comparable, V any](t T, m M, settings ...Setting)

MapNotEmpty asserts map is not empty.

Example
Output:

func Max added in v0.6.3

func Max[A any, C interfaces.MaxFunc[A]](t T, expect A, collection C, settings ...Setting)

Max asserts collection.Max() is equal to expect.

The equality method may be configured with Cmp options.

Example
Output:

func Min added in v0.6.3

func Min[A any, C interfaces.MinFunc[A]](t T, expect A, collection C, settings ...Setting)

Min asserts collection.Min() is equal to expect.

The equality method may be configured with Cmp options.

Example
Output:

func Negative added in v0.2.5

func Negative[N interfaces.Number](t T, n N, settings ...Setting)

Negative asserts n < 0.

Example
Output:

func Nil

func Nil(t T, a any, settings ...Setting)

Nil asserts a is nil.

Example
Output:

func NoError

func NoError(t T, err error, settings ...Setting)

NoError asserts err is a nil error.

Example
Output:

func NonNegative added in v0.4.0

func NonNegative[N interfaces.Number](t T, n N, settings ...Setting)

NonNegative asserts n >= 0.

Example
Output:

func NonPositive added in v0.4.0

func NonPositive[N interfaces.Number](t T, n N, settings ...Setting)

NonPositive asserts n ≤ 0.

Example
Output:

func NonZero added in v0.2.5

func NonZero[N interfaces.Number](t T, n N, settings ...Setting)

NonZero asserts n != 0.

Example
Output:

func NotContains added in v0.4.0

func NotContains[C any](t T, element C, container interfaces.ContainsFunc[C], settings ...Setting)

NotContains asserts container.ContainsFunc(element) is false.

Example
Output:

func NotEmpty added in v0.4.0

func NotEmpty(t T, e interfaces.EmptyFunc, settings ...Setting)

NotEmpty asserts e.Empty() is false.

Example
Output:

func NotEq

func NotEq[A any](t T, exp, val A, settings ...Setting)

NotEq asserts exp and val are not equal using cmp.Equal.

Example
Output:

func NotEqFunc

func NotEqFunc[A any](t T, exp, val A, eq func(a, b A) bool, settings ...Setting)

NotEqFunc asserts exp and val are not equal using eq.

Example
Output:

func NotEqOp added in v0.2.5

func NotEqOp[C comparable](t T, exp, val C, settings ...Setting)

NotEqOp asserts exp != val.

Example
Output:

func NotEqual added in v0.4.0

func NotEqual[E interfaces.EqualFunc[E]](t T, exp, val E, settings ...Setting)

NotEqual asserts !val.Equal(exp).

Example
Output:

func NotNil

func NotNil(t T, a any, settings ...Setting)

NotNil asserts a is not nil.

Example
Output:

func One added in v0.3.1

func One[N interfaces.Number](t T, n N, settings ...Setting)

One asserts n == 1.

Example
Output:

func Positive added in v0.2.5

func Positive[N interfaces.Number](t T, n N, settings ...Setting)

Positive asserts n > 0.

Example
Output:

func RegexCompiles added in v0.3.0

func RegexCompiles(t T, expr string, settings ...Setting)

RegexCompiles asserts expr compiles as a valid regular expression.

Example
Output:

func RegexCompilesPOSIX added in v0.3.0

func RegexCompilesPOSIX(t T, expr string, settings ...Setting)

RegexCompilesPOSIX asserts expr compiles as a valid POSIX regular expression.

Example
Output:

func RegexMatch added in v0.2.5

func RegexMatch(t T, re *regexp.Regexp, s string, settings ...Setting)

RegexMatch asserts regular expression re matches string s.

Example
Output:

func Size added in v0.4.0

func Size(t T, exp int, s interfaces.SizeFunc, settings ...Setting)

Size asserts s.Size() is equal to exp.

Example
Output:

func SliceContains added in v0.4.0

func SliceContains[A any](t T, slice []A, item A, settings ...Setting)

SliceContains asserts item exists in slice, using cmp.Equal to compare elements.

Example
Output:

func SliceContainsAll added in v0.4.0

func SliceContainsAll[A any](t T, slice, items []A, settings ...Setting)

SliceContainsAll asserts slice and items contain the same elements, but in no particular order, using cmp.Equal to compare elements. The number of elements in slice and items must be the same.

Example
Output:

func SliceContainsAllEqual added in v1.12.0

func SliceContainsAllEqual[E interfaces.EqualFunc[E]](t T, slice, items []E, settings ...Setting)

SliceContainsAllEqual asserts slice and items contain the same elements, but in no particular order, using Equal to compare elements. The number of elements in slice and items must be the same.

Example
Output:

func SliceContainsAllFunc added in v1.12.0

func SliceContainsAllFunc[A, B any](t T, slice []A, items []B, eq func(a A, b B) bool, settings ...Setting)

SliceContainsAllFunc asserts slice and items contain the same elements, but in no particular order, using eq to compare elements. The number of elements in slice and items must be the same.

Example
Output:

func SliceContainsAllOp added in v1.12.0

func SliceContainsAllOp[C comparable](t T, slice, items []C, settings ...Setting)

SliceContainsAllOp asserts slice and items contain the same elements, but in no particular order, using the == operator. The number of elements in slice and items must be the same.

Example
Output:

func SliceContainsEqual added in v0.4.0

func SliceContainsEqual[E interfaces.EqualFunc[E]](t T, slice []E, item E, settings ...Setting)

SliceContainsEqual asserts item exists in slice, using Equal to compare elements.

Example
Output:

func SliceContainsFunc added in v0.4.0

func SliceContainsFunc[A, B any](t T, slice []A, item B, eq func(a A, b B) bool, settings ...Setting)

SliceContainsFunc asserts item exists in slice, using eq to compare elements.

Example
Output:

func SliceContainsOp added in v0.4.0

func SliceContainsOp[C comparable](t T, slice []C, item C, settings ...Setting)

SliceContainsOp asserts item exists in slice using == operator.

Example
Output:

func SliceContainsSubset added in v0.4.1

func SliceContainsSubset[A any](t T, slice, items []A, settings ...Setting)

SliceContainsSubset asserts slice contains each item in items, in no particular order, using cmp.Equal to compare elements. There could be additional elements in slice not in items.

Example
Output:

func SliceContainsSubsetEqual added in v1.12.0

func SliceContainsSubsetEqual[E interfaces.EqualFunc[E]](t T, slice, items []E, settings ...Setting)

SliceContainsSubsetEqual asserts slice contains each item in items, in no particular order, using Equal to compare elements. There could be additional elements in slice not in items.

Example
Output:

func SliceContainsSubsetFunc added in v1.12.0

func SliceContainsSubsetFunc[A, B any](t T, slice []A, items []B, eq func(a A, b B) bool, settings ...Setting)

SliceContainsSubsetFunc asserts slice contains each item in items, in no particular order, using eq to compare elements. There could be additional elements in slice not in items.

Example
Output:

func SliceContainsSubsetOp added in v1.12.0

func SliceContainsSubsetOp[C comparable](t T, slice, items []C, settings ...Setting)

SliceContainsSubsetOp asserts slice contains each item in items, in no particular order, using the == operator. There could be additional elements in slice not in items.

Example
Output:

func SliceEmpty added in v0.4.0

func SliceEmpty[A any](t T, slice []A, settings ...Setting)

SliceEmpty asserts slice is empty.

Example
Output:

func SliceEqFunc added in v0.4.0

func SliceEqFunc[A, B any](t T, exp []B, val []A, eq func(expectation A, value B) bool, settings ...Setting)

SliceEqFunc asserts elements of val satisfy eq for the corresponding element in exp.

Example
Output:

func SliceEqOp added in v1.10.0

func SliceEqOp[A comparable, S ~[]A](t T, exp, val S, settings ...Setting)

SliceEqOp asserts exp[n] == val[n] for each element n.

Example
Output:

func SliceEqual added in v0.4.0

func SliceEqual[E interfaces.EqualFunc[E]](t T, exp, val []E, settings ...Setting)

SliceEqual asserts val[n].Equal(exp[n]) for each element n.

Example
Output:

func SliceLen added in v0.4.0

func SliceLen[A any](t T, n int, slice []A, settings ...Setting)

SliceLen asserts slice is of length n.

Example
Output:

func SliceNotContains added in v0.4.0

func SliceNotContains[A any](t T, slice []A, item A, settings ...Setting)

SliceNotContains asserts item does not exist in slice, using cmp.Equal to compare elements.

Example
Output:

func SliceNotContainsFunc added in v0.6.3

func SliceNotContainsFunc[A, B any](t T, slice []A, item B, eq func(a A, b B) bool, settings ...Setting)

SliceNotContainsFunc asserts item does not exist in slice, using eq to compare elements.

Example
Output:

func SliceNotEmpty added in v0.4.0

func SliceNotEmpty[A any](t T, slice []A, settings ...Setting)

SliceNotEmpty asserts slice is not empty.

Example
Output:

func StrContains added in v0.3.0

func StrContains(t T, s, sub string, settings ...Setting)

StrContains asserts s contains substring sub.

Example
Output:

func StrContainsAny added in v0.3.0

func StrContainsAny(t T, s, chars string, settings ...Setting)

StrContainsAny asserts s contains at least one character in chars.

Example
Output:

func StrContainsFields added in v0.3.0

func StrContainsFields(t T, s string, fields []string, settings ...Setting)

StrContainsFields asserts that fields is a subset of the result of strings.Fields(s).

Example
Output:

func StrContainsFold added in v0.3.0

func StrContainsFold(t T, s, sub string, settings ...Setting)

StrContainsFold asserts s contains substring sub, ignoring case.

Example
Output:

func StrCount added in v0.3.0

func StrCount(t T, s, sub string, count int, settings ...Setting)

StrCount asserts s contains exactly count instances of substring sub.

Example
Output:

func StrEqFold added in v0.3.0

func StrEqFold(t T, exp, val string, settings ...Setting)

StrEqFold asserts exp and val are equivalent, ignoring case.

Example
Output:

func StrHasPrefix added in v0.3.0

func StrHasPrefix(t T, prefix, s string, settings ...Setting)

StrHasPrefix asserts that s starts with prefix.

Example
Output:

func StrHasSuffix added in v0.3.0

func StrHasSuffix(t T, suffix, s string, settings ...Setting)

StrHasSuffix asserts that s ends with suffix.

Example
Output:

func StrNotContains added in v0.3.0

func StrNotContains(t T, s, sub string, settings ...Setting)

StrNotContains asserts s does not contain substring sub.

Example
Output:

func StrNotContainsAny added in v0.3.0

func StrNotContainsAny(t T, s, chars string, settings ...Setting)

StrNotContainsAny asserts s does not contain any character in chars.

Example
Output:

func StrNotContainsFold added in v0.3.0

func StrNotContainsFold(t T, s, sub string, settings ...Setting)

StrNotContainsFold asserts s does not contain substring sub, ignoring case.

Example
Output:

func StrNotEqFold added in v0.3.0

func StrNotEqFold(t T, exp, val string, settings ...Setting)

StrNotEqFold asserts exp and val are not equivalent, ignoring case.

Example
Output:

func StrNotHasPrefix added in v0.3.0

func StrNotHasPrefix(t T, prefix, s string, settings ...Setting)

StrNotHasPrefix asserts that s does not start with prefix.

Example
Output:

func StrNotHasSuffix added in v0.3.0

func StrNotHasSuffix(t T, suffix, s string, settings ...Setting)

StrNotHasSuffix asserts that s does not end with suffix.

func StructEqual added in v0.6.2

func StructEqual[E interfaces.CopyEqual[E]](t T, original E, tweaks Tweaks[E], settings ...Setting)

StructEqual will apply each Tweak and assert E.Equal captures the modification.

Example
Output:

func True

func True(t T, condition bool, settings ...Setting)

True asserts that condition is true.

Example
Output:

func UUIDv4 added in v0.3.1

func UUIDv4(t T, id string, settings ...Setting)

UUIDv4 asserts id meets the criteria of a v4 UUID.

Example
Output:

func Unreachable added in v0.2.8

func Unreachable(t T, settings ...Setting)

Unreachable asserts a code path is not executed.

Example
Output:

func ValidJSON added in v0.6.1

func ValidJSON(t T, js string, settings ...Setting)

ValidJSON asserts js is valid JSON.

Example
Output:

func ValidJSONBytes added in v0.6.1

func ValidJSONBytes(t T, js []byte, settings ...Setting)

ValidJSONBytes asserts js is valid JSON.

Example
Output:

func Wait added in v0.5.0

func Wait(t T, wc *wait.Constraint, settings ...Setting)

Wait asserts wc.

Example (Continual_success)
Output:

Example (Initial_success)
Output:

func Zero added in v0.2.5

func Zero[N interfaces.Number](t T, n N, settings ...Setting)

Zero asserts n == 0.

Example
Output:

Types

type ErrorAssertionFunc added in v1.8.1

type ErrorAssertionFunc func(t T, err error, settings ...Setting)

ErrorAssertionFunc allows passing Error and NoError in table driven tests

type PostScript added in v0.3.0

type PostScript interface {
	// Label should categorize what is in Content.
	Label() string

	// Content contains extra contextual information for debugging a test failure.
	Content() string
}

A PostScript is used to annotate a test failure with additional information.

Can be useful in large e2e style test cases, where adding additional context beyond an assertion helps in debugging.

type Setting added in v0.6.0

type Setting func(s *Settings)

A Setting changes the behavior of a test case assertion.

func Cmp added in v0.6.0

func Cmp(options ...cmp.Option) Setting

Cmp enables configuring cmp.Option values for modifying the behavior of the cmp.Equal function. Custom cmp.Option values control how the cmp.Equal function determines equality between the two objects.

https://github.com/google/go-cmp/blob/master/cmp/options.go#L16

func Func added in v0.3.0

func Func(f func() string) Setting

Func adds the string produced by f as an annotation to the output of a test case failure.

func Sprint added in v0.3.0

func Sprint(args ...any) Setting

Sprint appends a Sprint-string as an annotation to the output of a test case failure.

func Sprintf added in v0.3.0

func Sprintf(msg string, args ...any) Setting

Sprintf appends a Sprintf-string as an annotation to the output of a test case failure.

func Values added in v0.3.0

func Values(vals ...any) Setting

Values adds formatted key-val mappings as an annotation to the output of a test case failure.

type Settings added in v0.6.0

type Settings struct {
	// contains filtered or unexported fields
}

Settings are used to manage a collection of Setting values used to modify the behavior of a test case assertion. Currently supports specifying custom error output content, and custom cmp.Option comparators / transforms.

Use Cmp for specifying custom cmp.Option values.

Use Sprint, Sprintf, Values, Func for specifying custom failure output messages.

type T

type T interface {
	Helper()
	Fatalf(string, ...any)
}

T is the minimal set of functions to be implemented by any testing framework compatible with the must package.

type Tweak added in v0.6.2

type Tweak[E interfaces.CopyEqual[E]] struct {
	Field string
	Apply interfaces.TweakFunc[E]
}

Tweak is used to modify a struct and assert its Equal method captures the modification.

Field is the name of the struct field and is used only for error printing. Apply is a function that modifies E.

type Tweaks added in v0.6.3

type Tweaks[E interfaces.CopyEqual[E]] []Tweak[E]

Tweaks is a slice of Tweak.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL