Documentation
¶
Overview ¶
package check is a minimalist Go assertion package.
Example ¶
// SPDX-FileCopyrightText: 2024 Olivier Charvin <git@olivier.pfad.fr>
//
// SPDX-License-Identifier: CC0-1.0
package main
import (
"testing"
"code.pfad.fr/check"
)
func testExample(t *testing.T) {
obj, err := newObj()
check.Equal(t, nil, err).Fatal() // Fatal will stop the test on failure (by calling testing.FailNow)
check.Equal(t, o{}, obj) // if this fails, mark the test as failed and continue
check.Equal(t, 42, obj.Answer).
Log("wrong answer"). // Logs are only printed on failure
Logf("question: %q", "Ultimate Question"). // (printf syntax also supported)
Fatal() // Methods can be chained
// To get a nice diff, use the output of go-cmp as a Log argument:
// import "github.com/google/go-cmp/cmp"
expectedObj := o{}
check.EqualDeep(t, expectedObj, obj).
Log(cmp.Diff(expectedObj, obj))
}
// ignore anything below this line (tricks to show the test in the documentation as an example)
func newObj() (o, error) { return o{}, nil }
func main() { testExample(nil) }
type o struct{ Answer int }
var cmp = struct{ Diff func(x, y any) string }{}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Failed ¶
type Failed struct {
// contains filtered or unexported fields
}
Failed allows logging 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.Error if want != got.
func EqualSlice ¶ added in v0.8.0
func EqualSlice[S ~[]E, E comparable](t testing.TB, want, got S) Failed
EqualSlice is the slice version of Equal. Calls t.Error 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.
Click to show internal directories.
Click to hide internal directories.