Documentation
¶
Overview ¶
Package testx provides a comprehensive set of generic matchers and assertion utilities for Go testing.
It is designed to be highly extensible, type-safe, and easy to use alongside the standard `testing` package.
Core Assertions ¶
The package provides two primary assertion functions:
- `Expect`: Asserts that an actual value satisfies one or more matchers.
- `ExpectPanic`: Asserts that a function panics, and optionally verifies the panic value against matchers.
Example:
func TestMath(t *testing.T) {
result := 1 + 1
testx.Expect(t, result, testx.Equal(2), testx.BeGt(0))
testx.ExpectPanic(t, func() {
panic("something went wrong")
}, testx.Equal("something went wrong"))
}
Matchers ¶
Matchers are the building blocks of assertions. A `Matcher[T]` evaluates an actual value of type `T` and returns whether it meets the expected criteria. The package includes a wide variety of built-in matchers:
- Equality & Comparison: `Equal`, `Be`, `BeGt`, `BeLt`, `IsZero`, etc.
- Collections: `HaveLen`, `HaveCap`, `HaveKey`, `Contains`, `EquivalentSlice`, etc.
- Strings: `HavePrefix`, `HaveSuffix`, `ContainsSubString`, `MatchRegexp`.
- Types & Interfaces: `BeAssignableTo`, `BeConvertibleTo`, `IsType`.
- Errors: `IsError`, `AsError`, `ErrorEqual`, `Succeed`, `Failed`, etc.
Custom Matchers ¶
You can easily create custom matchers using `NewMatcher` or `NewComparedMatcher`:
func BeEven() testx.Matcher[int] {
return testx.NewMatcher("BeEven", func(actual int) bool {
return actual%2 == 0
})
}
BDD Integration ¶
For a more expressive, Behavior-Driven Development style, consider using the `github.com/xoctopus/x/testx/bdd` sub-package, which builds upon the matchers defined here.
Index ¶
- func Expect[A any](t testing.TB, actual A, matchers ...Matcher[A])
- func ExpectPanic[A any](t testing.TB, f func(), matchers ...Matcher[A])
- func NewComparedMatcher[Actual any, Expect any](name string, matcher ComparableMatchFunc[Actual, Expect]) internal.MatcherNewer[Actual, Expect]
- type ComparableMatchFunc
- type MatchFunc
- type Matcher
- func AsError[T any](expect *T) Matcher[error]
- func AsErrorType[T error]() Matcher[error]
- func Be[T any](expect T) Matcher[T]
- func BeAssignableTo[T any]() Matcher[any]
- func BeConvertibleTo[T any]() Matcher[any]
- func BeFalse() Matcher[bool]
- func BeGt[T cmp.Ordered](expect T) Matcher[T]
- func BeGte[T cmp.Ordered](expect T) Matcher[T]
- func BeLt[T cmp.Ordered](expect T) Matcher[T]
- func BeLte[T cmp.Ordered](expect T) Matcher[T]
- func BeNil[Actual any]() Matcher[Actual]
- func BeTrue() Matcher[bool]
- func ConsistOfSlice[E comparable, S ~[]E](expect S) Matcher[S]
- func Contains[E comparable, S ~[]E](v E) Matcher[S]
- func ContainsSubString(sub string) Matcher[string]
- func Equal[T any](expect T) Matcher[T]
- func EquivalentSlice[E comparable, S ~[]E](expect S) Matcher[S]
- func ErrorContains(sub string) Matcher[error]
- func ErrorEqual(expect string) Matcher[error]
- func Failed() Matcher[error]
- func HaveCap[T any](cap int) Matcher[T]
- func HaveKey[K comparable, V any, M ~map[K]V](key K) Matcher[M]
- func HaveLen[T any](len int) Matcher[T]
- func HavePrefix(prefix string) Matcher[string]
- func HaveSuffix(suffix string) Matcher[string]
- func IsCodeError[Code codex.Code](expect Code) Matcher[error]
- func IsError(expect error) Matcher[error]
- func IsNotZero[Actual any]() Matcher[Actual]
- func IsType[T any]() Matcher[any]
- func IsZero[Actual any]() Matcher[Actual]
- func MatchRegexp(pattern string) Matcher[string]
- func NewMatcher[Actual any](name string, matcher MatchFunc[Actual]) Matcher[Actual]
- func Not[Actual any](matcher Matcher[Actual]) Matcher[Actual]
- func NotBe[T any](expect T) Matcher[T]
- func NotBeNil[Actual any]() Matcher[Actual]
- func NotEqual[T any](expect T) Matcher[T]
- func Succeed() Matcher[error]
- type NormalizedExpectedMatcher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Expect ¶ added in v0.2.0
Expect asserts that the actual value satisfies all the provided matchers. It fails the test if any matcher returns false.
func ExpectPanic ¶ added in v0.2.0
ExpectPanic asserts that the provided function f panics with a value of type A, and optionally asserts that the recovered panic value satisfies the provided matchers.
Example ¶
package main
import (
"fmt"
. "github.com/xoctopus/x/testx"
"github.com/xoctopus/x/testx/testutil"
)
type MockCodeErr int
func (MockCodeErr) Message() string { return "mock" }
func crash(i int) {
switch i {
case 0:
return
case 1:
panic(fmt.Errorf("any"))
}
}
func main() {
t := &testutil.MockTB{}
t.Reset()
ExpectPanic[error](t, func() {
panic("any")
})
fmt.Println(t.Output2())
t.Reset()
ExpectPanic[string](t, func() {
crash(1)
})
fmt.Println(t.Output2())
t.Reset()
ExpectPanic[string](t, func() {})
fmt.Println(t.Output2())
}
Output: expect a panic of `error`, but got string expect a panic of `string`, but got *errors.errorString expect a panic of `string`, but f returned normally
func NewComparedMatcher ¶ added in v0.2.0
func NewComparedMatcher[Actual any, Expect any](name string, matcher ComparableMatchFunc[Actual, Expect]) internal.MatcherNewer[Actual, Expect]
NewComparedMatcher creates a matcher constructor that binds an expected value to a ComparableMatchFunc.
Types ¶
type ComparableMatchFunc ¶ added in v0.5.0
ComparableMatchFunc defines a function that compares an actual value against an expected value.
type MatchFunc ¶ added in v0.5.0
MatchFunc defines a function that evaluates an actual value and returns a boolean indicating success.
type Matcher ¶ added in v0.2.0
Matcher is an alias for internal.Matcher.
func AsError ¶ added in v0.5.0
AsError creates a matcher that asserts the actual error can be assigned to the expected error target (using errors.As).
func AsErrorType ¶ added in v0.5.0
AsErrorType creates a matcher that asserts the actual error can be assigned to a specific error type T.
func Be ¶ added in v0.2.0
Be creates a matcher that asserts the actual value is strictly identical to the expected value.
func BeAssignableTo ¶ added in v0.2.0
BeAssignableTo creates a matcher that asserts the actual value is assignable to the type T.
func BeConvertibleTo ¶ added in v0.2.0
BeConvertibleTo creates a matcher that asserts the actual value is convertible to the type T.
func BeFalse ¶ added in v0.2.0
BeFalse creates a matcher that asserts the actual boolean value is false.
func BeGt ¶ added in v0.5.0
BeGt creates a matcher that asserts the actual value is greater than the expected value.
func BeGte ¶ added in v0.5.0
BeGte creates a matcher that asserts the actual value is greater than or equal to the expected value.
func BeLt ¶ added in v0.5.0
BeLt creates a matcher that asserts the actual value is less than the expected value.
func BeLte ¶ added in v0.5.0
BeLte creates a matcher that asserts the actual value is less than or equal to the expected value.
func BeTrue ¶ added in v0.2.0
BeTrue creates a matcher that asserts the actual boolean value is true.
func ConsistOfSlice ¶ added in v0.5.0
func ConsistOfSlice[E comparable, S ~[]E](expect S) Matcher[S]
ConsistOfSlice creates a matcher that asserts the actual slice has exactly the same elements in the same order as the expected slice.
func Contains ¶ added in v0.2.0
func Contains[E comparable, S ~[]E](v E) Matcher[S]
Contains creates a matcher that asserts the actual slice contains the expected element.
func ContainsSubString ¶ added in v0.2.0
ContainsSubString creates a matcher that asserts the actual string contains the expected substring.
func Equal ¶ added in v0.2.0
Equal creates a matcher that asserts the actual value is deeply equal to the expected value.
func EquivalentSlice ¶ added in v0.2.1
func EquivalentSlice[E comparable, S ~[]E](expect S) Matcher[S]
EquivalentSlice creates a matcher that asserts the actual slice has the same elements as the expected slice, regardless of order.
func ErrorContains ¶ added in v0.2.0
ErrorContains creates a matcher that asserts the actual error's message contains the expected substring.
func ErrorEqual ¶ added in v0.2.0
ErrorEqual creates a matcher that asserts the actual error's message exactly matches the expected string.
func HaveCap ¶ added in v0.2.0
HaveCap creates a matcher that asserts the actual collection has the expected capacity.
func HaveKey ¶ added in v0.5.0
func HaveKey[K comparable, V any, M ~map[K]V](key K) Matcher[M]
HaveKey creates a matcher that asserts the actual map contains the expected key.
func HaveLen ¶ added in v0.2.0
HaveLen creates a matcher that asserts the actual collection has the expected length.
func HavePrefix ¶ added in v0.2.0
HavePrefix creates a matcher that asserts the actual string starts with the expected prefix.
func HaveSuffix ¶ added in v0.2.0
HaveSuffix creates a matcher that asserts the actual string ends with the expected suffix.
func IsCodeError ¶ added in v0.2.5
IsCodeError creates a matcher that asserts the actual error implements codex.Error and matches the expected code.
func IsError ¶ added in v0.2.0
IsError creates a matcher that asserts the actual error is the expected error (using errors.Is).
func IsNotZero ¶ added in v0.2.12
IsNotZero creates a matcher that asserts the actual value is not the zero value for its type.
func IsType ¶ added in v0.5.0
IsType creates a matcher that asserts the actual value is exactly of the type T.
func IsZero ¶ added in v0.2.12
IsZero creates a matcher that asserts the actual value is the zero value for its type.
func MatchRegexp ¶ added in v0.5.0
MatchRegexp creates a matcher that asserts the actual string matches the expected regular expression pattern.
func NewMatcher ¶ added in v0.2.0
NewMatcher creates a new Matcher with the given name and matching function.
func NotBe ¶ added in v0.2.0
NotBe creates a matcher that asserts the actual value is not strictly identical to the expected value.
func NotBeNil ¶ added in v0.2.0
NotBeNil creates a matcher that asserts the actual value is not nil.
type NormalizedExpectedMatcher ¶ added in v0.2.0
type NormalizedExpectedMatcher = internal.NormalizedExpectedMatcher
NormalizedExpectedMatcher is an alias for internal.NormalizedExpectedMatcher.