2021-06-21 19:30:33 +00:00
|
|
|
package validator_test
|
2019-05-03 18:09:04 +00:00
|
|
|
|
|
|
|
import (
|
2019-05-04 08:35:29 +00:00
|
|
|
"fmt"
|
2019-05-03 18:09:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
"github.com/xdrm-io/aicra/validator"
|
2019-05-03 18:09:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAny_AvailableTypes(t *testing.T) {
|
2019-05-04 08:11:47 +00:00
|
|
|
t.Parallel()
|
2019-05-03 18:09:04 +00:00
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
dt := validator.AnyType{}
|
2019-05-03 18:09:04 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Type string
|
|
|
|
Handled bool
|
|
|
|
}{
|
|
|
|
{"any", true},
|
|
|
|
{" any", false},
|
|
|
|
{"any ", false},
|
|
|
|
{" any ", false},
|
|
|
|
{"Any", false},
|
|
|
|
{"ANY", false},
|
|
|
|
{"anything-else", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2021-06-21 19:08:22 +00:00
|
|
|
validator := dt.Validator(test.Type)
|
2019-05-03 18:09:04 +00:00
|
|
|
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-05-03 18:09:04 +00:00
|
|
|
if test.Handled {
|
|
|
|
t.Errorf("expect %q to be handled", test.Type)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.Handled {
|
|
|
|
t.Errorf("expect %q NOT to be handled", test.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAny_AlwaysTrue(t *testing.T) {
|
2019-05-04 08:11:47 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-05-03 18:09:04 +00:00
|
|
|
const typeName = "any"
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
validator := validator.AnyType{}.Validator(typeName)
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-05-03 18:09:04 +00:00
|
|
|
t.Errorf("expect %q to be handled", typeName)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
values := []interface{}{
|
|
|
|
1,
|
|
|
|
0.1,
|
|
|
|
nil,
|
|
|
|
"string",
|
|
|
|
[]byte("bytes"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, value := range values {
|
2019-05-04 08:35:29 +00:00
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
2020-03-14 15:13:38 +00:00
|
|
|
if _, isValid := validator(value); !isValid {
|
2019-05-04 08:35:29 +00:00
|
|
|
t.Errorf("expect value to be valid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|