| 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 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
} |