testutil

package
v0.0.0-...-623dc82 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

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

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

func AssertErrorMsg(t *testing.T, err error, msg string)

AssertErrorMsg checks if the error is not nil.

func AssertNoError

func AssertNoError(t *testing.T, err error, msg ...string)

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

func Diff[T any](got, want T, opts ...cmp.Option) string

Diff compares two items and returns a human-readable diff string. If the items are equal, the string is empty.

func DiffTxtar

func DiffTxtar(got, want *txtar.Archive) string

func DiffTxtarSort

func DiffTxtarSort(got, want *txtar.Archive) string

func ErrorIs

func ErrorIs(t *testing.T, err, expected error)

func False

func False(t *testing.T, condition bool, msg string)

func Filenames

func Filenames(ar *txtar.Archive) []string

func Folder2Txtar

func Folder2Txtar(folder string) (*txtar.Archive, error)

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 IsNil

func IsNil(t *testing.T, obj any)

IsNil checks if the obj is nil. 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 IsNotNil

func IsNotNil(t *testing.T, obj any)

IsNotNil checks if the obj is not nil. Errors if it is.

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 ReadGolden

func ReadGolden(t *testing.T, path string) string

func True

func True(t *testing.T, condition bool, msg string)

func VerifyGo

func VerifyGo(ar *txtar.Archive) error

VerifyGo parses a txtar.Archive, if the files are not valid Go code and error is returned

func WithTimeout

func WithTimeout(
	t *testing.T,
	ctx context.Context,
	timeout time.Duration,
) context.Context

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.

func Write

func Write(a *txtar.Archive, dir string) error

Write writes each File in an Archive to the given directory, returning any errors encountered. An error is also returned in the event a file would be written outside of dir.

Types

This section is empty.

Jump to

Keyboard shortcuts

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