/
decode_test.go
363 lines (339 loc) · 12.4 KB
/
decode_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
// Copyright 2013 Joshua Tacoma. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zpl
import (
"bytes"
"strings"
"testing"
"testing/iotest"
)
var (
raw0 = []byte(`
# Notice that indentation is always 4 spaces, there are no tabs.
#
version = 0.1
context
iothreads = 1
verbose = 1
auxiliary
type = foo
main
type = zmq_queue
frontend
option
hwm = 1000
swap = 25000000
subscribe = "#2"
bind = tcp://eth0:5555
backend
bind = tcp://eth0:5556
bind = inproc://device
`)
raw1 = []byte(`version = 1
# The first line is not a comment. What happens?
words
cat
kind = mammal
alias = feline
dog
kind = mammal
alias = canine`)
bad0 = []byte(`
# This is an example of an invalid ZPL document.
invalid line with spaces`)
bad1 = []byte(`
# This is an example of an invalid ZPL document.
key = overly indented value`)
other_crlf = []byte("key = 1\r\nkey = 0")
other_lfcr = []byte("key = 1\n\rkey = 0")
other_cr = []byte("key = 1\rkey = 0")
other_lf = []byte("key = 1\nkey = 0")
)
func TestUnmarshal_Map(t *testing.T) {
conf := make(map[string]interface{})
err := Unmarshal(raw0, conf)
if err != nil {
t.Fatalf("failed to unmarshal: %s", err)
}
tmp, ok := conf["context"]
if !ok {
t.Fatalf("context not found.")
}
context := tmp.(map[string]interface{})
tmp, ok = context["iothreads"]
if !ok {
t.Fatalf("iothreads not found.")
}
iothreads := tmp.([]string)
if len(iothreads) != 1 {
t.Fatalf("len(iothreads) = %d", len(iothreads))
}
if iothreads[0] != "1" {
t.Fatalf("context/iothreads[0] = %v", iothreads[0])
}
tmp, ok = context["verbose"]
verbose := tmp.([]string)
if verbose[0] != "1" {
t.Fatalf("context/verbose[0] = %v", verbose[0])
}
main := conf["main"].(map[string]interface{})
frontend := main["frontend"].(map[string]interface{})
option := frontend["option"].(map[string]interface{})
subscribe := option["subscribe"].([]string)
if subscribe[0] != "#2" {
t.Fatalf("main/frontend/subscribe[0] = %v (length %d)", subscribe[0], len(subscribe[0]))
}
backend := main["backend"].(map[string]interface{})
ibind, ok := backend["bind"]
if !ok {
t.Fatalf("main/backend/bind = nil")
}
bind := ibind.([]string)
if bind[0] != "tcp://eth0:5556" {
t.Fatalf("main/backend/bind[0] = %v", bind[0])
}
if bind[1] != "inproc://device" {
t.Fatalf("main/backend/bind[1] = %v", bind[1])
}
}
type ZdcfRoot struct {
Context *ZdcfContext `context`
Devices map[string]*ZdcfDevice `*`
Version float32 `version`
}
type ZdcfContext struct {
IoThreads int `iothreads`
Verbose bool `verbose`
}
type ZdcfDevice struct {
Type string `type`
Sockets map[string]*ZdcfSocket `*`
}
type ZdcfSocket struct {
Type string `type`
Options *ZdcfOptions `option`
Bind []string `bind`
Connect []string `connect`
}
type ZdcfOptions struct {
Hwm *int `zpl:"hwm"`
Swap []int64 `swap`
Subscribe []string `zpl:"subscribe"`
}
type Dictionary struct {
Version float32 `version`
Words map[string]*Word `words`
}
type Word struct {
Kind string `kind`
Aliases []string `alias`
}
func TestUnmarshal_Reflect(t *testing.T) {
var conf ZdcfRoot
err := Unmarshal(raw0, &conf)
if err != nil {
t.Fatalf("failed to unmarshal: %s", err)
}
if conf.Version != 0.1 {
t.Fatalf("version = %v", conf.Version)
}
if conf.Context.IoThreads != 1 {
t.Fatalf("context/iothreads = %v", conf.Context.IoThreads)
}
if conf.Context.Verbose != true {
t.Fatalf("context/verbose = %v", conf.Context.Verbose)
}
if conf.Devices["main"].Sockets["frontend"].Options.Hwm == nil {
t.Errorf("main/frontend/hwm = nil")
} else if *conf.Devices["main"].Sockets["frontend"].Options.Hwm != 1000 {
t.Errorf("main/frontend/hwm = %v",
*conf.Devices["main"].Sockets["frontend"].Options.Hwm)
}
if len(conf.Devices["main"].Sockets["frontend"].Options.Swap) != 1 {
t.Errorf("len(main/frontend/hwm) = %s", len(conf.Devices["main"].Sockets["frontend"].Options.Swap))
} else if conf.Devices["main"].Sockets["frontend"].Options.Swap[0] != 25000000 {
t.Errorf("main/frontend/hwm[0] = %v",
conf.Devices["main"].Sockets["frontend"].Options.Swap[0])
}
if conf.Devices["main"].Sockets["frontend"].Options.Subscribe[0] != "#2" {
t.Errorf("main/frontend/subscribe[0] = %v",
conf.Devices["main"].Sockets["frontend"].Options.Subscribe[0])
}
if conf.Devices["main"].Sockets["backend"].Bind[0] != "tcp://eth0:5556" {
t.Errorf("main/backend/bind[0] = %v", conf.Devices["main"].Sockets["backend"].Bind[0])
}
if conf.Devices["main"].Sockets["backend"].Bind[1] != "inproc://device" {
t.Errorf("main/backend/bind[1] = %v", conf.Devices["main"].Sockets["backend"].Bind[1])
}
var dict Dictionary
err = Unmarshal(raw1, &dict)
if err != nil {
t.Fatalf("failed to unmarshal: %s", err)
}
if _, ok := dict.Words["words"]; ok {
t.Fatalf("words/words exists")
}
if dict.Words["cat"].Aliases[0] != "feline" {
t.Fatalf("words/cat/alias[0] = %v", dict.Words["cat"].Aliases[0])
}
}
func TestUnmarshal_Bad(t *testing.T) {
var conf ZdcfRoot
err := Unmarshal(bad0, &conf)
if err == nil {
t.Fatalf("expected error unmarshalling bad0, got none.")
} else {
switch err.(type) {
case *SyntaxError:
synerr := err.(*SyntaxError)
if synerr.Line != 3 {
t.Fatalf("expected syntax error on line 3, got line %d.", synerr.Line)
}
default:
t.Fatalf("expected syntax error, got %T.", err)
}
}
err = Unmarshal(bad1, &conf)
if err == nil {
t.Fatalf("expected error unmarshalling bad1, got none.")
}
}
type decodeCase struct {
Raw []byte
Value interface{}
ErrSub string
}
func TestDecoder_Decode_InvalidUnmarshalError(t *testing.T) {
var i int
invalid := []decodeCase{
{raw0, nil, " nil"},
{raw0, 3, " int"},
{raw0, &i, " *int"},
{raw0, make(map[int]bool), " map[int]bool"},
}
for _, c := range invalid {
reader := bytes.NewReader(c.Raw)
decoder := NewDecoder(reader)
if err := decoder.Decode(c.Value); err == nil {
t.Errorf("expected InvalidUnmarshalError, got nil.")
} else if _, ok := err.(*InvalidUnmarshalError); !ok {
t.Errorf("expected InvalidUnmarshalError, got %T: %s.", err, err.Error())
} else if !strings.Contains(err.Error(), c.ErrSub) {
t.Errorf("expected error message about %s, got %s.", c.ErrSub, err.Error())
}
}
}
func TestDecoder_Decode_MixedLineEndings(t *testing.T) {
weirdos := [][]byte{other_cr, other_crlf, other_lf, other_lfcr}
for _, w := range weirdos {
m := make(map[string]bool)
if err := NewDecoder(bytes.NewReader(w)).Decode(m); err != nil {
t.Errorf("while parsing weird line endings: %T: %s", err, err.Error())
}
}
}
func TestDecoder_Decode_Timeout(t *testing.T) {
m := &decodeCase{}
if err := NewDecoder(iotest.TimeoutReader(bytes.NewReader(raw0))).Decode(m); err == nil {
t.Errorf("expected error, got success.")
}
}
func TestDecoder_Decode_UnmarshalFieldError(t *testing.T) {
m := &decodeCase{}
if err := NewDecoder(bytes.NewReader(raw0)).Decode(m); err == nil {
t.Errorf("expected error, got success.")
} else if _, ok := err.(*UnmarshalFieldError); !ok {
t.Errorf("expected UnmarshalFieldError, got %T: %s", err, err.Error())
} else if !strings.Contains(err.Error(), "decodeCase") {
t.Errorf("expected error message about decodeCase, got %s", err.Error())
}
}
type fieldMap struct {
Field map[int]interface{} `field`
}
func TestDecoder_Decode_UnmarshalTypeError(t *testing.T) {
unmarshaltype := []decodeCase{
{raw0, make(map[string]bool), " bool"},
{raw0, make(map[string]map[string]string), " map[string]string"},
{[]byte("key = A"), make(map[string][]float32), " float32"},
{[]byte("key = A"), make(map[string][]int64), " int64"},
{[]byte("key = A"), make(map[string][]uint64), " uint64"},
{[]byte("section\n key = A"), make(map[string]bool), " bool"},
{[]byte("section\n key = A"), make(map[string]map[int]bool), " map[int]bool"},
{[]byte("section\n key = A"), make(map[string]map[string]bool), " bool"},
}
unmarshaltype = append(unmarshaltype, []decodeCase{
{
Raw: []byte("section\n sub\n key = A"),
Value: map[string]map[string]interface{}{
"section": map[string]interface{}{"sub": map[int]int{}},
},
ErrSub: " int",
},
{
Raw: []byte("section\n key = A"),
Value: map[string]map[string]int{"section": nil},
ErrSub: " int",
},
{
Raw: []byte("field\n key = A"),
Value: &fieldMap{},
ErrSub: " map[int]interface",
},
}...)
for _, c := range unmarshaltype {
reader := bytes.NewReader(c.Raw)
decoder := NewDecoder(reader)
if err := decoder.Decode(c.Value); err == nil {
t.Errorf("expected error decoding into %T, got success.", c.Value)
} else if _, ok := err.(*UnmarshalTypeError); !ok {
t.Errorf("expected UnmarshalTypeError decoding into %T, got %T: %s", c.Value, err, err.Error())
} else if !strings.Contains(err.Error(), c.ErrSub) {
t.Errorf("expected error message about %s, got %s.", c.ErrSub, err.Error())
}
}
}
func TestDecoder_Decode_Slice(t *testing.T) {
raw := []byte("key = 1")
test := func(v interface{}, g func(interface{}) int) {
if err := Unmarshal(raw, v); err != nil {
t.Errorf("error decoding into %T, got %T: %s", v, err, err.Error())
} else {
result := g(v)
if result != 1 {
t.Errorf("expected 1, got %v", result)
}
}
}
test(make(map[string][]*float32), func(m interface{}) int { return int(*m.(map[string][]*float32)["key"][0]) })
test(make(map[string][]*float64), func(m interface{}) int { return int(*m.(map[string][]*float64)["key"][0]) })
test(make(map[string][]*int), func(m interface{}) int { return int(*m.(map[string][]*int)["key"][0]) })
test(make(map[string][]*int16), func(m interface{}) int { return int(*m.(map[string][]*int16)["key"][0]) })
test(make(map[string][]*int32), func(m interface{}) int { return int(*m.(map[string][]*int32)["key"][0]) })
test(make(map[string][]*int64), func(m interface{}) int { return int(*m.(map[string][]*int64)["key"][0]) })
test(make(map[string][]*uint), func(m interface{}) int { return int(*m.(map[string][]*uint)["key"][0]) })
test(make(map[string][]*uint16), func(m interface{}) int { return int(*m.(map[string][]*uint16)["key"][0]) })
test(make(map[string][]*uint32), func(m interface{}) int { return int(*m.(map[string][]*uint32)["key"][0]) })
test(make(map[string][]*uint64), func(m interface{}) int { return int(*m.(map[string][]*uint64)["key"][0]) })
test(make(map[string][]float32), func(m interface{}) int { return int(m.(map[string][]float32)["key"][0]) })
test(make(map[string][]float64), func(m interface{}) int { return int(m.(map[string][]float64)["key"][0]) })
test(make(map[string][]int), func(m interface{}) int { return int(m.(map[string][]int)["key"][0]) })
test(make(map[string][]int16), func(m interface{}) int { return int(m.(map[string][]int16)["key"][0]) })
test(make(map[string][]int32), func(m interface{}) int { return int(m.(map[string][]int32)["key"][0]) })
test(make(map[string][]int64), func(m interface{}) int { return int(m.(map[string][]int64)["key"][0]) })
test(make(map[string][]uint), func(m interface{}) int { return int(m.(map[string][]uint)["key"][0]) })
test(make(map[string][]uint16), func(m interface{}) int { return int(m.(map[string][]uint16)["key"][0]) })
test(make(map[string][]uint32), func(m interface{}) int { return int(m.(map[string][]uint32)["key"][0]) })
test(make(map[string][]uint64), func(m interface{}) int { return int(m.(map[string][]uint64)["key"][0]) })
test(make(map[string]float32), func(m interface{}) int { return int(m.(map[string]float32)["key"]) })
test(make(map[string]float64), func(m interface{}) int { return int(m.(map[string]float64)["key"]) })
test(make(map[string]int), func(m interface{}) int { return int(m.(map[string]int)["key"]) })
test(make(map[string]int16), func(m interface{}) int { return int(m.(map[string]int16)["key"]) })
test(make(map[string]int32), func(m interface{}) int { return int(m.(map[string]int32)["key"]) })
test(make(map[string]int64), func(m interface{}) int { return int(m.(map[string]int64)["key"]) })
test(make(map[string]uint), func(m interface{}) int { return int(m.(map[string]uint)["key"]) })
test(make(map[string]uint16), func(m interface{}) int { return int(m.(map[string]uint16)["key"]) })
test(make(map[string]uint32), func(m interface{}) int { return int(m.(map[string]uint32)["key"]) })
test(make(map[string]uint64), func(m interface{}) int { return int(m.(map[string]uint64)["key"]) })
}