Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1131,14 +1131,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 @@ -1172,6 +1173,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 @@ -1182,6 +1184,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 @@ -1196,13 +1199,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