This page permanently redirects to gemini://code.pfad.fr/check/.

check package - code.pfad.fr/check

import "code.pfad.fr/check"

package check is a minimalist Go assertion package (single file with actual code, dedicated to the public domain).

Example
// SPDX-FileCopyrightText: 2024 Olivier Charvin 
// 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 t.FailNow)
	check.Equal(t, 1, len(obj.Answers)) // if this fails, mark the test as failed and continue

	// EqualSlice and EqualDeep are the 2 other available assertions
	check.EqualSlice(t, []int{42}, obj.Answers).
		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 }

type o struct{ Answers []int }

var cmp = struct{ Diff func(x, y any) string }{Diff: func(x, y any) string { return "" }}

Types

type Failure

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

Failure allows logging more information in case of failure. All method calls will be no-op on nil (when the check succeeded).

func Equal

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

Equal calls t.Error if want != got.

func EqualDeep

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

EqualDeep calls t.Error if ![reflect.DeepEqual](want, got).

=> reflect.DeepEqual

func EqualSlice

func EqualSlice[S ~[]E, E comparable](t testing.TB, want, got S) *Failure

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

func (*Failure) Fatal

func (f *Failure) Fatal()

Fatal stops the test execution if the Failure is not nil (no-op otherwise), see [testing.T.FailNow].

=> testing.T.FailNow

func (*Failure) Log

func (f *Failure) Log(args ...any) *Failure

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

func (*Failure) Logf

func (f *Failure) Logf(format string, args ...any) *Failure

Logf formats its arguments according to the format, analogous to Printf, and records the text in the error log if the Failure is not nil (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
Proxy Information
Original URL
gemini://code.pfad.fr/check
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
300.841196 milliseconds
Gemini-to-HTML Time
0.541087 milliseconds

This content has been proxied by September (ba2dc).