-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_test.go
90 lines (84 loc) · 2.02 KB
/
map_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
// 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 (
"fmt"
"reflect"
"testing"
)
// Convert Map -> bson -> Map then compare Maps.
var mapTest = []Map{
Map{"Float": Float(123.123)},
Map{"String": String("123")},
Map{"embed": Map{"foo": String("bar")}},
Map{"Array": Array{String("foo"), String("bar")}},
Map{"Binary": Binary{0x00, 0x01}},
Map{"Undefined": Undefined{}},
Map{"ObjectId": ObjectId{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00}},
Map{"Bool": Bool(true), "false": Bool(false)},
Map{"UTCDateTime": UTCDateTime(123)},
Map{"Null": Null{}},
Map{"Regexp": Regexp{"foo", "bar"}},
Map{"DBPointer": DBPointer{"foo", ObjectId{0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}},
Map{"Javascript": Javascript("foo")},
Map{"Symbol": Symbol("foo")},
Map{"JavascriptScope": JavascriptScope{"foo", Map{"bar": String("baz")}}},
Map{"Int32": Int32(123)},
Map{"Timestamp": Timestamp(123)},
Map{"Int64": Int64(123)},
Map{"MinKey": MinKey{}},
Map{"MaxKey": MaxKey{}},
}
// Example of reaching in to a document.
func Example() {
doc := Map{"foo": Map{"bar": String("baz")}}
var dst string
ok, err := doc.Reach(&dst, "foo", "bar")
if err != nil {
panic(err)
}
if !ok {
panic("foo.bar not found.")
}
fmt.Print(dst)
// Output: baz
}
func TestMap(t *testing.T) {
for _, d0 := range mapTest {
bs, err := d0.Encode()
if err != nil {
t.Fatal(err, d0)
}
d1, err := bs.Map()
if err != nil {
t.Fatal(err, d0, d1)
}
if !reflect.DeepEqual(d0, d1) {
t.Fatal(d0, d1)
}
}
}
func TestMapNoNest(t *testing.T) {
nest := Slice{{"abc", Int64(123)}}
src := Map{
"foo": String("bar"),
"nest": nest,
}
exp := Map{
"foo": String("bar"),
"nest": nest.MustEncode(),
}
bs, err := src.Encode()
if err != nil {
t.Fatal(err)
}
dst, err := bs.MapNoNest()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(dst, exp) {
t.Fatal(dst)
}
}