diff --git a/EF/alias.go b/EF/alias.go index c973a7d..2fc20e0 100644 --- a/EF/alias.go +++ b/EF/alias.go @@ -1,5 +1,5 @@ // Package EF provides type aliases for the parent [assert] package. -// It is intended to be dot-imported so that [E] and [F] can be used as local types. +// It should be dot-imported so that [E] and [F] can be used as local types. package EF import "go-simpler.org/assert" diff --git a/README.md b/README.md index 308db5f..c3f9b62 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![goreportcard](https://goreportcard.com/badge/go-simpler.org/assert)](https://goreportcard.com/report/go-simpler.org/assert) [![codecov](https://codecov.io/gh/go-simpler/assert/branch/main/graph/badge.svg)](https://codecov.io/gh/go-simpler/assert) -Common assertions to use with the `testing` package. +Assertions for the standard `testing` package. ## 📌 About @@ -20,19 +20,16 @@ assert.Equal[F](t, 1, 2) // [F] for t.Fatalf() ## 📦 Install +Go 1.21+ + ```shell go get go-simpler.org/assert ``` -> [!note] -> This package is not even meant to be a dependency! -> It's tiny (<100 LoC), so you can just copy-paste it into your project. -> There is also a special tool to do this automatically: -> just add the following directive to any `.go` file and run `go generate ./...`: -> ```go -> //go:generate go run -tags=copier go-simpler.org/assert/cmd/copier@latest -> ``` -> See the `cmd/copier` documentation for details. +> [!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/copier` helper to do this automatically. ## 📋 Usage @@ -43,7 +40,7 @@ The `EF` subpackage should be dot-imported so that `E` and `F` can be used as lo . "go-simpler.org/assert/EF" ``` -Optional format and arguments can be provided to any assertion to customize the error message: +Optional format and arguments can be provided to each assertion to customize the error message: ```go assert.Equal[E](t, 1, 2, "%d != %d", 1, 2) // prints "1 != 2" diff --git a/assert.go b/assert.go index 6968ab8..6657686 100644 --- a/assert.go +++ b/assert.go @@ -1,4 +1,4 @@ -// Package assert provides common assertions to use with the standard [testing] package. +// Package assert implements assertions for the standard [testing] package. package assert import ( @@ -6,25 +6,25 @@ import ( "reflect" ) -// TB is a tiny subset of [testing.TB] used by [assert]. +// TB is a tiny subset of [testing.TB]. type TB interface { Helper() Errorf(format string, args ...any) Fatalf(format string, args ...any) } -// Param controls the behaviour of an assertion in case it fails. -// Either [E] or [F] must be specified as a type parameter when calling the assertion. +// Param controls the behavior of an assertion if it fails. +// Either [E] or [F] must be specified as the type parameter. type Param interface { method(t TB) func(format string, args ...any) } -// E is a [Param] that marks the test as having failed but continues its execution (similar to [testing.T.Errorf]). +// E is a [Param] that marks the test as failed but continues execution (similar to [testing.T.Errorf]). type E struct{} func (E) method(t TB) func(format string, args ...any) { return t.Errorf } -// F is a [Param] that marks the test as having failed and stops its execution (similar to [testing.T.Fatalf]). +// F is a [Param] that marks the test as failed and stops execution (similar to [testing.T.Fatalf]). type F struct{} func (F) method(t TB) func(format string, args ...any) { return t.Fatalf } @@ -77,7 +77,6 @@ func Panics[T Param](t TB, fn func(), v any, formatAndArgs ...any) { fn() } -// fail marks the test as having failed and continues/stops its execution based on T's type. func fail[T Param](t TB, customFormatAndArgs []any, format string, args ...any) { t.Helper() if len(customFormatAndArgs) > 0 {