Skip to content

Commit

Permalink
Add test cases for IsZeroOfUnderlyingType
Browse files Browse the repository at this point in the history
Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>
  • Loading branch information
aeneasr committed Dec 9, 2021
1 parent c47e802 commit 950114c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions associations/association.go
Expand Up @@ -160,5 +160,9 @@ func fieldIsNil(f reflect.Value) bool {

// IsZeroOfUnderlyingType will check if the value of anything is the equal to the Zero value of that type.
func IsZeroOfUnderlyingType(x interface{}) bool {
if x == nil {
return true
}

return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}
37 changes: 37 additions & 0 deletions associations/association_test.go
@@ -0,0 +1,37 @@
package associations

import (
"database/sql"
"fmt"
"github.com/gobuffalo/nulls"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_IsZeroOfUnderlyingType(t *testing.T) {
for k, tc := range []struct {
in interface{}
zero bool
}{
{in: nil, zero: true},
{in: 0, zero: true},
{in: 1, zero: false},
{in: false, zero: true},
{in: "", zero: true},
{in: interface{}(nil), zero: true},
{in: uuid.NullUUID{}, zero: true},
{in: uuid.UUID{}, zero: true},
{in: uuid.NullUUID{Valid: true}, zero: false},
{in: nulls.Int{}, zero: true},
{in: nulls.String{}, zero: true},
{in: nulls.Bool{}, zero: true},
{in: nulls.Float64{}, zero: true},
{in: sql.NullString{}, zero: true},
{in: sql.NullString{Valid: true}, zero: false},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
assert.EqualValues(t, tc.zero, IsZeroOfUnderlyingType(tc.in))
})
}
}

0 comments on commit 950114c

Please sign in to comment.