This repository has been archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_test.go
83 lines (66 loc) · 1.99 KB
/
encoder_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
package netconf
import (
"bytes"
"encoding/xml"
"fmt"
"testing"
)
func TestEncoder_Encode(t *testing.T) {
type ShowInterfacesRPC struct {
XMLName xml.Name `xml:"get-interface-information"`
Detail *struct{} `xml:"detail,omitempty"`
}
var buf bytes.Buffer
showIfaceRPC := ShowInterfacesRPC{
Detail: &struct{}{},
}
want := []byte(`<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"><get-interface-information><detail></detail></get-interface-information></rpc>]]>]]>
`)
if err := NewEncoder(&buf).Encode(WrapMethod(&showIfaceRPC)); err != nil {
t.Error(err)
} else if !bytes.Equal(want, buf.Bytes()) {
t.Logf("unexpected bytes decoded\nwant:\t%q\ngot:\t%q", want, buf.Bytes())
} else {
t.Log("successfully marshalled get-interface-information rpc")
}
}
func BenchmarkEncoder_Encode(b *testing.B) {
type ShowInterfacesRPC struct {
XMLName xml.Name `xml:"get-interface-information"`
Detail *struct{} `xml:"detail,omitempty"`
}
var buf bytes.Buffer
showIfaceRPC := ShowInterfacesRPC{
Detail: &struct{}{},
}
enc := NewEncoder(&buf)
if err := NewEncoder(&buf).Encode(WrapMethod(&showIfaceRPC)); err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := enc.Encode(WrapMethod(&showIfaceRPC)); err != nil {
b.Error(err)
}
}
}
func Test_Marshal(t *testing.T) {
type ShowInterfacesRPC struct {
XMLName xml.Name `xml:"get-interface-information"`
Detail *struct{} `xml:"detail,omitempty"`
}
showIfaceRPC := ShowInterfacesRPC{
Detail: &struct{}{},
}
b, err := Marshal(showIfaceRPC)
if err != nil {
t.Error(err)
}
want := fmt.Sprintf(`<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="%d"><get-interface-information><detail></detail></get-interface-information></rpc>]]>]]>
`, GlobalCounter.Value())
if wantBytes := []byte(want); !bytes.Equal(wantBytes, b) {
t.Errorf("unexpected bytes decoded\nwant:\t%q\ngot:\t%q", want, b)
} else {
t.Log("successfully marshalled get-interface-information rpc")
}
}