Documentation
¶
Overview ¶
Package testassert provides fluent, type-safe test assertions for Go.
It offers a chainable API built with generics for asserting values in tests. Each assertion type is constructed with a That* function and exposes domain-specific methods that call t.Errorf on failure with readable messages.
Index ¶
- func NotPanics(t testing.TB, fn func())
- func Panics(t testing.TB, fn func())
- type Assertion
- type ErrorAssertion
- type JSONAssertion
- type MapAssertion
- type Numeric
- type NumericAssertion
- type OrderedAssertion
- func (a *OrderedAssertion[T]) Equals(want T) *OrderedAssertion[T]
- func (a *OrderedAssertion[T]) IsGreaterOrEqual(v T) *OrderedAssertion[T]
- func (a *OrderedAssertion[T]) IsGreaterThan(v T) *OrderedAssertion[T]
- func (a *OrderedAssertion[T]) IsLessOrEqual(v T) *OrderedAssertion[T]
- func (a *OrderedAssertion[T]) IsLessThan(v T) *OrderedAssertion[T]
- func (a *OrderedAssertion[T]) NotEquals(want T) *OrderedAssertion[T]
- type SliceAssertion
- type StringAssertion
- func (a *StringAssertion) Contains(substr string) *StringAssertion
- func (a *StringAssertion) Equals(want string) *StringAssertion
- func (a *StringAssertion) HasLen(n int) *StringAssertion
- func (a *StringAssertion) HasPrefix(prefix string) *StringAssertion
- func (a *StringAssertion) HasSuffix(suffix string) *StringAssertion
- func (a *StringAssertion) IsEmpty() *StringAssertion
- func (a *StringAssertion) Matches(pattern string) *StringAssertion
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Assertion ¶
type Assertion[T any] struct { // contains filtered or unexported fields }
Assertion provides fluent assertions for any type using reflect.DeepEqual.
func (*Assertion[T]) IsNil ¶
IsNil asserts that the value is nil. It handles both typed and untyped nils using reflection.
func (*Assertion[T]) WithMessage ¶ added in v0.2.0
WithMessage sets a custom failure message prefix for this assertion chain.
type ErrorAssertion ¶
type ErrorAssertion struct {
// contains filtered or unexported fields
}
ErrorAssertion provides fluent assertions for error values.
func ThatError ¶
func ThatError(t testing.TB, got error) *ErrorAssertion
ThatError creates a new ErrorAssertion for an error value.
func (*ErrorAssertion) As ¶
func (a *ErrorAssertion) As(target any) *ErrorAssertion
As asserts that errors.As(got, target) returns true. The target must be a pointer to the desired error type.
func (*ErrorAssertion) Contains ¶
func (a *ErrorAssertion) Contains(msg string) *ErrorAssertion
Contains asserts that the error message contains msg.
func (*ErrorAssertion) Is ¶
func (a *ErrorAssertion) Is(target error) *ErrorAssertion
Is asserts that errors.Is(got, target) returns true.
func (*ErrorAssertion) IsNil ¶
func (a *ErrorAssertion) IsNil() *ErrorAssertion
IsNil asserts that the error is nil.
func (*ErrorAssertion) IsNotNil ¶
func (a *ErrorAssertion) IsNotNil() *ErrorAssertion
IsNotNil asserts that the error is not nil.
type JSONAssertion ¶
type JSONAssertion struct {
// contains filtered or unexported fields
}
JSONAssertion provides fluent assertions for JSON strings.
func ThatJSON ¶
func ThatJSON(t testing.TB, got string) *JSONAssertion
ThatJSON creates a new JSONAssertion for a JSON string.
func (*JSONAssertion) Contains ¶
func (a *JSONAssertion) Contains(key string, value any) *JSONAssertion
Contains asserts that the top-level JSON object contains the given key with the given value. The value is compared after JSON unmarshalling for type consistency.
func (*JSONAssertion) Equals ¶
func (a *JSONAssertion) Equals(want string) *JSONAssertion
Equals asserts that the JSON is semantically equal to want, ignoring formatting. Both strings are unmarshalled and compared with reflect.DeepEqual.
func (*JSONAssertion) HasKey ¶
func (a *JSONAssertion) HasKey(key string) *JSONAssertion
HasKey asserts that the top-level JSON object contains the given key.
type MapAssertion ¶ added in v0.2.0
type MapAssertion[K comparable, V any] struct { // contains filtered or unexported fields }
MapAssertion provides fluent assertions for map values.
func ThatMap ¶ added in v0.2.0
func ThatMap[K comparable, V any](t testing.TB, got map[K]V) *MapAssertion[K, V]
ThatMap creates a new MapAssertion for a map value.
func (*MapAssertion[K, V]) HasKey ¶ added in v0.2.0
func (a *MapAssertion[K, V]) HasKey(key K) *MapAssertion[K, V]
HasKey asserts that the map contains the given key.
func (*MapAssertion[K, V]) HasLen ¶ added in v0.2.0
func (a *MapAssertion[K, V]) HasLen(n int) *MapAssertion[K, V]
HasLen asserts that the map has length n.
func (*MapAssertion[K, V]) IsEmpty ¶ added in v0.2.0
func (a *MapAssertion[K, V]) IsEmpty() *MapAssertion[K, V]
IsEmpty asserts that the map is empty.
func (*MapAssertion[K, V]) IsNotEmpty ¶ added in v0.2.0
func (a *MapAssertion[K, V]) IsNotEmpty() *MapAssertion[K, V]
IsNotEmpty asserts that the map is not empty.
type Numeric ¶ added in v0.2.0
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64
}
Numeric is a constraint for numeric types that support arithmetic operations.
type NumericAssertion ¶ added in v0.2.0
type NumericAssertion[T Numeric] struct { // contains filtered or unexported fields }
NumericAssertion provides fluent assertions for numeric types that support arithmetic operations like tolerance checks.
func ThatNumeric ¶ added in v0.2.0
func ThatNumeric[T Numeric](t testing.TB, got T) *NumericAssertion[T]
ThatNumeric creates a new NumericAssertion for a numeric value. It provides all ordered comparison methods plus arithmetic-based assertions.
func (*NumericAssertion[T]) Within ¶ added in v0.2.0
func (a *NumericAssertion[T]) Within(expected T, tolerance T) *NumericAssertion[T]
Within asserts that got is within tolerance of expected. The check passes when expected - tolerance <= got <= expected + tolerance.
type OrderedAssertion ¶
OrderedAssertion provides fluent assertions for ordered types that support comparison operators.
func ThatOrdered ¶
func ThatOrdered[T cmp.Ordered](t testing.TB, got T) *OrderedAssertion[T]
ThatOrdered creates a new OrderedAssertion for a value with an ordered type.
func (*OrderedAssertion[T]) Equals ¶
func (a *OrderedAssertion[T]) Equals(want T) *OrderedAssertion[T]
Equals asserts that got equals want.
func (*OrderedAssertion[T]) IsGreaterOrEqual ¶
func (a *OrderedAssertion[T]) IsGreaterOrEqual(v T) *OrderedAssertion[T]
IsGreaterOrEqual asserts that got >= v.
func (*OrderedAssertion[T]) IsGreaterThan ¶
func (a *OrderedAssertion[T]) IsGreaterThan(v T) *OrderedAssertion[T]
IsGreaterThan asserts that got > v.
func (*OrderedAssertion[T]) IsLessOrEqual ¶
func (a *OrderedAssertion[T]) IsLessOrEqual(v T) *OrderedAssertion[T]
IsLessOrEqual asserts that got <= v.
func (*OrderedAssertion[T]) IsLessThan ¶
func (a *OrderedAssertion[T]) IsLessThan(v T) *OrderedAssertion[T]
IsLessThan asserts that got < v.
func (*OrderedAssertion[T]) NotEquals ¶
func (a *OrderedAssertion[T]) NotEquals(want T) *OrderedAssertion[T]
NotEquals asserts that got does not equal want.
type SliceAssertion ¶
type SliceAssertion[T any] struct { // contains filtered or unexported fields }
SliceAssertion provides fluent assertions for slice values.
func ThatSlice ¶
func ThatSlice[T any](t testing.TB, got []T) *SliceAssertion[T]
ThatSlice creates a new SliceAssertion for a slice value.
func (*SliceAssertion[T]) Contains ¶
func (a *SliceAssertion[T]) Contains(elem T) *SliceAssertion[T]
Contains asserts that the slice contains elem using reflect.DeepEqual.
func (*SliceAssertion[T]) HasLen ¶
func (a *SliceAssertion[T]) HasLen(n int) *SliceAssertion[T]
HasLen asserts that the slice has length n.
func (*SliceAssertion[T]) IsEmpty ¶
func (a *SliceAssertion[T]) IsEmpty() *SliceAssertion[T]
IsEmpty asserts that the slice is empty.
func (*SliceAssertion[T]) IsNotEmpty ¶
func (a *SliceAssertion[T]) IsNotEmpty() *SliceAssertion[T]
IsNotEmpty asserts that the slice is not empty.
type StringAssertion ¶
type StringAssertion struct {
// contains filtered or unexported fields
}
StringAssertion provides fluent assertions for string values.
func ThatString ¶
func ThatString(t testing.TB, got string) *StringAssertion
ThatString creates a new StringAssertion for a string value.
func (*StringAssertion) Contains ¶
func (a *StringAssertion) Contains(substr string) *StringAssertion
Contains asserts that the string contains substr.
func (*StringAssertion) Equals ¶
func (a *StringAssertion) Equals(want string) *StringAssertion
Equals asserts that the string equals want.
func (*StringAssertion) HasLen ¶
func (a *StringAssertion) HasLen(n int) *StringAssertion
HasLen asserts that the string has length n.
func (*StringAssertion) HasPrefix ¶
func (a *StringAssertion) HasPrefix(prefix string) *StringAssertion
HasPrefix asserts that the string starts with prefix.
func (*StringAssertion) HasSuffix ¶
func (a *StringAssertion) HasSuffix(suffix string) *StringAssertion
HasSuffix asserts that the string ends with suffix.
func (*StringAssertion) IsEmpty ¶
func (a *StringAssertion) IsEmpty() *StringAssertion
IsEmpty asserts that the string is empty.
func (*StringAssertion) Matches ¶
func (a *StringAssertion) Matches(pattern string) *StringAssertion
Matches asserts that the string matches the regular expression pattern.