forked from clbanning/mxj
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml_test.go
141 lines (111 loc) · 3.31 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
package mxj
import (
"bytes"
"fmt"
"io"
"testing"
)
func TestXmlHeader(t *testing.T) {
fmt.Println("\n---------------- xml_test.go ...\n")
}
func TestNewMapXml(t *testing.T) {
x := []byte(`<root2><newtag attr="foo">something more</newtag><list><item>1</item><item>2</item></list></root2>`)
mv, merr := NewMapXml(x)
if merr != nil {
t.Fatal("merr:", merr.Error())
}
fmt.Println("NewMapXml, x :", string(x))
fmt.Println("NewMapXml, mv:", mv)
}
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)
}
fmt.Println("NewMapXmlError, x :", string(x))
fmt.Println("NewMapXmlError, merr:", merr.Error())
x = []byte(`<root2><newtag>something more</newtag><list><item>1<item>2</item></list></root2>`)
m, merr = NewMapJson(x)
if merr == nil {
t.Fatal("NewMapXmlError, m:", m)
}
fmt.Println("NewMapXmlError, x :", string(x))
fmt.Println("NewMapXmlError, merr:", merr.Error())
}
func TestNewMapXmlReader(t *testing.T) {
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(t *testing.T) {
mv := Map{"tag1": "some data", "tag2": "more data", "boolean": true, "float": 3.14159625}
x, err := mv.Xml()
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("Xml, mv:", mv)
fmt.Println("Xml, 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)
raw, err := mv.XmlWriterRaw(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, raw:", string(*raw))
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
}
*/