assert

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: MIT Imports: 2 Imported by: 0

README

assert

ci docs report codecov

Common assertions to use with the standard testing package

📌 About

assert is a minimalistic replacement for the famous testify package, providing an alternative syntax for switching 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 github.com/junk1tm/assert

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

//go:generate go run -tags=installer github.com/junk1tm/assert/cmd/installer .

See the cmd/installer documentation for details.

📋 Usage

The dotimport subpackage should be dot-imported, so the E/F parameters could be used as local types:

"github.com/junk1tm/assert"
. "github.com/junk1tm/assert/dotimport"

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

// prints "actual 1; expected 2" instead of "got %1; want %2"
assert.Equal[E](t, 1, 2, "actual %d; expected %d", 1, 2)

🧪 Assertions

Equal

Asserts that two values are equal.

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

Asserts that err 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))

❤️ 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 Parameter](t TB, err error, target any, formatAndArgs ...any)

AsErr asserts that errors.As(err, target) is true. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise AsErr panics.

Example
err := errors.New("test")
assert.AsErr[E](t, err, new(*fs.PathError)) // prints "got *errors.errorString; want **fs.PathError"
assert.AsErr[F](t, err, new(*fs.PathError)) // prints "got *errors.errorString; want **fs.PathError" and stops the test

func Equal

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

Equal asserts that got and want are equal. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise Equal panics.

Example
assert.Equal[E](t, 1, 2) // prints "got 1; want 2"
assert.Equal[F](t, 1, 2) // prints "got 1; want 2" and stops the test

func IsErr

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

IsErr asserts that errors.Is(err, target) is true. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise IsErr panics.

Example
err := errors.New("test")
assert.IsErr[E](t, err, fs.ErrNotExist) // prints "got test; want file does not exist"
assert.IsErr[F](t, err, fs.ErrNotExist) // prints "got test; want file does not exist" and stops the test

func NoErr

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

NoErr asserts that err is nil. Optional formatAndArgs can be provided to customize the error message, the first element must be a string, otherwise NoErr panics.

Example
err := errors.New("test")
assert.NoErr[E](t, err) // prints "got test; want no error"
assert.NoErr[F](t, err) // prints "got test; want no error" and stops the test

Types

type E

type E struct{}

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

type F

type F struct{}

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

type Parameter

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

Parameter is type parameter that control the behaviour of an assertion in case it fails. Either E or F should be specified 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 dotimport provides type aliases for the parent [assert] package.
Package dotimport 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