@@ -3,18 +3,18 @@ import { bufferFromHexArray } from './tools/utils';
3
3
import { expect } from 'chai' ;
4
4
import { BSON_DATA_LONG } from '../../src/constants' ;
5
5
6
- describe ( 'BSON BigInt support' , function ( ) {
7
- beforeEach ( function ( ) {
6
+ describe ( 'BSON BigInt support' , function ( ) {
7
+ beforeEach ( function ( ) {
8
8
if ( __noBigInt__ ) {
9
9
this . currentTest ?. skip ( ) ;
10
10
}
11
11
} ) ;
12
12
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 ] ;
15
15
16
16
for ( const number of numbers ) {
17
- it ( `correctly roundtrips ${ number } ` , function ( ) {
17
+ it ( `correctly roundtrips ${ number } ` , function ( ) {
18
18
const inputDoc = { number } ;
19
19
const serializedDoc = BSON . serialize ( inputDoc ) ;
20
20
const outputDoc = BSON . deserialize ( serializedDoc , { useBigInt64 : true } ) ;
@@ -24,7 +24,7 @@ describe('BSON BigInt support', function() {
24
24
}
25
25
} ) ;
26
26
27
- describe ( 'BSON.deserialize()' , function ( ) {
27
+ describe ( 'BSON.deserialize()' , function ( ) {
28
28
type DeserialzationOptions = {
29
29
useBigInt64 : boolean | undefined ;
30
30
promoteValues : boolean | undefined ;
@@ -79,12 +79,15 @@ describe('BSON BigInt support', function() {
79
79
80
80
function generateTestDescription ( entry : TestTableEntry ) : string {
81
81
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
+ } `;
88
91
const flagString = `${ useBigInt64 } , ${ promoteValues } , and ${ promoteLongs } ` ;
89
92
if ( entry . shouldThrow ) {
90
93
return `throws when ${ flagString } ` ;
@@ -118,7 +121,7 @@ describe('BSON BigInt support', function() {
118
121
}
119
122
} ) ;
120
123
121
- describe ( 'BSON.serialize()' , function ( ) {
124
+ describe ( 'BSON.serialize()' , function ( ) {
122
125
// Index for the data type byte of a BSON document with a
123
126
// NOTE: These offsets only apply for documents with the shape {a : <n>}
124
127
// where n is a BigInt
@@ -158,13 +161,13 @@ describe('BSON BigInt support', function() {
158
161
} ;
159
162
}
160
163
161
- it ( 'serializes bigints with the correct BSON type' , function ( ) {
164
+ it ( 'serializes bigints with the correct BSON type' , function ( ) {
162
165
const testDoc = { a : 0n } ;
163
166
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
164
167
expect ( serializedDoc . dataType ) . to . equal ( BSON_DATA_LONG ) ;
165
168
} ) ;
166
169
167
- it ( 'serializes bigints into little-endian byte order' , function ( ) {
170
+ it ( 'serializes bigints into little-endian byte order' , function ( ) {
168
171
const testDoc = { a : 0x1234567812345678n } ;
169
172
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
170
173
const expectedResult = getSerializedDocParts (
@@ -178,7 +181,7 @@ describe('BSON BigInt support', function() {
178
181
expect ( expectedResult . value ) . to . equal ( serializedDoc . value ) ;
179
182
} ) ;
180
183
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 ( ) {
182
185
const testDoc = { a : 0x23n } ;
183
186
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
184
187
const expectedResult = getSerializedDocParts (
@@ -191,7 +194,7 @@ describe('BSON BigInt support', function() {
191
194
expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
192
195
} ) ;
193
196
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 ( ) {
195
198
const testDoc = { a : 0xfffffffffffffff1n } ;
196
199
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
197
200
const expectedResult = getSerializedDocParts (
@@ -204,7 +207,7 @@ describe('BSON BigInt support', function() {
204
207
expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
205
208
} ) ;
206
209
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 ( ) {
208
211
const maxIntPlusOne = { a : 2n ** 63n } ;
209
212
const serializedMaxIntPlusOne = getSerializedDocParts ( BSON . serialize ( maxIntPlusOne ) ) ;
210
213
const expectedResultForMaxIntPlusOne = getSerializedDocParts (
@@ -217,7 +220,7 @@ describe('BSON BigInt support', function() {
217
220
expect ( serializedMaxIntPlusOne ) . to . deep . equal ( expectedResultForMaxIntPlusOne ) ;
218
221
} ) ;
219
222
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 ( ) {
221
224
const maxPositiveInt64 = { a : 2n ** 63n - 1n } ;
222
225
const serializedMaxPositiveInt64 = getSerializedDocParts ( BSON . serialize ( maxPositiveInt64 ) ) ;
223
226
const expectedSerializationForMaxPositiveInt64 = getSerializedDocParts (
@@ -241,7 +244,7 @@ describe('BSON BigInt support', function() {
241
244
expect ( serializedMinPositiveInt64 ) . to . deep . equal ( expectedSerializationForMinPositiveInt64 ) ;
242
245
} ) ;
243
246
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 ( ) {
245
248
const testDoc = { a : 2n ** 64n + 1n } ;
246
249
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
247
250
const expectedSerialization = getSerializedDocParts (
@@ -254,7 +257,7 @@ describe('BSON BigInt support', function() {
254
257
expect ( serializedDoc ) . to . deep . equal ( expectedSerialization ) ;
255
258
} ) ;
256
259
257
- it ( 'serializes array of BigInts' , function ( ) {
260
+ it ( 'serializes array of BigInts' , function ( ) {
258
261
const testArr = { a : [ 1n ] } ;
259
262
const serializedArr = BSON . serialize ( testArr ) ;
260
263
const expectedSerialization = bufferFromHexArray ( [
@@ -269,7 +272,7 @@ describe('BSON BigInt support', function() {
269
272
expect ( serializedArr ) . to . deep . equal ( expectedSerialization ) ;
270
273
} ) ;
271
274
272
- it ( 'serializes Map with BigInt values' , function ( ) {
275
+ it ( 'serializes Map with BigInt values' , function ( ) {
273
276
const testMap = new Map ( ) ;
274
277
testMap . set ( 'a' , 1n ) ;
275
278
const serializedMap = getSerializedDocParts ( BSON . serialize ( testMap ) ) ;
@@ -284,7 +287,7 @@ describe('BSON BigInt support', function() {
284
287
} ) ;
285
288
} ) ;
286
289
287
- describe ( 'EJSON.parse()' , function ( ) {
290
+ describe ( 'EJSON.parse()' , function ( ) {
288
291
type ParseOptions = {
289
292
useBigInt64 : boolean | undefined ;
290
293
relaxed : boolean | undefined ;
@@ -341,13 +344,13 @@ describe('BSON BigInt support', function() {
341
344
const condDescription = generateConditionDescription ( entry ) ;
342
345
const behaviourDescription = generateBehaviourDescription ( entry , sampleString ) ;
343
346
344
- describe ( condDescription , function ( ) {
347
+ describe ( condDescription , function ( ) {
345
348
it ( behaviourDescription , test ) ;
346
349
} ) ;
347
350
}
348
351
}
349
352
350
- describe ( 'canonical input' , function ( ) {
353
+ describe ( 'canonical input' , function ( ) {
351
354
const canonicalInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
352
355
return relaxedValues . flatMap ( relaxed => {
353
356
return genTestTable (
@@ -370,7 +373,7 @@ describe('BSON BigInt support', function() {
370
373
createTestsFromTestTable ( canonicalInputTestTable , sampleCanonicalString ) ;
371
374
} ) ;
372
375
373
- describe ( 'relaxed integer input' , function ( ) {
376
+ describe ( 'relaxed integer input' , function ( ) {
374
377
const relaxedIntegerInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
375
378
return relaxedValues . flatMap ( relaxed => {
376
379
return genTestTable (
@@ -392,7 +395,7 @@ describe('BSON BigInt support', function() {
392
395
createTestsFromTestTable ( relaxedIntegerInputTestTable , sampleRelaxedIntegerString ) ;
393
396
} ) ;
394
397
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 ( ) {
396
399
const relaxedDoubleInputTestTable = relaxedValues . flatMap ( relaxed => {
397
400
return genTestTable ( true , relaxed , ( _ , relaxedIsSet : boolean ) =>
398
401
relaxedIsSet ? { a : 2147483647.9 } : { a : new BSON . Double ( 2147483647.9 ) }
@@ -407,15 +410,15 @@ describe('BSON BigInt support', function() {
407
410
} ) ;
408
411
} ) ;
409
412
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 ( ) {
413
416
const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
414
417
const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
415
418
expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
416
419
} ) ;
417
420
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 ( ) {
419
422
const number = { a : 0x1234_5678_1234_5678_9999n } ;
420
423
const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
421
424
const serialized = BSON . serialize ( number ) ;
@@ -435,15 +438,15 @@ describe('BSON BigInt support', function() {
435
438
436
439
expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
437
440
} ) ;
438
- it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
441
+ it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
439
442
const number = { a : 2n } ;
440
443
const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
441
444
expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
442
445
} ) ;
443
446
} ) ;
444
447
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 ( ) {
447
450
const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
448
451
const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
449
452
const serializedDoc = BSON . serialize ( number ) ;
@@ -462,23 +465,23 @@ describe('BSON BigInt support', function() {
462
465
expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
463
466
} ) ;
464
467
465
- it ( 'serializes bigint values to Number' , function ( ) {
468
+ it ( 'serializes bigint values to Number' , function ( ) {
466
469
const number = { a : 10000n } ;
467
470
const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
468
471
expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
469
472
} ) ;
470
473
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 ( ) {
472
475
const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
473
476
const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
474
477
expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
475
478
} ) ;
476
479
} ) ;
477
480
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 ( ) {
479
482
let parsed ;
480
483
481
- before ( function ( ) {
484
+ before ( function ( ) {
482
485
if ( __noBigInt__ ) {
483
486
return ;
484
487
}
@@ -487,20 +490,20 @@ describe('BSON BigInt support', function() {
487
490
parsed = JSON . parse ( serialized ) ;
488
491
} ) ;
489
492
490
- it ( 'passes loose equality checks with native bigint values' , function ( ) {
493
+ it ( 'passes loose equality checks with native bigint values' , function ( ) {
491
494
// eslint-disable-next-line eqeqeq
492
495
expect ( parsed . a . $numberLong == 12345n ) . true ;
493
496
} ) ;
494
497
495
- it ( 'equals the result of BigInt.toString' , function ( ) {
498
+ it ( 'equals the result of BigInt.toString' , function ( ) {
496
499
expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
497
500
} ) ;
498
501
} ) ;
499
502
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 ( ) {
501
504
let parsed ;
502
505
503
- before ( function ( ) {
506
+ before ( function ( ) {
504
507
if ( __noBigInt__ ) {
505
508
return ;
506
509
}
@@ -509,12 +512,12 @@ describe('BSON BigInt support', function() {
509
512
parsed = JSON . parse ( serialized ) ;
510
513
} ) ;
511
514
512
- it ( 'fails loose equality checks with native bigint values' , function ( ) {
515
+ it ( 'fails loose equality checks with native bigint values' , function ( ) {
513
516
// eslint-disable-next-line eqeqeq
514
517
expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
515
518
} ) ;
516
519
517
- it ( 'not equal to results of BigInt.toString' , function ( ) {
520
+ it ( 'not equal to results of BigInt.toString' , function ( ) {
518
521
expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
519
522
} ) ;
520
523
} ) ;
0 commit comments