This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
marshal_test.go
233 lines (222 loc) · 6.54 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bson
import (
"testing"
"time"
)
type String1 string
func (cs String1) MarshalBson(buf *ChunkedWriter, key string) {
// Hardcode value to verify that function is called
EncodeString(buf, key, "test")
}
type String2 string
func (cs *String2) MarshalBson(buf *ChunkedWriter, key string) {
// Hardcode value to verify that function is called
EncodeString(buf, key, "test")
}
var marshaltest = []struct {
desc string
in interface{}
out string
}{{
"struct encode",
struct{ Val string }{"test"},
"\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00",
}, {
"struct encode nil",
struct{ Val *int }{},
"\n\x00\x00\x00\nVal\x00\x00",
}, {
"struct encode nil interface",
struct{ Val interface{} }{},
"\n\x00\x00\x00\nVal\x00\x00",
}, {
"map encode",
map[string]string{"Val": "test"},
"\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00",
}, {
"embedded map encode",
struct{ Inner map[string]string }{map[string]string{"Val": "test"}},
"\x1f\x00\x00\x00\x03Inner\x00\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00\x00",
}, {
"embedded map encode nil",
struct{ Inner map[string]string }{},
"\x11\x00\x00\x00\x03Inner\x00\x05\x00\x00\x00\x00\x00",
}, {
"slice encode",
[]string{"test1", "test2"},
"\x1f\x00\x00\x00\x050\x00\x05\x00\x00\x00\x00test1\x051\x00\x05\x00\x00\x00\x00test2\x00",
}, {
"embedded slice encode",
struct{ Inner []string }{[]string{"test1", "test2"}},
"+\x00\x00\x00\x04Inner\x00\x1f\x00\x00\x00\x050\x00\x05\x00\x00\x00\x00test1\x051\x00\x05\x00\x00\x00\x00test2\x00\x00",
}, {
"embedded slice encode nil",
struct{ Inner []string }{},
"\x11\x00\x00\x00\x04Inner\x00\x05\x00\x00\x00\x00\x00",
}, {
"array encode",
[2]string{"test1", "test2"},
"\x1f\x00\x00\x00\x050\x00\x05\x00\x00\x00\x00test1\x051\x00\x05\x00\x00\x00\x00test2\x00",
}, {
"string encode",
"test",
"\x15\x00\x00\x00\x05_Val_\x00\x04\x00\x00\x00\x00test\x00",
}, {
"bytes encode",
[]byte("test"),
"\x15\x00\x00\x00\x05_Val_\x00\x04\x00\x00\x00\x00test\x00",
}, {
"int64 encode",
int64(1),
"\x14\x00\x00\x00\x12_Val_\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"int32 encode",
int32(1),
"\x10\x00\x00\x00\x10_Val_\x00\x01\x00\x00\x00\x00",
}, {
"int encode",
int(1),
"\x14\x00\x00\x00\x12_Val_\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"uint64 encode",
uint64(1),
"\x14\x00\x00\x00\x12_Val_\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"uint32 encode",
uint32(1),
"\x14\x00\x00\x00\x12_Val_\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"uint encode",
uint(1),
"\x14\x00\x00\x00\x12_Val_\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"float encode",
float64(1.0),
"\x14\x00\x00\x00\x01_Val_\x00\x00\x00\x00\x00\x00\x00\xf0?\x00",
}, {
"bool encode",
true,
"\r\x00\x00\x00\b_Val_\x00\x01\x00",
}, {
"time encode",
time.Unix(1136243045, 0).UTC(),
"\x14\x00\x00\x00\t_Val_\x00\x88\xf2\\\x8d\b\x01\x00\x00\x00",
}, {
// Following encodes are for reference. They're used for
// the decode tests.
"embedded Object encode",
struct{ Val struct{ Val2 string } }{struct{ Val2 string }{"test"}},
"\x1e\x00\x00\x00\x03Val\x00\x14\x00\x00\x00\x05Val2\x00\x04\x00\x00\x00\x00test\x00\x00",
}, {
"embedded Object encode nil element",
struct{ Val struct{ Val2 *int64 } }{struct{ Val2 *int64 }{nil}},
"\x15\x00\x00\x00\x03Val\x00\v\x00\x00\x00\nVal2\x00\x00\x00",
}, {
"embedded Array encode",
struct{ Val []string }{Val: []string{"test"}},
"\x1b\x00\x00\x00\x04Val\x00\x11\x00\x00\x00\x050\x00\x04\x00\x00\x00\x00test\x00\x00",
}, {
"Array encode nil element",
struct{ Val []*int64 }{Val: []*int64{nil, newint64(1)}},
"\x1d\x00\x00\x00\x04Val\x00\x13\x00\x00\x00\n0\x00\x121\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"embedded Number encode",
struct{ Val float64 }{1.0},
"\x12\x00\x00\x00\x01Val\x00\x00\x00\x00\x00\x00\x00\xf0?\x00",
}, {
"embedded Binary encode",
struct{ Val string }{"test"},
"\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00",
}, {
"embedded Boolean encode",
struct{ Val bool }{true},
"\v\x00\x00\x00\bVal\x00\x01\x00",
}, {
"embedded Datetime encode",
struct{ Val time.Time }{time.Unix(1136243045, 0).UTC()},
"\x12\x00\x00\x00\tVal\x00\x88\xf2\\\x8d\b\x01\x00\x00\x00",
}, {
"embedded Null encode",
struct{ Val *int }{},
"\n\x00\x00\x00\nVal\x00\x00",
}, {
"embedded Int encode",
struct{ Val int32 }{1},
"\x0e\x00\x00\x00\x10Val\x00\x01\x00\x00\x00\x00",
}, {
"embedded Long encode",
struct{ Val int64 }{1},
"\x12\x00\x00\x00\x12Val\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"embedded Ulong encode",
struct{ Val uint64 }{1},
"\x12\x00\x00\x00\x12Val\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00",
}, {
"embedded non-pointer encode with custom marshaler",
struct{ Val String1 }{String1("foo")},
"\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00",
}, {
"embedded pointer encode with custom marshaler",
struct{ Val *String1 }{func(cs String1) *String1 { return &cs }(String1("foo"))},
"\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00",
}, {
"embedded nil pointer encode with custom marshaler",
struct{ Val *String1 }{},
"\n\x00\x00\x00\nVal\x00\x00",
}, {
"embedded pointer encode with custom pointer marshaler",
struct{ Val *String2 }{func(cs String2) *String2 { return &cs }(String2("foo"))},
"\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00",
}, {
"embedded addressable encode with custom pointer marshaler",
&struct{ Val String2 }{String2("foo")},
"\x13\x00\x00\x00\x05Val\x00\x04\x00\x00\x00\x00test\x00",
}, {
"embedded non-addressable encode with custom pointer marshaler",
struct{ Val String2 }{String2("foo")},
"\x12\x00\x00\x00\x05Val\x00\x03\x00\x00\x00\x00foo\x00",
}}
func TestMarshal(t *testing.T) {
for _, tcase := range marshaltest {
got := verifyMarshal(t, tcase.in)
if string(got) != tcase.out {
t.Errorf("%s: encoded: \n%q, want\n%q", tcase.desc, got, tcase.out)
}
}
}
var marshalErrorCases = []struct {
desc string
in interface{}
out string
}{{
"nil input",
nil,
"cannot marshal nil",
}, {
"chan input",
make(chan int),
"unexpected type chan int",
}, {
"embedded chan input",
struct{ Val chan int }{},
"don't know how to marshal chan int",
}, {
"map with int key",
map[int]int{},
"can't marshall maps with non-string key types",
}}
func TestMarshalErrors(t *testing.T) {
for _, tcase := range marshalErrorCases {
_, err := Marshal(tcase.in)
got := ""
if err != nil {
got = err.Error()
}
if got != tcase.out {
t.Errorf("%s: received: %q, want %q", tcase.desc, got, tcase.out)
}
}
}