testutils

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert added in v1.2.0

func Assert(tt TestingIface, condition bool, msgAndArgs ...any) bool

Assert checks that the specified condition is true, failing the test if false.

Example:

testutils.Assert(t, x > 0, "x should be positive, got %d", x)

func Contains added in v1.1.1

func Contains[Container Containable[Item], Item comparable](
	tt TestingIface,
	container Container,
	item Item,
	msgAndArgs ...any,
) bool

Contains is the unified generic method that dispatches to the correct implementation

func ContainsExact added in v1.1.1

func ContainsExact[Container SliceOrMap[Item], Item comparable](
	tt TestingIface,
	container Container,
	item Item,
	msgAndArgs ...any,
) bool

ContainsExact for exact matching (slices and map keys)

func ContainsFunc added in v1.1.1

func ContainsFunc[T any](tt TestingIface, container []T, predicate func(T) bool, msgAndArgs ...any) bool

ContainsFunc provides functional matching for slices (can't be unified easily)

func ContainsKey added in v1.1.1

func ContainsKey[K comparable, V any](tt TestingIface, m map[K]V, key K, msgAndArgs ...any) bool

func ContainsSlice added in v1.1.1

func ContainsSlice[T comparable](tt TestingIface, slice []T, item T, msgAndArgs ...any) bool

More specific versions are still available if needed for clarity

func ContainsString added in v1.1.1

func ContainsString(tt TestingIface, str, substr string, msgAndArgs ...any) bool

func ContainsValue added in v1.1.1

func ContainsValue[K comparable, V comparable](tt TestingIface, m map[K]V, value V, msgAndArgs ...any) bool

ContainsValue for checking map values (separate since it's a different operation)

func ContainsWithEq added in v1.1.1

func ContainsWithEq[T any](tt TestingIface, container []T, item T, eq func(T, T) bool, msgAndArgs ...any) bool

func Equal

func Equal[V comparable](tt TestingIface, want, got V, msgAndArgs ...any) bool

Equal asserts comparables.

testutils.Equal(t, "hello", "hello")

func EqualAny

func EqualAny(tt TestingIface, want, got any, msgAndArgs ...any) bool

func EqualAnyf

func EqualAnyf(tt TestingIface, want, got any, msg string, args ...any) bool

func Error

func Error(tt TestingIface, err error, msgAndArgs ...any) bool

Error assert that err != nil

func ErrorIs

func ErrorIs(tt TestingIface, err, target error, msgAndArgs ...any) bool

ErrorIs asserts that errors.Is(err, target) returns true, meaning target is in err's chain.

func ExtractCoverage added in v1.1.0

func ExtractCoverage(s string) (string, error)

func False deprecated

func False(tt TestingIface, value bool, msgAndArgs ...any) bool

False asserts that the specified value is false.

Deprecated: Use Assert with negation instead. Replace:

testutils.False(t, myBool, "myBool should be false")

With:

testutils.Assert(t, !myBool, "myBool should be false")

func HasPrefix added in v0.5.0

func HasPrefix(tt TestingIface, s, prefix string, msgAndArgs ...any) bool

func HasSuffix added in v0.5.0

func HasSuffix(tt TestingIface, s, suffix string, msgAndArgs ...any) bool

func IsType added in v1.2.0

func IsType(tt TestingIface, expectedType any, got any, msgAndArgs ...any) bool

IsType asserts that the given value is of the expected type. If the value is not of the expected type, the test fails with a descriptive message.

Example:

var x interface{} = 42
testutils.IsType(t, 0, x, "Expected int type")
// Fails if x is not an int

func KeyValErrorMsg

func KeyValErrorMsg(key string, val any) string

func Len added in v1.0.0

func Len(tt TestingIface, value any, expected int, msgAndArgs ...any) bool

Len asserts that the specified value has the expected length.

func Nil

func Nil(tt TestingIface, value any, msgAndArgs ...any) bool

Nil asserts that the specified value is nil.

func NoError

func NoError(ti TestingIface, err error, msgAndArgs ...any) bool

NoError asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if testutils.NoError(t, err) {
	   testutils.Equal(t, expectedObj, actualObj)
  }

func NotEqual

func NotEqual[V comparable](tt TestingIface, want, got V, msgAndArgs ...any) bool

func NotNil

func NotNil(tt TestingIface, value any, msgAndArgs ...any) bool

NotNil asserts that the specified value is not nil.

testutils.NotNil(t, &val)

func ObjectsAreEqual

func ObjectsAreEqual(expected, actual any) bool

ObjectsAreEqual determines if two objects are considered equal.

This function does no assertion of any kind.

func True deprecated

func True(tt TestingIface, value bool, msgAndArgs ...any) bool

True asserts that the specified value is true.

Deprecated: Use Assert instead. Replace:

testutils.True(t, myBool, "myBool should be true")

With:

testutils.Assert(t, myBool, "myBool should be true")

Types

type Containable added in v1.1.1

type Containable[T comparable] interface {
	~[]T | ~string | ~map[T]any
}

Containable defines types that can be searched for containment

type Data added in v0.6.0

type Data[IN, WANT any] []struct {
	In   IN
	Want WANT
}

type NamedData added in v1.2.0

type NamedData[IN, WANT any] []struct {
	Name string
	In   IN
	Want WANT
}

type Result added in v0.6.0

type Result[T comparable] struct {
	Title string
	Want  T
	Got   T
}

type Results added in v0.6.0

type Results[T comparable] []Result[T]

func NewResults added in v0.6.0

func NewResults[T comparable]() Results[T]

func (*Results[T]) Add added in v0.6.0

func (td *Results[T]) Add(name string, want, got T)

func (Results[T]) Test added in v0.6.0

func (td Results[T]) Test(ti TestingIface)

type SliceOrMap added in v1.1.1

type SliceOrMap[T comparable] interface {
	~[]T | ~map[T]any
}

Alternative: Separate interfaces for better type safety

type TestingIface

type TestingIface interface {
	Errorf(format string, args ...any)
	Name() string
	Helper()
}

TestingIface is an interface wrapper for *testing.T, *testing.B, *testing.F

Jump to

Keyboard shortcuts

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