forked from basgys/goxml2json
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoder_test.go
82 lines (69 loc) · 1.56 KB
/
encoder_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
package xml2json
import (
"bytes"
"testing"
sj "github.com/bitly/go-simplejson"
"github.com/stretchr/testify/assert"
)
type bio struct {
Firstname string
Lastname string
Hobbies []string
Misc map[string]string
}
// TestEncode ensures that encode outputs the expected JSON document.
func TestEncode(t *testing.T) {
assert := assert.New(t)
author := bio{
Firstname: "Bastien",
Lastname: "Gysler",
Hobbies: []string{"DJ", "Running", "Tennis"},
Misc: map[string]string{
"Nationality": "Swiss",
"City": "Zürich",
"foo": "",
"bar": "\"quoted text\"",
},
}
// Build document
root := &Node{}
root.AddChild("firstname", &Node{
Data: author.Firstname,
})
root.AddChild("lastname", &Node{
Data: author.Lastname,
})
for _, h := range author.Hobbies {
root.AddChild("hobbies", &Node{
Data: h,
})
}
misc := &Node{}
for k, v := range author.Misc {
misc.AddChild(k, &Node{
Data: v,
})
}
root.AddChild("misc", misc)
// Convert to JSON string
buf := new(bytes.Buffer)
err := NewEncoder(buf).Encode(root)
if err != nil {
assert.NoError(err)
}
// Build SimpleJSON
sj, err := sj.NewJson(buf.Bytes())
res, err := sj.Map()
assert.NoError(err)
// Assertions
assert.Equal(author.Firstname, res["firstname"])
assert.Equal(author.Lastname, res["lastname"])
resHobbies, err := sj.Get("hobbies").StringArray()
assert.NoError(err)
assert.Equal(author.Hobbies, resHobbies)
resMisc, err := sj.Get("misc").Map()
assert.NoError(err)
for k, v := range resMisc {
assert.Equal(author.Misc[k], v)
}
}