Skip to content

Commit

Permalink
align assert.ErrorIs with the rest of the package in a backward compa…
Browse files Browse the repository at this point in the history
…tible way
  • Loading branch information
adamluzsi committed Jun 16, 2023
1 parent 08a44a9 commit 747f601
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
45 changes: 24 additions & 21 deletions assert/Asserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,38 +851,41 @@ func (a Asserter) NotEmpty(v any, msg ...any) {
// ErrorIs allow you to assert an error value by an expectation.
// ErrorIs allow asserting an error regardless if it's wrapped or not.
// Suppose the implementation of the test subject later changes by wrap errors to add more context to the return error.
// TODO: think about it if this could be swapped safely. Maybe check it back and forth as well.
func (a Asserter) ErrorIs(exp, got error, msg ...any) {
func (a Asserter) ErrorIs(err, oth error, msg ...any) {
a.TB.Helper()

if errors.Is(got, exp) {
return
}
if a.eq(exp, got) {
if a.errorIs(err, oth) || a.errorIs(oth, err) {
return
}
if ErrorEqAs := func(expected, actual error) bool {
if actual == nil || expected == nil {
return false
}
nErr := reflect.New(reflect.TypeOf(expected))
return errors.As(actual, nErr.Interface()) &&
a.eq(expected, nErr.Elem().Interface())
}; ErrorEqAs(exp, got) {
return
}

a.fn(fmterror.Message{
Method: "ErrorIs",
Cause: "The actual error is not what was expected.",
Cause: "error value is not what was expected",
Message: msg,
Values: []fmterror.Value{
{Label: "expected", Value: exp},
{Label: "actual", Value: got},
{Label: "err", Value: err},
{Label: "oth", Value: oth},
},
})
}

func (a Asserter) errorIs(err, oth error) bool {
a.TB.Helper()
if err == nil && oth == nil {
return true
}
if errors.Is(err, oth) {
return true
}
if a.eq(oth, err) {
return true
}
if oth != nil {
if ptr := reflect.New(reflect.TypeOf(oth)); errors.As(err, ptr.Interface()) {
return a.eq(oth, ptr.Elem().Interface())
}
}
return false
}

func (a Asserter) Error(err error, msg ...any) {
a.TB.Helper()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion assert/Asserter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,11 @@ func TestAsserter_ErrorIs(t *testing.T) {
} {
tc := tc
t.Run(tc.Desc, func(t *testing.T) {
Equal(t, tc.IsFailed, subject(t, tc.IsFailed, tc.Expected, tc.Actual))
Equal(t, tc.IsFailed, subject(t, tc.IsFailed, tc.Actual, tc.Expected))

t.Run("backward compatibility", func(t *testing.T) {
Equal(t, tc.IsFailed, subject(t, tc.IsFailed, tc.Expected, tc.Actual))
})
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions assert/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ func ExampleContainExactly() {
func ExampleErrorIs() {
var tb testing.TB
actualErr := errors.New("boom")
assert.ErrorIs(tb, errors.New("boom"), actualErr) // passes for equality
assert.ErrorIs(tb, errors.New("boom"), fmt.Errorf("wrapped error: %w", actualErr)) // passes for wrapped errors
assert.ErrorIs(tb, actualErr, errors.New("boom")) // passes for equality
assert.ErrorIs(tb, fmt.Errorf("wrapped error: %w", actualErr), errors.New("boom")) // passes for wrapped errors
}

func ExampleWaiter_Wait() {
Expand Down
4 changes: 2 additions & 2 deletions assert/pkgfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func Sub[T any](tb testing.TB, haystack, needle []T, msg ...any) {
Must(tb).Sub(haystack, needle, msg...)
}

func ErrorIs(tb testing.TB, exp, got error, msg ...any) {
func ErrorIs(tb testing.TB, err, oth error, msg ...any) {
tb.Helper()
Must(tb).ErrorIs(exp, got, msg...)
Must(tb).ErrorIs(err, oth, msg...)
}

func Error(tb testing.TB, err error, msg ...any) {
Expand Down

0 comments on commit 747f601

Please sign in to comment.