Documentation ¶
Overview ¶
Package testutils provides utilities for testing.
Example:
func TestExample(t *testing.T) { type TT struct { got string want string } assert := func(t *testing.T, tt TT) { if diff := Diff(tt.got, tt.want); diff != "" { t.Error(Callers(), diff) } } t.Run( "1", func(t *testing.T) { t.Parallel() assert(t, TT{"lorem ipsum dolor amet", "lorem ipsum dolor sit amet"}) }, ) t.Run( "2", func(t *testing.T) { t.Parallel() assert(t, TT{"the quick fox jumped over lazy dog", "the quick brown fox jumped over the lazy dog"}) }, ) t.Run( "2", func(t *testing.T) { t.Parallel() assert(t, TT{"Sphinx of black quartz judge my vow", "Sphinx of black quartz, judge my vow"}) }, ) }
Index ¶
- func AssertEqual[C comparable](t *testing.T, expected, actual C)
- func AssertEqualSlice[C comparable](t *testing.T, expected, actual []C)
- func AssertErrorMsg(t *testing.T, err error, msg string)
- func AssertNoError(t *testing.T, err error, msg ...string)
- func Callers() string
- func Contains[C comparable](t *testing.T, haystack []C, needle C)
- func Diff[T any](got, want T, opts ...cmp.Option) string
- func DiffTxtar(got, want *txtar.Archive) string
- func DiffTxtarSort(got, want *txtar.Archive) string
- func ErrorIs(t *testing.T, err, expected error)
- func False(t *testing.T, condition bool, msg string)
- func Filenames(ar *txtar.Archive) []string
- func Folder2Txtar(folder string) (*txtar.Archive, error)
- func IsEqual[C comparable](t *testing.T, expected, actual C)
- func IsNil(t *testing.T, obj any)
- func IsNotEqual[C comparable](t *testing.T, expected, actual C)
- func IsNotNil(t *testing.T, obj any)
- func NotContains[C comparable](t *testing.T, haystack []C, needle C)
- func ReadGolden(t *testing.T, path string) string
- func True(t *testing.T, condition bool, msg string)
- func VerifyGo(ar *txtar.Archive) error
- func WithTimeout(t *testing.T, ctx context.Context, timeout time.Duration) context.Context
- func Write(a *txtar.Archive, dir string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertEqual ¶
func AssertEqual[C comparable](t *testing.T, expected, actual C)
AssertEqual checks if the expected and actual are equal. Errors if not.
func AssertEqualSlice ¶
func AssertEqualSlice[C comparable](t *testing.T, expected, actual []C)
AssertEqualSlice checks if the expected and actual slices are equal. Errors if not.
func AssertErrorMsg ¶
AssertErrorMsg checks if the error is not nil.
func AssertNoError ¶
AssertNoError checks if the error is nil.
func Callers ¶
func Callers() string
Callers prints the stack trace of everything up til the line where Callers() was invoked.
func Contains ¶
func Contains[C comparable](t *testing.T, haystack []C, needle C)
Contains checks if the needle is in the haystack. Errors if not.
func Diff ¶
Diff compares two items and returns a human-readable diff string. If the items are equal, the string is empty.
func DiffTxtarSort ¶
func IsEqual ¶
func IsEqual[C comparable](t *testing.T, expected, actual C)
IsEqual checks if the expected and actual are equal. Errors if not.
func IsNotEqual ¶
func IsNotEqual[C comparable](t *testing.T, expected, actual C)
IsNotEqual checks if the expected and actual are not equal. Errors if not.
func NotContains ¶
func NotContains[C comparable](t *testing.T, haystack []C, needle C)
NotContains checks if the needle is not in the haystack. Errors if it is.
func VerifyGo ¶
VerifyGo parses a txtar.Archive, if the files are not valid Go code and error is returned
func WithTimeout ¶
WithTimeout returns a context that will be canceled after the test timeout. If the test timeout given via `go test -timeout <timeout>` is less than the required timeout, the test will fail immediately. This is to prevent tests that you know require a long time to run from starting and then timing out. Might as well fail early.
Types ¶
This section is empty.