assert

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: MPL-2.0 Imports: 2 Imported by: 0

README

assert

checks pkg.go.dev goreportcard codecov

Common assertions to use with the standard testing package

📌 About

assert is a minimalistic replacement for the stretchr/testify package, providing an alternative syntax to switch between t.Errorf() and t.Fatalf(): instead of using separate packages (assert/require), it abuses type parameters:

assert.Equal[E](t, 1, 2) // [E] for t.Errorf()
assert.Equal[F](t, 1, 2) // [F] for t.Fatalf()

📦 Install

go get go-simpler.org/assert

⚠️ This package is not even meant to be a dependency! It's tiny (<100 LoC), so you can just copy-paste it into your project. There is also a special tool to do this automatically: just add the following directive to any .go file and run go generate ./...:

//go:generate go run -tags=copier go-simpler.org/assert/cmd/copier@latest

See the cmd/copier documentation for details.

📋 Usage

The EF subpackage should be dot-imported so that E and F can be used as local types:

"go-simpler.org/assert"
. "go-simpler.org/assert/EF"

Optional format and arguments can be provided to any assertion to customize the error message:

// prints "actual/expected" instead of "got/want".
assert.Equal[E](t, 1, 2, "values are not equal\nactual: %v\nexpected: %v", 1, 2)
Equal

Asserts that two values are equal.

assert.Equal[E](t, 1, 2)
NoErr

Asserts that the error is nil.

assert.NoErr[E](t, err)
IsErr

Asserts that errors.Is(err, target) is true.

assert.IsErr[E](t, err, os.ErrNotExist)
AsErr

Asserts that errors.As(err, target) is true.

assert.AsErr[E](t, err, new(*os.PathError))
Panics

Asserts that the given function panics with the argument v.

assert.Panics[E](t, func() { /* panic? */ }, 42)

❤️ Credits

Inspired by matryer/is. The idea of the internal implementation belongs to xakep666.

Documentation

Overview

Package assert provides common assertions to use with the standard testing package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsErr

func AsErr[T Param](t TB, err error, target any, formatAndArgs ...any)

AsErr asserts that errors.As(err, target) is true.

Example
assert.AsErr[E](t, err, new(*os.PathError))
Output:

errors.As() mismatch
got:  *errors.errorString
want: *fs.PathError

func Equal

func Equal[T Param, V any](t TB, got, want V, formatAndArgs ...any)

Equal asserts that two values are equal.

Example
assert.Equal[E](t, 1, 2)
Output:

values are not equal
got:  1
want: 2

func IsErr

func IsErr[T Param](t TB, err, target error, formatAndArgs ...any)

IsErr asserts that errors.Is(err, target) is true.

Example
assert.IsErr[E](t, err, os.ErrNotExist)
Output:

errors.Is() mismatch
got:  file already exists
want: file does not exist

func NoErr

func NoErr[T Param](t TB, err error, formatAndArgs ...any)

NoErr asserts that the error is nil.

Example
assert.NoErr[E](t, err)
Output:

unexpected error: file already exists

func Panics added in v0.6.0

func Panics[T Param](t TB, fn func(), v any, formatAndArgs ...any)

Panics asserts that the given function panics with the argument v.

Example
assert.Panics[E](t, func() { /* panic? */ }, 42)
Output:

the function didn't panic

Types

type E

type E struct{}

E is a Param that marks the test as having failed but continues its execution (similar to testing.T.Errorf).

type F

type F struct{}

F is a Param that marks the test as having failed and stops its execution (similar to testing.T.Fatalf).

type Param added in v0.5.0

type Param interface {
	// contains filtered or unexported methods
}

Param controls the behaviour of an assertion in case it fails. Either E or F must be specified as a type parameter when calling the assertion.

type TB

type TB interface {
	Helper()
	Errorf(format string, args ...any)
	Fatalf(format string, args ...any)
}

TB is a tiny subset of testing.TB used by assert.

Directories

Path Synopsis
Package EF provides type aliases for the parent [assert] package.
Package EF provides type aliases for the parent [assert] package.

Jump to

Keyboard shortcuts

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