@@ -13,12 +13,19 @@ import (
13
13
14
14
"go.mongodb.org/mongo-driver/bson/bsoncodec"
15
15
"go.mongodb.org/mongo-driver/bson/bsontype"
16
+ "go.mongodb.org/mongo-driver/internal/assert"
16
17
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
17
18
)
18
19
19
20
func TestRawValue (t * testing.T ) {
21
+ t .Parallel ()
22
+
20
23
t .Run ("Unmarshal" , func (t * testing.T ) {
24
+ t .Parallel ()
25
+
21
26
t .Run ("Uses registry attached to value" , func (t * testing.T ) {
27
+ t .Parallel ()
28
+
22
29
reg := bsoncodec .NewRegistryBuilder ().Build ()
23
30
val := RawValue {Type : bsontype .String , Value : bsoncore .AppendString (nil , "foobar" ), r : reg }
24
31
var s string
@@ -29,6 +36,8 @@ func TestRawValue(t *testing.T) {
29
36
}
30
37
})
31
38
t .Run ("Uses default registry if no registry attached" , func (t * testing.T ) {
39
+ t .Parallel ()
40
+
32
41
want := "foobar"
33
42
val := RawValue {Type : bsontype .String , Value : bsoncore .AppendString (nil , want )}
34
43
var got string
@@ -40,7 +49,11 @@ func TestRawValue(t *testing.T) {
40
49
})
41
50
})
42
51
t .Run ("UnmarshalWithRegistry" , func (t * testing.T ) {
52
+ t .Parallel ()
53
+
43
54
t .Run ("Returns error when registry is nil" , func (t * testing.T ) {
55
+ t .Parallel ()
56
+
44
57
want := ErrNilRegistry
45
58
var val RawValue
46
59
got := val .UnmarshalWithRegistry (nil , & D {})
@@ -49,6 +62,8 @@ func TestRawValue(t *testing.T) {
49
62
}
50
63
})
51
64
t .Run ("Returns lookup error" , func (t * testing.T ) {
65
+ t .Parallel ()
66
+
52
67
reg := bsoncodec .NewRegistryBuilder ().Build ()
53
68
var val RawValue
54
69
var s string
@@ -59,6 +74,8 @@ func TestRawValue(t *testing.T) {
59
74
}
60
75
})
61
76
t .Run ("Returns DecodeValue error" , func (t * testing.T ) {
77
+ t .Parallel ()
78
+
62
79
reg := NewRegistryBuilder ().Build ()
63
80
val := RawValue {Type : bsontype .Double , Value : bsoncore .AppendDouble (nil , 3.14159 )}
64
81
var s string
@@ -69,6 +86,8 @@ func TestRawValue(t *testing.T) {
69
86
}
70
87
})
71
88
t .Run ("Success" , func (t * testing.T ) {
89
+ t .Parallel ()
90
+
72
91
reg := NewRegistryBuilder ().Build ()
73
92
want := float64 (3.14159 )
74
93
val := RawValue {Type : bsontype .Double , Value : bsoncore .AppendDouble (nil , want )}
@@ -81,7 +100,11 @@ func TestRawValue(t *testing.T) {
81
100
})
82
101
})
83
102
t .Run ("UnmarshalWithContext" , func (t * testing.T ) {
103
+ t .Parallel ()
104
+
84
105
t .Run ("Returns error when DecodeContext is nil" , func (t * testing.T ) {
106
+ t .Parallel ()
107
+
85
108
want := ErrNilContext
86
109
var val RawValue
87
110
got := val .UnmarshalWithContext (nil , & D {})
@@ -90,6 +113,8 @@ func TestRawValue(t *testing.T) {
90
113
}
91
114
})
92
115
t .Run ("Returns lookup error" , func (t * testing.T ) {
116
+ t .Parallel ()
117
+
93
118
dc := bsoncodec.DecodeContext {Registry : bsoncodec .NewRegistryBuilder ().Build ()}
94
119
var val RawValue
95
120
var s string
@@ -100,6 +125,8 @@ func TestRawValue(t *testing.T) {
100
125
}
101
126
})
102
127
t .Run ("Returns DecodeValue error" , func (t * testing.T ) {
128
+ t .Parallel ()
129
+
103
130
dc := bsoncodec.DecodeContext {Registry : NewRegistryBuilder ().Build ()}
104
131
val := RawValue {Type : bsontype .Double , Value : bsoncore .AppendDouble (nil , 3.14159 )}
105
132
var s string
@@ -110,6 +137,8 @@ func TestRawValue(t *testing.T) {
110
137
}
111
138
})
112
139
t .Run ("Success" , func (t * testing.T ) {
140
+ t .Parallel ()
141
+
113
142
dc := bsoncodec.DecodeContext {Registry : NewRegistryBuilder ().Build ()}
114
143
want := float64 (3.14159 )
115
144
val := RawValue {Type : bsontype .Double , Value : bsoncore .AppendDouble (nil , want )}
@@ -121,4 +150,59 @@ func TestRawValue(t *testing.T) {
121
150
}
122
151
})
123
152
})
153
+
154
+ t .Run ("IsZero" , func (t * testing.T ) {
155
+ t .Parallel ()
156
+
157
+ tests := []struct {
158
+ name string
159
+ val RawValue
160
+ want bool
161
+ }{
162
+ {
163
+ name : "empty" ,
164
+ val : RawValue {},
165
+ want : true ,
166
+ },
167
+ {
168
+ name : "zero type but non-zero value" ,
169
+ val : RawValue {
170
+ Type : 0x00 ,
171
+ Value : bsoncore .AppendInt32 (nil , 0 ),
172
+ },
173
+ want : false ,
174
+ },
175
+ {
176
+ name : "zero type and zero value" ,
177
+ val : RawValue {
178
+ Type : 0x00 ,
179
+ Value : bsoncore .AppendInt32 (nil , 0 ),
180
+ },
181
+ },
182
+ {
183
+ name : "non-zero type and non-zero value" ,
184
+ val : RawValue {
185
+ Type : bsontype .String ,
186
+ Value : bsoncore .AppendString (nil , "foobar" ),
187
+ },
188
+ want : false ,
189
+ },
190
+ {
191
+ name : "non-zero type and zero value" ,
192
+ val : RawValue {
193
+ Type : bsontype .String ,
194
+ Value : bsoncore .AppendString (nil , "foobar" ),
195
+ },
196
+ },
197
+ }
198
+
199
+ for _ , tt := range tests {
200
+ tt := tt // Capture the range variable
201
+ t .Run (tt .name , func (t * testing.T ) {
202
+ t .Parallel ()
203
+
204
+ assert .Equal (t , tt .want , tt .val .IsZero ())
205
+ })
206
+ }
207
+ })
124
208
}
0 commit comments