Skip to content

Commit

Permalink
test: added ErrorAs
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored and shoenig committed Sep 10, 2024
1 parent eee3fc0 commit bff98a4
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ There are five key packages,
- `portal` - utilities for allocating free ports for network listeners in tests

### Changes
:ballot_box_with_check: v1.11.0 adds an ErrorAs helper

:ballot_box_with_check: v1.10.0 adds a `util` package for helpers that return values

- Adds ability to create and automatically clean up temporary files
Expand Down
11 changes: 11 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ func ExampleErrorIs() {
// Output:
}

func ExampleErrorAs() {
e1 := errors.New("e1")
e2 := FakeError("foo")
e3 := errors.New("e3")
errorChain := errors.Join(e1, e2, e3)
var target FakeError
ErrorAs(t, errorChain, &target)
fmt.Println(target.Error())
// Output: foo
}

func ExampleFalse() {
False(t, 1 == int('a'))
// Output:
Expand Down
17 changes: 17 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ func ErrorIs(err error, target error) (s string) {
return
}

func ErrorAs[E error, Target *E](err error, target Target) (s string) {
if err == nil {
s = "expected error; got nil\n"
return
}
if target == nil {
s = "expected target not to be nil"
return
}
if !errors.As(err, target) {
s = "expected errors.As match\n"
s += bullet(" error: %v\n", err)
s += bullet("target: %v\n", target)
}
return
}

func NoError(err error) (s string) {
if err != nil {
s = "expected nil error\n"
Expand Down
11 changes: 11 additions & 0 deletions must/examples_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions must/must.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func ErrorIs(t T, err error, target error, settings ...Setting) {
invoke(t, assertions.ErrorIs(err, target), settings...)
}

// ErrorAs asserts err's tree contains an error that matches target.
// If so, it sets target to the error value.
func ErrorAs[E error, Target *E](t T, err error, target Target, settings ...Setting) {
t.Helper()
invoke(t, assertions.ErrorAs(err, target), settings...)
}

// NoError asserts err is a nil error.
func NoError(t T, err error, settings ...Setting) {
t.Helper()
Expand Down
32 changes: 32 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,38 @@ func TestErrorIs_nil(t *testing.T) {
ErrorIs(tc, nil, err)
}

type FakeError string

func (e FakeError) Error() string {
return string(e)
}

func TestErrorAs(t *testing.T) {
tc := newCase(t, `expected errors.As match`)
t.Cleanup(tc.assert)

var target FakeError
e := errors.New("foo")
ErrorAs(tc, e, &target)
}

func TestErrorAs_nilErr(t *testing.T) {
tc := newCase(t, `expected error; got nil`)
t.Cleanup(tc.assert)

var target FakeError
ErrorAs(tc, nil, &target)
}

func TestErrorAs_nilTarget(t *testing.T) {
tc := newCase(t, `expected target not to be nil`)
t.Cleanup(tc.assert)

var target *FakeError
e := errors.New("foo")
ErrorAs(tc, e, target)
}

func TestNoError(t *testing.T) {
tc := newCase(t, `expected nil error`)
t.Cleanup(tc.assert)
Expand Down

0 comments on commit bff98a4

Please sign in to comment.