Debian Codegraph @

marshal_test.go

raw symbols SCIP
1 package plist
2
3 import (
4 "reflect"
5 "testing"
6 "time"
7 )
8
9 func BenchmarkStructMarshal(b *testing.B) {
10 for i := 0; i < b.N; i++ {
11 e := &Encoder{}
12 e.marshal(reflect.ValueOf(plistValueTreeRawData))
13 }
14 }
15
16 func BenchmarkMapMarshal(b *testing.B) {
17 data := map[string]interface{}{
18 "intarray": []interface{}{
19 int(1),
20 int8(8),
21 int16(16),
22 int32(32),
23 int64(64),
24 uint(2),
25 uint8(9),
26 uint16(17),
27 uint32(33),
28 uint64(65),
29 },
30 "floats": []interface{}{
31 float32(32.0),
32 float64(64.0),
33 },
34 "booleans": []bool{
35 true,
36 false,
37 },
38 "strings": []string{
39 "Hello, ASCII",
40 "Hello, 世界",
41 },
42 "data": []byte{1, 2, 3, 4},
43 "date": time.Date(2013, 11, 27, 0, 34, 0, 0, time.UTC),
44 }
45 b.ResetTimer()
46 for i := 0; i < b.N; i++ {
47 e := &Encoder{}
48 e.marshal(reflect.ValueOf(data))
49 }
50 }
51
52 func TestInvalidMarshal(t *testing.T) {
53 tests := []struct {
54 Name string
55 Thing interface{}
56 }{
57 {"Function", func() {}},
58 {"Nil", nil},
59 {"Map with integer keys", map[int]string{1: "hi"}},
60 {"Channel", make(chan int)},
61 }
62
63 for _, v := range tests {
64 subtest(t, v.Name, func(t *testing.T) {
65 data, err := Marshal(v.Thing, OpenStepFormat)
66 if err == nil {
67 t.Fatalf("expected error; got plist data: %x", data)
68 } else {
69 t.Log(err)
70 }
71 })
72 }
73 }
74
75 type Cat struct{}
76
77 func (c *Cat) MarshalPlist() (interface{}, error) {
78 return "cat", nil
79 }
80
81 func TestInterfaceMarshal(t *testing.T) {
82 var c Cat
83 b, err := Marshal(&c, XMLFormat)
84 if err != nil {
85 t.Log(err)
86 } else if len(b) == 0 {
87 t.Log("expect non-zero data")
88 }
89 }
90
91 func TestInterfaceFieldMarshal(t *testing.T) {
92 type X struct {
93 C interface{} // C's type does not implement Marshaler
94 }
95 x := &X{
96 C: &Cat{}, // C's value implements Marshaler
97 }
98
99 b, err := Marshal(x, XMLFormat)
100 if err != nil {
101 t.Log(err)
102 } else if len(b) == 0 {
103 t.Log("expect non-zero data")
104 }
105 }