forked from go-edn/edn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_test.go
438 lines (398 loc) · 12.2 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright 2015 Jean Niklas L'orange. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package edn
import (
"fmt"
"math/big"
"reflect"
"testing"
)
// your basic unit tests.. unfinished, probably.
func TestIntReading(t *testing.T) {
intStrs := [...]string{"0", "1", "+100", "-982", "8223372036854775808", "-5N", "-0N"}
ints := [...]int64{0, 1, 100, -982, 8223372036854775808, -5, 0}
for i, istr := range intStrs {
var n int64
err := UnmarshalString(istr, &n)
if err != nil {
t.Errorf("int64 '%s' failed, but expected success", istr)
} else if n != ints[i] {
t.Errorf("int64 '%s' was decoded to %d, but expected %d", istr, n, ints[i])
}
}
}
func TestBigIntReading(t *testing.T) {
const huge = "32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389647960126939249806625440700685819469589938384356951833568218188663"
bigIntStrs := [...]string{"0", "1", "-1N", "0N", huge + "N"}
_1 := func(v *big.Int, _ bool) *big.Int { return v }
bigInts := [...]*big.Int{
big.NewInt(0), big.NewInt(1), big.NewInt(-1),
big.NewInt(0), _1(big.NewInt(0).SetString(huge, 10)),
}
for i, istr := range bigIntStrs {
var n *big.Int
err := UnmarshalString(istr, &n)
if err != nil {
t.Errorf("*big.Int '%s' failed, but expected success", istr)
} else if n.Cmp(bigInts[i]) != 0 {
t.Errorf("*big.Int '%s' was decoded to %s, but expected %s", istr, n, bigInts[i])
}
}
}
func TestBigFloat(t *testing.T) {
const huge = "123456789123456789123456789123456789123456789123456789.123456789"
bigFloatStrs := [...]string{"0", "1M", "-0.1M", "1.1e-10M", huge + "M"}
bigFloat := func(s string) *big.Float {
f, _, err := big.ParseFloat(s, 10, 192, big.ToNearestEven)
if err != nil {
t.Fatal(err)
}
return f
}
bigFloats := [...]*big.Float{
bigFloat("0"), bigFloat("1"), bigFloat("-0.1"),
bigFloat("1.1e-10"), bigFloat(huge),
}
for i, istr := range bigFloatStrs {
var n *big.Float
err := UnmarshalString(istr, &n)
if err != nil {
t.Errorf("*big.Float '%s' failed, but expected success", istr)
t.Error(err)
} else if n.Cmp(bigFloats[i]) != 0 {
t.Errorf("*big.Float '%s' was decoded to %s, but expected %s", istr, n, bigFloats[i])
}
}
}
func TestArray(t *testing.T) {
stringArray := `("foo" "bar" "baz")`
stringExpected := [...]string{"foo", "bar", "baz"}
var sa [3]string
err := UnmarshalString(stringArray, &sa)
if err != nil {
t.Error(`expected '("foo" "bar" "baz")' to decode fine, but didn't`)
} else {
for i, expected := range stringExpected {
if expected != sa[i] {
t.Errorf(`Element %d in '("foo" "bar" "baz")' (%q) was encoded to %q`,
i, expected, sa[i])
}
}
}
}
func TestStruct(t *testing.T) {
type Animal struct {
Name string
Type string `edn:"kind"`
}
type Person struct {
Name string
Birthyear int `edn:"born"`
Pets []Animal
}
hans := `{:name "Hans",
:born 1970,
:pets [{:name "Cap'n Jack" :kind "Sparrow"}
{:name "Freddy" :kind "Cockatiel"}]}`
goHans := Person{"Hans", 1970,
[]Animal{{"Cap'n Jack", "Sparrow"}, {"Freddy", "Cockatiel"}}}
var ednHans Person
err := UnmarshalString(hans, &ednHans)
if err != nil {
t.Error("Error when decoding Hans")
} else if !reflect.DeepEqual(goHans, ednHans) {
t.Error("EDN Hans is not equal to Go hans")
}
}
func TestRec(t *testing.T) {
type Node struct {
Left *Node
Val int
Right *Node
}
// here we're using symbols
tree := "{left {left {val 3} val 5 right nil} val 10 right {val 15 right {val 17}}}"
goTree := Node{Left: &Node{Left: &Node{Val: 3}, Val: 5, Right: nil},
Val: 10, Right: &Node{Val: 15, Right: &Node{Val: 17}}}
var ednTree Node
err := UnmarshalString(tree, &ednTree)
if err != nil {
t.Errorf("Couldn't unmarshal tree: %s", err.Error())
} else if !reflect.DeepEqual(goTree, ednTree) {
t.Error("Mismatch between the Go tree and the tree encoded as EDN")
}
}
func TestDiscard(t *testing.T) {
var s Symbol
discarding := "#_ #zap #_ xyz foo bar"
expected := Symbol("bar")
err := UnmarshalString(discarding, &s)
if err != nil {
t.Errorf("Expected '#_ #zap #_ xyz foo bar' to successfully read")
t.Log(err.Error())
} else if expected != s {
t.Error("Mismatch between the Go symbol and the symbol encoded as EDN")
}
discarding = "#_ #foo #foo #foo #_#_bar baz zip quux"
expected = Symbol("quux")
err = UnmarshalString(discarding, &s)
if err != nil {
t.Errorf("Expected '#_ #foo #foo #foo #_#_bar baz zip quux' to successfully read")
t.Log(err.Error())
} else if expected != s {
t.Error("Mismatch between the Go symbol and the symbol encoded as EDN")
}
}
// Test that we can read self-defined unmarshalEDNs
type testUnmarshalEDN string
func (t *testUnmarshalEDN) UnmarshalEDN(bs []byte) (err error) {
var kw Keyword
err = Unmarshal(bs, &kw)
if err == nil && string(kw) != "all" {
return fmt.Errorf("testUnmarshalEDN must be :all if it's a keyword, not %s", kw)
}
if err == nil {
*t = testUnmarshalEDN(kw)
return
}
// try to parse set of keywords
var m map[Keyword]bool
err = Unmarshal(bs, &m)
if err == nil {
*t = "set elements"
}
return
}
func TestUnmarshalEDN(t *testing.T) {
var tm testUnmarshalEDN
data := ":all"
expected := testUnmarshalEDN("all")
err := UnmarshalString(data, &tm)
if err != nil {
t.Errorf("Expected ':all' to successfully read into testUnmarshalEDN")
t.Log(err.Error())
} else if expected != tm {
t.Error("Mismatch between testUnmarshalEDN unmarshaling and the expected value")
t.Logf("Was %s, expected %s", tm, expected)
}
data = "#{:foo :bar :baz}"
expected = testUnmarshalEDN("set elements")
err = UnmarshalString(data, &tm)
if err != nil {
t.Errorf("Expected '#{:foo :bar :baz}' to successfully read into testUnmarshalEDN")
t.Log(err.Error())
} else if expected != tm {
t.Error("Mismatch between testUnmarshalEDN unmarshaling and the expected value")
t.Logf("Was %s, expected %s", tm, expected)
}
data = "#{:all #{:foo :bar :baz}}"
var tms map[testUnmarshalEDN]bool
err = UnmarshalString(data, &tms)
if err != nil {
t.Errorf("Expected '#{:all #{:foo :bar :baz}}' to successfully read into a map[testUnmarshalEDN]bool")
t.Log(err.Error())
} else {
fail := false
if len(tms) != 2 {
fail = true
}
if !tms[testUnmarshalEDN("all")] {
fail = true
}
if !tms[testUnmarshalEDN("set elements")] {
fail = true
}
if fail {
t.Error("Mismatch between testUnmarshalEDN unmarshaling and the expected value")
t.Logf("Was %s", tm)
}
}
}
type vectorCounter int
func (v *vectorCounter) UnmarshalEDN(bs []byte) (err error) {
var vec []interface{}
err = Unmarshal(bs, &vec)
if err != nil {
return
}
*v = vectorCounter(len(vec))
return
}
func TestVectorCounter(t *testing.T) {
var v vectorCounter
data := "[foo bar baz]"
var expected vectorCounter = 3
err := UnmarshalString(data, &v)
if err != nil {
t.Errorf("Expected '%s' to successfully read into vectorCounter", data)
t.Log(err.Error())
} else if expected != v {
t.Error("Mismatch between vectorCounter unmarshaling and the expected value")
t.Logf("Was %d, expected %d", v, expected)
}
data = "(a b c d e f)"
expected = 6
err = UnmarshalString(data, &v)
if err != nil {
t.Errorf("Expected '%s' to successfully read into vectorCounter", data)
t.Log(err.Error())
} else if expected != v {
t.Error("Mismatch between vectorCounter unmarshaling and the expected value")
t.Logf("Was %d, expected %d", v, expected)
}
data = `[[a b c][d e f g h],[#_3 z 2 \c]()["c d e"](2 3.0M)]`
var vs []vectorCounter
expected2 := []vectorCounter{3, 5, 3, 0, 1, 2}
err = UnmarshalString(data, &vs)
if err != nil {
t.Errorf("Expected '%s' to successfully read into []vectorCounter", data)
t.Log(err.Error())
} else if !reflect.DeepEqual(vs, expected2) {
t.Errorf("Mismatch between %#v and %#v", vs, expected2)
}
data = `{[a b c] "quux", [] "frob"}`
var vmap map[vectorCounter]string
expected3 := map[vectorCounter]string{3: "quux", 0: "frob"}
err = UnmarshalString(data, &vmap)
if err != nil {
t.Errorf("Expected '%s' to successfully read into map[vectorCounter]string", data)
t.Log(err.Error())
} else if !reflect.DeepEqual(vmap, expected3) {
t.Errorf("Mismatch between %#v and %#v", vmap, expected3)
}
}
type mapCounter int
func (mc *mapCounter) UnmarshalEDN(bs []byte) (err error) {
var m map[interface{}]interface{}
err = Unmarshal(bs, &m)
if err != nil {
return
}
*mc = mapCounter(len(m))
return
}
func TestMapCounter(t *testing.T) {
var mc mapCounter
data := `{nil foo :a :b :c :d 1 0}`
var expected mapCounter = 4
err := UnmarshalString(data, &mc)
if err != nil {
t.Errorf("Expected '%s' to successfully read into mapCounter", data)
t.Log(err.Error())
} else if expected != mc {
t.Error("Mismatch between mapCounter unmarshaling and the expected value")
t.Logf("Was %d, expected %d", mc, expected)
}
}
func TestSliceSet(t *testing.T) {
var ss []string // no need to specify `edn:",set"` for this
data := `#{"a" "b" "c"}`
err := UnmarshalString(data, &ss)
if err != nil {
t.Errorf("Expected '%s' to succesfully read into a string slice", data)
t.Log(err.Error())
} else if !reflect.DeepEqual(ss, []string{"a", "b", "c"}) {
t.Error("Mismatch between string slice unmarshaling and expected value")
t.Logf(`Was %#v, expected []string{"a", "b", "c"}.`, ss)
}
}
type ExtraField struct {
Foo string
}
// TestExtraFields checks whether reading extra fields - in any order, is done
// correctly.
func TestExtraFields(t *testing.T) {
expected := ExtraField{Foo: "123"}
inputs := []string{
`{:foo "123" :extra "456"}`,
`{:extra "456" :foo "123"}`,
`{:foo "123" :extra 456}`,
`{:extra 456 :foo "123"}`,
`{nil 456 :foo "123"}`,
}
for _, input := range inputs {
var ef ExtraField
err := UnmarshalString(input, &ef)
if err != nil {
t.Errorf("Expected '%s' to succesfully read into an ExtraField type", input)
t.Log(err.Error())
} else if ef != expected {
t.Error("Mismatch between struct unmarshaling and expected value")
t.Logf(`Was %#v, expected %#v.`, ef, expected)
}
}
}
func TestNilSet(t *testing.T) {
inputs := []string{
`#{1 2 nil 3}`,
`#{nil}`,
`#{#{nil} #{nil 1}}`,
`#{nil 1 2}`,
`#{1 2 3 nil}`,
}
for _, input := range inputs {
var val []interface{}
err := UnmarshalString(input, &val)
if err != nil {
t.Errorf("Expected '%s' to succesfully read into []interface{}", input)
t.Log(err.Error())
}
var ival interface{}
err = UnmarshalString(input, &ival)
if err != nil {
t.Errorf("Expected '%s' to succesfully read into interface{}", input)
t.Log(err.Error())
}
var mval map[interface{}]bool
err = UnmarshalString(input, &mval)
if err != nil {
t.Errorf("Expected '%s' to succesfully read into map[interface{}]bool", input)
t.Log(err.Error())
}
}
}
func TestNilMap(t *testing.T) {
inputs := []string{
`{1 2 nil 3}`,
`{nil foo}`,
`{{nil nil} 2 nil 1}`,
`{nil 1 2 3}`,
`{1 2 3 nil}`,
}
for _, input := range inputs {
var ival interface{}
err := UnmarshalString(input, &ival)
if err != nil {
t.Errorf("Expected '%s' to succesfully read into interface{}", input)
t.Log(err.Error())
}
var mval map[interface{}]interface{}
err = UnmarshalString(input, &mval)
if err != nil {
t.Errorf("Expected '%s' to succesfully read into map[interface{}]interface{}", input)
t.Log(err.Error())
}
}
}
func TestNilNotFunnilyCoerced(t *testing.T) {
inputs := []string{
`{"1" 2 nil 3}`,
}
for _, input := range inputs {
var val map[string]int
err := UnmarshalString(input, &val)
if err == nil {
t.Errorf("Expected '%s' to error out when read into map[string]int", input)
t.Logf("Value is %#v", val)
}
}
}
func TestNilNotEmptyString(t *testing.T) {
input := "nil"
var val string
err := UnmarshalString(input, &val)
if err == nil {
t.Error("Expected nil to not be a string")
}
}