-
Notifications
You must be signed in to change notification settings - Fork 1
/
coercion_test.go
251 lines (233 loc) · 4.69 KB
/
coercion_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2013 Seth Bunce. 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 (
"reflect"
"testing"
"time"
)
func TestEncodeCoercion(t *testing.T) {
now := time.Now()
src := Map{
"null": nil,
"bool": true,
"int" : int(123),
"int8": int8(123),
"int16": int16(123),
"int32": int32(123),
"int64": int64(123),
"float64": float64(123.123),
"string": "foo",
"gotime": now,
}
exp := Map{
"null": Null{},
"bool": Bool(true),
"int" : Int64(123),
"int8": Int32(123),
"int16": Int32(123),
"int32": Int32(123),
"int64": Int64(123),
"float64": Float(123.123),
"string": String("foo"),
"gotime": UTCDateTime(now.UnixNano()/1000/1000),
}
bs, err := src.Encode()
if err != nil {
t.Fatal(err, src)
}
dst, err := bs.Map()
if err != nil {
t.Fatal(err, dst, exp)
}
if !reflect.DeepEqual(dst, exp) {
t.Fatal(dst, exp)
}
}
func TestReachCoerce(t *testing.T) {
src := Map{
"foo": Map{
"Float": Float(123.123),
"String": String("foo"),
"Binary": Binary{0x00, 0x01},
"ObjectId": ObjectId{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00},
"Bool": Bool(true),
"UTCDateTime": UTCDateTime(123),
"Javascript": Javascript("foo"),
"Symbol": Symbol("foo"),
"Int32": Int32(123),
"Timestamp": Timestamp(123),
"Int64": Int64(123),
},
}
// Float
var floatTest float64
ok, err := src.Reach(&floatTest, "foo", "Float")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Float'.")
}
if floatTest != 123.123 {
t.Fatal(floatTest)
}
// String
var stringTest string
ok, err = src.Reach(&stringTest, "foo", "String")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'String'.")
}
if stringTest != "foo" {
t.Fatal(stringTest)
}
// Binary
var binaryTest []byte
ok, err = src.Reach(&binaryTest, "foo", "Binary")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Binary'.")
}
if !reflect.DeepEqual(binaryTest, []byte{0x00, 0x01}) {
t.Fatal(binaryTest)
}
// ObjectId
var objectIdTest []byte
ok, err = src.Reach(&objectIdTest, "foo", "ObjectId")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'ObjectId.")
}
if !reflect.DeepEqual(objectIdTest, []byte{0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) {
t.Fatal(objectIdTest)
}
// Bool
var boolTest bool
ok, err = src.Reach(&boolTest, "foo", "Bool")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Bool'.")
}
if boolTest != true {
t.Fatal(boolTest)
}
// UTCDateTime
var uTest0 int64
ok, err = src.Reach(&uTest0, "foo", "UTCDateTime")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'UTCDateTime'.")
}
if uTest0 != 123 {
t.Fatal(uTest0)
}
// UTCDateTime
var uTest1 time.Time
ok, err = src.Reach(&uTest1, "foo", "UTCDateTime")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'UTCDateTime'.")
}
if uTest1.UnixNano() != 123*1e3 {
t.Fatal(uTest1.UnixNano())
}
// Javascript
var jsTest string
ok, err = src.Reach(&jsTest, "foo", "Javascript")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Javascript'.")
}
if jsTest != "foo" {
t.Fatal(jsTest)
}
// Symbol
var symbolTest string
ok, err = src.Reach(&symbolTest, "foo", "Symbol")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Symbol'.")
}
if symbolTest != "foo" {
t.Fatal(symbolTest)
}
// Int32
var int32Test0 int32
ok, err = src.Reach(&int32Test0, "foo", "Int32")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Int32'.")
}
if int32Test0 != 123 {
t.Fatal(int32Test0)
}
// Int32
var int32Test1 int64
ok, err = src.Reach(&int32Test1, "foo", "Int32")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Int32'.")
}
if int32Test1 != 123 {
t.Fatal(int32Test1)
}
// Timestamp
var tsTest0 int64
ok, err = src.Reach(&tsTest0, "foo", "UTCDateTime")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'UTCDateTime.")
}
if tsTest0 != 123 {
t.Fatal(tsTest0)
}
// Timestamp
var tsTest1 time.Time
ok, err = src.Reach(&tsTest1, "foo", "UTCDateTime")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'UTCDateTime'.")
}
if tsTest1.UnixNano() != 123*1e3 {
t.Fatal(tsTest1.UnixNano())
}
// Int64
var int64Test int32
ok, err = src.Reach(&int64Test, "foo", "Int32")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected to find 'Int32'.")
}
if int64Test != 123 {
t.Fatal(int64Test)
}
}