forked from Oudwins/zog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slices_test.go
218 lines (182 loc) · 4.82 KB
/
slices_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
package zog
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// !STRUCTS
// NOT YET SUPPORTED. Should be simple. Just need to check if its a struct processor in which case we need to build an any data provider
//
// func TestSliceStruct(t *testing.T) {
// type TestStruct struct {
// T string
// }
// schema := Slice(Struct(Schema{"t": String().Required()}))
// sl := []TestStruct{}
// errs := schema.Parse([]any{map[string]any{"t": "a"}, map[string]any{"t": "b"}}, &sl)
// }
func TestSlicePassSchema(t *testing.T) {
s := []string{}
schema := Slice(String().Required())
errs := schema.Parse([]any{"a", "b", "c"}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, s[0], "a")
assert.Equal(t, s[1], "b")
assert.Equal(t, s[2], "c")
}
func TestSliceErrors(t *testing.T) {
s := []string{}
schema := Slice(String().Required().Min(2))
errs := schema.Parse([]any{"a", "b"}, &s)
assert.Len(t, errs, 3)
assert.NotEmpty(t, errs["[0]"])
assert.NotEmpty(t, errs["[1]"])
assert.Empty(t, errs["[2]"])
}
func TestSliceLen(t *testing.T) {
s := []string{}
els := []string{"a", "b", "c", "d", "e"}
schema := Slice(String().Required()).Len(2)
errs := schema.Parse(els[:2], &s)
assert.Len(t, s, 2)
assert.Nil(t, errs)
errs = schema.Parse(els[:1], &s)
assert.NotEmpty(t, errs)
// min
schema = Slice(String().Required()).Min(2)
errs = schema.Parse(els[:4], &s)
assert.Nil(t, errs)
errs = schema.Parse(els[:1], &s)
assert.NotEmpty(t, errs)
// max
schema = Slice(String().Required()).Max(3)
errs = schema.Parse(els[:1], &s)
assert.Nil(t, errs)
errs = schema.Parse(els[:4], &s)
assert.NotNil(t, errs)
}
func TestSliceContains(t *testing.T) {
s := []string{}
items := []string{"a", "b", "c"}
schema := Slice(String()).Contains("a")
errs := schema.Parse(items, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
schema = Slice(String()).Contains("d")
errs = schema.Parse(items, &s)
assert.NotEmpty(t, errs)
}
func TestSliceDefaultCoercing(t *testing.T) {
s := []string{}
schema := Slice(String())
errs := schema.Parse("a", &s)
assert.Nil(t, errs)
assert.Len(t, s, 1)
assert.Equal(t, s[0], "a")
}
func TestSliceDefault(t *testing.T) {
schema := Slice(String()).Default([]string{"a", "b", "c"})
s := []string{}
err := schema.Parse(nil, &s)
assert.Nil(t, err)
assert.Len(t, s, 3)
assert.Equal(t, s[0], "a")
assert.Equal(t, s[1], "b")
assert.Equal(t, s[2], "c")
}
type User struct {
Name string
}
type Team struct {
Users []User
}
func TestSliceOfStructs(t *testing.T) {
var userSchema = Struct(Schema{
"name": String().Required(),
})
var teamSchema = Struct(Schema{
"users": Slice(userSchema),
})
var data = map[string]interface{}{
"users": []interface{}{
map[string]interface{}{
"name": "Jane",
},
map[string]interface{}{
"name": "John",
},
},
}
var team Team
errsMap := teamSchema.Parse(NewMapDataProvider(data), &team)
assert.Nil(t, errsMap)
assert.Len(t, team.Users, 2)
assert.Equal(t, team.Users[0].Name, "Jane")
assert.Equal(t, team.Users[1].Name, "John")
data = map[string]interface{}{
"users": []interface{}{
map[string]interface{}{},
map[string]interface{}{},
},
}
errsMap = teamSchema.Parse(NewMapDataProvider(data), &team)
assert.Len(t, errsMap["users[0].name"], 1)
assert.Len(t, errsMap["users[1].name"], 1)
}
func TestSliceCustomTest(t *testing.T) {
input := []string{"abc", "defg", "hijkl"}
s := []string{}
schema := Slice(String()).Test(TestFunc("custom_test", func(val any, ctx ParseCtx) bool {
// Custom test logic here
x := val.(*[]string)
return assert.Equal(t, input, *x)
}))
errs := schema.Parse(input, &s)
assert.Empty(t, errs)
}
func TestSliceWithStringInput(t *testing.T) {
input := "not a slice"
s := []string{}
schema := Slice(String()).Required()
errs := schema.Parse(input, &s)
assert.Nil(t, errs)
assert.Equal(t, s, []string{input})
}
func TestSliceOptionalSlice(t *testing.T) {
s := []string{}
schema := Slice(String())
errs := schema.Parse(nil, &s)
assert.Nil(t, errs)
assert.Len(t, s, 0)
}
func TestSlicePostTransform(t *testing.T) {
s := []string{}
schema := Slice(String()).PostTransform(func(dataPtr any, ctx ParseCtx) error {
s := dataPtr.(*[]string)
for i := 0; i < len(*s); i++ {
(*s)[i] = strings.ToUpper((*s)[i])
}
return nil
})
errs := schema.Parse([]string{"a", "b", "c"}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, []string{"A", "B", "C"}, s)
}
func TestSliceCustomPreTransform(t *testing.T) {
s := []string{}
schema := Slice(String()).PreTransform(func(data any, ctx ParseCtx) (any, error) {
s := data.([]string)
for i := 0; i < len(s); i++ {
s[i] = strings.ToUpper(s[i])
}
return s, nil
})
errs := schema.Parse([]string{"a", "b", "c"}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, s[0], "A")
assert.Equal(t, s[1], "B")
assert.Equal(t, s[2], "C")
}