forked from clbanning/mxj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_test.go
240 lines (191 loc) · 5.79 KB
/
xml_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
package mxj
import (
"bytes"
"fmt"
"io"
"reflect"
"testing"
)
func TestXmlHeader(t *testing.T) {
fmt.Println("\n---------------- xml_test.go ...")
}
func TestNewMapXml(t *testing.T) {
x := []byte(`<root2><newtag newattr="some_attr_value">something more</newtag><list listattr="val"><item>1</item><item>2</item></list></root2>`)
mv, merr := NewMapXml(x)
if merr != nil {
t.Fatal("merr:", merr.Error())
}
want := Map{"root2":
map[string]interface{}{
"newtag":
map[string]interface{}{"-newattr": "some_attr_value", "#text":"something more"},
"list":
map[string]interface{}{"-listattr":"val", "item":[]interface{}{"1", "2"}},
}}
if !reflect.DeepEqual(mv, want) {
fmt.Println("NewMapXml, x :", string(x))
fmt.Printf("NewMapXml, mv : %#v\n", mv)
fmt.Printf("NewMapXml, want: %#v\n", want)
t.Fatal("not DeepEqual")
}
}
func TestAttrHyphenFalse(t *testing.T) {
PrependAttrWithHyphen(false)
defer PrependAttrWithHyphen(true)
x := []byte(`<root2><newtag newattr="some_attr_value">something more</newtag><list listattr="val"><item>1</item><item>2</item></list></root2>`)
mv, merr := NewMapXml(x)
if merr != nil {
t.Fatal("merr:", merr.Error())
}
want := Map{"root2":
map[string]interface{}{
"newtag":
map[string]interface{}{"newattr": "some_attr_value", "#text":"something more"},
"list":
map[string]interface{}{"listattr":"val", "item":[]interface{}{"1", "2"}},
}}
if !reflect.DeepEqual(mv, want) {
fmt.Println("AttrHyphenFalse, x :", string(x))
fmt.Printf("AttrHyphenFalse, mv : %#v\n", mv)
fmt.Printf("AttrHyphenFalse, want: %#v\n", want)
t.Fatal("not DeepEqual")
}
}
func TestNewMapXmlError(t *testing.T) {
x := []byte(`<root2><newtag>something more</newtag><list><item>1</item><item>2</item></list>`)
m, merr := NewMapJson(x)
if merr == nil {
t.Fatal("NewMapXmlError, m:", m)
}
want := `invalid character '<' looking for beginning of value`
if merr != nil && merr.Error() != want {
fmt.Println("NewMapXmlError, x :", string(x))
fmt.Println("NewMapXmlError, merr:", merr.Error())
fmt.Println("NewMapXmlError, want:", want)
}
}
func TestNewMapXmlReader(t *testing.T) {
fmt.Println("\n==================== TestNewMapXmlReader ...")
x := []byte(`<root><this>is a test</this></root><root2><newtag>something more</newtag><list><item>1</item><item>2</item></list></root2>`)
r := bytes.NewReader(x)
for {
m, raw, err := NewMapXmlReaderRaw(r)
if err != nil && err != io.EOF {
t.Fatal("err:", err.Error())
}
if err == io.EOF && len(m) == 0 {
break
}
fmt.Println("NewMapXmlReader, raw:", string(raw))
fmt.Println("NewMapXmlReader, m :", m)
}
}
// --------------------- Xml() and XmlWriter() test cases -------------------
func TestXml_1(t *testing.T) {
mv := Map{"tag1": "some data", "tag2": "more data", "boolean": true, "float": 3.14159625, "null": nil}
x, err := mv.Xml()
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("Xml_1, mv:", mv)
fmt.Println("Xml_1, x :", string(x))
}
func TestXml_2(t *testing.T) {
a := []interface{}{"string", true, 36.4}
mv := Map{"array": a}
x, err := mv.Xml()
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("Xml_2, mv:", mv)
fmt.Println("Xml_2, x :", string(x))
}
func TestXml_3(t *testing.T) {
a := []interface{}{"string", true, 36.4}
mv := Map{"array": []interface{}{a, "string2"}}
x, err := mv.Xml()
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("Xml_3, mv:", mv)
fmt.Println("Xml_3, x :", string(x))
}
func TestXml_4(t *testing.T) {
a := []interface{}{"string", true, 36.4}
mv := Map{"array": map[string]interface{}{"innerkey": []interface{}{a, "string2"}}}
x, err := mv.Xml()
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("Xml_4, mv:", mv)
fmt.Println("Xml_4, x :", string(x))
}
func TestXml_5(t *testing.T) {
a := []interface{}{"string", true, 36.4}
mv := Map{"array": []interface{}{map[string]interface{}{"innerkey": []interface{}{a, "string2"}}, map[string]interface{}{"some": "more"}}}
x, err := mv.Xml()
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("Xml_5, mv:", mv)
fmt.Println("Xml_5, x :", string(x))
}
func TestXml_Strings(t *testing.T) {
mv := Map{"sometag": "some data", "strings": []string{"string1", "string2"}}
x, err := mv.Xml()
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("Xml_strings, mv:", mv)
fmt.Println("Xml_strings, x :", string(x))
}
func TestXmlWriter(t *testing.T) {
mv := Map{"tag1": "some data", "tag2": "more data", "boolean": true, "float": 3.14159625}
w := new(bytes.Buffer)
err := mv.XmlWriter(w, "myRootTag")
if err != nil {
t.Fatal("err:", err.Error())
}
b := make([]byte, w.Len())
_, err = w.Read(b)
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("XmlWriter, b :", string(b))
}
// -------------------------- XML Handler test cases -------------------------
/* tested in bulk_test.go ...
var xhdata = []byte(`<root><this>is a test</this></root><root2><newtag>something more</newtag><list><item>1</item><item>2</item></list></root2><root3><tag></root3>`)
func TestHandleXmlReader(t *testing.T) {
fmt.Println("HandleXmlReader:", string(xhdata))
rdr := bytes.NewReader(xhdata)
err := HandleXmlReader(rdr, xmhandler, xehandler)
if err != nil {
t.Fatal("err:", err.Error())
}
}
var xt *testing.T
func xmhandler(m Map, raw []byte) bool {
x, xerr := m.Xml()
if xerr != nil {
xt.Fatal("... xmhandler:", xerr.Error())
return false
}
fmt.Println("... xmhandler, raw:", string(raw))
fmt.Println("... xmhandler, x :", string(x))
return true
}
func xehandler(err error, raw []byte) bool {
if err == nil {
// shouldn't be here
xt.Fatal("... xehandler: <nil>")
return false
}
if err == io.EOF {
return true
}
fmt.Println("... xehandler raw:", string(raw))
fmt.Println("... xehandler err:", err.Error())
return true
}
*/