go.astrophena.name/base Module
Base Go packages for my projects.
package testutil
import "go.astrophena.name/base/testutil"
Package testutil contains common testing helpers.
Index
- func AssertContains(t *testing.T, s S, v V)
- func AssertEqual(t *testing.T, got, want any)
- func AssertErrorType(t *testing.T, got, want error)
- func AssertNotContains(t *testing.T, s S, v V)
- func BuildTxtar(t *testing.T, dir string) []byte
- func ExtractTxtar(t *testing.T, ar *txtar.Archive, dir string)
- func MockHTTPClient(h http.Handler) *http.Client
- func Run(t *testing.T, glob string, f func(t *testing.T, match string))
- func RunGolden(t *testing.T, glob string, f func(t *testing.T, match string) []byte, ...)
- func UnmarshalJSON(t *testing.T, b []byte) V
Functions
func AssertContains
func AssertContains[S ~[]V, V comparable](t *testing.T, s S, v V)
AssertContains fails the test if v is not present in s.
func AssertEqual
func AssertEqual(t *testing.T, got, want any)
AssertEqual compares two values and if they differ, fails the test and prints the difference between them.
func AssertErrorType
func AssertErrorType(t *testing.T, got, want error)
AssertErrorType asserts that the got error is of the same type as the want error. It does not compare error messages or values, only the types.
func AssertNotContains
func AssertNotContains[S ~[]V, V comparable](t *testing.T, s S, v V)
AssertNotContains fails the test if v is present in s.
func BuildTxtar
func BuildTxtar(t *testing.T, dir string) []byte
BuildTxtar constructs a txtar archive from contents of dir.
func ExtractTxtar
func ExtractTxtar(t *testing.T, ar *txtar.Archive, dir string)
ExtractTxtar extracts a txtar archive to dir.
func MockHTTPClient
func MockHTTPClient(h http.Handler) *http.Client
MockHTTPClient returns a http.Client that serves all requests made through it from handler h.
func Run
func Run(t *testing.T, glob string, f func(t *testing.T, match string))
Run runs a subtest for each file matching the provided glob pattern.
func RunGolden
func RunGolden(t *testing.T, glob string, f func(t *testing.T, match string) []byte, update bool)
RunGolden runs a subtest for each file matching the provided glob pattern, computing the result and comparing it with a golden file, or updating a golden file if update is true.
f is a function that should compute the result and return it as a byte slice.
func UnmarshalJSON
func UnmarshalJSON[V any](t *testing.T, b []byte) V
UnmarshalJSON parses the JSON data into v, failing the test in case of failure.