check

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2025 License: CC0-1.0 Imports: 7 Imported by: 1

Documentation

Overview

Package check is a minimalist Go assertion package.

Although assert libraries are frowned upon by the Go team, this package aims at reducing the boilerplate while not getting in the way of your tests. Importantly:

Dedicated to the public domain.

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) {
	check.Equal(t, 2, 1+1) // simple equality check. Continues execution on inequality (test will fail)

	obj, err := newObj()
	check.Equal(t, nil, err).Fatal() // Fatal will stop the test on inequality (by calling t.FailNow)
	check.Equal(t, 1, len(obj.Answers)).
		Log("wrong answer size") // Log argument will only be printed on inequality

	// EqualSlice will print a unified diff on inequality
	check.EqualSlice(t, []int{42}, obj.Answers).
		Logf("question: %q", "Ultimate Question"). // Printf syntax also is also supported for Log
		Fatal()                                    // Methods can be chained

	// EqualDeep allows to compare complex structure.
	// To print a unified diff, you can 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 "" }}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Failure added in v0.9.0

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

func EqualSlice added in v0.8.0

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

EqualSlice is the slice version of Equal. Calls t.Error if want != got with an unified diff.

func (*Failure) Fatal added in v0.9.0

func (f *Failure) Fatal()

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

func (*Failure) Log added in v0.9.0

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 added in v0.9.0

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL