-
Notifications
You must be signed in to change notification settings - Fork 34
/
decoder_test.go
372 lines (315 loc) · 5.37 KB
/
decoder_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
// OpenRDAP
// Copyright 2017 Tom Harwood
// MIT License, see the LICENSE file.
package rdap
import (
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/openrdap/rdap/test"
)
func TestDecodeEmpty(t *testing.T) {
type Empty struct {
}
runDecodeAndCompareTest(t, &Empty{}, `
{}
`, &Empty{})
}
func TestDecodeDecodeData(t *testing.T) {
type XYZ struct {
DecodeData *DecodeData
S1 string
S2 string `rdap:"s2Name"`
SF string
}
result, ok := runDecode(t, &XYZ{}, `
{
"s1": "S1",
"s2Name": "S2",
"sF": 1.5,
"unknown" : "value"
}`)
if !ok {
return
}
x := result.(*XYZ)
if x.S1 != "S1" || x.S2 != "S2" || x.SF != "1.5" {
t.Errorf("Decode values bad %v", x)
}
if x.DecodeData == nil {
t.Errorf("DecodeData not instantiated")
} else if len(x.DecodeData.Notes("sF")) != 1 {
t.Errorf("DecodeData notes not added")
} else if len(x.DecodeData.Fields()) != 4 {
t.Errorf("DecodeData Fields() bad")
} else if len(x.DecodeData.UnknownFields()) != 1 {
t.Errorf("DecodeData UnknownFields() bad")
} else if !reflect.DeepEqual(x.DecodeData.Value("unknown"), "value") {
t.Errorf("DecodeData bad Value()")
}
}
func TestDecodeVCard(t *testing.T) {
type XYZ struct {
VCard *VCard
}
result, ok := runDecode(t, &XYZ{}, `
{
"vCard": [
"vcard",
[
["version", {}, "text", "4.0"],
["fn", {}, "text", "First Last"]
]
]
}
`)
if !ok {
return
}
x := result.(*XYZ)
if x.VCard == nil {
t.Errorf("VCard not decoded")
} else if len(x.VCard.Properties) != 2 {
t.Errorf("VCard properties not decoded")
}
}
func TestDecodeSlice(t *testing.T) {
type XYZ struct {
S []string
}
runDecodeAndCompareTest(t, &XYZ{}, `
{
"s": ["a", "b"]
}
`, &XYZ{
S: []string{"a", "b"},
})
}
func TestDecodeMap(t *testing.T) {
type XYZ struct {
M map[string]string
}
runDecodeAndCompareTest(t, &XYZ{}, `
{
"m": {"a": "av", "b": "bv"}
}
`, &XYZ{
M: map[string]string{"a": "av", "b": "bv"},
})
}
func TestDecodeUints(t *testing.T) {
type XYZ struct {
A uint8
AOverflow uint8
B uint16
C uint32
D uint64
S uint8
BF uint8
BT uint8
N uint8
}
runDecodeAndCompareTest(t, &XYZ{}, `
{
"a": 100,
"aOverflow": 256,
"b": 200,
"c": 42,
"d": 43,
"s": "10",
"bF": false,
"bT": true,
"n": null
}
`, &XYZ{
A: 100,
AOverflow: 0,
B: 200,
C: 42,
D: 43,
S: 10,
BF: 0,
BT: 1,
N: 0,
})
}
func TestDecodeInts(t *testing.T) {
type XYZ struct {
A int8
AUnderflow int8
AOverflow int8
B int16
C int32
D int64
S int8
BF int8
BT int8
N int8
}
runDecodeAndCompareTest(t, &XYZ{}, `
{
"a": 100,
"aUnderflow": -129,
"aOverflow": 128,
"b": 200,
"c": 42,
"d": 43,
"s": "10",
"bF": false,
"bT": true,
"n": null
}
`, &XYZ{
A: 100,
AUnderflow: 0,
AOverflow: 0,
B: 200,
C: 42,
D: 43,
S: 10,
BF: 0,
BT: 1,
N: 0,
})
}
func TestDecodeFloat64(t *testing.T) {
type XYZ struct {
F float64
FPtr *float64
S1 float64
S2 float64
BF float64
BT float64
N float64
}
fptr := 1.5
runDecodeAndCompareTest(t, &XYZ{}, `
{
"f": 1.5,
"fPtr": 1.5,
"s1": "1.5",
"s2": "-1.5",
"bF": false,
"bT": true,
"n": null
}
`, &XYZ{
F: 1.5,
FPtr: &fptr,
S1: 1.5,
S2: -1.5,
BF: 0.0,
BT: 1.0,
N: 0.0,
})
}
func TestDecodeBool(t *testing.T) {
type XYZ struct {
B bool
BPtr *bool
SF bool
ST bool
FF bool
FT bool
N bool
}
bptr := true
runDecodeAndCompareTest(t, &XYZ{}, `
{
"b": true,
"bPtr": true,
"sF": "false",
"sT": "true",
"fF": 0,
"fT": 1,
"n": null
}
`, &XYZ{
B: true,
BPtr: &bptr,
ST: true,
SF: false,
FF: false,
FT: true,
N: false,
})
}
func TestDecodeString(t *testing.T) {
type XYZ struct {
S string
SPtr *string
BT string
BF string
F1 string
F2 string
N string
}
sptr := "sptr"
runDecodeAndCompareTest(t, &XYZ{}, `
{
"s": "test",
"sPtr": "sptr",
"bT": true,
"bF": false,
"f1": 1.0,
"f2": -3.14,
"n2": null
}
`, &XYZ{
S: "test",
SPtr: &sptr,
BT: "true",
BF: "false",
F1: "1",
F2: "-3.14",
N: "",
})
}
func TestDecodeBug1(t *testing.T) {
jsonBlob := test.LoadFile("rdap/rdap-pilot.verisignlabs.com/entity-1-VRSN")
d := NewDecoder([]byte(jsonBlob))
result, err := d.Decode()
t.Logf("%s %s\n", result, err)
}
func TestDecodeMismatchedTypes(t *testing.T) {
type C struct {
}
type XYZ struct {
A []string
B map[string]string
C C
}
runDecodeAndCompareTest(t, &XYZ{}, `
{
"a": {},
"b": [1,2,3],
"c": false
}
`, &XYZ{
A: nil,
B: nil,
C: C{},
})
}
func runDecode(t *testing.T, target interface{}, jsonBlob string) (interface{}, bool) {
d := NewDecoder([]byte(jsonBlob))
d.target = target
result, err := d.Decode()
if err != nil {
t.Errorf("While decoding '%s', got error: %s", jsonBlob, err)
return result, false
}
return result, true
}
func runDecodeAndCompareTest(t *testing.T, target interface{}, jsonBlob string, expected interface{}) {
result, ok := runDecode(t, target, jsonBlob)
if !ok {
return
}
if !reflect.DeepEqual(expected, result) {
t.Errorf("While decoding '%s':\nexpected %s\ngot %s",
jsonBlob,
spew.Sdump(expected),
spew.Sdump(result))
}
}