-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnumbers_custom_test.go
315 lines (287 loc) · 7.51 KB
/
numbers_custom_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package zog
import (
"testing"
"github.com/Oudwins/zog/conf"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
type MyInt int64
func CustomInt(opts ...SchemaOption) *NumberSchema[MyInt] {
s := &NumberSchema[MyInt]{}
opts = append(
[]SchemaOption{
WithCoercer(func(x any) (any, error) {
v, e := conf.DefaultCoercers.Int(x)
if e != nil {
return nil, e
}
return MyInt(v.(int)), e
}),
},
opts...,
)
for _, opt := range opts {
opt(s)
}
return s
}
func TestCustomNumberParse(t *testing.T) {
dest := MyInt(0)
validator := CustomInt()
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(5), dest)
}
func TestCustomNumberParseFormatter(t *testing.T) {
dest := MyInt(0)
fmt := WithIssueFormatter(func(e *ZogIssue, ctx Ctx) {
e.SetMessage("test2")
})
validator := CustomInt().GTE(10, Message("test1"))
errs := validator.Parse(5, &dest, fmt)
assert.Equal(t, "test1", errs[0].Message)
validator2 := CustomInt().GTE(10)
errs2 := validator2.Parse(5, &dest, fmt)
assert.Equal(t, "test2", errs2[0].Message)
}
func TestCustomNumberSchemaOption(t *testing.T) {
s := CustomInt(WithCoercer(func(original any) (value any, err error) {
return MyInt(42), nil
}))
var result MyInt
err := s.Parse("123", &result)
assert.Nil(t, err)
assert.Equal(t, MyInt(42), result)
}
func TestCustomNumberRequired(t *testing.T) {
validator := CustomInt().Required(Message("custom"))
var dest MyInt
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(5), dest)
dest = 0
errs = validator.Parse("", &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
errs = validator.Parse(" ", &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
errs = validator.Parse(nil, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
}
func TestCustomNumberOptional(t *testing.T) {
validator := CustomInt().Optional()
dest := MyInt(0)
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(nil, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(5), dest)
}
func TestCustomNumberDefault(t *testing.T) {
validator := CustomInt().Default(10)
dest := MyInt(0)
errs := validator.Parse(nil, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(10), dest)
}
func TestCustomNumberCatch(t *testing.T) {
validator := CustomInt().Catch(0)
dest := MyInt(0)
errs := validator.Parse("not a number", &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(0), dest)
}
func TestCustomNumberPreTransform(t *testing.T) {
preTransform := func(val any, ctx Ctx) (any, error) {
switch v := val.(type) {
case MyInt:
return int(v) * 2, nil
case int:
return v * 2, nil
default:
return val, nil
}
}
validator := CustomInt().PreTransform(preTransform)
var dest MyInt
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(10), dest)
}
func TestCustomNumberPostTransform(t *testing.T) {
postTransform := func(val any, ctx Ctx) error {
if v, ok := val.(*MyInt); ok {
*v += 1
}
return nil
}
validator := CustomInt().PostTransform(postTransform)
var dest MyInt
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(6), dest)
}
func TestCustomNumberMultipleTransforms(t *testing.T) {
preTransform := func(val any, ctx Ctx) (any, error) {
if v, ok := val.(int); ok {
return v * 2, nil
}
return val, nil
}
postTransform := func(val any, ctx Ctx) error {
if v, ok := val.(*MyInt); ok {
*v += 1
}
return nil
}
validator := CustomInt().PreTransform(preTransform).PostTransform(postTransform)
var dest MyInt
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(11), dest)
}
func TestCustomNumberOneOf(t *testing.T) {
validator := CustomInt().OneOf([]MyInt{1, 2, 3}, Message("custom"))
dest := MyInt(0)
errs := validator.Parse(1, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(4, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
assert.Equal(t, MyInt(4), dest)
}
func TestCustomNumberEq(t *testing.T) {
validator := CustomInt().EQ(5, Message("custom"))
dest := MyInt(0)
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(4, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
assert.Equal(t, MyInt(4), dest)
}
func TestCustomNumberGt(t *testing.T) {
validator := CustomInt().GT(5, Message("custom"))
dest := MyInt(0)
errs := validator.Parse(6, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(5, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
errs = validator.Parse(4, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
assert.Equal(t, MyInt(4), dest)
}
func TestCustomNumberGte(t *testing.T) {
dest := MyInt(0)
validator := CustomInt().GTE(5, Message("custom"))
errs := validator.Parse(6, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(4, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
assert.Equal(t, MyInt(4), dest)
}
func TestCustomNumberLt(t *testing.T) {
dest := MyInt(0)
validator := CustomInt().LT(5, Message("custom"))
errs := validator.Parse(4, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(5, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
errs = validator.Parse(6, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
assert.Equal(t, MyInt(6), dest)
}
func TestCustomNumberLte(t *testing.T) {
dest := MyInt(0)
validator := CustomInt().LTE(5, Message("custom"))
errs := validator.Parse(4, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(6, &dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs[0].Message)
assert.Equal(t, MyInt(6), dest)
}
func TestCustomNumberCustomTest(t *testing.T) {
validator := CustomInt().TestFunc(func(val any, ctx Ctx) bool {
// Custom test logic here
assert.Equal(t, MyInt(5), val)
return true
}, Message("custom"))
dest := MyInt(0)
errs := validator.Parse(5, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, MyInt(5), dest)
}
func TestCustomNumberGetType(t *testing.T) {
i := CustomInt()
assert.Equal(t, zconst.TypeNumber, i.getType())
}