From 960e339d3adb7cd3ac4b642a1c964e81074ff28d Mon Sep 17 00:00:00 2001 From: Richard Hopper Date: Wed, 27 May 2026 19:51:04 +0000 Subject: [PATCH 1/2] docs: clarify Zero() is unrelated to encoding/json omitempty/omitzero assert.Zero uses reflect.Zero to compare against the zero value of a type. It does not use or check any IsZero() method, and is unrelated to encoding/json's omitempty or omitzero struct tags. This change adds documentation to assert.Zero() to prevent confusion and includes a test with an IsZero()-implementing struct to verify the behavior. Closes #1759 --- assert/assertions.go | 4 ++++ assert/assertions_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index 1419e4776..eff422cc4 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1762,6 +1762,10 @@ func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interf } // Zero asserts that i is the zero value for its type. +// +// This is unrelated to [encoding/json]'s `omitzero` tag and does not +// use or check the [encoding.TextMarshaler.IsZero] method. It simply +// compares against [reflect.Zero] for the given type. func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 11642e096..aa9e5864e 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2579,6 +2579,14 @@ func TestCallerInfoWithAutogeneratedFunctions(t *testing.T) { }) } +// isZeroStruct demonstrates that assert.Zero uses reflect.Zero and is unrelated +// to encoding/json's omitempty/omitzero or any IsZero() method. +type isZeroStruct struct { + Value int +} + +func (isZeroStruct) IsZero() bool { return false } + func TestZero(t *testing.T) { t.Parallel() @@ -2593,6 +2601,22 @@ func TestZero(t *testing.T) { } } +func TestZeroNotIsZero(t *testing.T) { + t.Parallel() + + // Verify that assert.Zero uses reflect.Zero and is unrelated to + // encoding/json's omitempty/omitzero or any IsZero() method. + + mockT := new(testing.T) + + // The zero value of the struct is "zero" per reflect.Zero, + // even though IsZero() would return false for Value==0. + True(t, Zero(mockT, isZeroStruct{Value: 0})) + + // A non-zero struct value is "not zero" + False(t, Zero(mockT, isZeroStruct{Value: 42})) +} + func TestNotZero(t *testing.T) { t.Parallel() From f8b5a5b639b506e2000f194547498b1218caf975 Mon Sep 17 00:00:00 2001 From: Richard Hopper Date: Fri, 29 May 2026 03:23:07 +0000 Subject: [PATCH 2/2] regenerate Zerof docs to match Zero() documentation --- assert/assertion_format.go | 4 ++++ assert/assertion_forward.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index a19a89279..d5a7a3e63 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -870,6 +870,10 @@ func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...int } // Zerof asserts that i is the zero value for its type. +// +// This is unrelated to [encoding/json]'s `omitzero` tag and does not +// use or check the [encoding.TextMarshaler.IsZero] method. It simply +// compares against [reflect.Zero] for the given type. func Zerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index cd2a86061..a95f27755 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -1739,6 +1739,10 @@ func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) bool { } // Zerof asserts that i is the zero value for its type. +// +// This is unrelated to [encoding/json]'s `omitzero` tag and does not +// use or check the [encoding.TextMarshaler.IsZero] method. It simply +// compares against [reflect.Zero] for the given type. func (a *Assertions) Zerof(i interface{}, msg string, args ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper()