2021-06-21 19:30:33 +00:00
|
|
|
package validator_test
|
2019-11-18 15:17:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-20 19:52:43 +00:00
|
|
|
"reflect"
|
2019-11-18 15:17:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
"github.com/xdrm-io/aicra/validator"
|
2019-11-18 15:17:02 +00:00
|
|
|
)
|
|
|
|
|
2021-06-20 19:52:43 +00:00
|
|
|
func TestBool_ReflectType(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
dt = validator.BoolType{}
|
|
|
|
expected = reflect.TypeOf(true)
|
|
|
|
)
|
|
|
|
if dt.GoType() != expected {
|
|
|
|
t.Fatalf("invalid GoType() %v ; expected %v", dt.GoType(), expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:17:02 +00:00
|
|
|
func TestBool_AvailableTypes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
dt := validator.BoolType{}
|
2019-11-18 15:17:02 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Type string
|
|
|
|
Handled bool
|
|
|
|
}{
|
|
|
|
{"bool", true},
|
|
|
|
{"Bool", false},
|
|
|
|
{"boolean", false},
|
|
|
|
{" bool", false},
|
|
|
|
{"bool ", false},
|
|
|
|
{" bool ", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Type, func(t *testing.T) {
|
2021-06-21 19:08:22 +00:00
|
|
|
validator := dt.Validator(test.Type)
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-11-18 15:17:02 +00:00
|
|
|
if test.Handled {
|
|
|
|
t.Errorf("expect %q to be handled", test.Type)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.Handled {
|
|
|
|
t.Errorf("expect %q NOT to be handled", test.Type)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBool_Values(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const typeName = "bool"
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
validator := validator.BoolType{}.Validator(typeName)
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-11-18 15:17:02 +00:00
|
|
|
t.Errorf("expect %q to be handled", typeName)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Value interface{}
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{true, true},
|
|
|
|
{false, true},
|
|
|
|
{1, false},
|
|
|
|
{0, false},
|
|
|
|
{-1, false},
|
|
|
|
|
|
|
|
// json number
|
|
|
|
{"-1", false},
|
|
|
|
{"0", false},
|
|
|
|
{"1", false},
|
|
|
|
|
|
|
|
// json string
|
|
|
|
{"true", true},
|
|
|
|
{"false", true},
|
|
|
|
{[]byte("true"), true},
|
|
|
|
{[]byte("false"), true},
|
|
|
|
|
|
|
|
{"string", false},
|
|
|
|
{[]byte("bytes"), false},
|
|
|
|
{-0.1, false},
|
|
|
|
{0.1, false},
|
|
|
|
{nil, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
2020-03-14 15:13:38 +00:00
|
|
|
if _, isValid := validator(test.Value); isValid {
|
2019-11-18 15:17:02 +00:00
|
|
|
if !test.Valid {
|
|
|
|
t.Errorf("expect value to be invalid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if test.Valid {
|
|
|
|
t.Errorf("expect value to be valid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|