Skip to content

Commit

Permalink
Merge pull request #355 from authzed/testutil-fix
Browse files Browse the repository at this point in the history
pkg/testutil: ensure types in RequireEqualEmptyNil
  • Loading branch information
jzelinskie committed Dec 28, 2021
2 parents 698ec60 + c024a1b commit 9a8f598
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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{})
}

0 comments on commit 9a8f598

Please sign in to comment.