Skip to content

Commit

Permalink
arrays value types in a zero-initialized state are considered empty (#…
Browse files Browse the repository at this point in the history
…1126)

* fix .Empty assertion with Array types

* refactor .Empty assertion for array types
  • Loading branch information
adamluzsi committed Jun 20, 2022
1 parent 07dc7ee commit 840cb80
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions assert/assertions.go
Expand Up @@ -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())
Expand Down
7 changes: 6 additions & 1 deletion assert/assertions_test.go
Expand Up @@ -1145,14 +1145,15 @@ 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"}))
False(t, isEmpty(1))
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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -1210,13 +1213,15 @@ 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")
True(t, NotEmpty(mockT, []string{"something"}), "Non empty string array 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, chWithValue), "Channel with values is not empty")
True(t, NotEmpty(mockT, [1]int{42}), "array is not state")
}

func Test_getLen(t *testing.T) {
Expand Down

0 comments on commit 840cb80

Please sign in to comment.