helpers

package
v2.24.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package helpers contains various utilities for writing tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyOptions

func ApplyOptions[T any, U ConfigOption[T]](target *T, options ...U) error

ApplyOptions calls any number of ConfigOption implementations against the target value. If any returns an error, it immediately stops and returns that error.

func AssertEventually

func AssertEventually(
	t TestContext,
	testFn func() bool,
	timeout time.Duration,
	interval time.Duration,
	failureMsgFormat string,
	failureMsgArgs ...interface{},
) bool

AssertEventually is equivalent to assert.Eventually from stretchr/testify/assert, except that it does not use a separate goroutine so it does not cause problems with our test framework. It calls testFn repeatedly at intervals until it gets a true value; if the timeout elapses, the test fails.

func AssertNever

func AssertNever(
	t TestContext,
	testFn func() bool,
	timeout time.Duration,
	interval time.Duration,
	failureMsgFormat string,
	failureMsgArgs ...interface{},
) bool

AssertNever is equivalent to assert.Never from stretchr/testify/assert, except that it does not use a separate goroutine so it does not cause problems with our test framework. It calls testFn repeatedly at intervals until either the timeout elapses or it receives a true value; if it receives a true value, the test fails.

func CopyOf

func CopyOf[V any](slice []V) []V

CopyOf returns a shallow copy of a slice.

func IfElse

func IfElse[V any](isTrue bool, valueIfTrue, valueIfFalse V) V

IfElse returns valueIfTrue or valueIfFalse depending on isTrue.

func NonBlockingSend

func NonBlockingSend[V any](ch chan<- V, value V) bool

NonBlockingSend is a shortcut for using select to do a non-blocking send. It returns true on success or false if the channel was full.

func PollForSpecificResultValue

func PollForSpecificResultValue[V comparable](
	testFn func() V,
	timeout time.Duration,
	interval time.Duration,
	expectedValue V,
) bool

PollForSpecificResultValue calls testFn repeatedly at intervals until the expected value is seen or the timeout elapses. Returns true if the value was matched, false if timed out.

func RequireEventually

func RequireEventually(
	t TestContext,
	testFn func() bool,
	timeout time.Duration,
	interval time.Duration,
	failureMsgFormat string,
	failureMsgArgs ...interface{},
)

RequireEventually is equivalent to require.Eventually from stretchr/testify/assert, except that it does not use a separate goroutine so it does not cause problems with our test framework. It calls testFn repeatedly at intervals until it gets a true value; if the timeout elapses, the test fails and immediately exits.

func RequireNever

func RequireNever(
	t TestContext,
	testFn func() bool,
	timeout time.Duration,
	interval time.Duration,
	failureMsgFormat string,
	failureMsgArgs ...interface{},
)

RequireNever is equivalent to require.Never from stretchr/testify/assert, except that it does not use a separate goroutine so it does not cause problems with our test framework. It calls testFn repeatedly at intervals until either the timeout elapses or it receives a true value; if it receives a true value, the test fails and exits immediately

func RequireNoMoreValues

func RequireNoMoreValues[V any](t TestContext, ch <-chan V, timeout time.Duration)

RequireNoMoreValues tries to receive a value within the given timeout, and causes the test to fail and terminate immediately if a value was received.

func RequireNoMoreValuesWithMessage

func RequireNoMoreValuesWithMessage[V any](
	t TestContext,
	ch <-chan V,
	timeout time.Duration,
	msgFormat string,
	msgArgs ...interface{},
)

RequireNoMoreValuesWithMessage is the same as RequireNoMoreValues, but allows customization of the failure message.

func RequireValue

func RequireValue[V any](t TestContext, ch <-chan V, timeout time.Duration) V

RequireValue tries to receive a value and returns it if successful, or causes the test to fail and terminate immediately if it timed out.

func RequireValueWithMessage

func RequireValueWithMessage[V any](
	t TestContext,
	ch <-chan V,
	timeout time.Duration,
	msgFormat string,
	msgArgs ...interface{},
) V

RequireValueWithMessage is the same as RequireValue, but allows customization of the failure message.

func SliceContains

func SliceContains[V comparable](value V, slice []V) bool

SliceContains returns true if and only if the slice has an element that equals the value.

func Sorted

func Sorted[V constraints.Ordered](slice []V) []V

Sorted returns a sorted copy of a slice.

func TryReceive

func TryReceive[V any](ch <-chan V, timeout time.Duration) opt.Maybe[V]

TryReceive is a shortcut for using select to do a receive with timeout. It returns a Maybe that has a value if one was available, or no value if it timed out.

Types

type ConfigOption

type ConfigOption[T any] interface {
	// Configure makes whatever configuration change the option represents.
	Configure(*T) error
}

ConfigOption is an interface for use with the vararg options pattern and ApplyOptions.

type ConfigOptionFunc

type ConfigOptionFunc[T any] func(*T) error

ConfigOptionFunc is a decorator for a function being used as a ConfigOption.

func (ConfigOptionFunc[T]) Configure

func (f ConfigOptionFunc[T]) Configure(target *T) error

type TestContext

type TestContext interface {
	Errorf(msgFormat string, msgArgs ...interface{})
	FailNow()
	Helper()
}

TestContext is a minimal interface for types like *testing.T and *ldtest.T representing a test that can fail. Functions can use this to avoid specific dependencies on those packages.

type TestRecorder

type TestRecorder struct {
	Errors           []string
	Terminated       bool
	PanicOnTerminate bool
}

TestRecorder is a stub implementation of TestContext for testing test logic.

func (*TestRecorder) Err

func (t *TestRecorder) Err() error

Err returns an error whose message is a concatenation of all error messages so far, or nil if none.

func (*TestRecorder) Errorf

func (t *TestRecorder) Errorf(format string, args ...interface{})

Errorf records an error message.

func (*TestRecorder) FailNow

func (t *TestRecorder) FailNow()

FailNow simulates a condition where the test is supposed to immediately terminate. It sets t.Terminated to true, and then does a panic(t) if and only if t.PanicOnTerminate is true.

func (*TestRecorder) Helper

func (t *TestRecorder) Helper()

Jump to

Keyboard shortcuts

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