mirror of https://github.com/stretchr/testify.git
arrays value types in a zero-initialized state are considered empty (#1126)
* fix .Empty assertion with Array types * refactor .Empty assertion for array typespull/1207/head
parent
07dc7ee5ab
commit
840cb80149
|
@ -563,16 +563,17 @@ func isEmpty(object interface{}) bool {
|
|||
|
||||
switch objValue.Kind() {
|
||||
// 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
|
||||
// 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:
|
||||
if objValue.IsNil() {
|
||||
return true
|
||||
}
|
||||
deref := objValue.Elem().Interface()
|
||||
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:
|
||||
zero := reflect.Zero(objValue.Type())
|
||||
return reflect.DeepEqual(object, zero.Interface())
|
||||
|
|
|
@ -1145,6 +1145,7 @@ func Test_isEmpty(t *testing.T) {
|
|||
True(t, isEmpty(new(time.Time)))
|
||||
True(t, isEmpty(time.Time{}))
|
||||
True(t, isEmpty(make(chan struct{})))
|
||||
True(t, isEmpty([1]int{}))
|
||||
False(t, isEmpty("something"))
|
||||
False(t, isEmpty(errors.New("something")))
|
||||
False(t, isEmpty([]string{"something"}))
|
||||
|
@ -1152,7 +1153,7 @@ func Test_isEmpty(t *testing.T) {
|
|||
False(t, isEmpty(true))
|
||||
False(t, isEmpty(map[string]string{"Hello": "World"}))
|
||||
False(t, isEmpty(chWithValue))
|
||||
|
||||
False(t, isEmpty([1]int{42}))
|
||||
}
|
||||
|
||||
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, TString("")), "empty aliased string 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, 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, 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, [1]int{42}), "array is not state")
|
||||
}
|
||||
|
||||
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, false), "False value 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, 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, true), "True value 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) {
|
||||
|
|
Loading…
Reference in New Issue