forked from gocraft/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nested_test.go
194 lines (160 loc) · 5.05 KB
/
nested_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
package meta
import (
"net/url"
"testing"
)
type nested struct {
A String `meta_required:"true"`
B String
C struct {
D String `meta_required:"true"`
E String
} `meta_required:"true"`
F struct {
G String `meta_required:"true"`
H String
}
}
var withNestedDecoder = NewDecoder(&nested{})
func TestNestedSuccess(t *testing.T) {
var inputs nested
e := withNestedDecoder.DecodeValues(&inputs, url.Values{
"a": {"1"},
"c.d": {"2"},
})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, "1")
assertEqual(t, inputs.C.D.Val, "2")
assertEqual(t, inputs.C.E.Present, false)
assertEqual(t, inputs.C.E.Val, "")
assertEqual(t, inputs.F.G.Val, "")
assertEqual(t, inputs.F.H.Present, false)
assertEqual(t, inputs.F.H.Val, "")
inputs = nested{}
e = withNestedDecoder.DecodeJSON(&inputs, []byte(`{"a":1,"c":{"d":2}}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, "1")
assertEqual(t, inputs.C.D.Val, "2")
assertEqual(t, inputs.C.E.Present, false)
assertEqual(t, inputs.C.E.Val, "")
assertEqual(t, inputs.F.G.Val, "")
assertEqual(t, inputs.F.H.Present, false)
assertEqual(t, inputs.F.H.Val, "")
// The downside of this design is that it's not clear whether F.G has a valid value in it.
// (Use a pointer to a struct if you need to know)
}
func TestNestedErrors(t *testing.T) {
var inputs nested
e := withNestedDecoder.DecodeValues(&inputs, url.Values{
"b": {"1"},
})
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrRequired})
inputs = nested{}
e = withNestedDecoder.DecodeJSON(&inputs, []byte(`{"b":1}`))
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrRequired})
inputs = nested{}
e = withNestedDecoder.DecodeValues(&inputs, url.Values{
"c.e": {"1"},
})
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrorHash{"d": ErrRequired}})
inputs = nested{}
e = withNestedDecoder.DecodeJSON(&inputs, []byte(`{"c":{"e":1}}`))
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrorHash{"d": ErrRequired}})
}
type nestedWithPointers struct {
A String `meta_required:"true"`
B String
C *struct {
D String `meta_required:"true"`
E String
} `meta_required:"true"`
F *struct {
G String `meta_required:"true"`
H String
}
}
var withNestedPointersDecoder = NewDecoder(&nestedWithPointers{})
func TestNestedWithPointersSuccess(t *testing.T) {
var inputs nestedWithPointers
e := withNestedPointersDecoder.DecodeValues(&inputs, url.Values{
"a": {"1"},
"c.d": {"2"},
})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, "1")
assert(t, inputs.C != nil)
if inputs.C != nil {
assertEqual(t, inputs.C.D.Val, "2")
assertEqual(t, inputs.C.E.Present, false)
assertEqual(t, inputs.C.E.Val, "")
}
assert(t, inputs.F == nil)
inputs = nestedWithPointers{}
e = withNestedPointersDecoder.DecodeJSON(&inputs, []byte(`{"a":1,"c":{"d":2}}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, "1")
assert(t, inputs.C != nil)
if inputs.C != nil {
assertEqual(t, inputs.C.D.Val, "2")
assertEqual(t, inputs.C.E.Present, false)
assertEqual(t, inputs.C.E.Val, "")
}
assert(t, inputs.F == nil)
}
func TestNestedWithPointersErrors(t *testing.T) {
var inputs nestedWithPointers
e := withNestedPointersDecoder.DecodeValues(&inputs, url.Values{
"b": {"1"},
})
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrRequired})
inputs = nestedWithPointers{}
e = withNestedPointersDecoder.DecodeJSON(&inputs, []byte(`{"b":1}`))
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrRequired})
inputs = nestedWithPointers{}
e = withNestedPointersDecoder.DecodeValues(&inputs, url.Values{
"c.e": {"1"},
})
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrorHash{"d": ErrRequired}})
inputs = nestedWithPointers{}
e = withNestedPointersDecoder.DecodeJSON(&inputs, []byte(`{"c":{"e":1}}`))
assertEqual(t, e, ErrorHash{"a": ErrRequired, "c": ErrorHash{"d": ErrRequired}})
}
type Embedded struct {
A String `meta_required:"true"`
B String `meta_required:"true"`
}
type embedder struct {
Embedded `meta_required:"true"`
C String `meta_required:"true"`
}
var withEmbedDecoder = NewDecoder(&embedder{})
func TestEmbeddingSuccess(t *testing.T) {
var inputs embedder
e := withEmbedDecoder.DecodeValues(&inputs, url.Values{
"a": {"1"},
"b": {"2"},
"c": {"3"},
})
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, "1")
assertEqual(t, inputs.B.Val, "2")
assertEqual(t, inputs.C.Val, "3")
inputs = embedder{}
e = withEmbedDecoder.DecodeJSON(&inputs, []byte(`{"a":1,"b":2,"c":3}`))
assertEqual(t, e, ErrorHash(nil))
assertEqual(t, inputs.A.Val, "1")
assertEqual(t, inputs.B.Val, "2")
assertEqual(t, inputs.C.Val, "3")
}
func TestEmbeddingErrors(t *testing.T) {
var inputs embedder
e := withEmbedDecoder.DecodeValues(&inputs, url.Values{
"a": {"1"},
})
assertEqual(t, e, ErrorHash{"b": ErrRequired, "c": ErrRequired})
inputs = embedder{}
e = withEmbedDecoder.DecodeJSON(&inputs, []byte(`{"a":1}`))
assertEqual(t, e, ErrorHash{"b": ErrRequired, "c": ErrRequired})
}
// TODO: Test embedding where embedded struct is a ptr
// TODO: test embedding which embeds even more shit