forked from wacul/valval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
values_test.go
71 lines (62 loc) · 1.58 KB
/
values_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package valval
import (
"errors"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestValuValidators(t *testing.T) {
checkValid := func(v Validator, val interface{}, expected bool) {
err := v.Validate(val)
if expected == true {
if err != nil {
t.Errorf("validation for %v ( %v ) should be valid", v, val)
}
} else {
if err == nil {
t.Errorf("validation for %v ( %v ) should be invalid", v, val)
}
}
}
i1 := NewIntValidator(func(v int64) error {
if v >= 0 && v <= 100 {
return nil
}
return errors.New("invalid value")
})
s1 := NewStringValidator(func(v string) error {
if v == "abc" {
return nil
}
return errors.New("invalid value")
})
Convey("Values validator", t, func() {
Convey("int", func() {
checkValid(Number(i1), 10, true)
checkValid(Number(i1), 1000, false)
checkValid(Number(i1), nil, true)
checkValid(Number(i1), "string", false)
checkValid(Number(i1), true, false)
checkValid(Number(i1), struct{}{}, false)
})
Convey("string", func() {
checkValid(String(s1), "abc", true)
checkValid(String(s1), "abcd", false)
checkValid(String(s1), 123, false)
})
Convey("bool", func() {
checkValid(Bool(), "abc", false)
checkValid(Bool(), 123, false)
checkValid(Bool(), struct{}{}, false)
checkValid(Bool(), nil, true)
checkValid(Bool(), true, true)
checkValid(Bool(), false, true)
})
Convey("any", func() {
checkValid(Any(), "abc", true)
checkValid(Any(), 123, true)
checkValid(Any(), true, true)
checkValid(Any(), struct{}{}, true)
checkValid(Any(), nil, true)
})
})
}