@@ -3,14 +3,14 @@ import { bufferFromHexArray } from './tools/utils';
33import { expect } from 'chai' ;
44import { BSON_DATA_LONG } from '../../src/constants' ;
55
6- describe ( 'BSON BigInt support' , function ( ) {
7- beforeEach ( function ( ) {
6+ describe ( 'BSON BigInt support' , function ( ) {
7+ beforeEach ( function ( ) {
88 if ( __noBigInt__ ) {
99 this . currentTest ?. skip ( ) ;
1010 }
1111 } ) ;
1212
13- describe ( 'BSON.deserialize()' , function ( ) {
13+ describe ( 'BSON.deserialize()' , function ( ) {
1414 type DeserialzationOptions = {
1515 useBigInt64 : boolean | undefined ;
1616 promoteValues : boolean | undefined ;
@@ -65,12 +65,15 @@ describe('BSON BigInt support', function() {
6565
6666 function generateTestDescription ( entry : TestTableEntry ) : string {
6767 const options = entry . options ;
68- const promoteValues = `promoteValues ${ options . promoteValues === undefined ? 'is default' : `is ${ options . promoteValues } `
69- } `;
70- const promoteLongs = `promoteLongs ${ options . promoteLongs === undefined ? 'is default' : `is ${ options . promoteLongs } `
71- } `;
72- const useBigInt64 = `useBigInt64 ${ options . useBigInt64 === undefined ? 'is default' : `is ${ options . useBigInt64 } `
73- } `;
68+ const promoteValues = `promoteValues ${
69+ options . promoteValues === undefined ? 'is default' : `is ${ options . promoteValues } `
70+ } `;
71+ const promoteLongs = `promoteLongs ${
72+ options . promoteLongs === undefined ? 'is default' : `is ${ options . promoteLongs } `
73+ } `;
74+ const useBigInt64 = `useBigInt64 ${
75+ options . useBigInt64 === undefined ? 'is default' : `is ${ options . useBigInt64 } `
76+ } `;
7477 const flagString = `${ useBigInt64 } , ${ promoteValues } , and ${ promoteLongs } ` ;
7578 if ( entry . shouldThrow ) {
7679 return `throws when ${ flagString } ` ;
@@ -103,33 +106,48 @@ describe('BSON BigInt support', function() {
103106 it ( description , test ) ;
104107 }
105108
106- it ( 'correctly deserializes min 64 bit int (-2n**63n)' , function ( ) {
107- expect ( BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
108- - ( 2n ** 63n ) ) ;
109+ it ( 'correctly deserializes min 64 bit int (-2n**63n)' , function ( ) {
110+ expect (
111+ BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , {
112+ useBigInt64 : true
113+ } )
114+ ) . to . deep . equal ( - ( 2n ** 63n ) ) ;
109115 } ) ;
110116
111- it ( 'correctly deserializes -1n' , function ( ) {
112- expect ( BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFFFF00' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
113- - 1n ) ;
117+ it ( 'correctly deserializes -1n' , function ( ) {
118+ expect (
119+ BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFFFF00' , 'hex' ) , {
120+ useBigInt64 : true
121+ } )
122+ ) . to . deep . equal ( - 1n ) ;
114123 } ) ;
115124
116- it ( 'correctly deserializes 0n' , function ( ) {
117- expect ( BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
118- 0n ) ;
125+ it ( 'correctly deserializes 0n' , function ( ) {
126+ expect (
127+ BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , {
128+ useBigInt64 : true
129+ } )
130+ ) . to . deep . equal ( 0n ) ;
119131 } ) ;
120132
121- it ( 'correctly deserializes 1n' , function ( ) {
122- expect ( BSON . deserialize ( Buffer . from ( '10000000126100010000000000000000' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
123- 1n ) ;
133+ it ( 'correctly deserializes 1n' , function ( ) {
134+ expect (
135+ BSON . deserialize ( Buffer . from ( '10000000126100010000000000000000' , 'hex' ) , {
136+ useBigInt64 : true
137+ } )
138+ ) . to . deep . equal ( 1n ) ;
124139 } ) ;
125140
126- it ( 'correctly deserializes max 64 bit int (2n**63n -1n)' , function ( ) {
127- expect ( BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFF7F00' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
128- ( 2n ** 63n - 1n ) ) ;
141+ it ( 'correctly deserializes max 64 bit int (2n**63n -1n)' , function ( ) {
142+ expect (
143+ BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFF7F00' , 'hex' ) , {
144+ useBigInt64 : true
145+ } )
146+ ) . to . deep . equal ( 2n ** 63n - 1n ) ;
129147 } ) ;
130148 } ) ;
131149
132- describe ( 'BSON.serialize()' , function ( ) {
150+ describe ( 'BSON.serialize()' , function ( ) {
133151 // Index for the data type byte of a BSON document with a
134152 // NOTE: These offsets only apply for documents with the shape {a : <n>}
135153 // where n is a BigInt
@@ -169,13 +187,13 @@ describe('BSON BigInt support', function() {
169187 } ;
170188 }
171189
172- it ( 'serializes bigints with the correct BSON type' , function ( ) {
190+ it ( 'serializes bigints with the correct BSON type' , function ( ) {
173191 const testDoc = { a : 0n } ;
174192 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
175193 expect ( serializedDoc . dataType ) . to . equal ( BSON_DATA_LONG ) ;
176194 } ) ;
177195
178- it ( 'serializes bigints into little-endian byte order' , function ( ) {
196+ it ( 'serializes bigints into little-endian byte order' , function ( ) {
179197 const testDoc = { a : 0x1234567812345678n } ;
180198 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
181199 const expectedResult = getSerializedDocParts (
@@ -189,7 +207,7 @@ describe('BSON BigInt support', function() {
189207 expect ( expectedResult . value ) . to . equal ( serializedDoc . value ) ;
190208 } ) ;
191209
192- it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
210+ it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
193211 const testDoc = { a : 0x23n } ;
194212 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
195213 const expectedResult = getSerializedDocParts (
@@ -202,7 +220,7 @@ describe('BSON BigInt support', function() {
202220 expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
203221 } ) ;
204222
205- it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
223+ it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
206224 const testDoc = { a : 0xfffffffffffffff1n } ;
207225 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
208226 const expectedResult = getSerializedDocParts (
@@ -215,7 +233,7 @@ describe('BSON BigInt support', function() {
215233 expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
216234 } ) ;
217235
218- it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
236+ it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
219237 const maxIntPlusOne = { a : 2n ** 63n } ;
220238 const serializedMaxIntPlusOne = getSerializedDocParts ( BSON . serialize ( maxIntPlusOne ) ) ;
221239 const expectedResultForMaxIntPlusOne = getSerializedDocParts (
@@ -228,7 +246,7 @@ describe('BSON BigInt support', function() {
228246 expect ( serializedMaxIntPlusOne ) . to . deep . equal ( expectedResultForMaxIntPlusOne ) ;
229247 } ) ;
230248
231- it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
249+ it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
232250 const maxPositiveInt64 = { a : 2n ** 63n - 1n } ;
233251 const serializedMaxPositiveInt64 = getSerializedDocParts ( BSON . serialize ( maxPositiveInt64 ) ) ;
234252 const expectedSerializationForMaxPositiveInt64 = getSerializedDocParts (
@@ -252,7 +270,7 @@ describe('BSON BigInt support', function() {
252270 expect ( serializedMinPositiveInt64 ) . to . deep . equal ( expectedSerializationForMinPositiveInt64 ) ;
253271 } ) ;
254272
255- it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
273+ it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
256274 const testDoc = { a : 2n ** 64n + 1n } ;
257275 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
258276 const expectedSerialization = getSerializedDocParts (
@@ -265,7 +283,7 @@ describe('BSON BigInt support', function() {
265283 expect ( serializedDoc ) . to . deep . equal ( expectedSerialization ) ;
266284 } ) ;
267285
268- it ( 'serializes array of BigInts' , function ( ) {
286+ it ( 'serializes array of BigInts' , function ( ) {
269287 const testArr = { a : [ 1n ] } ;
270288 const serializedArr = BSON . serialize ( testArr ) ;
271289 const expectedSerialization = bufferFromHexArray ( [
@@ -280,7 +298,7 @@ describe('BSON BigInt support', function() {
280298 expect ( serializedArr ) . to . deep . equal ( expectedSerialization ) ;
281299 } ) ;
282300
283- it ( 'serializes Map with BigInt values' , function ( ) {
301+ it ( 'serializes Map with BigInt values' , function ( ) {
284302 const testMap = new Map ( ) ;
285303 testMap . set ( 'a' , 1n ) ;
286304 const serializedMap = getSerializedDocParts ( BSON . serialize ( testMap ) ) ;
@@ -295,7 +313,7 @@ describe('BSON BigInt support', function() {
295313 } ) ;
296314 } ) ;
297315
298- describe ( 'EJSON.parse()' , function ( ) {
316+ describe ( 'EJSON.parse()' , function ( ) {
299317 type ParseOptions = {
300318 useBigInt64 : boolean | undefined ;
301319 relaxed : boolean | undefined ;
@@ -352,13 +370,13 @@ describe('BSON BigInt support', function() {
352370 const condDescription = generateConditionDescription ( entry ) ;
353371 const behaviourDescription = generateBehaviourDescription ( entry , sampleString ) ;
354372
355- describe ( condDescription , function ( ) {
373+ describe ( condDescription , function ( ) {
356374 it ( behaviourDescription , test ) ;
357375 } ) ;
358376 }
359377 }
360378
361- describe ( 'canonical input' , function ( ) {
379+ describe ( 'canonical input' , function ( ) {
362380 const canonicalInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
363381 return relaxedValues . flatMap ( relaxed => {
364382 return genTestTable (
@@ -381,7 +399,7 @@ describe('BSON BigInt support', function() {
381399 createTestsFromTestTable ( canonicalInputTestTable , sampleCanonicalString ) ;
382400 } ) ;
383401
384- describe ( 'relaxed integer input' , function ( ) {
402+ describe ( 'relaxed integer input' , function ( ) {
385403 const relaxedIntegerInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
386404 return relaxedValues . flatMap ( relaxed => {
387405 return genTestTable (
@@ -403,7 +421,7 @@ describe('BSON BigInt support', function() {
403421 createTestsFromTestTable ( relaxedIntegerInputTestTable , sampleRelaxedIntegerString ) ;
404422 } ) ;
405423
406- describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
424+ describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
407425 const relaxedDoubleInputTestTable = relaxedValues . flatMap ( relaxed => {
408426 return genTestTable ( true , relaxed , ( _ , relaxedIsSet : boolean ) =>
409427 relaxedIsSet ? { a : 2147483647.9 } : { a : new BSON . Double ( 2147483647.9 ) }
@@ -418,15 +436,15 @@ describe('BSON BigInt support', function() {
418436 } ) ;
419437 } ) ;
420438
421- describe ( 'EJSON.stringify()' , function ( ) {
422- context ( 'canonical mode (relaxed=false)' , function ( ) {
423- it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]' , function ( ) {
439+ describe ( 'EJSON.stringify()' , function ( ) {
440+ context ( 'canonical mode (relaxed=false)' , function ( ) {
441+ it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]' , function ( ) {
424442 const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
425443 const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
426444 expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
427445 } ) ;
428446
429- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
447+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
430448 const number = { a : 0x1234_5678_1234_5678_9999n } ;
431449 const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
432450 const serialized = BSON . serialize ( number ) ;
@@ -446,15 +464,15 @@ describe('BSON BigInt support', function() {
446464
447465 expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
448466 } ) ;
449- it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
467+ it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
450468 const number = { a : 2n } ;
451469 const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
452470 expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
453471 } ) ;
454472 } ) ;
455473
456- context ( 'relaxed mode (relaxed=true)' , function ( ) {
457- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
474+ context ( 'relaxed mode (relaxed=true)' , function ( ) {
475+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
458476 const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
459477 const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
460478 const serializedDoc = BSON . serialize ( number ) ;
@@ -473,23 +491,23 @@ describe('BSON BigInt support', function() {
473491 expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
474492 } ) ;
475493
476- it ( 'serializes bigint values to Number' , function ( ) {
494+ it ( 'serializes bigint values to Number' , function ( ) {
477495 const number = { a : 10000n } ;
478496 const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
479497 expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
480498 } ) ;
481499
482- it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
500+ it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
483501 const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
484502 const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
485503 expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
486504 } ) ;
487505 } ) ;
488506
489- context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
507+ context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
490508 let parsed ;
491509
492- before ( function ( ) {
510+ before ( function ( ) {
493511 if ( __noBigInt__ ) {
494512 return ;
495513 }
@@ -498,20 +516,20 @@ describe('BSON BigInt support', function() {
498516 parsed = JSON . parse ( serialized ) ;
499517 } ) ;
500518
501- it ( 'passes loose equality checks with native bigint values' , function ( ) {
519+ it ( 'passes loose equality checks with native bigint values' , function ( ) {
502520 // eslint-disable-next-line eqeqeq
503521 expect ( parsed . a . $numberLong == 12345n ) . true ;
504522 } ) ;
505523
506- it ( 'equals the result of BigInt.toString' , function ( ) {
524+ it ( 'equals the result of BigInt.toString' , function ( ) {
507525 expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
508526 } ) ;
509527 } ) ;
510528
511- context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
529+ context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
512530 let parsed ;
513531
514- before ( function ( ) {
532+ before ( function ( ) {
515533 if ( __noBigInt__ ) {
516534 return ;
517535 }
@@ -520,12 +538,12 @@ describe('BSON BigInt support', function() {
520538 parsed = JSON . parse ( serialized ) ;
521539 } ) ;
522540
523- it ( 'fails loose equality checks with native bigint values' , function ( ) {
541+ it ( 'fails loose equality checks with native bigint values' , function ( ) {
524542 // eslint-disable-next-line eqeqeq
525543 expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
526544 } ) ;
527545
528- it ( 'not equal to results of BigInt.toString' , function ( ) {
546+ it ( 'not equal to results of BigInt.toString' , function ( ) {
529547 expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
530548 } ) ;
531549 } ) ;
0 commit comments