-
Notifications
You must be signed in to change notification settings - Fork 7
/
pointers_test.go
200 lines (170 loc) · 3.56 KB
/
pointers_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
package zog
import (
"testing"
"github.com/stretchr/testify/assert"
p "github.com/Oudwins/zog/internals"
)
func TestPtrPrimitive(t *testing.T) {
// in := 10
var out *int
s := Ptr(Int().Required())
err := s.Parse("", &out)
assert.Empty(t, err)
assert.Nil(t, out)
err = s.Parse("not_empty", &out)
assert.NotNil(t, err)
assert.Equal(t, 0, *out)
err = s.Parse(10, &out)
assert.Empty(t, err)
assert.Equal(t, 10, *out)
// with zero value it also works
err = s.Parse(0, &out)
assert.Empty(t, err)
assert.Equal(t, 0, *out)
}
func TestPtrInStruct(t *testing.T) {
type TestStruct struct {
Value *int
}
s := Struct(Schema{
"value": Ptr(Int()),
})
in := map[string]any{
"value": 10,
}
var out TestStruct
err := s.Parse(in, &out)
assert.Nil(t, err)
assert.NotNil(t, out)
assert.NotNil(t, out.Value)
assert.Equal(t, 10, *out.Value)
}
func TestPtrPtrInStruct(t *testing.T) {
type TestStruct struct {
Value **int
}
s := Struct(Schema{
"value": Ptr(Ptr(Int())),
})
in := map[string]any{
"value": 10,
}
var out TestStruct
// empty input
err := s.Parse("", &out)
assert.Empty(t, err)
assert.Nil(t, out.Value)
// good input
err = s.Parse(in, &out)
assert.Nil(t, err)
assert.NotNil(t, out)
assert.NotNil(t, out.Value)
assert.NotNil(t, *out.Value)
assert.Equal(t, 10, **out.Value)
}
func TestPtrNestedStructs(t *testing.T) {
type Inner struct {
Value *int
}
type Outer struct {
Inner *Inner
}
schema := Struct(Schema{
"inner": Ptr(Struct(Schema{
"value": Ptr(Int()),
})),
})
var out Outer
data := map[string]any{
"inner": map[string]any{
"value": 10,
},
}
err := schema.Parse(data, &out)
assert.Nil(t, err)
assert.NotNil(t, out.Inner)
assert.NotNil(t, out.Inner.Value)
assert.Equal(t, 10, *out.Inner.Value)
}
func TestPtrInSlice(t *testing.T) {
schema := Slice(Ptr(Int()))
var out []*int
data := []any{10, 20, 30}
err := schema.Parse(data, &out)
assert.Nil(t, err)
assert.Len(t, out, 3)
assert.Equal(t, 10, *out[0])
assert.Equal(t, 20, *out[1])
assert.Equal(t, 30, *out[2])
}
func TestPtrSliceStruct(t *testing.T) {
type TestStruct struct {
Value int
}
schema := Slice(Ptr(Struct(Schema{
"value": Int(),
})))
var out []*TestStruct
data := []any{
map[string]any{"value": 10},
map[string]any{"value": 20},
map[string]any{"value": 30},
}
err := schema.Parse(data, &out)
assert.Nil(t, err)
assert.Len(t, out, 3)
assert.Equal(t, 10, out[0].Value)
assert.Equal(t, 20, out[1].Value)
assert.Equal(t, 30, out[2].Value)
}
func TestPtrRequired(t *testing.T) {
schema := Ptr(String()).NotNil(Message("Testing"))
var dest *string
tests := []struct {
Val any
ExpectedErr bool
}{
{nil, true},
{"", true},
{0, false},
{false, false},
}
for _, test := range tests {
err := schema.Parse(test.Val, &dest)
if test.ExpectedErr {
assert.NotNil(t, err)
x := err[p.ERROR_KEY_ROOT]
assert.Equal(t, "Testing", x[0].Message())
} else {
assert.Nil(t, err)
}
}
}
func TestPtrToStruct(t *testing.T) {
type TestStruct struct {
Value *int
}
var dest *TestStruct
s := Ptr(Struct(Schema{
"value": Ptr(Int()),
}))
in := map[string]any{
"value": 10,
}
err := s.Parse(in, &dest)
assert.Nil(t, err)
assert.NotNil(t, dest)
assert.NotNil(t, dest.Value)
assert.Equal(t, 10, *dest.Value)
}
func TestPtrToSlice(t *testing.T) {
var dest *[]*int
s := Ptr(Slice(Ptr(Int())))
err := s.Parse([]any{10, 20, 30}, &dest)
assert.Nil(t, err)
assert.NotNil(t, dest)
assert.Len(t, *dest, 3)
assert.Equal(t, 10, *(*dest)[0])
assert.Equal(t, 20, *(*dest)[1])
assert.Equal(t, 30, *(*dest)[2])
}