forked from Oudwins/zog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslices_test.go
106 lines (88 loc) · 2.39 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
package zog
import (
"fmt"
"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)
fmt.Println(s)
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")
}