Skip to content

Commit 974779e

Browse files
committed
replacing NoFieldIsEmpty with NoFieldIsZero
1 parent 060a6c1 commit 974779e

File tree

6 files changed

+90
-58
lines changed

6 files changed

+90
-58
lines changed

assert/assertion_format.go

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertion_forward.go

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertions.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,18 +2275,21 @@ func buildErrorChainString(err error, withType bool) string {
22752275
return chain
22762276
}
22772277

2278-
// NoFieldIsEmpty asserts that object, which must be a struct or eventually
2279-
// reference to one, has no exported field with a value that is empty (following
2280-
// the definition of empty used in [Empty]).
2281-
func NoFieldIsEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
2282-
if reflect.TypeOf(object).Kind() == reflect.Ptr {
2283-
return NoFieldIsEmpty(t, reflect.ValueOf(object).Elem().Interface(), msgAndArgs)
2284-
}
2285-
2278+
// NoFieldIsZero asserts that object, which must be a struct or eventually
2279+
// reference to one, has no exported field with a value that is zero.
2280+
//
2281+
// The assertion is not recursive, meaning it only checks that the exported
2282+
// fields of the struct (including any embedded structs) are not zero values.
2283+
// It does not check any fields of nested or embedded structs.
2284+
func NoFieldIsZero(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
22862285
if h, ok := t.(tHelper); ok {
22872286
h.Helper()
22882287
}
22892288

2289+
if reflect.TypeOf(object).Kind() == reflect.Ptr {
2290+
return NoFieldIsZero(t, reflect.ValueOf(object).Elem().Interface(), msgAndArgs)
2291+
}
2292+
22902293
objectType := reflect.TypeOf(object)
22912294
if objectType.Kind() != reflect.Struct {
22922295
return Fail(t, "Input must be a struct or eventually reference one", msgAndArgs...)
@@ -2300,13 +2303,13 @@ func NoFieldIsEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) b
23002303
continue
23012304
}
23022305

2303-
if isEmpty(objectValue.Field(i).Interface()) {
2306+
if objectValue.Field(i).IsZero() {
23042307
emptyFields = append(emptyFields, field.Name)
23052308
}
23062309
}
23072310

23082311
if len(emptyFields) > 0 {
2309-
return Fail(t, fmt.Sprintf("Object contained empty fields: %s", strings.Join(emptyFields, ", ")), msgAndArgs...)
2312+
return Fail(t, fmt.Sprintf("Object contained fields with zero values: %s", strings.Join(emptyFields, ", ")), msgAndArgs...)
23102313
}
23112314

23122315
return true

assert/assertions_test.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3665,8 +3665,13 @@ func TestNotErrorAs(t *testing.T) {
36653665
}
36663666
}
36673667

3668-
func TestNoFieldIsEmpty(t *testing.T) {
3668+
func TestNoFieldIsZero(t *testing.T) {
36693669
str := "a string"
3670+
type Embeddable struct {
3671+
StringA string
3672+
StringB string
3673+
}
3674+
36703675
tests := []struct {
36713676
name string
36723677
input interface{}
@@ -3676,23 +3681,25 @@ func TestNoFieldIsEmpty(t *testing.T) {
36763681
{
36773682
name: "success",
36783683
input: struct {
3684+
Embeddable
36793685
Float64 float64
36803686
Func func()
36813687
Int int
36823688
Interface interface{}
36833689
Pointer *string
36843690
Slice []string
36853691
String string
3686-
Struct struct{ String string }
3692+
Struct struct{ StringA, StringB string }
36873693
}{
3688-
Float64: 1.5,
3689-
Func: func() {},
3690-
Int: 1,
3691-
Interface: "interface",
3692-
Pointer: &str,
3693-
Slice: []string{"a", "b"},
3694-
String: "a string",
3695-
Struct: struct{ String string }{String: "a nested string"},
3694+
Embeddable: Embeddable{StringA: "string"}, // For Embeddable to be non-zero, only one field its fields needs to be non-zero
3695+
Float64: 1.5,
3696+
Func: func() {},
3697+
Int: 1,
3698+
Interface: "interface",
3699+
Pointer: &str,
3700+
Slice: []string{"a", "b"},
3701+
String: "a string",
3702+
Struct: struct{ StringA, StringB string }{StringA: "a nested string"},
36963703
},
36973704
result: true,
36983705
},
@@ -3713,6 +3720,7 @@ func TestNoFieldIsEmpty(t *testing.T) {
37133720
{
37143721
name: "failure",
37153722
input: struct {
3723+
Embeddable
37163724
Float64 float64
37173725
Func func()
37183726
Int int
@@ -3723,7 +3731,7 @@ func TestNoFieldIsEmpty(t *testing.T) {
37233731
Struct struct{ String string }
37243732
}{},
37253733
result: false,
3726-
resultErrMsg: "Object contained empty fields: Float64, Func, Int, Interface, Pointer, Slice, String, Struct\n",
3734+
resultErrMsg: "Object contained fields with zero values: Embeddable, Float64, Func, Int, Interface, Pointer, Slice, String, Struct\n",
37273735
},
37283736
{
37293737
name: "failure_partial",
@@ -3732,7 +3740,7 @@ func TestNoFieldIsEmpty(t *testing.T) {
37323740
StringB string
37333741
}{StringA: "not_empty"},
37343742
result: false,
3735-
resultErrMsg: "Object contained empty fields: StringB\n",
3743+
resultErrMsg: "Object contained fields with zero values: StringB\n",
37363744
},
37373745
{
37383746
name: "failure_wrong_type",
@@ -3744,7 +3752,7 @@ func TestNoFieldIsEmpty(t *testing.T) {
37443752
for _, tt := range tests {
37453753
t.Run(tt.name, func(t *testing.T) {
37463754
mockT := new(captureTestingT)
3747-
result := NoFieldIsEmpty(mockT, tt.input)
3755+
result := NoFieldIsZero(mockT, tt.input)
37483756
mockT.checkResultAndErrMsg(t, tt.result, result, tt.resultErrMsg)
37493757
})
37503758
}

require/require.go

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

require/require_forward.go

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)