Skip to content

Commit

Permalink
docs: update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Mar 29, 2024
1 parent 09db1f9 commit 7084046
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion EF/alias.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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"
Expand Down
13 changes: 6 additions & 7 deletions assert.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// Package assert provides common assertions to use with the standard [testing] package.
// Package assert implements assertions for the standard [testing] package.
package assert

import (
"errors"
"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 }
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7084046

Please sign in to comment.