forked from jamescun/legit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegit_test.go
155 lines (128 loc) · 3.64 KB
/
legit_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package legit
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLegit_New(t *testing.T) {
v := New()
assert.False(t, v.Strict)
}
func TestValidate(t *testing.T) {
err := Validate(struct {
Name Lower
}{"FOO"})
if assert.NotNil(t, err) {
assert.Equal(t, Errors{StructError{Field: "Name", Message: errLower}}, err)
}
}
func TestLegit_Validate(t *testing.T) {
err := legit.Validate([]Lower{"FOO"})
if assert.NotNil(t, err) {
assert.Equal(t, Errors{SliceError{Index: 0, Message: errLower}}, err)
}
}
func TestLegit_Validate_customValidator(t *testing.T) {
err := legit.Validate(Lower("foo"))
assert.NoError(t, err)
err = legit.Validate(Lower("FOO"))
if assert.NotNil(t, err) {
assert.Equal(t, errLower, err)
}
}
func TestLegit_validate_struct(t *testing.T) {
err := legit.validate(reflected(struct {
Name Lower
}{"FOO"}))
if assert.NotNil(t, err) {
assert.Equal(t, Errors{StructError{Field: "Name", Message: errLower}}, err)
}
}
func TestLegit_validate_slice(t *testing.T) {
err := legit.validate(reflected([]Lower{"FOO"}))
if assert.NotNil(t, err) {
assert.Equal(t, Errors{SliceError{Index: 0, Message: errLower}}, err)
}
}
func TestLegit_validate_customValidator(t *testing.T) {
err := legit.validate(reflected(Lower("foo")))
assert.NoError(t, err)
err = legit.validate(reflected(Lower("FOO")))
if assert.NotNil(t, err) {
assert.Equal(t, errLower, err)
}
}
func TestLegit_validate_strict(t *testing.T) {
l := Legit{Strict: true}
err := l.validate(reflected("foo"))
assert.Equal(t, ErrStrict, err)
}
func TestLegit_validate_unknown(t *testing.T) {
err := legit.validate(reflected("foo"))
assert.NoError(t, err)
}
func TestValidateStruct(t *testing.T) {
err := ValidateStruct(struct {
Name Lower
}{"FOO"})
if assert.NotNil(t, err) {
assert.Equal(t, Errors{StructError{Field: "Name", Message: errLower}}, err)
}
}
func TestLegit_ValidateStruct(t *testing.T) {
err := legit.ValidateStruct(Lower("foo"))
assert.Equal(t, ErrNotStruct, err)
err = legit.ValidateStruct(struct {
Name Lower
}{"FOO"})
if assert.NotNil(t, err) {
assert.Equal(t, Errors{StructError{Field: "Name", Message: errLower}}, err)
}
}
func TestLegit_validateStruct(t *testing.T) {
err := legit.validateStruct(reflected(struct {
Name Lower
name Lower // unexported fields should NOT be validated
}{"foo", "FOO"}))
assert.NoError(t, err)
err = legit.validateStruct(reflected(struct {
First Lower
Last Lower
}{"foo", "FOO"}))
if assert.NotNil(t, err) {
assert.Equal(t, Errors{StructError{Field: "Last", Message: errLower}}, err)
}
}
func TestValidateSlice(t *testing.T) {
err := ValidateSlice([]Lower{"FOO"})
if assert.NotNil(t, err) {
assert.Equal(t, Errors{SliceError{Index: 0, Message: errLower}}, err)
}
}
func TestLegit_ValidateSlice(t *testing.T) {
err := legit.ValidateSlice(Lower("foo"))
assert.Equal(t, ErrNotSlice, err)
err = legit.ValidateSlice([]Lower{"FOO"})
if assert.NotNil(t, err) {
assert.Equal(t, Errors{SliceError{Index: 0, Message: errLower}}, err)
}
}
func TestLegit_validateSlice(t *testing.T) {
err := legit.validateSlice(reflected([]Lower{}))
assert.NoError(t, err)
err = legit.validateSlice(reflected([]Lower{"foo"}))
assert.NoError(t, err)
err = legit.validateSlice(reflected([]Lower{"foo", "FOO"}))
if assert.NotNil(t, err) {
assert.Equal(t, Errors{SliceError{Index: 1, Message: errLower}}, err)
}
}
func TestResolvePointer(t *testing.T) {
v := resolvePointer(reflect.ValueOf(&struct{ Foo string }{"bar"}))
assert.Equal(t, reflect.Struct, v.Kind())
}
func reflected(src interface{}) (objv reflect.Value, objt reflect.Type) {
objv = reflect.ValueOf(src)
objt = objv.Type()
return
}