-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxmlfmt_test.go
223 lines (195 loc) · 7.7 KB
/
xmlfmt_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
package xmlfmt_test
import (
"fmt"
"testing"
"github.com/go-xmlfmt/xmlfmt"
)
const xml1 = `<root><this><is>a</is><test /><message><!-- with comment --><org><cn>Some org-or-other</cn><ph>Wouldnt you like to know</ph></org><contact><fn>Pat</fn><ln>Califia</ln></contact></message></this></root>`
const xml2 = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com/ns"><soapenv:Header/><soapenv:Body><ns:request><ns:customer><ns:id>123</ns:id><ns:name type="NCHZ">John Brown</ns:name></ns:customer></ns:request></soapenv:Body></soapenv:Envelope>`
const xml3 = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_xmlns="xmlns" _xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" _xmlns:ns="http://example.com/ns"><Header xmlns="http://schemas.xmlsoap.org/soap/envelope/"></Header><Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"><request xmlns="http://example.com/ns"><customer xmlns="http://example.com/ns"><id xmlns="http://example.com/ns">123</id><name xmlns="http://example.com/ns" type="NCHZ">John Brown</name></customer></request></Body></Envelope>`
const xml4 = `<?xml version="1.0" encoding="UTF-8">
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/></Relationships>`
const xml5 = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com/ns"><soapenv:Header/><soapenv:Body><ns:request><ns:customer><ns:id>123</ns:id><ns:name type="NCHZ">John Brown super long super long super long super long super long super long super long super longlong super long super long super long super long super long super long super longlong super long super long super long super long super long super long super long</ns:name></ns:customer></ns:request></soapenv:Body></soapenv:Envelope>`
func Example_output() {
x3 := xmlfmt.FormatXML(xml3, "\t", " ")
x2 := xmlfmt.FormatXML(xml2, "x ", " ")
_ = x2
_ = x3
x1 := xmlfmt.FormatXML(xml1, "", " ")
fmt.Println(x1)
// Output:
// <root>
// <this>
// <is>a</is>
// <test />
// <message>
// <!-- with comment -->
// <org>
// <cn>Some org-or-other</cn>
// <ph>Wouldnt you like to know</ph>
// </org>
// <contact>
// <fn>Pat</fn>
// <ln>Califia</ln>
// </contact>
// </message>
// </this>
// </root>
}
const w1 = `..
..<root>
.. <this>
.. <is>a</is>
.. <test />
.. <message>
.. <!-- with comment -->
.. <org>
.. <cn>Some org-or-other</cn>
.. <ph>Wouldnt you like to know</ph>
.. </org>
.. <contact>
.. <fn>Pat</fn>
.. <ln>Califia</ln>
.. </contact>
.. </message>
.. </this>
..</root>`
const w2 = `x
x <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com/ns">
x <soapenv:Header/>
x <soapenv:Body>
x <ns:request>
x <ns:customer>
x <ns:id>123</ns:id>
x <ns:name type="NCHZ">John Brown</ns:name>
x </ns:customer>
x </ns:request>
x </soapenv:Body>
x </soapenv:Envelope>`
const w3 = `
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_xmlns="xmlns" _xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" _xmlns:ns="http://example.com/ns">
<Header xmlns="http://schemas.xmlsoap.org/soap/envelope/"></Header>
<Body xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<request xmlns="http://example.com/ns">
<customer xmlns="http://example.com/ns">
<id xmlns="http://example.com/ns">123</id>
<name xmlns="http://example.com/ns" type="NCHZ">John Brown</name>
</customer>
</request>
</Body>
</Envelope>`
const w4 = `
<?xml version="1.0" encoding="UTF-8">
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/>
</Relationships>`
const w5 = `x
x <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com/ns">
x <soapenv:Header/>
x <soapenv:Body>
x <ns:request>
x <ns:customer>
x <ns:id>123</ns:id>
x <ns:name type="NCHZ">John Brown super long super long super long super long super long super long super long super longlong super long super long super long super long super long super long super longlong super long super long super long super long super long super long super long</ns:name>
x </ns:customer>
x </ns:request>
x </soapenv:Body>
x </soapenv:Envelope>`
func TestFormatXML_t0(t *testing.T) {
xmlfmt.NL = "\n"
}
func TestFormatXML_t1(t *testing.T) {
x1 := xmlfmt.FormatXML(xml1, "..", " ")
if x1 != w1 {
t.Errorf("got:\n%s, want:\n%s.", x1, w1)
}
}
func TestFormatXML_t2(t *testing.T) {
x2 := xmlfmt.FormatXML(xml2, "x ", " ")
if x2 != w2 {
t.Errorf("got:\n%s, want:\n%s.", x2, w2)
}
}
func TestFormatXML_t3(t *testing.T) {
x3 := xmlfmt.FormatXML(xml3, "", " ")
if x3 != w3 {
t.Errorf("got:\n%s, want:\n%s.", x3, w3)
}
}
func TestFormatXML_t4(t *testing.T) {
x4 := xmlfmt.FormatXML(xml4, "", " ")
if x4 != w4 {
t.Errorf("got:\n%s, want:\n%s.", x4, w4)
}
}
func TestFormatXML_t5(t *testing.T) {
x5 := xmlfmt.FormatXML(xml5, "x ", " ")
if x5 != w5 {
t.Errorf("got:\n%s, want:\n%s.", x5, w5)
}
}
const xmlc1 = `
<book> <author>Fred</author>
<!--
<price>20</price><currency>USD</currency>
-->
<isbn>23456</isbn> </book>
<!-- c1 --> <?xml version="1.0" encoding="utf-8"?> <message name="DIS_USER_SSVC" tid="1591918441"> <!-- c2 --> <Result>0</Result> <parameter> <SsvcList> <!-- c3 --> <CFU>2</CFU> <!-- c4 --> <!-- <DATA>0261216281</DATA> --> </SsvcList> </parameter> </message>`
const wc1 = `
<book>
<author>Fred</author>
<!-- <price>20</price><currency>USD</currency> -->
<isbn>23456</isbn>
</book>
<!-- c1 -->
<?xml version="1.0" encoding="utf-8"?>
<message name="DIS_USER_SSVC" tid="1591918441">
<!-- c2 -->
<Result>0</Result>
<parameter>
<SsvcList>
<!-- c3 -->
<CFU>2</CFU>
<!-- c4 -->
<!-- <DATA>0261216281</DATA> -->
</SsvcList>
</parameter>
</message>`
func TestFormatXML_comments_t1(t *testing.T) {
x1 := xmlfmt.FormatXML(xmlc1, "", " ", true)
if x1 != wc1 {
t.Errorf("got:\n%s, want:\n%s.", x1, wc1)
}
}
////////////////////////////////////////////////////////////////////////////
// Benchmarking
/*
BenchmarkFormatXML show compare metrics between different xml strings.
*/
func BenchmarkFormatXML(b *testing.B) {
for _, size := range []int{1, 10, 100, 1000, 10000, 100000} { // , 1000000
benchmarkFormatXML(b, size)
}
}
func benchmarkFormatXML(b *testing.B, size int) {
b.Run(fmt.Sprintf("XML1_%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
x := xmlfmt.FormatXML(xml1, "..", " ")
_ = x
}
})
b.Run(fmt.Sprintf("XML2_%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
x := xmlfmt.FormatXML(xml2, "x ", " ")
_ = x
}
})
b.Run(fmt.Sprintf("XML3_%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
x := xmlfmt.FormatXML(xml3, "", " ")
_ = x
}
})
}