Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

go-simpler.org/assert

Package Overview
Dependencies
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

go-simpler.org/assert

Go Modules
Version
v0.9.0
Version published
Created
Source

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)

FAQs

Package last updated on 01 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts