-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodec_test.go
197 lines (167 loc) · 4.17 KB
/
codec_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright © 2019 Bjørn Erik Pedersen <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package tmc
import (
"encoding/json"
"math/big"
"testing"
"time"
yaml "gopkg.in/yaml.v2"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
)
func TestRoundtrip(t *testing.T) {
c := qt.New(t)
src := newTestMap()
for _, test := range []struct {
name string
options []Option
dataAssert func(c *qt.C, data string)
}{
{"Default", nil,
func(c *qt.C, data string) { c.Assert(data, qt.Contains, `{"nested":{"vduration|time.Duration":"5s"`) }},
{"Custom type separator",
[]Option{
WithTypeSep("TYPE:"),
},
func(c *qt.C, data string) { c.Assert(data, qt.Contains, "TYPE:") },
},
{"YAML",
[]Option{
WithMarshalUnmarshaler(new(yamlMarshaler)),
},
func(c *qt.C, data string) { c.Assert(data, qt.Contains, "vduration|time.Duration: 3s") },
},
{"JSON indent",
[]Option{
WithMarshalUnmarshaler(new(jsonMarshalerIndent)),
},
func(c *qt.C, data string) {
},
},
} {
test := test
c.Run(test.name, func(c *qt.C) {
c.Parallel()
codec, err := New(test.options...)
c.Assert(err, qt.IsNil)
data, err := codec.Marshal(src)
c.Assert(err, qt.IsNil)
//c.Log(string(data))
if test.dataAssert != nil {
test.dataAssert(c, string(data))
}
dst := make(map[string]interface{})
c.Assert(codec.Unmarshal(data, &dst), qt.IsNil)
c.Assert(dst, eq, src)
})
}
}
func TestErrors(t *testing.T) {
c := qt.New(t)
codec, err := New()
c.Assert(err, qt.IsNil)
marshal := func(v interface{}) error {
_, err := codec.Marshal(v)
return err
}
// OK
c.Assert(marshal(map[string]interface{}{"32": "a"}), qt.IsNil)
c.Assert(marshal(map[string]int{"32": 32}), qt.IsNil)
// Should fail
c.Assert(marshal([]string{"a"}), qt.Not(qt.IsNil))
c.Assert(marshal(map[int]interface{}{32: "a"}), qt.Not(qt.IsNil))
c.Assert(marshal(map[string]interface{}{"a": map[int]string{32: "32"}}), qt.Not(qt.IsNil))
}
func BenchmarkCodec(b *testing.B) {
b.Run("JSON regular", func(b *testing.B) {
b.StopTimer()
mi := newTestMap()
b.StartTimer()
for i := 0; i < b.N; i++ {
data, err := json.Marshal(mi)
if err != nil {
b.Fatal(err)
}
m := make(map[string]interface{})
if err := json.Unmarshal(data, &m); err != nil {
b.Fatal(err)
}
}
})
b.Run("JSON typed", func(b *testing.B) {
b.StopTimer()
mi := newTestMap()
c, err := New()
if err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
data, err := c.Marshal(mi)
if err != nil {
b.Fatal(err)
}
m := make(map[string]interface{})
if err := c.Unmarshal(data, &m); err != nil {
b.Fatal(err)
}
}
})
}
func newTestMap() map[string]interface{} {
return map[string]interface{}{
"vstring": "Hello1",
"vstring|": "Hello2",
"vstring|foo": "Hello3",
// Numbers
"vint": 32,
"vfloat64": float64(3.14159),
"vrat": big.NewRat(1, 2),
// Time
"vtime": time.Now(),
"vduration": 3 * time.Second,
"vsliceint": []int{1, 3, 4},
"nested": map[string]interface{}{
"vint": 55,
"vduration": 5 * time.Second,
},
"nested-typed-int": map[string]int{
"vint": 42,
},
"nested-typed-duration": map[string]time.Duration{
"v1": 5 * time.Second,
"v2": 10 * time.Second,
},
}
}
var eq = qt.CmpEquals(
cmp.Comparer(
func(v1, v2 *big.Rat) bool {
return v1.RatString() == v2.RatString()
},
),
cmp.Comparer(func(v1, v2 time.Time) bool {
// UnmarshalText always create times with no monotonic clock reading,
// so we cannot compare with ==.
// TODO(bep) improve this
return v1.Unix() == v2.Unix()
}),
)
type yamlMarshaler int
func (yamlMarshaler) Marshal(v interface{}) ([]byte, error) {
return yaml.Marshal(v)
}
func (yamlMarshaler) Unmarshal(b []byte, v interface{}) error {
return yaml.Unmarshal(b, v)
}
// Useful for debugging
type jsonMarshalerIndent int
func (jsonMarshalerIndent) Marshal(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}
func (jsonMarshalerIndent) Unmarshal(b []byte, v interface{}) error {
return json.Unmarshal(b, v)
}