@@ -265,90 +265,101 @@ describe('BSON BigInt support', function () {
265265 } ) ;
266266
267267 describe ( 'EJSON.stringify()' , function ( ) {
268- it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX] in canonical mode' , function ( ) {
269- const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
270- const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
271- expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
272- } ) ;
268+ context ( 'canonical mode (relaxed=false)' , function ( ) {
269+ it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]' , function ( ) {
270+ const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
271+ const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
272+ expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
273+ } ) ;
273274
274- it ( 'truncates bigint values in the same way as BSON.serialize in canonical mode ' , function ( ) {
275- const number = { a : 0x1234_5678_1234_5678_9999n } ;
276- const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
277- const serialized = BSON . serialize ( number ) ;
275+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
276+ const number = { a : 0x1234_5678_1234_5678_9999n } ;
277+ const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
278+ const serialized = BSON . serialize ( number ) ;
278279
279- const VALUE_OFFSET = 7 ;
280- const dataView = BSONDataView . fromUint8Array ( serialized ) ;
281- const serializedValue = dataView . getBigInt64 ( VALUE_OFFSET , true ) ;
282- const parsed = JSON . parse ( stringified ) ;
280+ const VALUE_OFFSET = 7 ;
281+ const dataView = BSONDataView . fromUint8Array ( serialized ) ;
282+ const serializedValue = dataView . getBigInt64 ( VALUE_OFFSET , true ) ;
283+ const parsed = JSON . parse ( stringified ) ;
283284
284- expect ( parsed ) . to . have . property ( 'a' ) ;
285- expect ( parsed [ 'a' ] ) . to . have . property ( '$numberLong' ) ;
286- expect ( parsed . a . $numberLong ) . to . equal ( 0x5678_1234_5678_9999n . toString ( ) ) ;
285+ expect ( parsed ) . to . have . property ( 'a' ) ;
286+ expect ( parsed [ 'a' ] ) . to . have . property ( '$numberLong' ) ;
287+ expect ( parsed . a . $numberLong ) . to . equal ( 0x5678_1234_5678_9999n . toString ( ) ) ;
287288
288- expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
289+ expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
290+ } ) ;
291+ it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
292+ const number = { a : 2n } ;
293+ const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
294+ expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
295+ } ) ;
289296 } ) ;
290297
291- it ( 'truncates bigint values in the same way as BSON.serialize in relaxed mode' , function ( ) {
292- const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
293- const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
294- const serializedDoc = BSON . serialize ( number ) ;
298+ context ( 'relaxed mode (relaxed=true)' , function ( ) {
299+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
300+ const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
301+ const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
302+ const serializedDoc = BSON . serialize ( number ) ;
295303
296- const VALUE_OFFSET = 7 ;
297- const dataView = BSONDataView . fromUint8Array ( serializedDoc ) ;
298- const parsed = JSON . parse ( stringified ) ;
304+ const VALUE_OFFSET = 7 ;
305+ const dataView = BSONDataView . fromUint8Array ( serializedDoc ) ;
306+ const parsed = JSON . parse ( stringified ) ;
299307
300- expect ( parsed ) . to . have . property ( 'a' ) ;
301- expect ( parsed . a ) . to . equal ( 0x0000_1234_5678_9999 ) ;
308+ expect ( parsed ) . to . have . property ( 'a' ) ;
309+ expect ( parsed . a ) . to . equal ( 0x0000_1234_5678_9999 ) ;
302310
303- expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
304- } ) ;
311+ expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
312+ } ) ;
305313
306- it ( 'serializes bigint values to numberLong in canonical mode ' , function ( ) {
307- const number = { a : 2n } ;
308- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
309- expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"} }' ) ;
310- } ) ;
314+ it ( 'serializes bigint values to Number ' , function ( ) {
315+ const number = { a : 10000n } ;
316+ const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
317+ expect ( serialized ) . to . equal ( '{"a":10000 }' ) ;
318+ } ) ;
311319
312- it ( 'serializes bigint values to Number in relaxed mode' , function ( ) {
313- const number = { a : 10000n } ;
314- const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
315- expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
320+ it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
321+ const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
322+ const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
323+ expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
324+ } ) ;
316325 } ) ;
317326
318- it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] in relaxed mode' , function ( ) {
319- const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
320- const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
321- expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
322- } ) ;
327+ context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
328+ let parsed ;
323329
324- it ( 'produces bigint strings that pass loose equality checks with native bigint values that are are 64 bits wide or less' , function ( ) {
325- const number = { a : 12345n } ;
326- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
327- const parsed = JSON . parse ( serialized ) ;
328- // eslint-disable-next-line eqeqeq
329- expect ( parsed . a . $numberLong == 12345n ) . true ;
330- } ) ;
330+ before ( function ( ) {
331+ const number = { a : 12345n } ;
332+ const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
333+ parsed = JSON . parse ( serialized ) ;
334+ } ) ;
331335
332- it ( 'produces bigint strings that are equal to the strings generated when using BigInt.toString when the bigint values used are 64 bits wide or less' , function ( ) {
333- const number = { a : 12345n } ;
334- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
335- const parsed = JSON . parse ( serialized ) ;
336- expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
337- } ) ;
336+ it ( 'passes loose equality checks with native bigint values' , function ( ) {
337+ // eslint-disable-next-line eqeqeq
338+ expect ( parsed . a . $numberLong == 12345n ) . true ;
339+ } ) ;
338340
339- it ( 'produces bigint strings that fail loose equality checks with native bigint values that are more than 64 bits wide' , function ( ) {
340- const number = { a : 0x1234_5678_1234_5678_9999n } ;
341- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
342- const parsed = JSON . parse ( serialized ) ;
343- // eslint-disable-next-line eqeqeq
344- expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
341+ it ( 'equals the result of BigInt.toString' , function ( ) {
342+ expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
343+ } ) ;
345344 } ) ;
346345
347- it ( 'produces bigint strings that are not equal to the strings generated when using BigInt.toString when the bigint values used are more than 64 bits wide' , function ( ) {
348- const number = { a : 0x1234_5678_1234_5678_9999n } ;
349- const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
350- const parsed = JSON . parse ( serialized ) ;
351- expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
346+ context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
347+ let parsed ;
348+
349+ before ( function ( ) {
350+ const number = { a : 0x1234_5678_1234_5678_9999n } ;
351+ const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
352+ parsed = JSON . parse ( serialized ) ;
353+ } ) ;
354+
355+ it ( 'fails loose equality checks with native bigint values' , function ( ) {
356+ // eslint-disable-next-line eqeqeq
357+ expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
358+ } ) ;
359+
360+ it ( 'not equal to results of BigInt.toString' , function ( ) {
361+ expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
362+ } ) ;
352363 } ) ;
353364 } ) ;
354365} ) ;
0 commit comments