@@ -8,12 +8,169 @@ package bson
8
8
9
9
import (
10
10
"io"
11
- "testing"
12
11
13
- "go.mongodb.org/mongo-driver/v2/internal/assert"
14
12
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
15
13
)
16
14
15
+ var marshalValueTestCases = []marshalValueTestCase {
16
+ {
17
+ name : "double" ,
18
+ val : 3.14 ,
19
+ bsontype : TypeDouble ,
20
+ bytes : bsoncore .AppendDouble (nil , 3.14 ),
21
+ },
22
+ {
23
+ name : "string" ,
24
+ val : "hello world" ,
25
+ bsontype : TypeString ,
26
+ bytes : bsoncore .AppendString (nil , "hello world" ),
27
+ },
28
+ {
29
+ name : "binary" ,
30
+ val : Binary {1 , []byte {1 , 2 }},
31
+ bsontype : TypeBinary ,
32
+ bytes : bsoncore .AppendBinary (nil , 1 , []byte {1 , 2 }),
33
+ },
34
+ {
35
+ name : "undefined" ,
36
+ val : Undefined {},
37
+ bsontype : TypeUndefined ,
38
+ bytes : []byte {},
39
+ },
40
+ {
41
+ name : "object id" ,
42
+ val : ObjectID {103 , 116 , 166 , 161 , 70 , 33 , 67 , 139 , 164 , 144 , 255 , 112 },
43
+ bsontype : TypeObjectID ,
44
+ bytes : bsoncore .AppendObjectID (nil , ObjectID {103 , 116 , 166 , 161 , 70 , 33 , 67 , 139 , 164 , 144 , 255 , 112 }),
45
+ },
46
+ {
47
+ name : "boolean" ,
48
+ val : true ,
49
+ bsontype : TypeBoolean ,
50
+ bytes : bsoncore .AppendBoolean (nil , true ),
51
+ },
52
+ {
53
+ name : "datetime" ,
54
+ val : DateTime (5 ),
55
+ bsontype : TypeDateTime ,
56
+ bytes : bsoncore .AppendDateTime (nil , 5 ),
57
+ },
58
+ {
59
+ name : "null" ,
60
+ val : Null {},
61
+ bsontype : TypeNull ,
62
+ bytes : []byte {},
63
+ },
64
+ {
65
+ name : "regex" ,
66
+ val : Regex {Pattern : "pattern" , Options : "imx" },
67
+ bsontype : TypeRegex ,
68
+ bytes : bsoncore .AppendRegex (nil , "pattern" , "imx" ),
69
+ },
70
+ {
71
+ name : "dbpointer" ,
72
+ val : DBPointer {
73
+ DB : "db" ,
74
+ Pointer : ObjectID {103 , 116 , 166 , 161 , 70 , 33 , 67 , 139 , 164 , 144 , 255 , 112 },
75
+ },
76
+ bsontype : TypeDBPointer ,
77
+ bytes : bsoncore .AppendDBPointer (
78
+ nil ,
79
+ "db" ,
80
+ ObjectID {103 , 116 , 166 , 161 , 70 , 33 , 67 , 139 , 164 , 144 , 255 , 112 },
81
+ ),
82
+ },
83
+ {
84
+ name : "javascript" ,
85
+ val : JavaScript ("js" ),
86
+ bsontype : TypeJavaScript ,
87
+ bytes : bsoncore .AppendJavaScript (nil , "js" ),
88
+ },
89
+ {
90
+ name : "symbol" ,
91
+ val : Symbol ("symbol" ),
92
+ bsontype : TypeSymbol ,
93
+ bytes : bsoncore .AppendSymbol (nil , "symbol" ),
94
+ },
95
+ {
96
+ name : "code with scope" ,
97
+ val : CodeWithScope {Code : "code" , Scope : D {{"a" , "b" }}},
98
+ bsontype : TypeCodeWithScope ,
99
+ bytes : bsoncore .AppendCodeWithScope (
100
+ nil ,
101
+ "code" ,
102
+ bsoncore .NewDocumentBuilder ().
103
+ AppendString ("a" , "b" ).
104
+ Build ()),
105
+ },
106
+ {
107
+ name : "int32" ,
108
+ val : 5 ,
109
+ bsontype : TypeInt32 ,
110
+ bytes : bsoncore .AppendInt32 (nil , 5 ),
111
+ },
112
+ {
113
+ name : "int64" ,
114
+ val : int64 (5 ),
115
+ bsontype : TypeInt64 ,
116
+ bytes : bsoncore .AppendInt64 (nil , 5 ),
117
+ },
118
+ {
119
+ name : "timestamp" ,
120
+ val : Timestamp {T : 1 , I : 5 },
121
+ bsontype : TypeTimestamp ,
122
+ bytes : bsoncore .AppendTimestamp (nil , 1 , 5 ),
123
+ },
124
+ {
125
+ name : "decimal128" ,
126
+ val : NewDecimal128 (5 , 10 ),
127
+ bsontype : TypeDecimal128 ,
128
+ bytes : bsoncore .AppendDecimal128 (nil , 5 , 10 ),
129
+ },
130
+ {
131
+ name : "min key" ,
132
+ val : MinKey {},
133
+ bsontype : TypeMinKey ,
134
+ bytes : []byte {},
135
+ },
136
+ {
137
+ name : "max key" ,
138
+ val : MaxKey {},
139
+ bsontype : TypeMaxKey ,
140
+ bytes : []byte {},
141
+ },
142
+ {
143
+ name : "struct" ,
144
+ val : marshalValueStruct {Foo : 10 },
145
+ bsontype : TypeEmbeddedDocument ,
146
+ bytes : bsoncore .NewDocumentBuilder ().
147
+ AppendInt32 ("foo" , 10 ).
148
+ Build (),
149
+ },
150
+ {
151
+ name : "D" ,
152
+ val : D {{"foo" , int32 (10 )}},
153
+ bsontype : TypeEmbeddedDocument ,
154
+ bytes : bsoncore .NewDocumentBuilder ().
155
+ AppendInt32 ("foo" , 10 ).
156
+ Build (),
157
+ },
158
+ {
159
+ name : "M" ,
160
+ val : M {"foo" : int32 (10 )},
161
+ bsontype : TypeEmbeddedDocument ,
162
+ bytes : bsoncore .NewDocumentBuilder ().
163
+ AppendInt32 ("foo" , 10 ).
164
+ Build (),
165
+ },
166
+ {
167
+ name : "ValueMarshaler" ,
168
+ val : marshalValueMarshaler {Foo : 10 },
169
+ bsontype : TypeInt32 ,
170
+ bytes : bsoncore .AppendInt32 (nil , 10 ),
171
+ },
172
+ }
173
+
17
174
// helper type for testing MarshalValue that implements io.Reader
18
175
type marshalValueInterfaceInner struct {
19
176
Foo int
@@ -59,70 +216,3 @@ type marshalValueTestCase struct {
59
216
bsontype Type
60
217
bytes []byte
61
218
}
62
-
63
- func newMarshalValueTestCases (t * testing.T ) []marshalValueTestCase {
64
- t .Helper ()
65
-
66
- var (
67
- oid = NewObjectID ()
68
- regex = Regex {Pattern : "pattern" , Options : "imx" }
69
- dbPointer = DBPointer {DB : "db" , Pointer : NewObjectID ()}
70
- codeWithScope = CodeWithScope {Code : "code" , Scope : D {{"a" , "b" }}}
71
- decimal128h , decimal128l = NewDecimal128 (5 , 10 ).GetBytes ()
72
- structTest = marshalValueStruct {Foo : 10 }
73
- )
74
- idx , scopeCore := bsoncore .AppendDocumentStart (nil )
75
- scopeCore = bsoncore .AppendStringElement (scopeCore , "a" , "b" )
76
- scopeCore , err := bsoncore .AppendDocumentEnd (scopeCore , idx )
77
- assert .Nil (t , err , "Document error: %v" , err )
78
- structCore , err := Marshal (structTest )
79
- assert .Nil (t , err , "Marshal error: %v" , err )
80
-
81
- return []marshalValueTestCase {
82
- {"double" , 3.14 , TypeDouble , bsoncore .AppendDouble (nil , 3.14 )},
83
- {"string" , "hello world" , TypeString , bsoncore .AppendString (nil , "hello world" )},
84
- {"binary" , Binary {1 , []byte {1 , 2 }}, TypeBinary , bsoncore .AppendBinary (nil , 1 , []byte {1 , 2 })},
85
- {"undefined" , Undefined {}, TypeUndefined , []byte {}},
86
- {"object id" , oid , TypeObjectID , bsoncore .AppendObjectID (nil , oid )},
87
- {"boolean" , true , TypeBoolean , bsoncore .AppendBoolean (nil , true )},
88
- {"datetime" , DateTime (5 ), TypeDateTime , bsoncore .AppendDateTime (nil , 5 )},
89
- {"null" , Null {}, TypeNull , []byte {}},
90
- {"regex" , regex , TypeRegex , bsoncore .AppendRegex (nil , regex .Pattern , regex .Options )},
91
- {"dbpointer" , dbPointer , TypeDBPointer , bsoncore .AppendDBPointer (nil , dbPointer .DB , dbPointer .Pointer )},
92
- {"javascript" , JavaScript ("js" ), TypeJavaScript , bsoncore .AppendJavaScript (nil , "js" )},
93
- {"symbol" , Symbol ("symbol" ), TypeSymbol , bsoncore .AppendSymbol (nil , "symbol" )},
94
- {"code with scope" , codeWithScope , TypeCodeWithScope , bsoncore .AppendCodeWithScope (nil , "code" , scopeCore )},
95
- {"int32" , 5 , TypeInt32 , bsoncore .AppendInt32 (nil , 5 )},
96
- {"int64" , int64 (5 ), TypeInt64 , bsoncore .AppendInt64 (nil , 5 )},
97
- {"timestamp" , Timestamp {T : 1 , I : 5 }, TypeTimestamp , bsoncore .AppendTimestamp (nil , 1 , 5 )},
98
- {"decimal128" , NewDecimal128 (decimal128h , decimal128l ), TypeDecimal128 , bsoncore .AppendDecimal128 (nil , decimal128h , decimal128l )},
99
- {"min key" , MinKey {}, TypeMinKey , []byte {}},
100
- {"max key" , MaxKey {}, TypeMaxKey , []byte {}},
101
- {"struct" , structTest , TypeEmbeddedDocument , structCore },
102
- {"D" , D {{"foo" , int32 (10 )}}, TypeEmbeddedDocument , structCore },
103
- {"M" , M {"foo" : int32 (10 )}, TypeEmbeddedDocument , structCore },
104
- {"ValueMarshaler" , marshalValueMarshaler {Foo : 10 }, TypeInt32 , bsoncore .AppendInt32 (nil , 10 )},
105
- }
106
-
107
- }
108
-
109
- func newMarshalValueTestCasesWithInterfaceCore (t * testing.T ) []marshalValueTestCase {
110
- t .Helper ()
111
-
112
- marshalValueTestCases := newMarshalValueTestCases (t )
113
-
114
- interfaceTest := marshalValueInterfaceOuter {
115
- Reader : marshalValueInterfaceInner {
116
- Foo : 10 ,
117
- },
118
- }
119
- interfaceCore , err := Marshal (interfaceTest )
120
- assert .Nil (t , err , "Marshal error: %v" , err )
121
-
122
- marshalValueTestCases = append (
123
- marshalValueTestCases ,
124
- marshalValueTestCase {"interface" , interfaceTest , TypeEmbeddedDocument , interfaceCore },
125
- )
126
-
127
- return marshalValueTestCases
128
- }
0 commit comments