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

pkg/testutil: ensure types in RequireEqualEmptyNil #355

Merged
merged 1 commit into from Dec 28, 2021
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
8 changes: 5 additions & 3 deletions pkg/testutil/require.go
Expand Up @@ -15,14 +15,16 @@ func RequireEqualEmptyNil(t *testing.T, expected, actual interface{}, msgAndArgs
expectedVal := reflect.ValueOf(expected)
actualVal := reflect.ValueOf(actual)

if hasLength(expectedVal) && hasLength(actualVal) && expectedVal.Len() == 0 && actualVal.Len() == 0 {
if expectedVal.Kind() == actualVal.Kind() &&
hasLength(expectedVal.Kind()) &&
expectedVal.Len() == 0 && actualVal.Len() == 0 {
return
}
require.Equal(t, expected, actual, msgAndArgs...)
}

func hasLength(v reflect.Value) bool {
switch v.Kind() {
func hasLength(k reflect.Kind) bool {
switch k {
case reflect.Array, reflect.Slice, reflect.Map:
return true
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/testutil/require_test.go
@@ -0,0 +1,10 @@
package testutil

import "testing"

func TestRequireEqualEmptyNil(t *testing.T) {
RequireEqualEmptyNil(t, []int(nil), []int(nil))
RequireEqualEmptyNil(t, []int(nil), []int{})
RequireEqualEmptyNil(t, []int{}, []int(nil))
RequireEqualEmptyNil(t, []int{}, []int{})
}