Skip to content

Commit 840cb80

Browse files
authored
arrays value types in a zero-initialized state are considered empty (#1126)
* fix .Empty assertion with Array types * refactor .Empty assertion for array types
1 parent 07dc7ee commit 840cb80

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

assert/assertions.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,17 @@ func isEmpty(object interface{}) bool {
563563

564564
switch objValue.Kind() {
565565
// collection types are empty when they have no element
566-
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
566+
case reflect.Chan, reflect.Map, reflect.Slice:
567567
return objValue.Len() == 0
568-
// pointers are empty if nil or if the value they point to is empty
568+
// pointers are empty if nil or if the value they point to is empty
569569
case reflect.Ptr:
570570
if objValue.IsNil() {
571571
return true
572572
}
573573
deref := objValue.Elem().Interface()
574574
return isEmpty(deref)
575-
// for all other types, compare against the zero value
575+
// for all other types, compare against the zero value
576+
// array types are empty when they match their zero-initialized state
576577
default:
577578
zero := reflect.Zero(objValue.Type())
578579
return reflect.DeepEqual(object, zero.Interface())

assert/assertions_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1145,14 +1145,15 @@ func Test_isEmpty(t *testing.T) {
11451145
True(t, isEmpty(new(time.Time)))
11461146
True(t, isEmpty(time.Time{}))
11471147
True(t, isEmpty(make(chan struct{})))
1148+
True(t, isEmpty([1]int{}))
11481149
False(t, isEmpty("something"))
11491150
False(t, isEmpty(errors.New("something")))
11501151
False(t, isEmpty([]string{"something"}))
11511152
False(t, isEmpty(1))
11521153
False(t, isEmpty(true))
11531154
False(t, isEmpty(map[string]string{"Hello": "World"}))
11541155
False(t, isEmpty(chWithValue))
1155-
1156+
False(t, isEmpty([1]int{42}))
11561157
}
11571158

11581159
func TestEmpty(t *testing.T) {
@@ -1186,6 +1187,7 @@ func TestEmpty(t *testing.T) {
11861187
True(t, Empty(mockT, TStruct{}), "struct with zero values is empty")
11871188
True(t, Empty(mockT, TString("")), "empty aliased string is empty")
11881189
True(t, Empty(mockT, sP), "ptr to nil value is empty")
1190+
True(t, Empty(mockT, [1]int{}), "array is state")
11891191

11901192
False(t, Empty(mockT, "something"), "Non Empty string is not empty")
11911193
False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty")
@@ -1196,6 +1198,7 @@ func TestEmpty(t *testing.T) {
11961198
False(t, Empty(mockT, TStruct{x: 1}), "struct with initialized values is empty")
11971199
False(t, Empty(mockT, TString("abc")), "non-empty aliased string is empty")
11981200
False(t, Empty(mockT, xP), "ptr to non-nil value is not empty")
1201+
False(t, Empty(mockT, [1]int{42}), "array is not state")
11991202
}
12001203

12011204
func TestNotEmpty(t *testing.T) {
@@ -1210,13 +1213,15 @@ func TestNotEmpty(t *testing.T) {
12101213
False(t, NotEmpty(mockT, 0), "Zero int value is empty")
12111214
False(t, NotEmpty(mockT, false), "False value is empty")
12121215
False(t, NotEmpty(mockT, make(chan struct{})), "Channel without values is empty")
1216+
False(t, NotEmpty(mockT, [1]int{}), "array is state")
12131217

12141218
True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty")
12151219
True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty")
12161220
True(t, NotEmpty(mockT, []string{"something"}), "Non empty string array is not empty")
12171221
True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty")
12181222
True(t, NotEmpty(mockT, true), "True value is not empty")
12191223
True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty")
1224+
True(t, NotEmpty(mockT, [1]int{42}), "array is not state")
12201225
}
12211226

12221227
func Test_getLen(t *testing.T) {

0 commit comments

Comments
 (0)