arrays value types in a zero-initialized state are considered empty (#1126)

* fix .Empty assertion with Array types

* refactor .Empty assertion for array types
pull/1207/head
Adam Luzsi 2022-06-20 12:44:27 +02:00 committed by GitHub
parent 07dc7ee5ab
commit 840cb80149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -563,16 +563,17 @@ func isEmpty(object interface{}) bool {
switch objValue.Kind() { switch objValue.Kind() {
// collection types are empty when they have no element // collection types are empty when they have no element
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: case reflect.Chan, reflect.Map, reflect.Slice:
return objValue.Len() == 0 return objValue.Len() == 0
// pointers are empty if nil or if the value they point to is empty // pointers are empty if nil or if the value they point to is empty
case reflect.Ptr: case reflect.Ptr:
if objValue.IsNil() { if objValue.IsNil() {
return true return true
} }
deref := objValue.Elem().Interface() deref := objValue.Elem().Interface()
return isEmpty(deref) return isEmpty(deref)
// for all other types, compare against the zero value // for all other types, compare against the zero value
// array types are empty when they match their zero-initialized state
default: default:
zero := reflect.Zero(objValue.Type()) zero := reflect.Zero(objValue.Type())
return reflect.DeepEqual(object, zero.Interface()) return reflect.DeepEqual(object, zero.Interface())

View File

@ -1145,6 +1145,7 @@ func Test_isEmpty(t *testing.T) {
True(t, isEmpty(new(time.Time))) True(t, isEmpty(new(time.Time)))
True(t, isEmpty(time.Time{})) True(t, isEmpty(time.Time{}))
True(t, isEmpty(make(chan struct{}))) True(t, isEmpty(make(chan struct{})))
True(t, isEmpty([1]int{}))
False(t, isEmpty("something")) False(t, isEmpty("something"))
False(t, isEmpty(errors.New("something"))) False(t, isEmpty(errors.New("something")))
False(t, isEmpty([]string{"something"})) False(t, isEmpty([]string{"something"}))
@ -1152,7 +1153,7 @@ func Test_isEmpty(t *testing.T) {
False(t, isEmpty(true)) False(t, isEmpty(true))
False(t, isEmpty(map[string]string{"Hello": "World"})) False(t, isEmpty(map[string]string{"Hello": "World"}))
False(t, isEmpty(chWithValue)) False(t, isEmpty(chWithValue))
False(t, isEmpty([1]int{42}))
} }
func TestEmpty(t *testing.T) { func TestEmpty(t *testing.T) {
@ -1186,6 +1187,7 @@ func TestEmpty(t *testing.T) {
True(t, Empty(mockT, TStruct{}), "struct with zero values is empty") True(t, Empty(mockT, TStruct{}), "struct with zero values is empty")
True(t, Empty(mockT, TString("")), "empty aliased string is empty") True(t, Empty(mockT, TString("")), "empty aliased string is empty")
True(t, Empty(mockT, sP), "ptr to nil value is empty") True(t, Empty(mockT, sP), "ptr to nil value is empty")
True(t, Empty(mockT, [1]int{}), "array is state")
False(t, Empty(mockT, "something"), "Non Empty string is not empty") False(t, Empty(mockT, "something"), "Non Empty string is not empty")
False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty") False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty")
@ -1196,6 +1198,7 @@ func TestEmpty(t *testing.T) {
False(t, Empty(mockT, TStruct{x: 1}), "struct with initialized values is empty") False(t, Empty(mockT, TStruct{x: 1}), "struct with initialized values is empty")
False(t, Empty(mockT, TString("abc")), "non-empty aliased string is empty") False(t, Empty(mockT, TString("abc")), "non-empty aliased string is empty")
False(t, Empty(mockT, xP), "ptr to non-nil value is not empty") False(t, Empty(mockT, xP), "ptr to non-nil value is not empty")
False(t, Empty(mockT, [1]int{42}), "array is not state")
} }
func TestNotEmpty(t *testing.T) { func TestNotEmpty(t *testing.T) {
@ -1210,6 +1213,7 @@ func TestNotEmpty(t *testing.T) {
False(t, NotEmpty(mockT, 0), "Zero int value is empty") False(t, NotEmpty(mockT, 0), "Zero int value is empty")
False(t, NotEmpty(mockT, false), "False value is empty") False(t, NotEmpty(mockT, false), "False value is empty")
False(t, NotEmpty(mockT, make(chan struct{})), "Channel without values is empty") False(t, NotEmpty(mockT, make(chan struct{})), "Channel without values is empty")
False(t, NotEmpty(mockT, [1]int{}), "array is state")
True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty") True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty")
True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty") True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty")
@ -1217,6 +1221,7 @@ func TestNotEmpty(t *testing.T) {
True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty") True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty")
True(t, NotEmpty(mockT, true), "True value is not empty") True(t, NotEmpty(mockT, true), "True value is not empty")
True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty") True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty")
True(t, NotEmpty(mockT, [1]int{42}), "array is not state")
} }
func Test_getLen(t *testing.T) { func Test_getLen(t *testing.T) {