Avatar Ilya Mateyko

go.astrophena.name/base Module

GitHub repository | Commit (47bc538)

Base Go packages for my projects.

package testutil

import "go.astrophena.name/base/testutil"

Package testutil contains common testing helpers.

Index

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 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.