From 1b8c3d0b1c6975bb492d33ecd685e9006c891806 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:22:37 -0600 Subject: [PATCH] Fix Empty for pointers to interfaces isEmptyValue now recurses through reflect.Interface as it does through reflect.Ptr, so Empty(&i) agrees with Empty(i) when i holds an empty concrete value. Restores the behavior from v1.10.0 before the IsZero rewrite in #1761. Fixes #1955 --- assert/assertions.go | 4 ++-- assert/issue1955_test.go | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 assert/issue1955_test.go diff --git a/assert/assertions.go b/assert/assertions.go index 166f63726..7a0ae8937 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -768,8 +768,8 @@ func isEmptyValue(objValue reflect.Value) bool { // Note: array types are empty when they match their zero-initialized state. case reflect.Chan, reflect.Map, reflect.Slice: return objValue.Len() == 0 - // non-nil pointers are empty if the value they point to is empty - case reflect.Ptr: + // non-nil pointers and interfaces are empty if the value they hold is empty + case reflect.Ptr, reflect.Interface: return isEmptyValue(objValue.Elem()) } return false diff --git a/assert/issue1955_test.go b/assert/issue1955_test.go new file mode 100644 index 000000000..acbf477b4 --- /dev/null +++ b/assert/issue1955_test.go @@ -0,0 +1,44 @@ +package assert + +import ( + "testing" +) + +type issue1955Shape interface{ Area() int } + +type issue1955Square struct{ Side int } + +func (s *issue1955Square) Area() int { return s.Side * s.Side } + +func TestIssue1955EmptyPointerToInterface(t *testing.T) { + var s issue1955Shape = &issue1955Square{} + var nilShape issue1955Shape + var nonEmpty issue1955Shape = &issue1955Square{Side: 2} + + tests := []struct { + name string + value interface{} + empty bool + }{ + {"unwrapped value", issue1955Square{}, true}, + {"pointer to value", &issue1955Square{}, true}, + {"interface", s, true}, + {"pointer to interface holding empty value", &s, true}, + {"pointer to empty interface", &nilShape, true}, + {"pointer to interface holding non-empty value", &nonEmpty, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockT := new(testing.T) + got := Empty(mockT, tt.value) + if got != tt.empty { + t.Errorf("Empty(%T) = %v, want %v", tt.value, got, tt.empty) + } + notGot := NotEmpty(mockT, tt.value) + if notGot == tt.empty { + t.Errorf("NotEmpty(%T) = %v, want %v", tt.value, notGot, !tt.empty) + } + }) + } +}