Documentation
¶
Overview ¶
Package observable provides lightweight, zero-dependency helpers for writing expressive assertions in Go tests. It embraces Go's standard testing package by integrating with testing.TB and keeps the API minimal while still supporting generic, user-defined predicates.
Index ¶
- func Assert(tb testing.TB, p Predicate) bool
- func Assertf(tb testing.TB, p Predicate, format string, args ...any) bool
- func Not[T any](a T) T
- type Predicate
- func All(ps ...Predicate) Predicate
- func Any(ps ...Predicate) Predicate
- func ChanLength[T any](c chan T, want int) Predicate
- func Contains[T comparable](slice []T, elem T) Predicate
- func ContainsKey[K comparable, V any](m map[K]V, key K) Predicate
- func ContainsSubstring(s, substr string) Predicate
- func ContainsValue[K comparable, V comparable](m map[K]V, val V) Predicate
- func ElementsMatch[T comparable](got, want []T) Predicate
- func Empty[T any](s []T) Predicate
- func EmptyString(s string) Predicate
- func Equal[T comparable](got, want T) Predicate
- func EqualFold(got, want string) Predicate
- func ErrorIs(err, target error) Predicate
- func Errors(f func() error) Predicate
- func ErrorsWith(f func() error, target error) Predicate
- func False() Predicate
- func HasPrefix(s, prefix string) Predicate
- func HasSuffix(s, suffix string) Predicate
- func Length[T any](s []T, want int) Predicate
- func MapEqual[K comparable, V any](got, want map[K]V) Predicate
- func MapLength[K comparable, V any](m map[K]V, want int) Predicate
- func Nil(v any) Predicate
- func Panics(f func()) Predicate
- func RegexpMatches[T string | *regexp.Regexp](s string, reOrString T) Predicate
- func Returns[T comparable](f func() T, want T) Predicate
- func RuneLength(s string, want int) Predicate
- func SequenceDeepEqual[T any](got, want []T) Predicate
- func SequenceEqual[T comparable](got, want []T) Predicate
- func StringLength(s string, want int) Predicate
- func That[T ~bool | ~func() bool](x T) Predicate
- func True() Predicate
- func Zero[T comparable](v T) Predicate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
Assert evaluates the predicate and records an error on the testing.TB when the predicate is false.
The returned bool is the evaluation result, which allows further composition or chaining inside a test when desired.
func Assertf ¶
Assertf behaves like Assert but lets the caller supply an explicit failure message via format and args, similar to fmt.Sprintf.
func Not ¶
func Not[T any](a T) T
Not returns the logical negation of its argument.
You can negate a: - Predicate, resulting in a Predicate - A function of any arity that returns a Predicate
Negating a function of positive arity will use runtime reflection.
Calling Not with anything else will **panic**!
Types ¶
type Predicate ¶
type Predicate struct {
// contains filtered or unexported fields
}
Predicate encapsulates a lazily‑evaluated boolean condition together with a descriptive failure message.
Generally, users will construct predicates with the helper functions in this package (e.g. Nil, Equal, Panics).The zero value is NOT valid.
func All ¶ added in v0.5.0
All returns a Predicate that is ok when all of the supplied predicates are ok.
func Any ¶ added in v0.5.0
Any returns a Predicate that is ok when any of the supplied predicates are ok.
func ChanLength ¶ added in v0.3.0
ChanLength returns a Predicate that is ok when len(c) == want (buffered channels only).
func Contains ¶ added in v0.3.0
func Contains[T comparable](slice []T, elem T) Predicate
Contains returns a Predicate that is ok when elem is present in slice.
func ContainsKey ¶ added in v0.3.0
func ContainsKey[K comparable, V any](m map[K]V, key K) Predicate
ContainsKey returns a Predicate that is ok when key exists in map m.
func ContainsSubstring ¶ added in v0.3.0
ContainsSubstring returns a Predicate that succeeds when strings.Contains(s, substr).
func ContainsValue ¶ added in v0.3.0
func ContainsValue[K comparable, V comparable](m map[K]V, val V) Predicate
ContainsValue returns a Predicate that is ok when val appears as a value in map m.
func ElementsMatch ¶ added in v0.3.0
func ElementsMatch[T comparable](got, want []T) Predicate
ElementsMatch returns a Predicate that is ok when the two slices contain the same multiset of elements, irrespective of order.
func EmptyString ¶ added in v0.3.0
EmptyString returns a Predicate that succeeds when the string is "".
func Equal ¶
func Equal[T comparable](got, want T) Predicate
Equal returns a Predicate that is ok when got == want.
func EqualFold ¶ added in v0.3.0
EqualFold returns a Predicate that succeeds when strings.EqualFold(got, want) (case-insensitive).
func ErrorsWith ¶
ErrorsWith returns a Predicate that is ok when f returns an error that matches target according to errors.Is.
func False ¶ added in v0.5.0
func False() Predicate
False returns a Predicate that always is not ok.
func HasPrefix ¶ added in v0.3.0
HasPrefix returns a Predicate that succeeds when strings.HasPrefix(s, prefix).
func HasSuffix ¶ added in v0.3.0
HasSuffix returns a Predicate that succeeds when strings.HasSuffix(s, suffix).
func MapEqual ¶ added in v0.3.0
func MapEqual[K comparable, V any](got, want map[K]V) Predicate
MapEqual returns a Predicate that is ok when two maps are deeply equal (reflect.DeepEqual).
func MapLength ¶ added in v0.3.0
func MapLength[K comparable, V any](m map[K]V, want int) Predicate
MapLength returns a Predicate that is ok when len(m) == want.
func RegexpMatches ¶ added in v0.3.0
RegexpMatches returns a Predicate that succeeds when the regular expression re matches s. The regular expression can either be a *regexp.Regexp or a string which will be compiled with regexp.MustCompile.
func Returns ¶
func Returns[T comparable](f func() T, want T) Predicate
Returns returns a Predicate that is ok when f's return value equals want.
func RuneLength ¶ added in v0.3.0
RuneLength returns a Predicate that succeeds when utf8.RuneCountInString(s) == want.
func SequenceDeepEqual ¶ added in v0.3.0
SequenceDeepEqual returns a Predicate that is ok when want and got reflect.DeepEqual each other. Allows comparing slices with non-comparable element types.
func SequenceEqual ¶ added in v0.3.0
func SequenceEqual[T comparable](got, want []T) Predicate
SequenceEqual returns a Predicate that is ok when got and want have identical length and elements appear in the same order.
func StringLength ¶ added in v0.3.0
StringLength returns a Predicate that succeeds when len(s) == want.
func Zero ¶
func Zero[T comparable](v T) Predicate
Zero returns a Predicate that is ok when v is the zero value of its type.