Documentation
¶
Index ¶
- func AddSetup(t *testing.T, f func(t *testing.T))
- func AssertFatalContains(t *testing.T, f func(), contains string)
- func FatalIfError(t *testing.T, err error)
- func FatalIfErrorElseReturn[T any](t *testing.T, err error, val T) T
- func ModifyLet[A any](t *testing.T, l LetWrapper[A], f func(t *testing.T, super func() A) A)
- func ModifyLetValue[A immutable](t *testing.T, l LetWrapper[A], a A)
- func RunSetup(t *testing.T)
- type LetWrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSetup ¶
Add adds a setup function to the list of setup functions to be run when `testsetup.RunSetup` is called.
It must be called before `testsetup.RunSetup`.
Functions added are removed after the current test finishes using `t.Cleanup`. This ensures that setup functions added only affect the current test and its children.
func AssertFatalContains ¶
AssertFatalContains is a helper to run a block of code with FatailIfError being mocked to panic with the given message so we can actually assert on it.
This is useful for testing test utilities.
func FatalIfError ¶
FatalIfError is a helper function that calls t.Fatalf if err is not nil.
It also formats the error with "%+v" to include the stack traces.
func ModifyLet ¶
ModifyLet modifies the value of the LetWrapper for the current test.
It receives a function that can access the previous constructor through the `super` function.
It must be called before RunSetup. Once setup starts, Let values may already be built, so calling ModifyLet (including from a setup function) fails the test instead of silently keeping a stale cached value.
func ModifyLetValue ¶
func ModifyLetValue[A immutable](t *testing.T, l LetWrapper[A], a A)
ModifyLetValue is a variant of ModifyLet that uses a constant value.
It must be called before RunSetup; see ModifyLet.
func RunSetup ¶
RunSetup runs setup functions added by `testsetup.AddSetup`.
Setup callbacks must not call ModifyLet or ModifyLetValue; those helpers must run before setup starts.
A Let can only be evaluated after RunSetup starts and before its lifecycle cleanup runs. Cached Let values are reset by that cleanup, after teardown callbacks registered by setup functions or the test body.
Types ¶
type LetWrapper ¶
type LetWrapper[A any] func() A
LetWrapper represents a value that is lazy loaded when requested for.
See `testsetup.Let` and `testsetup.LetValue` for creating instances of `LetWrapper`.
func Let ¶
Let creates a LetWrapper that lazily loads the value when requested for by calling the provider constructor.
A Let can only be evaluated while RunSetup is active. Calls before RunSetup or after the final cleanup fail the test with the same "Must call RunSetup" error as a call before RunSetup.
RunSetup cleanup stops new constructions and waits for constructions already in flight before the final reset. Cached reads may complete while cleanup waits; starting construction after cleanup begins fails. Concurrent evaluation is supported. Recursive construction (a Let whose constructor evaluates itself) deadlocks.
func LetValue ¶
func LetValue[A immutable](t *testing.T, x A) LetWrapper[A]
LetValue is a shorthand for Let that uses a constant value.
func (LetWrapper[A]) Eager ¶
func (l LetWrapper[A]) Eager(t *testing.T) LetWrapper[A]
Eager forces the value to be loaded eagerly for the current test.
Shorthand for:
AddSetup(t, func(t *testing.T) { letWrapper() })