-
Notifications
You must be signed in to change notification settings - Fork 10
/
xml_test.go
138 lines (117 loc) · 2.61 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
package utils
import (
"encoding/xml"
"testing"
"github.com/stretchr/testify/require"
)
type serversXMLStructure struct {
XMLName xml.Name `xml:"servers"`
Version string `xml:"version,attr"`
Servers []serverXMLStructure `xml:"server"`
}
type serverXMLStructure struct {
XMLName xml.Name `xml:"server"`
Name string `xml:"name"`
}
var xmlString = `<servers version="1"><server><name>fiber one</name></server><server><name>fiber two</name></server></servers>`
func Test_GolangXMLEncoder(t *testing.T) {
t.Parallel()
var (
ss = &serversXMLStructure{
Version: "1",
Servers: []serverXMLStructure{
{Name: "fiber one"},
{Name: "fiber two"},
},
}
xmlEncoder XMLMarshal = xml.Marshal
)
raw, err := xmlEncoder(ss)
require.NoError(t, err)
require.Equal(t, string(raw), xmlString)
}
func Test_DefaultXMLEncoder(t *testing.T) {
t.Parallel()
var (
ss = &serversXMLStructure{
Version: "1",
Servers: []serverXMLStructure{
{Name: "fiber one"},
{Name: "fiber two"},
},
}
xmlEncoder XMLMarshal = xml.Marshal
)
raw, err := xmlEncoder(ss)
require.NoError(t, err)
require.Equal(t, string(raw), xmlString)
}
func Test_DefaultXMLDecoder(t *testing.T) {
t.Parallel()
var (
ss serversXMLStructure
xmlBytes = []byte(xmlString)
xmlDecoder XMLUnmarshal = xml.Unmarshal
)
err := xmlDecoder(xmlBytes, &ss)
require.NoError(t, err)
require.Len(t, ss.Servers, 2)
require.Equal(t, "1", ss.Version)
require.Equal(t, "fiber one", ss.Servers[0].Name)
require.Equal(t, "fiber two", ss.Servers[1].Name)
}
func Benchmark_GolangXMLEncoder(b *testing.B) {
var (
ss = &serversXMLStructure{
Version: "1",
Servers: []serverXMLStructure{
{Name: "fiber one"},
{Name: "fiber two"},
},
}
xmlEncoder XMLMarshal = xml.Marshal
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := xmlEncoder(ss)
if err != nil {
b.Fatal(err)
}
}
}
func Benchmark_DefaultXMLEncoder(b *testing.B) {
var (
ss = &serversXMLStructure{
Version: "1",
Servers: []serverXMLStructure{
{Name: "fiber one"},
{Name: "fiber two"},
},
}
xmlEncoder XMLMarshal = xml.Marshal
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := xmlEncoder(ss)
if err != nil {
b.Fatal(err)
}
}
}
func Benchmark_DefaultXMLDecoder(b *testing.B) {
var (
ss serversXMLStructure
xmlBytes = []byte(xmlString)
xmlDecoder XMLUnmarshal = xml.Unmarshal
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := xmlDecoder(xmlBytes, &ss)
if err != nil {
b.Fatal(err)
}
}
}