-
Notifications
You must be signed in to change notification settings - Fork 94
/
encoder_test.go
150 lines (122 loc) · 2.96 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
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
package xml2json
import (
"bytes"
"encoding/json"
"fmt"
"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) {
var err error
assert := assert.New(t)
author := bio{
Firstname: "Bastien",
Lastname: "Gysler",
Hobbies: []string{"DJ", "Running", "Tennis"},
Misc: map[string]string{
"lineSeparator": "\u2028",
"Nationality": "Swiss",
"City": "Zürich",
"foo": "",
"bar": "\"quoted text\"",
"esc": "escaped \\ sanitized",
"r": "\r return line",
"default": "< >",
"runeError": "\uFFFD",
},
}
// 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)
var enc *Encoder
// Convert to JSON string
buf := new(bytes.Buffer)
enc = NewEncoder(buf)
err = enc.Encode(nil)
assert.NoError(err)
attr := WithAttrPrefix("test")
attr.AddToEncoder(enc)
content := WithContentPrefix("test2")
content.AddToEncoder(enc)
err = enc.Encode(root)
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)
}
enc.err = fmt.Errorf("Testing if error provided is returned")
assert.Error(enc.Encode(nil))
}
// TestEncodeWithChildrenAsExplicitArray ensures that ChildrenAlwaysAsArray flag works as expected.
func TestEncodeWithChildrenAsExplicitArray(t *testing.T) {
type hobbies struct {
Hobbies []string `json:"hobbies"`
}
var (
testBio hobbies
err error
)
assert := assert.New(t)
author := bio{
Hobbies: []string{"DJ"},
}
// ChildrenAlwaysAsArray is not set
root := &Node{}
for _, h := range author.Hobbies {
root.AddChild("hobbies", &Node{
Data: h,
})
}
var enc *Encoder
buf := new(bytes.Buffer)
enc = NewEncoder(buf)
err = enc.Encode(root)
assert.NoError(err)
json.Unmarshal(buf.Bytes(), &testBio)
assert.Equal(0, len(testBio.Hobbies))
// ChildrenAlwaysAsArray is set
root.ChildrenAlwaysAsArray = true
testBio = hobbies{}
buf = new(bytes.Buffer)
enc = NewEncoder(buf)
err = enc.Encode(root)
assert.NoError(err)
json.Unmarshal(buf.Bytes(), &testBio)
assert.Equal(1, len(testBio.Hobbies))
}