Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down
Loading