Skip to content

Commit

Permalink
assert: Add Error and ErrorContains
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Dec 12, 2019
1 parent 97798d6 commit f88a21f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
35 changes: 31 additions & 4 deletions sloggers/slogtest/assert/assert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Package assert is a helper package for test assertions.
//
// On failure, every assertion will fatal the test.
//
// The name parameter is available in each assertion for easier debugging.
package assert // import "cdr.dev/slog/sloggers/slogtest/assert"

import (
"errors"
"testing"

"cdr.dev/slog"
Expand All @@ -13,8 +18,15 @@ import (
//
// If they are not equal, it will fatal the test with a diff of the
// two objects.
//
// If act is an error it will be unwrapped.
func Equal(t testing.TB, exp, act interface{}, name string) {
slog.Helper()

if err, ok := act.(error); ok {
act = unwrapErr(err)
}

if diff := assert.CmpDiff(exp, act); diff != "" {
slogtest.Fatal(t, "unexpected value",
slog.F("name", name),
Expand All @@ -24,8 +36,6 @@ func Equal(t testing.TB, exp, act interface{}, name string) {
}

// Success asserts err == nil.
//
// If err isn't nil, it will fatal the test with the error.
func Success(t testing.TB, err error, name string) {
slog.Helper()
if err != nil {
Expand All @@ -37,9 +47,26 @@ func Success(t testing.TB, err error, name string) {
}

// True asserts act == true.
//
// If act isn't true, it will fatal the test.
func True(t testing.TB, act bool, name string) {
slog.Helper()
Equal(t, true, act, name)
}

// Error asserts err != nil.
func Error(t testing.TB, err error, name string) {
slog.Helper()
if err == nil {
slogtest.Fatal(t, "expected error",
slog.F("name", name),
)
}
}

func unwrapErr(err error) error {
uerr := errors.Unwrap(err)
for uerr != nil {
err = uerr
uerr = errors.Unwrap(uerr)
}
return err
}
27 changes: 27 additions & 0 deletions sloggers/slogtest/assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assert_test

import (
"fmt"
"io"
"testing"

Expand All @@ -21,6 +22,19 @@ func TestEqual(t *testing.T) {
assert.Equal(tb, 3, 4, "meow")
}

func TestEqual_error(t *testing.T) {
t.Parallel()

tb := &fakeTB{}
assert.Equal(tb, io.EOF, fmt.Errorf("failed: %w", io.EOF), "meow")

defer func() {
recover()
simpleassert.Equal(t, 1, tb.fatals, "fatals")
}()
assert.Equal(tb, io.ErrClosedPipe, fmt.Errorf("failed: %w", io.EOF), "meow")
}

func TestSuccess(t *testing.T) {
t.Parallel()

Expand All @@ -47,6 +61,19 @@ func TestTrue(t *testing.T) {
assert.True(tb, false, "meow")
}

func TestError(t *testing.T) {
t.Parallel()

tb := &fakeTB{}
assert.Error(tb, io.EOF, "meow")

defer func() {
recover()
simpleassert.Equal(t, 1, tb.fatals, "fatals")
}()
assert.Error(tb, nil, "meow")
}

type fakeTB struct {
testing.TB

Expand Down

0 comments on commit f88a21f

Please sign in to comment.