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 TestString_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.StringType{}
|
2019-05-03 18:09:04 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Type string
|
|
|
|
Handled bool
|
|
|
|
}{
|
|
|
|
{"string", true},
|
|
|
|
{"String", false},
|
|
|
|
{"STRING", false},
|
|
|
|
{" string", false},
|
|
|
|
{"string ", false},
|
|
|
|
{" string ", false},
|
|
|
|
|
|
|
|
{"string(1)", true},
|
|
|
|
{"string( 1)", false},
|
|
|
|
{"string(1 )", false},
|
|
|
|
{"string( 1 )", false},
|
|
|
|
|
2020-03-01 20:41:20 +00:00
|
|
|
{"string()", false},
|
|
|
|
{"string(a)", false},
|
|
|
|
{"string(-1)", false},
|
|
|
|
|
|
|
|
{"string(,)", false},
|
|
|
|
{"string(1,b)", false},
|
|
|
|
{"string(a,b)", false},
|
|
|
|
{"string(a,1)", false},
|
|
|
|
{"string(-1,1)", false},
|
|
|
|
{"string(1,-1)", false},
|
|
|
|
{"string(-1,-1)", false},
|
|
|
|
|
2019-05-03 18:09:04 +00:00
|
|
|
{"string(1,2)", true},
|
|
|
|
{"string(1, 2)", true},
|
|
|
|
{"string(1, 2)", false},
|
|
|
|
{"string( 1,2)", false},
|
|
|
|
{"string(1,2 )", false},
|
|
|
|
{"string( 1,2 )", false},
|
|
|
|
{"string( 1, 2)", false},
|
|
|
|
{"string(1, 2 )", false},
|
|
|
|
{"string( 1, 2 )", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2019-05-04 08:35:29 +00:00
|
|
|
t.Run(test.Type, func(t *testing.T) {
|
2021-06-21 19:08:22 +00:00
|
|
|
validator := dt.Validator(test.Type)
|
2019-05-04 08:35:29 +00:00
|
|
|
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-05-04 08:35:29 +00:00
|
|
|
if test.Handled {
|
|
|
|
t.Errorf("expect %q to be handled", test.Type)
|
|
|
|
}
|
|
|
|
return
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 08:35:29 +00:00
|
|
|
if !test.Handled {
|
|
|
|
t.Errorf("expect %q NOT to be handled", test.Type)
|
|
|
|
}
|
|
|
|
})
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestString_AnyLength(t *testing.T) {
|
2019-05-04 08:11:47 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-05-03 18:09:04 +00:00
|
|
|
const typeName = "string"
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
validator := validator.StringType{}.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()
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Value interface{}
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{"string", true},
|
2019-11-18 15:17:02 +00:00
|
|
|
{[]byte("bytes"), true},
|
2019-05-03 18:09:04 +00:00
|
|
|
{1, false},
|
|
|
|
{0.1, false},
|
|
|
|
{nil, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
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(test.Value); isValid {
|
2019-05-04 08:35:29 +00:00
|
|
|
if !test.Valid {
|
|
|
|
t.Errorf("expect value to be invalid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
return
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
2019-05-04 08:35:29 +00:00
|
|
|
if test.Valid {
|
|
|
|
t.Errorf("expect value to be valid")
|
|
|
|
t.Fail()
|
2019-05-03 18:09:04 +00:00
|
|
|
|
2019-05-04 08:35:29 +00:00
|
|
|
}
|
|
|
|
})
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestString_FixedLength(t *testing.T) {
|
2019-05-04 08:11:47 +00:00
|
|
|
t.Parallel()
|
2019-05-03 18:09:04 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Type string
|
|
|
|
Value interface{}
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{"string(0)", "", true},
|
|
|
|
{"string(0)", "", true},
|
|
|
|
{"string(0)", "1", false},
|
|
|
|
|
|
|
|
{"string(16)", "1234567890123456", true},
|
|
|
|
{"string(16)", "123456789012345", false},
|
|
|
|
{"string(16)", "12345678901234567", false},
|
|
|
|
|
|
|
|
{"string(1000)", string(make([]byte, 1000)), true},
|
|
|
|
{"string(1000)", string(make([]byte, 1000-1)), false},
|
|
|
|
{"string(1000)", string(make([]byte, 1000+1)), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2019-05-04 08:35:29 +00:00
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
2021-06-21 19:30:33 +00:00
|
|
|
validator := validator.StringType{}.Validator(test.Type)
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-05-04 08:35:29 +00:00
|
|
|
t.Errorf("expect %q to be handled", test.Type)
|
2019-05-03 18:09:04 +00:00
|
|
|
t.Fail()
|
2019-05-04 08:35:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-14 15:13:38 +00:00
|
|
|
if _, isValid := validator(test.Value); isValid {
|
2019-05-04 08:35:29 +00:00
|
|
|
if !test.Valid {
|
|
|
|
t.Errorf("expect value to be invalid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
return
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
2019-05-04 08:35:29 +00:00
|
|
|
if test.Valid {
|
|
|
|
t.Errorf("expect value to be valid")
|
|
|
|
t.Fail()
|
2019-05-03 18:09:04 +00:00
|
|
|
|
2019-05-04 08:35:29 +00:00
|
|
|
}
|
|
|
|
})
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestString_VariableLength(t *testing.T) {
|
2019-05-04 08:11:47 +00:00
|
|
|
t.Parallel()
|
2019-05-03 18:09:04 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Type string
|
|
|
|
Value interface{}
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{"string(0,0)", "", true},
|
|
|
|
{"string(0,0)", "1", false},
|
|
|
|
|
|
|
|
{"string(0,1)", "", true},
|
|
|
|
{"string(0,1)", "1", true},
|
|
|
|
{"string(0,1)", "12", false},
|
|
|
|
|
|
|
|
{"string(5,16)", "1234", false},
|
|
|
|
{"string(5,16)", "12345", true},
|
|
|
|
{"string(5,16)", "123456", true},
|
|
|
|
{"string(5,16)", "1234567", true},
|
|
|
|
{"string(5,16)", "12345678", true},
|
|
|
|
{"string(5,16)", "123456789", true},
|
|
|
|
{"string(5,16)", "1234567890", true},
|
|
|
|
{"string(5,16)", "12345678901", true},
|
|
|
|
{"string(5,16)", "123456789012", true},
|
|
|
|
{"string(5,16)", "1234567890123", true},
|
|
|
|
{"string(5,16)", "12345678901234", true},
|
|
|
|
{"string(5,16)", "123456789012345", true},
|
|
|
|
{"string(5,16)", "1234567890123456", true},
|
|
|
|
{"string(5,16)", "12345678901234567", false},
|
|
|
|
|
|
|
|
{"string(999,1000)", string(make([]byte, 998)), false},
|
|
|
|
{"string(999,1000)", string(make([]byte, 999)), true},
|
|
|
|
{"string(999,1000)", string(make([]byte, 1000)), true},
|
|
|
|
{"string(999,1000)", string(make([]byte, 1001)), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2019-05-04 08:35:29 +00:00
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
2021-06-21 19:30:33 +00:00
|
|
|
validator := validator.StringType{}.Validator(test.Type)
|
2020-03-14 15:13:38 +00:00
|
|
|
if validator == nil {
|
2019-05-04 08:35:29 +00:00
|
|
|
t.Errorf("expect %q to be handled", test.Type)
|
2019-05-03 18:09:04 +00:00
|
|
|
t.Fail()
|
2019-05-04 08:35:29 +00:00
|
|
|
return
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 15:13:38 +00:00
|
|
|
if _, isValid := validator(test.Value); isValid {
|
2019-05-04 08:35:29 +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()
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
2019-05-03 18:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|