-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint_test.go
More file actions
376 lines (344 loc) · 9.59 KB
/
int_test.go
File metadata and controls
376 lines (344 loc) · 9.59 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package schema
import (
"math"
"testing"
)
// Test Int Schema
func TestIntSchema_Basic(t *testing.T) {
ctx := DefaultValidationContext()
schema := Int()
tests := []struct {
name string
value interface{}
expected bool
}{
{"valid int", 42, true},
{"zero", 0, true},
{"negative int", -42, true},
{"max int", math.MaxInt, true},
{"min int", math.MinInt, true},
{"float", 3.14, false},
{"string", "42", false},
{"boolean", true, false},
{"nil", nil, false},
{"array", []int{1, 2, 3}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Int.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
}
})
}
}
func TestIntSchema_MinMax(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *IntSchema
value int
expected bool
}{
{"min valid", Int().Min(10), 15, true},
{"min invalid", Int().Min(10), 5, false},
{"min exact", Int().Min(10), 10, true},
{"max valid", Int().Max(100), 50, true},
{"max invalid", Int().Max(100), 150, false},
{"max exact", Int().Max(100), 100, true},
{"range valid", Int().Min(10).Max(100), 50, true},
{"range too small", Int().Min(10).Max(100), 5, false},
{"range too large", Int().Min(10).Max(100), 150, false},
{"negative range", Int().Min(-100).Max(-10), -50, true},
{"zero in range", Int().Min(-10).Max(10), 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("IntSchema.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if !result.Valid && len(result.Errors) > 0 {
t.Logf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
func TestIntSchema_Enum(t *testing.T) {
ctx := DefaultValidationContext()
schema := Int().Enum([]int{1, 2, 3, 5, 8, 13})
tests := []struct {
name string
value int
expected bool
}{
{"enum valid 1", 1, true},
{"enum valid 13", 13, true},
{"enum invalid", 4, false},
{"enum negative", -1, false},
{"enum zero", 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("IntSchema.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
}
})
}
}
// Test Int8 Schema
func TestInt8Schema_Basic(t *testing.T) {
ctx := DefaultValidationContext()
schema := Int8()
tests := []struct {
name string
value interface{}
expected bool
}{
{"valid int8", int8(42), true},
{"valid int within range", 100, true},
{"max int8", int8(127), true},
{"min int8", int8(-128), true},
{"int too large", 200, false},
{"int too small", -200, false},
{"float", 3.14, false},
{"string", "42", false},
{"nil", nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Int8.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if !result.Valid && len(result.Errors) > 0 {
t.Logf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Int16 Schema
func TestInt16Schema_Basic(t *testing.T) {
ctx := DefaultValidationContext()
schema := Int16()
tests := []struct {
name string
value interface{}
expected bool
}{
{"valid int16", int16(1000), true},
{"valid int within range", 5000, true},
{"max int16", int16(32767), true},
{"min int16", int16(-32768), true},
{"int too large", 40000, false},
{"int too small", -40000, false},
{"float", 3.14, false},
{"string", "1000", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Int16.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
}
})
}
}
// Test Int32 Schema
func TestInt32Schema_Basic(t *testing.T) {
ctx := DefaultValidationContext()
schema := Int32()
tests := []struct {
name string
value interface{}
expected bool
}{
{"valid int32", int32(1000000), true},
{"valid int within range", 1000000, true},
{"max int32", int32(2147483647), true},
{"min int32", int32(-2147483648), true},
{"int64 too large", int64(3000000000), false},
{"int64 too small", int64(-3000000000), false},
{"float", 3.14, false},
{"string", "1000000", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Int32.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
}
})
}
}
// Test Int64 Schema
func TestInt64Schema_Basic(t *testing.T) {
ctx := DefaultValidationContext()
schema := Int64()
tests := []struct {
name string
value interface{}
expected bool
}{
{"valid int64", int64(9223372036854775807), true},
{"valid int", 1000000, true},
{"max int64", int64(9223372036854775807), true},
{"min int64", int64(-9223372036854775808), true},
{"zero", int64(0), true},
{"float", 3.14, false},
{"string", "1000000", false},
{"boolean", true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Int64.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
}
})
}
}
// Test Combined Integer Constraints
func TestIntegerSchemas_Combined(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema Parseable
value interface{}
expected bool
}{
{"int with min/max/enum", Int().Min(0).Max(100).Enum([]int{10, 20, 30}), 20, true},
{"int with constraints invalid", Int().Min(0).Max(100).Enum([]int{10, 20, 30}), 15, false},
{"int8 with min/max", Int8().Min(0).Max(100), 50, true},
{"int8 with constraints invalid", Int8().Min(0).Max(100), 150, false},
{"int16 required", Int16().Required(), int16(1000), true},
{"int32 const", Int32().Const(12345), int32(12345), true},
{"int32 const invalid", Int32().Const(12345), int32(54321), false},
{"int64 default", Int64().Default(999), int64(123), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Schema.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if !result.Valid && len(result.Errors) > 0 {
t.Logf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test JSON Schema Generation
func TestIntegerSchemas_JSON(t *testing.T) {
tests := []struct {
name string
schema Parseable
expected map[string]interface{}
}{
{
name: "basic int",
schema: Int(),
expected: map[string]interface{}{
"type": "integer",
},
},
{
name: "int with constraints",
schema: Int().Min(0).Max(100),
expected: map[string]interface{}{
"type": "integer",
"minimum": 0,
"maximum": 100,
},
},
{
name: "int8 with enum",
schema: Int8().Enum([]int8{1, 2, 3}),
expected: map[string]interface{}{
"type": "integer",
"enum": []interface{}{int8(1), int8(2), int8(3)},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Use type assertion to access JSON method
var result map[string]interface{}
switch s := tt.schema.(type) {
case *IntSchema:
result = s.JSON()
case *Int8Schema:
result = s.JSON()
case *Int16Schema:
result = s.JSON()
case *Int32Schema:
result = s.JSON()
case *Int64Schema:
result = s.JSON()
default:
t.Fatalf("Unknown schema type: %T", tt.schema)
}
for key, expectedValue := range tt.expected {
actualValue, exists := result[key]
if !exists {
t.Errorf("JSON() missing field %s", key)
continue
}
// Handle slice comparison specially for enum
if key == "enum" {
expectedSlice, ok1 := expectedValue.([]interface{})
actualSlice, ok2 := actualValue.([]interface{})
if !ok1 || !ok2 {
t.Errorf("JSON()[%s] type mismatch", key)
continue
}
if len(expectedSlice) != len(actualSlice) {
t.Errorf("JSON()[%s] length mismatch: got %v, want %v", key, actualSlice, expectedSlice)
continue
}
for i, expectedItem := range expectedSlice {
if actualSlice[i] != expectedItem {
t.Errorf("JSON()[%s][%d] = %v, want %v", key, i, actualSlice[i], expectedItem)
}
}
} else {
if actualValue != expectedValue {
t.Errorf("JSON()[%s] = %v, want %v", key, actualValue, expectedValue)
}
}
}
})
}
}
// Test Edge Cases
func TestIntegerSchemas_EdgeCases(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema Parseable
value interface{}
expected bool
}{
{"int8 overflow", Int8(), 128, false},
{"int8 underflow", Int8(), -129, false},
{"int16 overflow", Int16(), 32768, false},
{"int16 underflow", Int16(), -32769, false},
{"int32 overflow", Int32(), int64(2147483648), false},
{"int32 underflow", Int32(), int64(-2147483649), false},
{"negative zero", Int(), -0, true},
{"int min constraint", Int().Min(math.MinInt), math.MinInt, true},
{"int max constraint", Int().Max(math.MaxInt), math.MaxInt, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Schema.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if !result.Valid && len(result.Errors) > 0 {
t.Logf("Error: %s", result.Errors[0].Message)
}
}
})
}
}