@@ -3,18 +3,18 @@ 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 roundtripping' , function ( ) {
14- const numbers = [ - ( 2n ** 63n ) , - 1n , 0n , 1n , ( 2n ** 63n ) - 1n ] ;
13+ describe ( 'BSON roundtripping' , function ( ) {
14+ const numbers = [ - ( 2n ** 63n ) , - 1n , 0n , 1n , 2n ** 63n - 1n ] ;
1515
1616 for ( const number of numbers ) {
17- it ( `correctly roundtrips ${ number } ` , function ( ) {
17+ it ( `correctly roundtrips ${ number } ` , function ( ) {
1818 const inputDoc = { number } ;
1919 const serializedDoc = BSON . serialize ( inputDoc ) ;
2020 const outputDoc = BSON . deserialize ( serializedDoc , { useBigInt64 : true } ) ;
@@ -24,7 +24,7 @@ describe('BSON BigInt support', function() {
2424 }
2525 } ) ;
2626
27- describe ( 'BSON.deserialize()' , function ( ) {
27+ describe ( 'BSON.deserialize()' , function ( ) {
2828 type DeserialzationOptions = {
2929 useBigInt64 : boolean | undefined ;
3030 promoteValues : boolean | undefined ;
@@ -79,12 +79,15 @@ describe('BSON BigInt support', function() {
7979
8080 function generateTestDescription ( entry : TestTableEntry ) : string {
8181 const options = entry . options ;
82- const promoteValues = `promoteValues ${ options . promoteValues === undefined ? 'is default' : `is ${ options . promoteValues } `
83- } `;
84- const promoteLongs = `promoteLongs ${ options . promoteLongs === undefined ? 'is default' : `is ${ options . promoteLongs } `
85- } `;
86- const useBigInt64 = `useBigInt64 ${ options . useBigInt64 === undefined ? 'is default' : `is ${ options . useBigInt64 } `
87- } `;
82+ const promoteValues = `promoteValues ${
83+ options . promoteValues === undefined ? 'is default' : `is ${ options . promoteValues } `
84+ } `;
85+ const promoteLongs = `promoteLongs ${
86+ options . promoteLongs === undefined ? 'is default' : `is ${ options . promoteLongs } `
87+ } `;
88+ const useBigInt64 = `useBigInt64 ${
89+ options . useBigInt64 === undefined ? 'is default' : `is ${ options . useBigInt64 } `
90+ } `;
8891 const flagString = `${ useBigInt64 } , ${ promoteValues } , and ${ promoteLongs } ` ;
8992 if ( entry . shouldThrow ) {
9093 return `throws when ${ flagString } ` ;
@@ -118,7 +121,7 @@ describe('BSON BigInt support', function() {
118121 }
119122 } ) ;
120123
121- describe ( 'BSON.serialize()' , function ( ) {
124+ describe ( 'BSON.serialize()' , function ( ) {
122125 // Index for the data type byte of a BSON document with a
123126 // NOTE: These offsets only apply for documents with the shape {a : <n>}
124127 // where n is a BigInt
@@ -158,13 +161,13 @@ describe('BSON BigInt support', function() {
158161 } ;
159162 }
160163
161- it ( 'serializes bigints with the correct BSON type' , function ( ) {
164+ it ( 'serializes bigints with the correct BSON type' , function ( ) {
162165 const testDoc = { a : 0n } ;
163166 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
164167 expect ( serializedDoc . dataType ) . to . equal ( BSON_DATA_LONG ) ;
165168 } ) ;
166169
167- it ( 'serializes bigints into little-endian byte order' , function ( ) {
170+ it ( 'serializes bigints into little-endian byte order' , function ( ) {
168171 const testDoc = { a : 0x1234567812345678n } ;
169172 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
170173 const expectedResult = getSerializedDocParts (
@@ -178,7 +181,7 @@ describe('BSON BigInt support', function() {
178181 expect ( expectedResult . value ) . to . equal ( serializedDoc . value ) ;
179182 } ) ;
180183
181- it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
184+ it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
182185 const testDoc = { a : 0x23n } ;
183186 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
184187 const expectedResult = getSerializedDocParts (
@@ -191,7 +194,7 @@ describe('BSON BigInt support', function() {
191194 expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
192195 } ) ;
193196
194- it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
197+ it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
195198 const testDoc = { a : 0xfffffffffffffff1n } ;
196199 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
197200 const expectedResult = getSerializedDocParts (
@@ -204,7 +207,7 @@ describe('BSON BigInt support', function() {
204207 expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
205208 } ) ;
206209
207- it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
210+ it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
208211 const maxIntPlusOne = { a : 2n ** 63n } ;
209212 const serializedMaxIntPlusOne = getSerializedDocParts ( BSON . serialize ( maxIntPlusOne ) ) ;
210213 const expectedResultForMaxIntPlusOne = getSerializedDocParts (
@@ -217,7 +220,7 @@ describe('BSON BigInt support', function() {
217220 expect ( serializedMaxIntPlusOne ) . to . deep . equal ( expectedResultForMaxIntPlusOne ) ;
218221 } ) ;
219222
220- it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
223+ it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
221224 const maxPositiveInt64 = { a : 2n ** 63n - 1n } ;
222225 const serializedMaxPositiveInt64 = getSerializedDocParts ( BSON . serialize ( maxPositiveInt64 ) ) ;
223226 const expectedSerializationForMaxPositiveInt64 = getSerializedDocParts (
@@ -241,7 +244,7 @@ describe('BSON BigInt support', function() {
241244 expect ( serializedMinPositiveInt64 ) . to . deep . equal ( expectedSerializationForMinPositiveInt64 ) ;
242245 } ) ;
243246
244- it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
247+ it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
245248 const testDoc = { a : 2n ** 64n + 1n } ;
246249 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
247250 const expectedSerialization = getSerializedDocParts (
@@ -254,7 +257,7 @@ describe('BSON BigInt support', function() {
254257 expect ( serializedDoc ) . to . deep . equal ( expectedSerialization ) ;
255258 } ) ;
256259
257- it ( 'serializes array of BigInts' , function ( ) {
260+ it ( 'serializes array of BigInts' , function ( ) {
258261 const testArr = { a : [ 1n ] } ;
259262 const serializedArr = BSON . serialize ( testArr ) ;
260263 const expectedSerialization = bufferFromHexArray ( [
@@ -269,7 +272,7 @@ describe('BSON BigInt support', function() {
269272 expect ( serializedArr ) . to . deep . equal ( expectedSerialization ) ;
270273 } ) ;
271274
272- it ( 'serializes Map with BigInt values' , function ( ) {
275+ it ( 'serializes Map with BigInt values' , function ( ) {
273276 const testMap = new Map ( ) ;
274277 testMap . set ( 'a' , 1n ) ;
275278 const serializedMap = getSerializedDocParts ( BSON . serialize ( testMap ) ) ;
@@ -284,7 +287,7 @@ describe('BSON BigInt support', function() {
284287 } ) ;
285288 } ) ;
286289
287- describe ( 'EJSON.parse()' , function ( ) {
290+ describe ( 'EJSON.parse()' , function ( ) {
288291 type ParseOptions = {
289292 useBigInt64 : boolean | undefined ;
290293 relaxed : boolean | undefined ;
@@ -341,13 +344,13 @@ describe('BSON BigInt support', function() {
341344 const condDescription = generateConditionDescription ( entry ) ;
342345 const behaviourDescription = generateBehaviourDescription ( entry , sampleString ) ;
343346
344- describe ( condDescription , function ( ) {
347+ describe ( condDescription , function ( ) {
345348 it ( behaviourDescription , test ) ;
346349 } ) ;
347350 }
348351 }
349352
350- describe ( 'canonical input' , function ( ) {
353+ describe ( 'canonical input' , function ( ) {
351354 const canonicalInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
352355 return relaxedValues . flatMap ( relaxed => {
353356 return genTestTable (
@@ -370,7 +373,7 @@ describe('BSON BigInt support', function() {
370373 createTestsFromTestTable ( canonicalInputTestTable , sampleCanonicalString ) ;
371374 } ) ;
372375
373- describe ( 'relaxed integer input' , function ( ) {
376+ describe ( 'relaxed integer input' , function ( ) {
374377 const relaxedIntegerInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
375378 return relaxedValues . flatMap ( relaxed => {
376379 return genTestTable (
@@ -392,7 +395,7 @@ describe('BSON BigInt support', function() {
392395 createTestsFromTestTable ( relaxedIntegerInputTestTable , sampleRelaxedIntegerString ) ;
393396 } ) ;
394397
395- describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
398+ describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
396399 const relaxedDoubleInputTestTable = relaxedValues . flatMap ( relaxed => {
397400 return genTestTable ( true , relaxed , ( _ , relaxedIsSet : boolean ) =>
398401 relaxedIsSet ? { a : 2147483647.9 } : { a : new BSON . Double ( 2147483647.9 ) }
@@ -407,15 +410,15 @@ describe('BSON BigInt support', function() {
407410 } ) ;
408411 } ) ;
409412
410- describe ( 'EJSON.stringify()' , function ( ) {
411- context ( 'canonical mode (relaxed=false)' , function ( ) {
412- it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]' , function ( ) {
413+ describe ( 'EJSON.stringify()' , function ( ) {
414+ context ( 'canonical mode (relaxed=false)' , function ( ) {
415+ it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]' , function ( ) {
413416 const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
414417 const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
415418 expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
416419 } ) ;
417420
418- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
421+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
419422 const number = { a : 0x1234_5678_1234_5678_9999n } ;
420423 const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
421424 const serialized = BSON . serialize ( number ) ;
@@ -435,15 +438,15 @@ describe('BSON BigInt support', function() {
435438
436439 expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
437440 } ) ;
438- it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
441+ it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
439442 const number = { a : 2n } ;
440443 const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
441444 expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
442445 } ) ;
443446 } ) ;
444447
445- context ( 'relaxed mode (relaxed=true)' , function ( ) {
446- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
448+ context ( 'relaxed mode (relaxed=true)' , function ( ) {
449+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
447450 const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
448451 const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
449452 const serializedDoc = BSON . serialize ( number ) ;
@@ -462,23 +465,23 @@ describe('BSON BigInt support', function() {
462465 expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
463466 } ) ;
464467
465- it ( 'serializes bigint values to Number' , function ( ) {
468+ it ( 'serializes bigint values to Number' , function ( ) {
466469 const number = { a : 10000n } ;
467470 const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
468471 expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
469472 } ) ;
470473
471- it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
474+ it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
472475 const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
473476 const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
474477 expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
475478 } ) ;
476479 } ) ;
477480
478- context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
481+ context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
479482 let parsed ;
480483
481- before ( function ( ) {
484+ before ( function ( ) {
482485 if ( __noBigInt__ ) {
483486 return ;
484487 }
@@ -487,20 +490,20 @@ describe('BSON BigInt support', function() {
487490 parsed = JSON . parse ( serialized ) ;
488491 } ) ;
489492
490- it ( 'passes loose equality checks with native bigint values' , function ( ) {
493+ it ( 'passes loose equality checks with native bigint values' , function ( ) {
491494 // eslint-disable-next-line eqeqeq
492495 expect ( parsed . a . $numberLong == 12345n ) . true ;
493496 } ) ;
494497
495- it ( 'equals the result of BigInt.toString' , function ( ) {
498+ it ( 'equals the result of BigInt.toString' , function ( ) {
496499 expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
497500 } ) ;
498501 } ) ;
499502
500- context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
503+ context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
501504 let parsed ;
502505
503- before ( function ( ) {
506+ before ( function ( ) {
504507 if ( __noBigInt__ ) {
505508 return ;
506509 }
@@ -509,12 +512,12 @@ describe('BSON BigInt support', function() {
509512 parsed = JSON . parse ( serialized ) ;
510513 } ) ;
511514
512- it ( 'fails loose equality checks with native bigint values' , function ( ) {
515+ it ( 'fails loose equality checks with native bigint values' , function ( ) {
513516 // eslint-disable-next-line eqeqeq
514517 expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
515518 } ) ;
516519
517- it ( 'not equal to results of BigInt.toString' , function ( ) {
520+ it ( 'not equal to results of BigInt.toString' , function ( ) {
518521 expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
519522 } ) ;
520523 } ) ;
0 commit comments