-
Notifications
You must be signed in to change notification settings - Fork 13
/
decoder_test.go
277 lines (245 loc) · 6.77 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
package xml2map
import (
"fmt"
"github.com/google/gofuzz"
"strings"
"testing"
)
func BenchmarkDecoder(b *testing.B) {
for n := 0; n < b.N; n++ {
NewDecoder(strings.NewReader(`<container uid="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
<cats>
<cat>
<id>CDA035B6-D453-4A17-B090-84295AE2DEC5</id>
<name>moritz</name>
<age>7</age>
<items>
<n>1293</n>
<n>1255</n>
<n>1257</n>
</items>
</cat>
<cat>
<id>1634C644-975F-4302-8336-1EF1366EC6A4</id>
<name>oliver</name>
<age>12</age>
</cat>
</cats>
<color>white</color>
<city>NY</city>
</container>`)).Decode()
}
}
func TestStartAttrs(t *testing.T) {
tests := []string{
`<container ="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
<color>white</color>
</container>`,
`<container i=d="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
<color>white</color>
</container>`,
`<container id="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
<color id=>white</color>
</container>`,
}
for _, s := range tests {
_, err := NewDecoder(strings.NewReader(s)).Decode()
if err == nil {
t.Fail()
}
}
}
func TestNs(t *testing.T) {
m, err := NewDecoder(strings.NewReader(
`<container xmlns:h="http://www.w3.org/TR/html4/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"></container>`)).Decode()
if err != nil {
t.Errorf("m: %v, err: %v\n", m, err)
}
}
func TestPars(t *testing.T) {
m, err := NewDecoder(strings.NewReader(
`<customer id="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
<items id="100" count="3">
<n id="10">1</n>
<n id="20">2</n>
<n id="30">3</n>
</items>
</customer>`)).Decode()
if err != nil {
t.Errorf("m: %v, err: %v\n", m, err)
}
}
func TestFuzz1000(t *testing.T) {
f := fuzz.New().NilChance(0).NumElements(1, 1000)
var myMap map[string]int
f.Fuzz(&myMap)
for v := range myMap {
m, err := NewDecoder(strings.NewReader(v)).Decode()
if err == nil {
fmt.Printf("m: %v", m)
}
}
}
func TestErrDecoder(t *testing.T) {
m, err := NewDecoder(strings.NewReader(
`<customer id="C1234">
<lname>Smith</lname>
<fname>John></fname>
<address type="biz">
<street>1310 Villa Street</street>
<city>Mountain View</city>
<state>CA</state>
<zip>94041</zip>
</address>
<customer>`)).Decode()
if m == nil && err != nil {
t.Logf("result: %v err: %v\n", m, err)
} else {
t.Errorf("err %v\n", err)
}
}
func TestEmpty(t *testing.T) {
tests := []string{"", " ", " ", ``, ` `, "\n"}
for _, s := range tests {
_, err := NewDecoder(strings.NewReader(s)).Decode()
if err != ErrInvalidDocument {
t.Fail()
}
}
}
func TestSpaces(t *testing.T) {
m, err := NewDecoder(strings.NewReader(` <note>
data
</note>`)).Decode()
if err != nil {
t.Errorf("err %v\n", err)
} else {
if m["note"] != "data" {
t.Errorf("data not found")
}
}
}
func TestInvalidStartIndex(t *testing.T) {
_, err := NewDecoder(strings.NewReader(`d<note>
data
</note>`)).Decode()
if err == nil || err.Error() != "data at the root level is invalid" {
t.Fail()
}
}
func TestDecode(t *testing.T) {
m, err := NewDecoder(strings.NewReader(
`<container uid="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
<cats>
<cat>
<id>CDA035B6-D453-4A17-B090-84295AE2DEC5</id>
<name>moritz</name>
<age>7</age>
<items>
<n>1293</n>
<n>1255</n>
<n>1257</n>
</items>
</cat>
<cat>
<id>1634C644-975F-4302-8336-1EF1366EC6A4</id>
<name>oliver</name>
<age>12</age>
<items>
<n>1293</n>
<n>1255</n>
<n>1257</n>
</items>
</cat>
<dog color="gray">hello</dog>
</cats>
<color>white</color>
<city>NY</city>
</container>`)).Decode()
if err != nil {
t.Errorf("err: %v", err)
}
container := m["container"].(map[string]interface{})
if container["@uid"] != "FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" && container["lifetime"] != "2019-10-10T18:00:11" {
t.Errorf("container attrs not exists")
} else {
cats := container["cats"].(map[string]interface{})
catsItems := cats["cat"].([]map[string]interface{})
if len(catsItems) != 2 {
t.Errorf("cats slice != 2")
}
dog := cats["dog"].(map[string]interface{})
if dog["@color"] != "gray" || dog["#text"] != "hello" {
t.Errorf("bad value or attr dog")
}
if container["color"] != "white" || container["city"] != "NY" {
t.Errorf("bad value color")
}
cat := catsItems[0]
if cat["id"] != "" && cat["name"] != "" && cat["age"] != "" {
items := cat["items"].(map[string]interface{})["n"].([]string)
if len(items) != 3 {
t.Errorf("items len %v", len(items))
}
}
}
}
func TestWithPrefix(t *testing.T) {
m, err := NewDecoderWithPrefix(strings.NewReader(
`<customer id="FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" lifetime="2019-10-10T18:00:11">
<items id="100" count="3">
<n id="10">1</n>
<n id="20">2</n>
<n id="30">3</n>
</items>
</customer>`), "$", "#").Decode()
if err != nil {
t.Errorf("m: %v, err: %v\n", m, err)
}
customer := m["customer"].(map[string]interface{})
if customer["$id"] != "FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" && customer["$lifetime"] != "2019-10-10T18:00:11" {
t.Errorf("customer tag attr not found")
} else {
items := customer["items"].(map[string]interface{})
if items["$id"] != "100" || items["$count"] != "3" {
t.Errorf("items tag attr not found")
} else {
list := items["n"].([]map[string]interface{})
if len(list) != 3 {
t.Errorf("list len %v", len(items))
} else {
if list[1]["$id"] != "20" && list[1]["%"] != "2" {
t.Errorf("invalid parse n element attr or text")
}
}
}
}
}
func TestWithNameSpace(t *testing.T) {
m, err := NewDecoder(strings.NewReader(
`<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>example.com RSS</title>
<link>https://www.example.com/</link>
<description>A cool website</description>
<atom:link href="http://www.example.com/rss.xml" rel="self" type="application/rss+xml" />
<atom:title>Atom Title</atom:title>
<item>
<title>Cool Article</title>
<link>https://www.example.com/cool-article</link>
<guid>https://www.example.com/cool-article</guid>
<pubDate>Sun, 10 Dec 2017 05:00:00 GMT</pubDate>
<description>My cool article description</description>
</item>
</channel>
</rss>`)).Decode()
if err != nil {
t.Errorf("m: %v, err: %v\n", m, err)
}
rss := m["rss"].(map[string]interface{})["channel"].(map[string]interface{})
if rss["atom:title"] != "Atom Title" {
t.Errorf("invalid value for namespace node")
}
}