@@ -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,15 +65,12 @@ describe('BSON BigInt support', function () {
6565
6666 function generateTestDescription ( entry : TestTableEntry ) : string {
6767 const options = entry . options ;
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- } `;
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+ } `;
7774 const flagString = `${ useBigInt64 } , ${ promoteValues } , and ${ promoteLongs } ` ;
7875 if ( entry . shouldThrow ) {
7976 return `throws when ${ flagString } ` ;
@@ -106,41 +103,33 @@ describe('BSON BigInt support', function () {
106103 it ( description , test ) ;
107104 }
108105
109- describe ( 'edge case tests' , function ( ) {
110- const tests = [
111- {
112- expectedResult : { a : - ( 2n ** 63n ) } ,
113- input : Buffer . from ( '10000000126100000000000000008000' , 'hex' )
114- } ,
115- {
116- expectedResult : { a : - 1n } ,
117- input : Buffer . from ( '10000000126100FFFFFFFFFFFFFFFF00' , 'hex' )
118- } ,
119- {
120- expectedResult : { a : 0n } ,
121- input : Buffer . from ( '10000000126100000000000000000000' , 'hex' )
122- } ,
123- {
124- expectedResult : { a : 1n } ,
125- input : Buffer . from ( '10000000126100010000000000000000' , 'hex' )
126- } ,
127- {
128- expectedResult : { a : 2n ** 63n - 1n } ,
129- input : Buffer . from ( '10000000126100FFFFFFFFFFFFFF7F00' , 'hex' )
130- }
131- ] ;
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+ } ) ;
132110
133- for ( const test of tests ) {
134- it ( `correctly deserializes the bson document encoded in ${ test . input . toString ( 'hex' ) } ` , function ( ) {
135- expect ( BSON . deserialize ( test . input , { useBigInt64 : true } ) ) . to . deep . equal (
136- test . expectedResult
137- ) ;
138- } ) ;
139- }
111+ it ( 'correctly deserializes -1n' , function ( ) {
112+ expect ( BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFFFF00' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
113+ - 1n ) ;
114+ } ) ;
115+
116+ it ( 'correctly deserializes 0n' , function ( ) {
117+ expect ( BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
118+ 0n ) ;
119+ } ) ;
120+
121+ it ( 'correctly deserializes 1n' , function ( ) {
122+ expect ( BSON . deserialize ( Buffer . from ( '10000000126100010000000000000000' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
123+ 1n ) ;
124+ } ) ;
125+
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 ) ) ;
140129 } ) ;
141130 } ) ;
142131
143- describe ( 'BSON.serialize()' , function ( ) {
132+ describe ( 'BSON.serialize()' , function ( ) {
144133 // Index for the data type byte of a BSON document with a
145134 // NOTE: These offsets only apply for documents with the shape {a : <n>}
146135 // where n is a BigInt
@@ -180,13 +169,13 @@ describe('BSON BigInt support', function () {
180169 } ;
181170 }
182171
183- it ( 'serializes bigints with the correct BSON type' , function ( ) {
172+ it ( 'serializes bigints with the correct BSON type' , function ( ) {
184173 const testDoc = { a : 0n } ;
185174 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
186175 expect ( serializedDoc . dataType ) . to . equal ( BSON_DATA_LONG ) ;
187176 } ) ;
188177
189- it ( 'serializes bigints into little-endian byte order' , function ( ) {
178+ it ( 'serializes bigints into little-endian byte order' , function ( ) {
190179 const testDoc = { a : 0x1234567812345678n } ;
191180 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
192181 const expectedResult = getSerializedDocParts (
@@ -200,7 +189,7 @@ describe('BSON BigInt support', function () {
200189 expect ( expectedResult . value ) . to . equal ( serializedDoc . value ) ;
201190 } ) ;
202191
203- it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
192+ it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
204193 const testDoc = { a : 0x23n } ;
205194 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
206195 const expectedResult = getSerializedDocParts (
@@ -213,7 +202,7 @@ describe('BSON BigInt support', function () {
213202 expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
214203 } ) ;
215204
216- it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
205+ it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
217206 const testDoc = { a : 0xfffffffffffffff1n } ;
218207 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
219208 const expectedResult = getSerializedDocParts (
@@ -226,7 +215,7 @@ describe('BSON BigInt support', function () {
226215 expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
227216 } ) ;
228217
229- it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
218+ it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
230219 const maxIntPlusOne = { a : 2n ** 63n } ;
231220 const serializedMaxIntPlusOne = getSerializedDocParts ( BSON . serialize ( maxIntPlusOne ) ) ;
232221 const expectedResultForMaxIntPlusOne = getSerializedDocParts (
@@ -239,7 +228,7 @@ describe('BSON BigInt support', function () {
239228 expect ( serializedMaxIntPlusOne ) . to . deep . equal ( expectedResultForMaxIntPlusOne ) ;
240229 } ) ;
241230
242- it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
231+ it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
243232 const maxPositiveInt64 = { a : 2n ** 63n - 1n } ;
244233 const serializedMaxPositiveInt64 = getSerializedDocParts ( BSON . serialize ( maxPositiveInt64 ) ) ;
245234 const expectedSerializationForMaxPositiveInt64 = getSerializedDocParts (
@@ -263,7 +252,7 @@ describe('BSON BigInt support', function () {
263252 expect ( serializedMinPositiveInt64 ) . to . deep . equal ( expectedSerializationForMinPositiveInt64 ) ;
264253 } ) ;
265254
266- it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
255+ it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
267256 const testDoc = { a : 2n ** 64n + 1n } ;
268257 const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
269258 const expectedSerialization = getSerializedDocParts (
@@ -276,7 +265,7 @@ describe('BSON BigInt support', function () {
276265 expect ( serializedDoc ) . to . deep . equal ( expectedSerialization ) ;
277266 } ) ;
278267
279- it ( 'serializes array of BigInts' , function ( ) {
268+ it ( 'serializes array of BigInts' , function ( ) {
280269 const testArr = { a : [ 1n ] } ;
281270 const serializedArr = BSON . serialize ( testArr ) ;
282271 const expectedSerialization = bufferFromHexArray ( [
@@ -291,7 +280,7 @@ describe('BSON BigInt support', function () {
291280 expect ( serializedArr ) . to . deep . equal ( expectedSerialization ) ;
292281 } ) ;
293282
294- it ( 'serializes Map with BigInt values' , function ( ) {
283+ it ( 'serializes Map with BigInt values' , function ( ) {
295284 const testMap = new Map ( ) ;
296285 testMap . set ( 'a' , 1n ) ;
297286 const serializedMap = getSerializedDocParts ( BSON . serialize ( testMap ) ) ;
@@ -306,7 +295,7 @@ describe('BSON BigInt support', function () {
306295 } ) ;
307296 } ) ;
308297
309- describe ( 'EJSON.parse()' , function ( ) {
298+ describe ( 'EJSON.parse()' , function ( ) {
310299 type ParseOptions = {
311300 useBigInt64 : boolean | undefined ;
312301 relaxed : boolean | undefined ;
@@ -363,13 +352,13 @@ describe('BSON BigInt support', function () {
363352 const condDescription = generateConditionDescription ( entry ) ;
364353 const behaviourDescription = generateBehaviourDescription ( entry , sampleString ) ;
365354
366- describe ( condDescription , function ( ) {
355+ describe ( condDescription , function ( ) {
367356 it ( behaviourDescription , test ) ;
368357 } ) ;
369358 }
370359 }
371360
372- describe ( 'canonical input' , function ( ) {
361+ describe ( 'canonical input' , function ( ) {
373362 const canonicalInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
374363 return relaxedValues . flatMap ( relaxed => {
375364 return genTestTable (
@@ -392,7 +381,7 @@ describe('BSON BigInt support', function () {
392381 createTestsFromTestTable ( canonicalInputTestTable , sampleCanonicalString ) ;
393382 } ) ;
394383
395- describe ( 'relaxed integer input' , function ( ) {
384+ describe ( 'relaxed integer input' , function ( ) {
396385 const relaxedIntegerInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
397386 return relaxedValues . flatMap ( relaxed => {
398387 return genTestTable (
@@ -414,7 +403,7 @@ describe('BSON BigInt support', function () {
414403 createTestsFromTestTable ( relaxedIntegerInputTestTable , sampleRelaxedIntegerString ) ;
415404 } ) ;
416405
417- describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
406+ describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
418407 const relaxedDoubleInputTestTable = relaxedValues . flatMap ( relaxed => {
419408 return genTestTable ( true , relaxed , ( _ , relaxedIsSet : boolean ) =>
420409 relaxedIsSet ? { a : 2147483647.9 } : { a : new BSON . Double ( 2147483647.9 ) }
@@ -429,15 +418,15 @@ describe('BSON BigInt support', function () {
429418 } ) ;
430419 } ) ;
431420
432- describe ( 'EJSON.stringify()' , function ( ) {
433- context ( 'canonical mode (relaxed=false)' , function ( ) {
434- it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]' , function ( ) {
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 ( ) {
435424 const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
436425 const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
437426 expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
438427 } ) ;
439428
440- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
429+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
441430 const number = { a : 0x1234_5678_1234_5678_9999n } ;
442431 const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
443432 const serialized = BSON . serialize ( number ) ;
@@ -457,15 +446,15 @@ describe('BSON BigInt support', function () {
457446
458447 expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
459448 } ) ;
460- it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
449+ it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
461450 const number = { a : 2n } ;
462451 const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
463452 expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
464453 } ) ;
465454 } ) ;
466455
467- context ( 'relaxed mode (relaxed=true)' , function ( ) {
468- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
456+ context ( 'relaxed mode (relaxed=true)' , function ( ) {
457+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
469458 const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
470459 const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
471460 const serializedDoc = BSON . serialize ( number ) ;
@@ -484,23 +473,23 @@ describe('BSON BigInt support', function () {
484473 expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
485474 } ) ;
486475
487- it ( 'serializes bigint values to Number' , function ( ) {
476+ it ( 'serializes bigint values to Number' , function ( ) {
488477 const number = { a : 10000n } ;
489478 const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
490479 expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
491480 } ) ;
492481
493- it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
482+ it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
494483 const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
495484 const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
496485 expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
497486 } ) ;
498487 } ) ;
499488
500- context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
489+ context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
501490 let parsed ;
502491
503- before ( function ( ) {
492+ before ( function ( ) {
504493 if ( __noBigInt__ ) {
505494 return ;
506495 }
@@ -509,20 +498,20 @@ describe('BSON BigInt support', function () {
509498 parsed = JSON . parse ( serialized ) ;
510499 } ) ;
511500
512- it ( 'passes loose equality checks with native bigint values' , function ( ) {
501+ it ( 'passes loose equality checks with native bigint values' , function ( ) {
513502 // eslint-disable-next-line eqeqeq
514503 expect ( parsed . a . $numberLong == 12345n ) . true ;
515504 } ) ;
516505
517- it ( 'equals the result of BigInt.toString' , function ( ) {
506+ it ( 'equals the result of BigInt.toString' , function ( ) {
518507 expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
519508 } ) ;
520509 } ) ;
521510
522- context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
511+ context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
523512 let parsed ;
524513
525- before ( function ( ) {
514+ before ( function ( ) {
526515 if ( __noBigInt__ ) {
527516 return ;
528517 }
@@ -531,12 +520,12 @@ describe('BSON BigInt support', function () {
531520 parsed = JSON . parse ( serialized ) ;
532521 } ) ;
533522
534- it ( 'fails loose equality checks with native bigint values' , function ( ) {
523+ it ( 'fails loose equality checks with native bigint values' , function ( ) {
535524 // eslint-disable-next-line eqeqeq
536525 expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
537526 } ) ;
538527
539- it ( 'not equal to results of BigInt.toString' , function ( ) {
528+ it ( 'not equal to results of BigInt.toString' , function ( ) {
540529 expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
541530 } ) ;
542531 } ) ;
0 commit comments