summaryrefslogtreecommitdiff
path: root/assert
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-06-20 21:32:49 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-06-20 21:32:49 +0200
commiteb839b45b1da29038500ee6ae52526cbc854c6a1 (patch)
tree016117a9420a946a2d52f5af90ae79a12b3f656d /assert
parentf244daebd51216265669ff38143d1ff1fb9cb293 (diff)
downloadgo-gg-eb839b45b1da29038500ee6ae52526cbc854c6a1.tar.gz
assert: fix crash in ErrEqual on expected being a struct kind value
Diffstat (limited to 'assert')
-rw-r--r--assert/errequal.go4
-rw-r--r--assert/errequal_test.go14
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
}