assert

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MPL-2.0 Imports: 2 Imported by: 0

README ¶

assert

checks pkg.go.dev goreportcard codecov

Assertions for the standard testing package.

📌 About

assert is a minimalistic replacement for the stretchr/testify package, providing an alternative API 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 1.21+

go get go-simpler.org/assert

[!tip] When developing a library, you may want to keep go.mod small (or, even better, empty). In such cases, it is recommended to just copy-paste assert, as it is tiny and rarely updated. See the cmd/cp utility to do this automatically.

📋 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 each assertion to customize the error message:

assert.Equal[E](t, 1, 2, "%d != %d", 1, 2) // prints "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)

Documentation ¶

Overview ¶

Package assert implements assertions for 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. If v is nil, the panic argument is ignored.

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 failed but continues execution (similar to testing.T.Errorf).

type F ¶

type F struct{}

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

type Param ¶ added in v0.5.0

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

Param controls the behavior of an assertion if it fails. Either E or F must be specified as the type parameter.

type TB ¶

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

TB is a tiny subset of testing.TB.

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