testx

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 12 Imported by: 5

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expect added in v0.2.0

func Expect[A any](t testing.TB, actual A, matchers ...Matcher[A])

func ExpectPanic added in v0.2.0

func ExpectPanic[A any](t testing.TB, f func(), matchers ...Matcher[A])
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]

Types

type ComparableMatchFunc added in v0.5.0

type ComparableMatchFunc[Actual any, Expect any] func(Actual, Expect) bool

type MatchFunc added in v0.5.0

type MatchFunc[Actual any] func(Actual) bool

type Matcher added in v0.2.0

type Matcher[Actual any] = internal.Matcher[Actual]

func AsError added in v0.5.0

func AsError[T any](expect *T) Matcher[error]

func AsErrorType added in v0.5.0

func AsErrorType[T error]() Matcher[error]

func Be added in v0.2.0

func Be[T any](expect T) Matcher[T]

func BeAssignableTo added in v0.2.0

func BeAssignableTo[T any]() Matcher[any]

func BeConvertibleTo added in v0.2.0

func BeConvertibleTo[T any]() Matcher[any]

func BeFalse added in v0.2.0

func BeFalse() Matcher[bool]

func BeGt added in v0.5.0

func BeGt[T cmp.Ordered](expect T) Matcher[T]

func BeGte added in v0.5.0

func BeGte[T cmp.Ordered](expect T) Matcher[T]

func BeLt added in v0.5.0

func BeLt[T cmp.Ordered](expect T) Matcher[T]

func BeLte added in v0.5.0

func BeLte[T cmp.Ordered](expect T) Matcher[T]

func BeNil added in v0.2.0

func BeNil[Actual any]() Matcher[Actual]

func BeTrue added in v0.2.0

func BeTrue() Matcher[bool]

func ConsistOfSlice added in v0.5.0

func ConsistOfSlice[E comparable, S ~[]E](expect S) Matcher[S]

func Contains added in v0.2.0

func Contains[E comparable, S ~[]E](v E) Matcher[S]

func ContainsSubString added in v0.2.0

func ContainsSubString(sub string) Matcher[string]

func Equal added in v0.2.0

func Equal[T any](expect T) Matcher[T]

func EquivalentSlice added in v0.2.1

func EquivalentSlice[E comparable, S ~[]E](expect S) Matcher[S]

func ErrorContains added in v0.2.0

func ErrorContains(sub string) Matcher[error]

func ErrorEqual added in v0.2.0

func ErrorEqual(expect string) Matcher[error]

func Failed added in v0.2.0

func Failed() Matcher[error]

func HaveCap added in v0.2.0

func HaveCap[T any](cap int) Matcher[T]

func HaveKey added in v0.5.0

func HaveKey[K comparable, V any, M ~map[K]V](key K) Matcher[M]

func HaveLen added in v0.2.0

func HaveLen[T any](len int) Matcher[T]

func HavePrefix added in v0.2.0

func HavePrefix(prefix string) Matcher[string]

func HaveSuffix added in v0.2.0

func HaveSuffix(suffix string) Matcher[string]

func IsCodeError added in v0.2.5

func IsCodeError[Code codex.Code](expect Code) Matcher[error]

func IsError added in v0.2.0

func IsError(expect error) Matcher[error]

func IsNotZero added in v0.2.12

func IsNotZero[Actual any]() Matcher[Actual]

func IsType added in v0.5.0

func IsType[T any]() Matcher[any]

func IsZero added in v0.2.12

func IsZero[Actual any]() Matcher[Actual]

func MatchRegexp added in v0.5.0

func MatchRegexp(pattern string) Matcher[string]

func NewMatcher added in v0.2.0

func NewMatcher[Actual any](name string, matcher MatchFunc[Actual]) Matcher[Actual]

func Not added in v0.2.0

func Not[Actual any](matcher Matcher[Actual]) Matcher[Actual]

func NotBe added in v0.2.0

func NotBe[T any](expect T) Matcher[T]

func NotBeNil added in v0.2.0

func NotBeNil[Actual any]() Matcher[Actual]

func NotEqual added in v0.2.0

func NotEqual[T any](expect T) Matcher[T]

func Succeed added in v0.2.0

func Succeed() Matcher[error]

type NormalizedExpectedMatcher added in v0.2.0

type NormalizedExpectedMatcher = internal.NormalizedExpectedMatcher

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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