https://github.com/akshayjshah/attest
Type-safe assertion helpers for Go
https://github.com/akshayjshah/attest
assert generics go testing
Last synced: 12 months ago
JSON representation
Type-safe assertion helpers for Go
- Host: GitHub
- URL: https://github.com/akshayjshah/attest
- Owner: akshayjshah
- License: mit
- Created: 2022-03-20T21:22:57.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-09T05:03:56.000Z (almost 2 years ago)
- Last Synced: 2025-04-02T02:38:30.156Z (about 1 year ago)
- Topics: assert, generics, go, testing
- Language: Go
- Homepage: https://pkg.go.dev/go.akshayshah.org/attest
- Size: 38.1 KB
- Stars: 16
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
attest
======
[](https://github.com/akshayjshah/attest/actions/workflows/ci.yaml)
[](https://goreportcard.com/report/go.akshayshah.org/attest)
[](https://pkg.go.dev/go.akshayshah.org/attest)
`attest` is a small package of type-safe assertion helpers. Under the hood,
it uses [cmp] for equality testing and diffing. You may enjoy `attest` if you
prefer:
- Type safety: it's impossible to compare values with different types.
- Brevity: assertions usually print diffs rather than full values.
- Minimalism: just a few assertions, not a whole DSL.
- Natural ordering: every assertion uses `got == want` order.
- Interoperability: assertions work with any `cmp.Option`.
## Installation
```
go get go.akshayshah.org/attest
```
## Usage
```go
package main
import (
"testing"
"time"
"go.akshayshah.org/attest"
)
func TestExample(t *testing.T) {
attest.Equal(t, 1, 1)
attest.NotEqual(t, 2, 1)
attest.Approximately(
t,
time.Minute - 1, // got
time.Minute, // want
time.Second, // tolerance
)
attest.Zero(t, "")
attest.Contains(t, []int{0, 1, 2}, 2)
var err error
attest.Ok(t, err)
err = fmt.Errorf("read config: %w", io.EOF)
attest.Error(t, err)
attest.ErrorIs(t, err, io.EOF)
// You can enrich the default failure message.
attest.Equal(t, 1, 2, attest.Sprintf("integer %s", "addition"))
// The next two assertions won't compile.
attest.Equal(t, int64(1), int(1))
attest.Approximately(t, 9, 10, 0.5)
}
```
Failed assertions usually print a diff. Here's an example using `attest.Equal`:
```
--- FAIL: TestEqual (0.00s)
attest_test.go:58: got != want
diff (+got, -want):
attest.Point{
X: 1,
- Y: 4.2,
+ Y: 3.5,
}
```
## Status: Stable
This module is stable. It supports the [two most recent major
releases][go-support-policy] of Go.
Within those parameters, `attest` follows semantic versioning. No
breaking changes will be made without incrementing the major version.
## Legal
Offered under the [MIT license][license].
[cmp]: https://pkg.go.dev/github.com/google/go-cmp/cmp
[go-support-policy]: https://golang.org/doc/devel/release#policy
[license]: https://github.com/akshayjshah/attest/blob/main/LICENSE