-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathunmarshal_test.go
116 lines (100 loc) · 2.24 KB
/
unmarshal_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
package plist
import (
"reflect"
"testing"
"time"
)
func BenchmarkStructUnmarshal(b *testing.B) {
type Data struct {
Intarray []uint64 `plist:"intarray"`
Floats []float64 `plist:"floats"`
Booleans []bool `plist:"booleans"`
Strings []string `plist:"strings"`
Dat []byte `plist:"data"`
Date time.Time `plist:"date"`
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var xval Data
d := &Decoder{}
d.unmarshal(plistValueTree, reflect.ValueOf(&xval))
}
}
func BenchmarkInterfaceUnmarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
var xval interface{}
d := &Decoder{}
d.unmarshal(plistValueTree, reflect.ValueOf(&xval))
}
}
func BenchmarkLargeArrayUnmarshal(b *testing.B) {
var xval [1024]byte
pval := cfData(make([]byte, 1024))
b.ResetTimer()
for i := 0; i < b.N; i++ {
d := &Decoder{}
d.unmarshal(pval, reflect.ValueOf(&xval))
}
}
type CustomDate struct{}
func (cd *CustomDate) UnmarshalPlist(unmarshal func(interface{}) error) error { return nil }
func TestCustomDateUnmarshal(t *testing.T) {
input := `<?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">
<date>2003-02-03T09:00:00.00Z</date>
</plist>`
var custom CustomDate
if _, err := Unmarshal([]byte(input), &custom); err != nil {
t.Error(err)
}
}
func TestInvalidMapKeyTypeUnmarshal(t *testing.T) {
m := make(map[int]string)
dict := &cfDictionary{
keys: []string{"1", "2"},
values: []cfValue{
cfString("first"),
cfString("second"),
},
}
var caught bool
{
defer func() {
if e := recover(); e != nil {
t.Log("error:", e)
caught = true
}
}()
d := &Decoder{}
d.unmarshalDictionary(dict, reflect.ValueOf(m))
}
if !caught {
t.Fail()
}
}
func TestValidButAliasedMapKeyTypeUnmarshal(t *testing.T) {
type sortaString string
m := make(map[sortaString]string)
dict := &cfDictionary{
keys: []string{"1", "2"},
values: []cfValue{
cfString("first"),
cfString("second"),
},
}
var caught bool
{
defer func() {
if e := recover(); e != nil {
t.Error("error:", e)
caught = true
}
}()
d := &Decoder{}
d.unmarshalDictionary(dict, reflect.ValueOf(m))
}
if caught {
t.Fail()
}
}