-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathmarshal_test.go
158 lines (138 loc) · 2.88 KB
/
marshal_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
156
157
158
package plist
import (
"reflect"
"testing"
"time"
)
func BenchmarkStructMarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
e := &Encoder{}
e.marshal(reflect.ValueOf(plistValueTreeRawData))
}
}
func BenchmarkMapMarshal(b *testing.B) {
data := map[string]interface{}{
"intarray": []interface{}{
int(1),
int8(8),
int16(16),
int32(32),
int64(64),
uint(2),
uint8(9),
uint16(17),
uint32(33),
uint64(65),
},
"floats": []interface{}{
float32(32.0),
float64(64.0),
},
"booleans": []bool{
true,
false,
},
"strings": []string{
"Hello, ASCII",
"Hello, 世界",
},
"data": []byte{1, 2, 3, 4},
"date": time.Date(2013, 11, 27, 0, 34, 0, 0, time.UTC),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
e := &Encoder{}
e.marshal(reflect.ValueOf(data))
}
}
func TestInvalidMarshal(t *testing.T) {
tests := []struct {
Name string
Thing interface{}
}{
{"Function", func() {}},
{"Nil", nil},
{"Map with integer keys", map[int]string{1: "hi"}},
{"Channel", make(chan int)},
}
for _, v := range tests {
subtest(t, v.Name, func(t *testing.T) {
data, err := Marshal(v.Thing, OpenStepFormat)
if err == nil {
t.Fatalf("expected error; got plist data: %x", data)
} else {
t.Log(err)
}
})
}
}
type Cat struct{}
func (c *Cat) MarshalPlist() (interface{}, error) {
return "cat", nil
}
func TestInterfaceMarshal(t *testing.T) {
var c Cat
b, err := Marshal(&c, XMLFormat)
if err != nil {
t.Log(err)
} else if len(b) == 0 {
t.Log("expect non-zero data")
}
}
func TestInterfaceFieldMarshal(t *testing.T) {
type X struct {
C interface{} // C's type does not implement Marshaler
}
x := &X{
C: &Cat{}, // C's value implements Marshaler
}
b, err := Marshal(x, XMLFormat)
if err != nil {
t.Log(err)
} else if len(b) == 0 {
t.Log("expect non-zero data")
}
}
func TestMarshalInterfaceFieldPtrTime(t *testing.T) {
type X struct {
C interface{} // C's type is unknown
}
var sentinelTime = time.Date(2013, 11, 27, 0, 34, 0, 0, time.UTC)
x := &X{
C: &sentinelTime,
}
e := &Encoder{}
rval := reflect.ValueOf(x)
cf := e.marshal(rval)
if dict, ok := cf.(*cfDictionary); ok {
if _, ok := dict.values[0].(cfDate); !ok {
t.Error("inner value is not a cfDate")
}
} else {
t.Error("failed to marshal toplevel dictionary (?)")
}
}
type Dog struct {
Name string
}
type Animal interface{}
func TestInterfaceSliceMarshal(t *testing.T) {
x := make([]Animal, 0)
x = append(x, &Dog{Name: "dog"})
b, err := Marshal(x, XMLFormat)
if err != nil {
t.Error(err)
} else if len(b) == 0 {
t.Error("expect non-zero data")
}
}
func TestInterfaceGeneralSliceMarshal(t *testing.T) {
x := make([]interface{}, 0) // accept any type
x = append(x, &Dog{Name: "dog"}, "a string", 1, true)
b, err := Marshal(x, XMLFormat)
if err != nil {
t.Error(err)
} else if len(b) == 0 {
t.Error("expect non-zero data")
}
}