Debian Codegraph @

encode_test.go

raw symbols SCIP
1 package plist
2
3 import (
4 "bytes"
5 "fmt"
6 "testing"
7 )
8
9 func BenchmarkXMLEncode(b *testing.B) {
10 for i := 0; i < b.N; i++ {
11 NewEncoder(&bytes.Buffer{}).Encode(plistValueTreeRawData)
12 }
13 }
14
15 func BenchmarkBplistEncode(b *testing.B) {
16 for i := 0; i < b.N; i++ {
17 NewBinaryEncoder(&bytes.Buffer{}).Encode(plistValueTreeRawData)
18 }
19 }
20
21 func BenchmarkOpenStepEncode(b *testing.B) {
22 for i := 0; i < b.N; i++ {
23 NewEncoderForFormat(&bytes.Buffer{}, OpenStepFormat).Encode(plistValueTreeRawData)
24 }
25 }
26
27 func TestEncode(t *testing.T) {
28 for _, test := range tests {
29 subtest(t, test.Name, func(t *testing.T) {
30 for fmt, doc := range test.Documents {
31 if test.SkipEncode[fmt] {
32 continue
33 }
34 subtest(t, FormatNames[fmt], func(t *testing.T) {
35 encoded, err := Marshal(test.Value, fmt)
36
37 if err != nil {
38 t.Error(err)
39 }
40
41 if !bytes.Equal(doc, encoded) {
42 printype := "%s"
43 if fmt == BinaryFormat {
44 printype = "%2x"
45 }
46 t.Logf("Value: %#v", test.Value)
47 t.Logf("Expected: "+printype+"\n", doc)
48 t.Logf("Received: "+printype+"\n", encoded)
49 t.Fail()
50 }
51 })
52 }
53 })
54 }
55 }
56
57 func ExampleEncoder_Encode() {
58 type sparseBundleHeader struct {
59 InfoDictionaryVersion string `plist:"CFBundleInfoDictionaryVersion"`
60 BandSize uint64 `plist:"band-size"`
61 BackingStoreVersion int `plist:"bundle-backingstore-version"`
62 DiskImageBundleType string `plist:"diskimage-bundle-type"`
63 Size uint64 `plist:"size"`
64 }
65 data := &sparseBundleHeader{
66 InfoDictionaryVersion: "6.0",
67 BandSize: 8388608,
68 Size: 4 * 1048576 * 1024 * 1024,
69 DiskImageBundleType: "com.apple.diskimage.sparsebundle",
70 BackingStoreVersion: 1,
71 }
72
73 buf := &bytes.Buffer{}
74 encoder := NewEncoder(buf)
75 err := encoder.Encode(data)
76 if err != nil {
77 fmt.Println(err)
78 }
79 fmt.Println(buf.String())
80
81 // Output: <?xml version="1.0" encoding="UTF-8"?>
82 // <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
83 // <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>
84 }
85
86 func ExampleMarshal_xml() {
87 type sparseBundleHeader struct {
88 InfoDictionaryVersion string `plist:"CFBundleInfoDictionaryVersion"`
89 BandSize uint64 `plist:"band-size"`
90 BackingStoreVersion int `plist:"bundle-backingstore-version"`
91 DiskImageBundleType string `plist:"diskimage-bundle-type"`
92 Size uint64 `plist:"size"`
93 }
94 data := &sparseBundleHeader{
95 InfoDictionaryVersion: "6.0",
96 BandSize: 8388608,
97 Size: 4 * 1048576 * 1024 * 1024,
98 DiskImageBundleType: "com.apple.diskimage.sparsebundle",
99 BackingStoreVersion: 1,
100 }
101
102 plist, err := MarshalIndent(data, XMLFormat, "\t")
103 if err != nil {
104 fmt.Println(err)
105 }
106 fmt.Println(string(plist))
107
108 // Output: <?xml version="1.0" encoding="UTF-8"?>
109 // <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
110 // <plist version="1.0">
111 // <dict>
112 // <key>CFBundleInfoDictionaryVersion</key>
113 // <string>6.0</string>
114 // <key>band-size</key>
115 // <integer>8388608</integer>
116 // <key>bundle-backingstore-version</key>
117 // <integer>1</integer>
118 // <key>diskimage-bundle-type</key>
119 // <string>com.apple.diskimage.sparsebundle</string>
120 // <key>size</key>
121 // <integer>4398046511104</integer>
122 // </dict>
123 // </plist>
124 }
125
126 func ExampleMarshal_gnustep() {
127 type sparseBundleHeader struct {
128 InfoDictionaryVersion string `plist:"CFBundleInfoDictionaryVersion"`
129 BandSize uint64 `plist:"band-size"`
130 BackingStoreVersion int `plist:"bundle-backingstore-version"`
131 DiskImageBundleType string `plist:"diskimage-bundle-type"`
132 Size uint64 `plist:"size"`
133 }
134 data := &sparseBundleHeader{
135 InfoDictionaryVersion: "6.0",
136 BandSize: 8388608,
137 Size: 4 * 1048576 * 1024 * 1024,
138 DiskImageBundleType: "com.apple.diskimage.sparsebundle",
139 BackingStoreVersion: 1,
140 }
141
142 plist, err := MarshalIndent(data, GNUStepFormat, "\t")
143 if err != nil {
144 fmt.Println(err)
145 }
146 fmt.Println(string(plist))
147
148 // Output: {
149 // CFBundleInfoDictionaryVersion = 6.0;
150 // band-size = <*I8388608>;
151 // bundle-backingstore-version = <*I1>;
152 // diskimage-bundle-type = com.apple.diskimage.sparsebundle;
153 // size = <*I4398046511104>;
154 // }
155 }