@@ -3,14 +3,14 @@ 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.deserialize()' , function ( ) {
13
+ describe ( 'BSON.deserialize()' , function ( ) {
14
14
type DeserialzationOptions = {
15
15
useBigInt64 : boolean | undefined ;
16
16
promoteValues : boolean | undefined ;
@@ -65,15 +65,12 @@ describe('BSON BigInt support', function () {
65
65
66
66
function generateTestDescription ( entry : TestTableEntry ) : string {
67
67
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
+ } `;
77
74
const flagString = `${ useBigInt64 } , ${ promoteValues } , and ${ promoteLongs } ` ;
78
75
if ( entry . shouldThrow ) {
79
76
return `throws when ${ flagString } ` ;
@@ -106,41 +103,33 @@ describe('BSON BigInt support', function () {
106
103
it ( description , test ) ;
107
104
}
108
105
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
+ } ) ;
132
110
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 ) ) ;
140
129
} ) ;
141
130
} ) ;
142
131
143
- describe ( 'BSON.serialize()' , function ( ) {
132
+ describe ( 'BSON.serialize()' , function ( ) {
144
133
// Index for the data type byte of a BSON document with a
145
134
// NOTE: These offsets only apply for documents with the shape {a : <n>}
146
135
// where n is a BigInt
@@ -180,13 +169,13 @@ describe('BSON BigInt support', function () {
180
169
} ;
181
170
}
182
171
183
- it ( 'serializes bigints with the correct BSON type' , function ( ) {
172
+ it ( 'serializes bigints with the correct BSON type' , function ( ) {
184
173
const testDoc = { a : 0n } ;
185
174
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
186
175
expect ( serializedDoc . dataType ) . to . equal ( BSON_DATA_LONG ) ;
187
176
} ) ;
188
177
189
- it ( 'serializes bigints into little-endian byte order' , function ( ) {
178
+ it ( 'serializes bigints into little-endian byte order' , function ( ) {
190
179
const testDoc = { a : 0x1234567812345678n } ;
191
180
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
192
181
const expectedResult = getSerializedDocParts (
@@ -200,7 +189,7 @@ describe('BSON BigInt support', function () {
200
189
expect ( expectedResult . value ) . to . equal ( serializedDoc . value ) ;
201
190
} ) ;
202
191
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 ( ) {
204
193
const testDoc = { a : 0x23n } ;
205
194
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
206
195
const expectedResult = getSerializedDocParts (
@@ -213,7 +202,7 @@ describe('BSON BigInt support', function () {
213
202
expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
214
203
} ) ;
215
204
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 ( ) {
217
206
const testDoc = { a : 0xfffffffffffffff1n } ;
218
207
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
219
208
const expectedResult = getSerializedDocParts (
@@ -226,7 +215,7 @@ describe('BSON BigInt support', function () {
226
215
expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
227
216
} ) ;
228
217
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 ( ) {
230
219
const maxIntPlusOne = { a : 2n ** 63n } ;
231
220
const serializedMaxIntPlusOne = getSerializedDocParts ( BSON . serialize ( maxIntPlusOne ) ) ;
232
221
const expectedResultForMaxIntPlusOne = getSerializedDocParts (
@@ -239,7 +228,7 @@ describe('BSON BigInt support', function () {
239
228
expect ( serializedMaxIntPlusOne ) . to . deep . equal ( expectedResultForMaxIntPlusOne ) ;
240
229
} ) ;
241
230
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 ( ) {
243
232
const maxPositiveInt64 = { a : 2n ** 63n - 1n } ;
244
233
const serializedMaxPositiveInt64 = getSerializedDocParts ( BSON . serialize ( maxPositiveInt64 ) ) ;
245
234
const expectedSerializationForMaxPositiveInt64 = getSerializedDocParts (
@@ -263,7 +252,7 @@ describe('BSON BigInt support', function () {
263
252
expect ( serializedMinPositiveInt64 ) . to . deep . equal ( expectedSerializationForMinPositiveInt64 ) ;
264
253
} ) ;
265
254
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 ( ) {
267
256
const testDoc = { a : 2n ** 64n + 1n } ;
268
257
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
269
258
const expectedSerialization = getSerializedDocParts (
@@ -276,7 +265,7 @@ describe('BSON BigInt support', function () {
276
265
expect ( serializedDoc ) . to . deep . equal ( expectedSerialization ) ;
277
266
} ) ;
278
267
279
- it ( 'serializes array of BigInts' , function ( ) {
268
+ it ( 'serializes array of BigInts' , function ( ) {
280
269
const testArr = { a : [ 1n ] } ;
281
270
const serializedArr = BSON . serialize ( testArr ) ;
282
271
const expectedSerialization = bufferFromHexArray ( [
@@ -291,7 +280,7 @@ describe('BSON BigInt support', function () {
291
280
expect ( serializedArr ) . to . deep . equal ( expectedSerialization ) ;
292
281
} ) ;
293
282
294
- it ( 'serializes Map with BigInt values' , function ( ) {
283
+ it ( 'serializes Map with BigInt values' , function ( ) {
295
284
const testMap = new Map ( ) ;
296
285
testMap . set ( 'a' , 1n ) ;
297
286
const serializedMap = getSerializedDocParts ( BSON . serialize ( testMap ) ) ;
@@ -306,7 +295,7 @@ describe('BSON BigInt support', function () {
306
295
} ) ;
307
296
} ) ;
308
297
309
- describe ( 'EJSON.parse()' , function ( ) {
298
+ describe ( 'EJSON.parse()' , function ( ) {
310
299
type ParseOptions = {
311
300
useBigInt64 : boolean | undefined ;
312
301
relaxed : boolean | undefined ;
@@ -363,13 +352,13 @@ describe('BSON BigInt support', function () {
363
352
const condDescription = generateConditionDescription ( entry ) ;
364
353
const behaviourDescription = generateBehaviourDescription ( entry , sampleString ) ;
365
354
366
- describe ( condDescription , function ( ) {
355
+ describe ( condDescription , function ( ) {
367
356
it ( behaviourDescription , test ) ;
368
357
} ) ;
369
358
}
370
359
}
371
360
372
- describe ( 'canonical input' , function ( ) {
361
+ describe ( 'canonical input' , function ( ) {
373
362
const canonicalInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
374
363
return relaxedValues . flatMap ( relaxed => {
375
364
return genTestTable (
@@ -392,7 +381,7 @@ describe('BSON BigInt support', function () {
392
381
createTestsFromTestTable ( canonicalInputTestTable , sampleCanonicalString ) ;
393
382
} ) ;
394
383
395
- describe ( 'relaxed integer input' , function ( ) {
384
+ describe ( 'relaxed integer input' , function ( ) {
396
385
const relaxedIntegerInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
397
386
return relaxedValues . flatMap ( relaxed => {
398
387
return genTestTable (
@@ -414,7 +403,7 @@ describe('BSON BigInt support', function () {
414
403
createTestsFromTestTable ( relaxedIntegerInputTestTable , sampleRelaxedIntegerString ) ;
415
404
} ) ;
416
405
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 ( ) {
418
407
const relaxedDoubleInputTestTable = relaxedValues . flatMap ( relaxed => {
419
408
return genTestTable ( true , relaxed , ( _ , relaxedIsSet : boolean ) =>
420
409
relaxedIsSet ? { a : 2147483647.9 } : { a : new BSON . Double ( 2147483647.9 ) }
@@ -429,15 +418,15 @@ describe('BSON BigInt support', function () {
429
418
} ) ;
430
419
} ) ;
431
420
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 ( ) {
435
424
const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
436
425
const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
437
426
expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
438
427
} ) ;
439
428
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 ( ) {
441
430
const number = { a : 0x1234_5678_1234_5678_9999n } ;
442
431
const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
443
432
const serialized = BSON . serialize ( number ) ;
@@ -457,15 +446,15 @@ describe('BSON BigInt support', function () {
457
446
458
447
expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
459
448
} ) ;
460
- it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
449
+ it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
461
450
const number = { a : 2n } ;
462
451
const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
463
452
expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
464
453
} ) ;
465
454
} ) ;
466
455
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 ( ) {
469
458
const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
470
459
const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
471
460
const serializedDoc = BSON . serialize ( number ) ;
@@ -484,23 +473,23 @@ describe('BSON BigInt support', function () {
484
473
expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
485
474
} ) ;
486
475
487
- it ( 'serializes bigint values to Number' , function ( ) {
476
+ it ( 'serializes bigint values to Number' , function ( ) {
488
477
const number = { a : 10000n } ;
489
478
const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
490
479
expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
491
480
} ) ;
492
481
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 ( ) {
494
483
const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
495
484
const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
496
485
expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
497
486
} ) ;
498
487
} ) ;
499
488
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 ( ) {
501
490
let parsed ;
502
491
503
- before ( function ( ) {
492
+ before ( function ( ) {
504
493
if ( __noBigInt__ ) {
505
494
return ;
506
495
}
@@ -509,20 +498,20 @@ describe('BSON BigInt support', function () {
509
498
parsed = JSON . parse ( serialized ) ;
510
499
} ) ;
511
500
512
- it ( 'passes loose equality checks with native bigint values' , function ( ) {
501
+ it ( 'passes loose equality checks with native bigint values' , function ( ) {
513
502
// eslint-disable-next-line eqeqeq
514
503
expect ( parsed . a . $numberLong == 12345n ) . true ;
515
504
} ) ;
516
505
517
- it ( 'equals the result of BigInt.toString' , function ( ) {
506
+ it ( 'equals the result of BigInt.toString' , function ( ) {
518
507
expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
519
508
} ) ;
520
509
} ) ;
521
510
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 ( ) {
523
512
let parsed ;
524
513
525
- before ( function ( ) {
514
+ before ( function ( ) {
526
515
if ( __noBigInt__ ) {
527
516
return ;
528
517
}
@@ -531,12 +520,12 @@ describe('BSON BigInt support', function () {
531
520
parsed = JSON . parse ( serialized ) ;
532
521
} ) ;
533
522
534
- it ( 'fails loose equality checks with native bigint values' , function ( ) {
523
+ it ( 'fails loose equality checks with native bigint values' , function ( ) {
535
524
// eslint-disable-next-line eqeqeq
536
525
expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
537
526
} ) ;
538
527
539
- it ( 'not equal to results of BigInt.toString' , function ( ) {
528
+ it ( 'not equal to results of BigInt.toString' , function ( ) {
540
529
expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
541
530
} ) ;
542
531
} ) ;
0 commit comments