@@ -10,6 +10,7 @@ import (
1010 "testing"
1111
1212 "go.mongodb.org/mongo-driver/v2/bson"
13+ "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
1314)
1415
1516func TestEqualDocument (t * testing.T ) {
@@ -60,3 +61,189 @@ func TestEqualDocument(t *testing.T) {
6061 })
6162 }
6263}
64+
65+ func TestEqualValue (t * testing.T ) {
66+ t .Parallel ()
67+
68+ t .Run ("bson.RawValue" , func (t * testing.T ) {
69+ t .Parallel ()
70+
71+ testCases := []struct {
72+ name string
73+ expected bson.RawValue
74+ actual bson.RawValue
75+ want bool
76+ }{
77+ {
78+ name : "equal" ,
79+ expected : bson.RawValue {
80+ Type : bson .TypeInt32 ,
81+ Value : []byte {1 , 0 , 0 , 0 },
82+ },
83+ actual : bson.RawValue {
84+ Type : bson .TypeInt32 ,
85+ Value : []byte {1 , 0 , 0 , 0 },
86+ },
87+ want : true ,
88+ },
89+ {
90+ name : "same type, different value" ,
91+ expected : bson.RawValue {
92+ Type : bson .TypeInt32 ,
93+ Value : []byte {1 , 0 , 0 , 0 },
94+ },
95+ actual : bson.RawValue {
96+ Type : bson .TypeInt32 ,
97+ Value : []byte {1 , 1 , 1 , 1 },
98+ },
99+ want : false ,
100+ },
101+ {
102+ name : "same value, different type" ,
103+ expected : bson.RawValue {
104+ Type : bson .TypeDouble ,
105+ Value : []byte {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
106+ },
107+ actual : bson.RawValue {
108+ Type : bson .TypeInt64 ,
109+ Value : []byte {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
110+ },
111+ want : false ,
112+ },
113+ {
114+ name : "different value, different type" ,
115+ expected : bson.RawValue {
116+ Type : bson .TypeInt32 ,
117+ Value : []byte {1 , 0 , 0 , 0 },
118+ },
119+ actual : bson.RawValue {
120+ Type : bson .TypeString ,
121+ Value : []byte {1 , 1 , 1 , 1 },
122+ },
123+ want : false ,
124+ },
125+ {
126+ name : "invalid" ,
127+ expected : bson.RawValue {
128+ Type : bson .TypeInt64 ,
129+ Value : []byte {1 , 0 , 0 , 0 },
130+ },
131+ actual : bson.RawValue {
132+ Type : bson .TypeInt32 ,
133+ Value : []byte {1 , 0 , 0 , 0 },
134+ },
135+ want : false ,
136+ },
137+ {
138+ name : "empty" ,
139+ expected : bson.RawValue {},
140+ actual : bson.RawValue {},
141+ want : true ,
142+ },
143+ }
144+
145+ for _ , tc := range testCases {
146+ tc := tc // Capture range variable.
147+
148+ t .Run (tc .name , func (t * testing.T ) {
149+ t .Parallel ()
150+
151+ got := EqualValue (new (testing.T ), tc .expected , tc .actual )
152+ if got != tc .want {
153+ t .Errorf ("EqualValue(%#v, %#v) = %v, want %v" , tc .expected , tc .actual , got , tc .want )
154+ }
155+ })
156+ }
157+ })
158+
159+ t .Run ("bsoncore.Value" , func (t * testing.T ) {
160+ t .Parallel ()
161+
162+ testCases := []struct {
163+ name string
164+ expected bsoncore.Value
165+ actual bsoncore.Value
166+ want bool
167+ }{
168+ {
169+ name : "equal" ,
170+ expected : bsoncore.Value {
171+ Type : bsoncore .TypeInt32 ,
172+ Data : []byte {1 , 0 , 0 , 0 },
173+ },
174+ actual : bsoncore.Value {
175+ Type : bsoncore .TypeInt32 ,
176+ Data : []byte {1 , 0 , 0 , 0 },
177+ },
178+ want : true ,
179+ },
180+ {
181+ name : "same type, different value" ,
182+ expected : bsoncore.Value {
183+ Type : bsoncore .TypeInt32 ,
184+ Data : []byte {1 , 0 , 0 , 0 },
185+ },
186+ actual : bsoncore.Value {
187+ Type : bsoncore .TypeInt32 ,
188+ Data : []byte {1 , 1 , 1 , 1 },
189+ },
190+ want : false ,
191+ },
192+ {
193+ name : "same value, different type" ,
194+ expected : bsoncore.Value {
195+ Type : bsoncore .TypeDouble ,
196+ Data : []byte {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
197+ },
198+ actual : bsoncore.Value {
199+ Type : bsoncore .TypeInt64 ,
200+ Data : []byte {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
201+ },
202+ want : false ,
203+ },
204+ {
205+ name : "different value, different type" ,
206+ expected : bsoncore.Value {
207+ Type : bsoncore .TypeInt32 ,
208+ Data : []byte {1 , 0 , 0 , 0 },
209+ },
210+ actual : bsoncore.Value {
211+ Type : bsoncore .TypeString ,
212+ Data : []byte {1 , 1 , 1 , 1 },
213+ },
214+ want : false ,
215+ },
216+ {
217+ name : "invalid" ,
218+ expected : bsoncore.Value {
219+ Type : bsoncore .TypeInt64 ,
220+ Data : []byte {1 , 0 , 0 , 0 },
221+ },
222+ actual : bsoncore.Value {
223+ Type : bsoncore .TypeInt32 ,
224+ Data : []byte {1 , 0 , 0 , 0 },
225+ },
226+ want : false ,
227+ },
228+ {
229+ name : "empty" ,
230+ expected : bsoncore.Value {},
231+ actual : bsoncore.Value {},
232+ want : true ,
233+ },
234+ }
235+
236+ for _ , tc := range testCases {
237+ tc := tc // Capture range variable.
238+
239+ t .Run (tc .name , func (t * testing.T ) {
240+ t .Parallel ()
241+
242+ got := EqualValue (new (testing.T ), tc .expected , tc .actual )
243+ if got != tc .want {
244+ t .Errorf ("EqualValue(%#v, %#v) = %v, want %v" , tc .expected , tc .actual , got , tc .want )
245+ }
246+ })
247+ }
248+ })
249+ }
0 commit comments