💾 Archived View for code.pfad.fr › check captured on 2024-08-31 at 11:27:46. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-05-26)

-=-=-=-=-=-=-

check package - code.pfad.fr/check

import "code.pfad.fr/check"

package check is a very minimal Go assertion package, usage example:

obj, err := newObj()
check.Equal(t, nil, err).Fatal()      // Fatal will stop the test on failure (by calling testing.FailNow)
check.Equal(t, "property", obj.Field) // if this fails, the test continue

check.Equal(t, 0, obj.Answer("")).Log("wrong answer") // you can add some context on failure
check.Equal(t, 42, obj.Answer("Ultimate Question")).
	Logf("question: %q", "Ultimate Question") // context with a printf-like syntax if preferred

check.Equal(t, 42, obj.Answer("other question")).
	Logf("question: %q", "other question").
	Log("reference", "Hitchhiker's Guide to the Galaxy"). // Log(f) can be chained
	Fatal()

For slices see [Equals]; for not comparable structs see [EqualDeep] (all assertions begin with Equal for ease of discovery using auto-completion).

To get a nice diff, use the output of go-cmp as a Log argument:

import "github.com/google/go-cmp/cmp"

check.EqualDeep(t, expectedObj, obj).
	Log(cmp.Diff(expectedObj, obj))

Types

type Failed

type Failed struct {
	// contains filtered or unexported fields
}

Failed can be used to conditionnally add more information in case of failure (all method calls will be no-op if the check succeeded).

func Equal

func Equal[T comparable](t testing.TB, want, got T) Failed

Equal calls t.Errorf if want != got.

func EqualDeep

func EqualDeep[T any](t testing.TB, want, got T) Failed

EqualDeep calls t.Errorf if !reflect.DeepEqual(want, got).

func Equals

func Equals[S ~[]E, E comparable](t testing.TB, want, got S) Failed

Equals is the slice version of [Equal]. Calls t.Errorf if want != got.

func (Failed) Fatal

func (f Failed) Fatal()

Fatal stops the test execution if the check failed (no-op otherwise), see [testing.T.FailNow].

testing.T.FailNow

func (Failed) Log

func (f Failed) Log(args ...any) Failed

Log formats its arguments using default formatting, analogous to Println, and records the text in the error log if the check failed (no-op otherwise).

func (Failed) Logf

func (f Failed) Logf(format string, args ...any) Failed

Logf formats its arguments according to the format, analogous to Printf, and records the text in the error log if the check failed (no-op otherwise)

Files

check.go

Forge

https://codeberg.org/pfad.fr/check

git clone

https://codeberg.org/pfad.fr/check.git
git@codeberg.org:pfad.fr/check.git