diff options
Diffstat (limited to 'assert')
| -rw-r--r-- | assert/errequal.go | 4 | ||||
| -rw-r--r-- | assert/errequal_test.go | 14 |
2 files changed, 17 insertions, 1 deletions
diff --git a/assert/errequal.go b/assert/errequal.go index 4752f26..86cd719 100644 --- a/assert/errequal.go +++ b/assert/errequal.go @@ -21,7 +21,9 @@ func ErrEqual(t TestingTB, actual error, expected any) bool { // and also coerce all nil values of concrete `error` types into untyped nil if expectedErr, ok := expected.(error); ok { // convert nil values of concrete error types into a generic nil value - if reflect.ValueOf(expectedErr).IsNil() { + expectedValue := reflect.ValueOf(expectedErr) + kind := expectedValue.Kind() + if (kind == reflect.Pointer || kind == reflect.Interface) && expectedValue.IsNil() { expected = nil } else { expected = expectedErr diff --git a/assert/errequal_test.go b/assert/errequal_test.go index 5efbdc5..4bb9080 100644 --- a/assert/errequal_test.go +++ b/assert/errequal_test.go @@ -74,4 +74,18 @@ func TestErrEqual(t *testing.T) { Outcome: testcapture.OutcomePanicked, Panic: "cannot handle `expected` of type int", }) + + // an earlier version had a bug because this call caused reflect.Value.IsNil() to be called on a value of kind Struct + expectErrors(t, func(t assert.TestingTB) { + assert.ErrEqual(t, nil, structTypedError{"foo"}) + }, `expected "foo", but got no error`) +} + +type structTypedError struct { + Message string +} + +// Error implements the builtin/error interface. +func (e structTypedError) Error() string { + return e.Message } |
