Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions be_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package be_test

import (
"errors"
"os"
"strings"

"github.com/carlmjohnson/be"
Expand All @@ -22,11 +23,15 @@ func Example() {
be.AllEqual(t, []int{3, 2, 1}, s) // bad

var err error
be.NilErr(t, err) // good
be.Nonzero(t, err) // bad
be.Zero(t, err) // good
be.ErrorIs(t, nil, err) // good
be.Nonzero(t, err) // bad
be.ErrorIs(t, os.ErrPermission, err) // bad

err = errors.New("(O_o)")
be.NilErr(t, err) // bad
be.Nonzero(t, err) // good
var asErr *os.PathError
be.ErrorAs(t, &asErr, err) // bad
be.Nonzero(t, err) // good

type mytype string
var mystring mytype = "hello, world"
Expand All @@ -48,7 +53,8 @@ func Example() {
// got: goodbye
// want: [3 2 1]; got: [1 2 3]
// got: <nil>
// got: (O_o)
// got errors.Is(<nil>, permission denied) == false
// got errors.As((O_o), **fs.PathError) == false
// "World" not in "hello, world"
// "\x00" in "\a\b\x00\r\t"
// want len(seq) == 1; got at least 2
Expand Down
22 changes: 22 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package be

import (
"errors"
"testing"
)

// ErrorIs calls t.Fatalf if got is not want according to [errors.Is].
func ErrorIs(t testing.TB, want, got error) {
t.Helper()
if !errors.Is(got, want) {
t.Fatalf("got errors.Is(%v, %v) == false", got, want)
}
}

// ErrorIs calls t.Fatalf if got cannot be assigned to want by [errors.As].
func ErrorAs[T error](t testing.TB, want *T, got error) {
t.Helper()
if !errors.As(got, want) {
t.Fatalf("got errors.As(%v, %T) == false", got, want)
}
}