Skip to content

Commit

Permalink
up: add more tests for util func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 19, 2021
1 parent 94e8f2f commit 129d6cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,18 @@ func getVariadicKind(typString string) reflect.Kind {
return reflect.Int8
case "[]int16":
return reflect.Int16
case "[]int32":
return reflect.Int32
case "[]int64":
return reflect.Int64
case "[]uint":
return reflect.Uint
case "[]uint8":
return reflect.Uint8
case "[]uint16":
return reflect.Uint16
case "[]uint32":
return reflect.Uint32
case "[]uint64":
return reflect.Uint64
case "[]string":
Expand Down
44 changes: 44 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
package validate

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUtil_Func_valueToInt64(t *testing.T) {
noErrTests := []struct {
val interface{}
strict bool
want int64
}{
{" 12", false, 12},
{float32(12.23), false, 12},
{12.23, false, 12},
}

for _, item := range noErrTests {
i64, err := valueToInt64(item.val, item.strict)
assert.NoError(t, err)
assert.Equal(t, item.want, i64)
}
}

func TestUtil_Func_getVariadicKind(t *testing.T) {
noErrTests := []struct {
val interface{}
want reflect.Kind
}{
{"invalid", reflect.Invalid},
{[]int{1, 2}, reflect.Int},
{[]int8{1, 2}, reflect.Int8},
{[]int16{1, 2}, reflect.Int16},
{[]int32{1, 2}, reflect.Int32},
{[]int64{1, 2}, reflect.Int64},
{[]uint{1, 2}, reflect.Uint},
{[]uint8{1, 2}, reflect.Uint8},
{[]uint16{1, 2}, reflect.Uint16},
{[]uint32{1, 2}, reflect.Uint32},
{[]uint64{1, 2}, reflect.Uint64},
}

for _, item := range noErrTests {
vt := reflect.TypeOf(item.val)
eleType := getVariadicKind(vt.String())
assert.Equal(t, item.want, eleType)
}
}

func TestMS_String(t *testing.T) {
ms := MS{}

Expand Down

0 comments on commit 129d6cc

Please sign in to comment.