Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/akshayjshah/attest
Type-safe assertion helpers for Go
https://github.com/akshayjshah/attest
assert generics go testing
Last synced: 12 days 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 (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-09T05:03:56.000Z (3 months ago)
- Last Synced: 2024-10-20T11:43:09.501Z (19 days ago)
- Topics: assert, generics, go, testing
- Language: Go
- Homepage: https://pkg.go.dev/go.akshayshah.org/attest
- Size: 38.1 KB
- Stars: 15
- 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
======[![Build](https://github.com/akshayjshah/attest/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/akshayjshah/attest/actions/workflows/ci.yaml)
[![Report Card](https://goreportcard.com/badge/go.akshayshah.org/attest)](https://goreportcard.com/report/go.akshayshah.org/attest)
[![GoDoc](https://pkg.go.dev/badge/go.akshayshah.org/attest.svg)](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 mainimport (
"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