-
Notifications
You must be signed in to change notification settings - Fork 97
/
encode_test.go
155 lines (139 loc) · 4.57 KB
/
encode_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
package plist
import (
"bytes"
"fmt"
"testing"
)
func BenchmarkXMLEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
NewEncoder(&bytes.Buffer{}).Encode(plistValueTreeRawData)
}
}
func BenchmarkBplistEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
NewBinaryEncoder(&bytes.Buffer{}).Encode(plistValueTreeRawData)
}
}
func BenchmarkOpenStepEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
NewEncoderForFormat(&bytes.Buffer{}, OpenStepFormat).Encode(plistValueTreeRawData)
}
}
func TestEncode(t *testing.T) {
for _, test := range tests {
subtest(t, test.Name, func(t *testing.T) {
for fmt, doc := range test.Documents {
if test.SkipEncode[fmt] {
continue
}
subtest(t, FormatNames[fmt], func(t *testing.T) {
encoded, err := Marshal(test.Value, fmt)
if err != nil {
t.Error(err)
}
if !bytes.Equal(doc, encoded) {
printype := "%s"
if fmt == BinaryFormat {
printype = "%2x"
}
t.Logf("Value: %#v", test.Value)
t.Logf("Expected: "+printype+"\n", doc)
t.Logf("Received: "+printype+"\n", encoded)
t.Fail()
}
})
}
})
}
}
func ExampleEncoder_Encode() {
type sparseBundleHeader struct {
InfoDictionaryVersion string `plist:"CFBundleInfoDictionaryVersion"`
BandSize uint64 `plist:"band-size"`
BackingStoreVersion int `plist:"bundle-backingstore-version"`
DiskImageBundleType string `plist:"diskimage-bundle-type"`
Size uint64 `plist:"size"`
}
data := &sparseBundleHeader{
InfoDictionaryVersion: "6.0",
BandSize: 8388608,
Size: 4 * 1048576 * 1024 * 1024,
DiskImageBundleType: "com.apple.diskimage.sparsebundle",
BackingStoreVersion: 1,
}
buf := &bytes.Buffer{}
encoder := NewEncoder(buf)
err := encoder.Encode(data)
if err != nil {
fmt.Println(err)
}
fmt.Println(buf.String())
// Output: <?xml version="1.0" encoding="UTF-8"?>
// <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
// <plist version="1.0"><dict><key>CFBundleInfoDictionaryVersion</key><string>6.0</string><key>band-size</key><integer>8388608</integer><key>bundle-backingstore-version</key><integer>1</integer><key>diskimage-bundle-type</key><string>com.apple.diskimage.sparsebundle</string><key>size</key><integer>4398046511104</integer></dict></plist>
}
func ExampleMarshal_xml() {
type sparseBundleHeader struct {
InfoDictionaryVersion string `plist:"CFBundleInfoDictionaryVersion"`
BandSize uint64 `plist:"band-size"`
BackingStoreVersion int `plist:"bundle-backingstore-version"`
DiskImageBundleType string `plist:"diskimage-bundle-type"`
Size uint64 `plist:"size"`
}
data := &sparseBundleHeader{
InfoDictionaryVersion: "6.0",
BandSize: 8388608,
Size: 4 * 1048576 * 1024 * 1024,
DiskImageBundleType: "com.apple.diskimage.sparsebundle",
BackingStoreVersion: 1,
}
plist, err := MarshalIndent(data, XMLFormat, "\t")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(plist))
// Output: <?xml version="1.0" encoding="UTF-8"?>
// <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
// <plist version="1.0">
// <dict>
// <key>CFBundleInfoDictionaryVersion</key>
// <string>6.0</string>
// <key>band-size</key>
// <integer>8388608</integer>
// <key>bundle-backingstore-version</key>
// <integer>1</integer>
// <key>diskimage-bundle-type</key>
// <string>com.apple.diskimage.sparsebundle</string>
// <key>size</key>
// <integer>4398046511104</integer>
// </dict>
// </plist>
}
func ExampleMarshal_gnustep() {
type sparseBundleHeader struct {
InfoDictionaryVersion string `plist:"CFBundleInfoDictionaryVersion"`
BandSize uint64 `plist:"band-size"`
BackingStoreVersion int `plist:"bundle-backingstore-version"`
DiskImageBundleType string `plist:"diskimage-bundle-type"`
Size uint64 `plist:"size"`
}
data := &sparseBundleHeader{
InfoDictionaryVersion: "6.0",
BandSize: 8388608,
Size: 4 * 1048576 * 1024 * 1024,
DiskImageBundleType: "com.apple.diskimage.sparsebundle",
BackingStoreVersion: 1,
}
plist, err := MarshalIndent(data, GNUStepFormat, "\t")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(plist))
// Output: {
// CFBundleInfoDictionaryVersion = 6.0;
// band-size = <*I8388608>;
// bundle-backingstore-version = <*I1>;
// diskimage-bundle-type = com.apple.diskimage.sparsebundle;
// size = <*I4398046511104>;
// }
}