You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.6 KiB

4 years ago
package validator_test
import (
"errors"
"testing"
"time"
4 years ago
"gitoa.ru/go-4devs/console/input/validator"
4 years ago
"gitoa.ru/go-4devs/console/input/value"
"gitoa.ru/go-4devs/console/input/value/flag"
4 years ago
)
func TestNotBlank(t *testing.T) {
cases := map[string]struct {
4 years ago
flag flag.Flag
value value.Value
empty value.Value
4 years ago
}{
4 years ago
"any": {flag: flag.Any, value: value.New(float32(1))},
4 years ago
"array int": {
4 years ago
flag: flag.Int | flag.Array,
4 years ago
value: value.New([]int{1}),
empty: value.New([]int{10, 20, 0}),
},
"array int64": {
4 years ago
flag: flag.Int64 | flag.Array,
4 years ago
value: value.New([]int64{1}),
empty: value.New([]int64{0}),
},
"array uint": {
4 years ago
flag: flag.Uint | flag.Array,
4 years ago
value: value.New([]uint{1}),
empty: value.New([]uint{1, 0}),
},
"array uint64": {
4 years ago
flag: flag.Uint64 | flag.Array,
4 years ago
value: value.New([]uint64{1}),
empty: value.New([]uint64{0}),
},
"array float64": {
4 years ago
flag: flag.Float64 | flag.Array,
4 years ago
value: value.New([]float64{0.2}),
empty: value.New([]float64{0}),
},
"array bool": {
4 years ago
flag: flag.Bool | flag.Array,
4 years ago
value: value.New([]bool{true, false}),
empty: value.New([]bool{}),
},
"array duration": {
4 years ago
flag: flag.Duration | flag.Array,
4 years ago
value: value.New([]time.Duration{time.Second}),
empty: value.New([]time.Duration{time.Second, 0}),
},
"array time": {
4 years ago
flag: flag.Time | flag.Array,
4 years ago
value: value.New([]time.Time{time.Now()}),
empty: value.New([]time.Time{{}, time.Now()}),
},
"array string": {
4 years ago
flag: flag.Array,
4 years ago
value: value.New([]string{"value"}),
empty: value.New([]string{""}),
},
"int": {
4 years ago
flag: flag.Int,
4 years ago
value: value.New(int(1)),
},
"int64": {
4 years ago
flag: flag.Int64,
4 years ago
value: value.New(int64(2)),
},
"uint": {
4 years ago
flag: flag.Uint,
4 years ago
value: value.New(uint(1)),
empty: value.New([]uint{1}),
},
"uint64": {
4 years ago
flag: flag.Uint64,
4 years ago
value: value.New(uint64(10)),
},
"float64": {
4 years ago
flag: flag.Float64,
4 years ago
value: value.New(float64(.00001)),
},
"duration": {
4 years ago
flag: flag.Duration,
4 years ago
value: value.New(time.Minute),
empty: value.New("same string"),
},
4 years ago
"time": {flag: flag.Time, value: value.New(time.Now())},
4 years ago
"string": {value: value.New("string"), empty: value.New("")},
}
for name, ca := range cases {
valid := validator.NotBlank(ca.flag)
if err := valid(ca.value); err != nil {
t.Errorf("case: %s, expected error <nil>, got: %s", name, err)
}
if ca.empty == nil {
ca.empty = value.Empty
4 years ago
}
if err := valid(ca.empty); err == nil || !errors.Is(err, validator.ErrNotBlank) {
t.Errorf("case: %s, expect: %s, got:%s", name, validator.ErrNotBlank, err)
}
}
}