@@ -237,6 +237,96 @@ final class DocumentTests: BSONTestCase {
237237 . to ( equal ( [ " hi " : true , " hello " : " hi " , " cat " : 2 ] as BSONDocument ) )
238238 }
239239
240+ func testEqualsIgnoreKeyOrder( ) throws {
241+ // basic comparisons
242+ let doc1 : BSONDocument = [ " foo " : " bar " , " bread " : 1 ]
243+ let doc2 : BSONDocument = [ " foo " : " bar " , " bread " : 1 ]
244+ expect ( doc1. equalsIgnoreKeyOrder ( doc2) ) . to ( equal ( true ) )
245+
246+ let doc3 : BSONDocument = [ " foo " : " bar " , " bread " : 1 ]
247+ let doc4 : BSONDocument = [ " foo " : " foo " , " bread " : 2 ]
248+ expect ( doc3. equalsIgnoreKeyOrder ( doc4) ) . to ( equal ( false ) )
249+
250+ // more complex comparisons
251+ let a : BSONDocument = [
252+ " string " : " test string " ,
253+ " true " : true ,
254+ " false " : false ,
255+ " int " : 25 ,
256+ " int32 " : . int32( 5 ) ,
257+ " int64 " : . int64( 10 ) ,
258+ " double " : . double( 15 ) ,
259+ " regex " : . regex( BSONRegularExpression ( pattern: " ^abc " , options: " imx " ) ) ,
260+ " decimal128 " : . decimal128( try ! BSONDecimal128 ( " 1.2E+10 " ) ) ,
261+ " minkey " : . minKey,
262+ " maxkey " : . maxKey,
263+ " date " : . datetime( Date ( timeIntervalSince1970: 500.004 ) ) ,
264+ " timestamp " : . timestamp( BSONTimestamp ( timestamp: 5 , inc: 10 ) ) ,
265+ " nesteddoc " : [ " a " : 1 , " b " : 2 , " c " : false , " d " : [ 3 , 4 ] ] ,
266+ " oid " : . objectID( try ! BSONObjectID ( " 507f1f77bcf86cd799439011 " ) ) ,
267+ " array1 " : [ 1 , 2 ] ,
268+ " array2 " : [ " string1 " , " string2 " ] ,
269+ " null " : . null,
270+ " code " : . code( BSONCode ( code: " console.log('hi'); " ) ) ,
271+ " nestedarray " : [ [ 1 , 2 ] , [ . int32( 3 ) , . int32( 4 ) ] ] ,
272+ " codewscope " : . codeWithScope( BSONCodeWithScope ( code: " console.log(x); " , scope: [ " x " : 2 ] ) )
273+ ]
274+
275+ let b : BSONDocument = [
276+ " true " : true ,
277+ " int " : 25 ,
278+ " int32 " : . int32( 5 ) ,
279+ " int64 " : . int64( 10 ) ,
280+ " string " : " test string " ,
281+ " double " : . double( 15 ) ,
282+ " decimal128 " : . decimal128( try ! BSONDecimal128 ( " 1.2E+10 " ) ) ,
283+ " minkey " : . minKey,
284+ " date " : . datetime( Date ( timeIntervalSince1970: 500.004 ) ) ,
285+ " timestamp " : . timestamp( BSONTimestamp ( timestamp: 5 , inc: 10 ) ) ,
286+ " nestedarray " : [ [ 1 , 2 ] , [ . int32( 3 ) , . int32( 4 ) ] ] ,
287+ " codewscope " : . codeWithScope( BSONCodeWithScope ( code: " console.log(x); " , scope: [ " x " : 2 ] ) ) ,
288+ " nesteddoc " : [ " b " : 2 , " a " : 1 , " d " : [ 3 , 4 ] , " c " : false ] ,
289+ " oid " : . objectID( try ! BSONObjectID ( " 507f1f77bcf86cd799439011 " ) ) ,
290+ " false " : false ,
291+ " regex " : . regex( BSONRegularExpression ( pattern: " ^abc " , options: " imx " ) ) ,
292+ " array1 " : [ 1 , 2 ] ,
293+ " array2 " : [ " string1 " , " string2 " ] ,
294+ " null " : . null,
295+ " code " : . code( BSONCode ( code: " console.log('hi'); " ) ) ,
296+ " maxkey " : . maxKey
297+ ]
298+
299+ // comparing two documents with the same key-value pairs in different order should return true
300+ expect ( a. equalsIgnoreKeyOrder ( b) ) . to ( equal ( true ) )
301+
302+ let c : BSONDocument = [
303+ " true " : true ,
304+ " int " : 52 ,
305+ " int32 " : . int32( 15 ) ,
306+ " int64 " : . int64( 100 ) ,
307+ " string " : " this is different string " ,
308+ " double " : . double( 15 ) ,
309+ " decimal128 " : . decimal128( try ! BSONDecimal128 ( " 1.2E+10 " ) ) ,
310+ " minkey " : . minKey,
311+ " date " : . datetime( Date ( timeIntervalSince1970: 500.004 ) ) ,
312+ " array1 " : [ 1 , 2 ] ,
313+ " timestamp " : . timestamp( BSONTimestamp ( timestamp: 5 , inc: 10 ) ) ,
314+ " nestedarray " : [ [ 1 , 2 ] , [ . int32( 3 ) , . int32( 4 ) ] ] ,
315+ " codewscope " : . codeWithScope( BSONCodeWithScope ( code: " console.log(x); " , scope: [ " x " : 2 ] ) ) ,
316+ " nesteddoc " : [ " 1 " : 1 , " 2 " : 2 , " 3 " : true , " 4 " : [ 5 , 6 ] ] ,
317+ " oid " : . objectID( try ! BSONObjectID ( " 507f1f77bcf86cd799439011 " ) ) ,
318+ " false " : false ,
319+ " regex " : . regex( BSONRegularExpression ( pattern: " ^abc " , options: " imx " ) ) ,
320+ " array2 " : [ " string3 " , " string2 " , " string1 " ] ,
321+ " null " : . null,
322+ " code " : . code( BSONCode ( code: " console.log('hi'); " ) ) ,
323+ " maxkey " : . maxKey
324+ ]
325+
326+ // comparing two documents with same keys but different values should return false
327+ expect ( a. equalsIgnoreKeyOrder ( c) ) . to ( equal ( false ) )
328+ }
329+
240330 func testRawBSON( ) throws {
241331 let doc = try BSONDocument ( fromJSON: " { \" a \" :[{ \" $numberInt \" : \" 10 \" }]} " )
242332 let fromRawBSON = try BSONDocument ( fromBSON: doc. buffer)
0 commit comments