-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_test.go
88 lines (77 loc) · 3.85 KB
/
validator_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package csvlib
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ValidatorLT(t *testing.T) {
assert.Nil(t, ValidatorLT(100)(99))
assert.Nil(t, ValidatorLT(100)(0))
assert.ErrorIs(t, ValidatorLT(100)("abc"), ErrValidationConversion)
assert.ErrorIs(t, ValidatorLT(100)(100), ErrValidationLT)
assert.ErrorIs(t, ValidatorLT(100)(101), ErrValidation)
}
func Test_ValidatorLTE(t *testing.T) {
assert.Nil(t, ValidatorLTE(int64(0))(int64(0)))
assert.Nil(t, ValidatorLTE(int64(0))(int64(-1)))
assert.ErrorIs(t, ValidatorLTE(100)(true), ErrValidationConversion)
assert.ErrorIs(t, ValidatorLTE(int64(0))(int64(1)), ErrValidationLTE)
assert.ErrorIs(t, ValidatorLTE(int64(0))(int64(100)), ErrValidation)
}
func Test_ValidatorGT(t *testing.T) {
assert.Nil(t, ValidatorGT(100)(101))
assert.Nil(t, ValidatorGT(100)(10000))
assert.ErrorIs(t, ValidatorGT(100)(int8(1)), ErrValidationConversion)
assert.ErrorIs(t, ValidatorGT(100)(100), ErrValidationGT)
assert.ErrorIs(t, ValidatorGT(100)(99), ErrValidation)
}
func Test_ValidatorGTE(t *testing.T) {
assert.Nil(t, ValidatorGTE(int64(0))(int64(0)))
assert.Nil(t, ValidatorGTE(int64(0))(int64(1)))
assert.ErrorIs(t, ValidatorGTE(100)(int32(1)), ErrValidationConversion)
assert.ErrorIs(t, ValidatorGTE(int64(0))(int64(-1)), ErrValidationGTE)
assert.ErrorIs(t, ValidatorGTE(int64(0))(int64(-10)), ErrValidation)
}
func Test_ValidatorRange(t *testing.T) {
assert.Nil(t, ValidatorRange(0, 10)(0))
assert.Nil(t, ValidatorRange(0, 10)(10))
assert.ErrorIs(t, ValidatorRange(0, 10)(int32(1)), ErrValidationConversion)
assert.ErrorIs(t, ValidatorRange("a", "g")("h"), ErrValidationRange)
assert.ErrorIs(t, ValidatorRange("a", "g")("0bc"), ErrValidation)
}
func Test_ValidatorIN(t *testing.T) {
assert.Nil(t, ValidatorIN("a", "b", "c")("b"))
assert.Nil(t, ValidatorIN("a", "b", "")(""))
assert.ErrorIs(t, ValidatorIN("a", "b", "")(1), ErrValidationConversion)
assert.ErrorIs(t, ValidatorIN("a", "b", "")("c"), ErrValidationIN)
assert.ErrorIs(t, ValidatorIN("a", "b", "")("d"), ErrValidation)
}
func Test_ValidatorStrLen(t *testing.T) {
lenFn := func(s string) int { return len(s) }
assert.Nil(t, ValidatorStrLen[string](0, 5)("abc"))
assert.Nil(t, ValidatorStrLen[string](0, 5)(""))
assert.Nil(t, ValidatorStrLen[string](0, 5, lenFn)("abc"))
assert.Nil(t, ValidatorStrLen[StrType](0, 5)(StrType("abc")))
assert.ErrorIs(t, ValidatorStrLen[string](0, 5)(StrType("abc")), ErrValidationConversion)
assert.ErrorIs(t, ValidatorStrLen[string](1, 5)(""), ErrValidationStrLen)
assert.ErrorIs(t, ValidatorStrLen[string](0, 5)("abc123"), ErrValidation)
assert.ErrorIs(t, ValidatorStrLen[string](0, 5, lenFn)("abc123"), ErrValidation)
assert.ErrorIs(t, ValidatorStrLen[StrType](0, 5)(StrType("abc123")), ErrValidation)
}
func Test_ValidatorStrPrefix(t *testing.T) {
assert.Nil(t, ValidatorStrPrefix[string]("a")("abc"))
assert.Nil(t, ValidatorStrPrefix[string](" a")(" abc"))
assert.Nil(t, ValidatorStrPrefix[StrType](" a")(StrType(" abc")))
assert.ErrorIs(t, ValidatorStrPrefix[string]("x")(StrType("abc")), ErrValidationConversion)
assert.ErrorIs(t, ValidatorStrPrefix[string]("x")("abc"), ErrValidationStrPrefix)
assert.ErrorIs(t, ValidatorStrPrefix[string]("x")("abc"), ErrValidation)
assert.ErrorIs(t, ValidatorStrPrefix[StrType]("x")(StrType("abc123")), ErrValidationStrPrefix)
}
func Test_ValidatorStrSuffix(t *testing.T) {
assert.Nil(t, ValidatorStrSuffix[string]("c")("abc"))
assert.Nil(t, ValidatorStrSuffix[string]("c ")(" abc "))
assert.Nil(t, ValidatorStrSuffix[StrType]("c ")(StrType("abc ")))
assert.ErrorIs(t, ValidatorStrSuffix[string]("x")(StrType("abc")), ErrValidationConversion)
assert.ErrorIs(t, ValidatorStrSuffix[string]("x")("abc"), ErrValidationStrSuffix)
assert.ErrorIs(t, ValidatorStrSuffix[string]("x")("abc"), ErrValidation)
assert.ErrorIs(t, ValidatorStrSuffix[StrType]("x")(StrType("abc123")), ErrValidationStrSuffix)
}