/
pinata_test.go
418 lines (385 loc) · 8.43 KB
/
pinata_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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package pinata_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/robbiev/pinata"
)
func start(t *testing.T) (pinata.Stick, pinata.Pinata) {
const message = `
{
"Name": "Kevin",
"Phone": ["+44 20 7123 4567", "+44 20 4567 7123"],
"Address": {
"Street": "1 Gopher Road",
"City": null
},
"Hobbies": [
{
"Indoors": ["napping", "watching TV", "jumping up and down"],
"Outdoors": ["napping", "hiking", "petanque"]
}
]
}`
var m map[string]interface{}
err := json.Unmarshal([]byte(message), &m)
if err != nil {
t.Log(err)
return nil, pinata.Pinata{}
}
return pinata.New(m)
}
func ExampleStick() {
const message = `
{
"Name": "Kevin",
"Phone": ["+44 20 7123 4567", "+44 20 4567 7123"],
"Address": {
"Street": "1 Gopher Road",
"City": "Gophertown"
}
}`
var m map[string]interface{}
err := json.Unmarshal([]byte(message), &m)
if err != nil {
fmt.Println(err)
return
}
stick, p := pinata.New(m)
type gopher struct {
Name string
Phone string
City string
}
// no error handling here
kevin := gopher{
Name: stick.PathString(p, "Name"),
Phone: stick.IndexString(stick.Path(p, "Phone"), 0),
City: stick.PathString(p, "Address", "City"),
}
if err := stick.ClearError(); err != nil {
fmt.Println(err)
return
}
fmt.Println("Name:", kevin.Name)
fmt.Println("Phone:", kevin.Phone)
fmt.Println("City:", kevin.City)
// Output:
// Name: Kevin
// Phone: +44 20 7123 4567
// City: Gophertown
}
func TestFailureScenario(t *testing.T) {
const message = `
{
"Name": "Kevin",
"Phone": ["+44 20 7123 4567", "+44 20 4567 7123"],
"Address": {
"Street": "1 Gopher Road",
"City": "Gophertown"
}
}`
var m map[string]interface{}
{
err := json.Unmarshal([]byte(message), &m)
if err != nil {
fmt.Println(err)
return
}
}
stick, thePinata := pinata.New(m)
stick.IndexFloat64(stick.Path(thePinata, "Phone"), 1)
err := stick.ClearError()
if err == nil {
t.Error("phone must not be a float64")
return
}
pinataErr := err.(*pinata.Error)
if pinataErr.Reason() != pinata.ErrorReasonIncompatibleType {
t.Error("error reason must be incompatible type")
}
ctx, ok := pinataErr.Context()
if !ok {
t.Error("error must have a context")
}
if ctx.MethodName() != "IndexFloat64" {
t.Error("error method name must be IndexFloat64")
}
if len(ctx.MethodArgs()) != 1 {
t.Error("error method input must have one element")
}
if ctx.MethodArgs()[0] != 1 {
t.Error("error method input must be 1")
}
ctx2, ok2 := ctx.Next()
if !ok2 {
t.Error("error must have a linked context")
}
if ctx2.MethodName() != "Path" {
t.Error("error method name must be Path")
}
if len(ctx2.MethodArgs()) != 1 {
t.Error("error method input must have one element")
}
if ctx2.MethodArgs()[0] != "Phone" {
t.Error("error method input must be 1")
}
_, ok3 := ctx2.Next()
if ok3 {
t.Error("linked context must have no further linked contexts")
}
}
func TestIndex(t *testing.T) {
stick, thePinata := start(t)
{
stick.Index(pinata.Pinata{}, 0)
err := stick.ClearError()
if err == nil {
t.Error("empty pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.Index(stick.Path(thePinata, "Address"), 0)
err := stick.ClearError()
if err == nil {
t.Error("accessing a non-slice pinata by index must result in an error")
} else {
t.Log(err)
}
}
{
stick.Index(stick.Path(thePinata, "Phone"), -1)
err := stick.ClearError()
if err == nil {
t.Error("negative index must result in an error")
} else {
t.Log(err)
}
}
{
stick.Index(stick.Path(thePinata, "Phone"), 1)
if err := stick.ClearError(); err != nil {
t.Error("Phone entry with index 1 must exist", err)
}
}
}
func TestPath(t *testing.T) {
stick, thePinata := start(t)
{
stick.Path(pinata.Pinata{}, "nope")
err := stick.ClearError()
if err == nil {
t.Error("empty pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.Path(thePinata)
err := stick.ClearError()
if err == nil {
t.Error("empty path must result in an error")
} else {
t.Log(err)
}
}
{
stick.Path(thePinata, "nope")
err := stick.ClearError()
if err == nil {
t.Error("non-existent path must result in an error")
} else {
t.Log(err)
}
}
{
stick.Path(thePinata, "nope", "nope")
err := stick.ClearError()
if err == nil {
t.Error("non-existent path must result in an error")
} else {
t.Log(err)
}
}
{
stick.Path(thePinata, "Address", "nope", "nope")
err := stick.ClearError()
if err == nil {
t.Error("non-existent path must result in an error")
} else {
t.Log(err)
}
}
{
stick.Path(thePinata, "Hobbies", "wrongtype", "Indoors")
err := stick.ClearError()
if err == nil {
t.Error("path that hits the wrong type must result in an error")
} else {
t.Log(err)
}
}
{
stick.Path(thePinata, "Address")
if err := stick.ClearError(); err != nil {
t.Error("Address must exist", err)
}
}
{
stick.Path(thePinata, "Address", "Street")
if err := stick.ClearError(); err != nil {
t.Error("Address/Street must exist", err)
}
}
{
stick.Path(thePinata, "Address", "City")
if err := stick.ClearError(); err != nil {
t.Error("Address/City must exist", err)
}
}
}
func TestString(t *testing.T) {
stick := pinata.NewStick()
{
stick.String(pinata.Pinata{})
err := stick.ClearError()
if err == nil {
t.Error("empty pinata must not be string")
} else {
t.Log(err)
}
}
{
stick.String(pinata.NewPinata(0))
err := stick.ClearError()
if err == nil {
t.Error("non-string pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.String(pinata.NewPinata(make(map[string]interface{})))
err := stick.ClearError()
if err == nil {
t.Error("non-string pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.String(pinata.NewPinata(make([]interface{}, 0)))
err := stick.ClearError()
if err == nil {
t.Error("non-string pinata must result in an error")
} else {
t.Log(err)
}
}
if stick.String(pinata.NewPinata("hello")); stick.ClearError() != nil {
t.Error("string pinata must not result in an error")
}
}
func TestFloat64(t *testing.T) {
stick := pinata.NewStick()
{
stick.Float64(pinata.Pinata{})
err := stick.ClearError()
if err == nil {
t.Error("empty pinata must not be a float64")
} else {
t.Log(err)
}
}
{
stick.Float64(pinata.NewPinata(""))
err := stick.ClearError()
if err == nil {
t.Error("non-float64 pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.Float64(pinata.NewPinata(make(map[string]interface{})))
err := stick.ClearError()
if err == nil {
t.Error("non-float64 pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.Float64(pinata.NewPinata(make([]interface{}, 0)))
err := stick.ClearError()
if err == nil {
t.Error("non-float64 pinata must result in an error")
} else {
t.Log(err)
}
}
if stick.Float64(pinata.NewPinata(float64(0))); stick.ClearError() != nil {
t.Error("float64 pinata must not result in an error")
}
}
func TestBool(t *testing.T) {
stick := pinata.NewStick()
{
stick.Bool(pinata.Pinata{})
err := stick.ClearError()
if err == nil {
t.Error("empty pinata must not be a bool")
} else {
t.Log(err)
}
}
{
stick.Bool(pinata.NewPinata(""))
err := stick.ClearError()
if err == nil {
t.Error("non-bool pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.Bool(pinata.NewPinata(make(map[string]interface{})))
err := stick.ClearError()
if err == nil {
t.Error("non-bool pinata must result in an error")
} else {
t.Log(err)
}
}
{
stick.Bool(pinata.NewPinata(make([]interface{}, 0)))
err := stick.ClearError()
if err == nil {
t.Error("non-bool pinata must result in an error")
} else {
t.Log(err)
}
}
if stick.Bool(pinata.NewPinata(true)); stick.ClearError() != nil {
t.Error("bool pinata must not result in an error")
}
}
func TestNullvsAbsent(t *testing.T) {
stick, pinata := start(t)
stick.PathNil(pinata, "Address", "City")
if err := stick.ClearError(); err != nil {
t.Errorf("error: %s", err)
}
if stick.PathNil(pinata, "Address"); stick.ClearError() == nil {
t.Error("Address must not be nil")
}
if stick.PathNil(pinata, "Address", "DoesNotExist"); stick.ClearError() == nil {
t.Error("non-existent path must not be nil")
}
_ = stick.Path(pinata, "does", "not", "exist")
if err := stick.ClearError(); err == nil {
t.Error("non-existent path must result in an error")
}
}