@@ -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,12 +65,15 @@ describe('BSON BigInt support', function() {
65
65
66
66
function generateTestDescription ( entry : TestTableEntry ) : string {
67
67
const options = entry . options ;
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
- } `;
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
+ } `;
74
77
const flagString = `${ useBigInt64 } , ${ promoteValues } , and ${ promoteLongs } ` ;
75
78
if ( entry . shouldThrow ) {
76
79
return `throws when ${ flagString } ` ;
@@ -103,33 +106,48 @@ describe('BSON BigInt support', function() {
103
106
it ( description , test ) ;
104
107
}
105
108
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
+ it ( 'correctly deserializes min 64 bit int (-2n**63n)' , function ( ) {
110
+ expect (
111
+ BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , {
112
+ useBigInt64 : true
113
+ } )
114
+ ) . to . deep . equal ( - ( 2n ** 63n ) ) ;
109
115
} ) ;
110
116
111
- it ( 'correctly deserializes -1n' , function ( ) {
112
- expect ( BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFFFF00' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
113
- - 1n ) ;
117
+ it ( 'correctly deserializes -1n' , function ( ) {
118
+ expect (
119
+ BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFFFF00' , 'hex' ) , {
120
+ useBigInt64 : true
121
+ } )
122
+ ) . to . deep . equal ( - 1n ) ;
114
123
} ) ;
115
124
116
- it ( 'correctly deserializes 0n' , function ( ) {
117
- expect ( BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
118
- 0n ) ;
125
+ it ( 'correctly deserializes 0n' , function ( ) {
126
+ expect (
127
+ BSON . deserialize ( Buffer . from ( '10000000126100000000000000008000' , 'hex' ) , {
128
+ useBigInt64 : true
129
+ } )
130
+ ) . to . deep . equal ( 0n ) ;
119
131
} ) ;
120
132
121
- it ( 'correctly deserializes 1n' , function ( ) {
122
- expect ( BSON . deserialize ( Buffer . from ( '10000000126100010000000000000000' , 'hex' ) , { useBigInt64 : true } ) ) . to . deep . equal (
123
- 1n ) ;
133
+ it ( 'correctly deserializes 1n' , function ( ) {
134
+ expect (
135
+ BSON . deserialize ( Buffer . from ( '10000000126100010000000000000000' , 'hex' ) , {
136
+ useBigInt64 : true
137
+ } )
138
+ ) . to . deep . equal ( 1n ) ;
124
139
} ) ;
125
140
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 ) ) ;
141
+ it ( 'correctly deserializes max 64 bit int (2n**63n -1n)' , function ( ) {
142
+ expect (
143
+ BSON . deserialize ( Buffer . from ( '10000000126100FFFFFFFFFFFFFF7F00' , 'hex' ) , {
144
+ useBigInt64 : true
145
+ } )
146
+ ) . to . deep . equal ( 2n ** 63n - 1n ) ;
129
147
} ) ;
130
148
} ) ;
131
149
132
- describe ( 'BSON.serialize()' , function ( ) {
150
+ describe ( 'BSON.serialize()' , function ( ) {
133
151
// Index for the data type byte of a BSON document with a
134
152
// NOTE: These offsets only apply for documents with the shape {a : <n>}
135
153
// where n is a BigInt
@@ -169,13 +187,13 @@ describe('BSON BigInt support', function() {
169
187
} ;
170
188
}
171
189
172
- it ( 'serializes bigints with the correct BSON type' , function ( ) {
190
+ it ( 'serializes bigints with the correct BSON type' , function ( ) {
173
191
const testDoc = { a : 0n } ;
174
192
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
175
193
expect ( serializedDoc . dataType ) . to . equal ( BSON_DATA_LONG ) ;
176
194
} ) ;
177
195
178
- it ( 'serializes bigints into little-endian byte order' , function ( ) {
196
+ it ( 'serializes bigints into little-endian byte order' , function ( ) {
179
197
const testDoc = { a : 0x1234567812345678n } ;
180
198
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
181
199
const expectedResult = getSerializedDocParts (
@@ -189,7 +207,7 @@ describe('BSON BigInt support', function() {
189
207
expect ( expectedResult . value ) . to . equal ( serializedDoc . value ) ;
190
208
} ) ;
191
209
192
- it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
210
+ it ( 'serializes a BigInt that can be safely represented as a Number' , function ( ) {
193
211
const testDoc = { a : 0x23n } ;
194
212
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
195
213
const expectedResult = getSerializedDocParts (
@@ -202,7 +220,7 @@ describe('BSON BigInt support', function() {
202
220
expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
203
221
} ) ;
204
222
205
- it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
223
+ it ( 'serializes a BigInt in the valid range [-2^63, 2^63 - 1]' , function ( ) {
206
224
const testDoc = { a : 0xfffffffffffffff1n } ;
207
225
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
208
226
const expectedResult = getSerializedDocParts (
@@ -215,7 +233,7 @@ describe('BSON BigInt support', function() {
215
233
expect ( serializedDoc ) . to . deep . equal ( expectedResult ) ;
216
234
} ) ;
217
235
218
- it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
236
+ it ( 'wraps to negative on a BigInt that is larger than (2^63 -1)' , function ( ) {
219
237
const maxIntPlusOne = { a : 2n ** 63n } ;
220
238
const serializedMaxIntPlusOne = getSerializedDocParts ( BSON . serialize ( maxIntPlusOne ) ) ;
221
239
const expectedResultForMaxIntPlusOne = getSerializedDocParts (
@@ -228,7 +246,7 @@ describe('BSON BigInt support', function() {
228
246
expect ( serializedMaxIntPlusOne ) . to . deep . equal ( expectedResultForMaxIntPlusOne ) ;
229
247
} ) ;
230
248
231
- it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
249
+ it ( 'serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]' , function ( ) {
232
250
const maxPositiveInt64 = { a : 2n ** 63n - 1n } ;
233
251
const serializedMaxPositiveInt64 = getSerializedDocParts ( BSON . serialize ( maxPositiveInt64 ) ) ;
234
252
const expectedSerializationForMaxPositiveInt64 = getSerializedDocParts (
@@ -252,7 +270,7 @@ describe('BSON BigInt support', function() {
252
270
expect ( serializedMinPositiveInt64 ) . to . deep . equal ( expectedSerializationForMinPositiveInt64 ) ;
253
271
} ) ;
254
272
255
- it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
273
+ it ( 'truncates a BigInt that is larger than a 64-bit int' , function ( ) {
256
274
const testDoc = { a : 2n ** 64n + 1n } ;
257
275
const serializedDoc = getSerializedDocParts ( BSON . serialize ( testDoc ) ) ;
258
276
const expectedSerialization = getSerializedDocParts (
@@ -265,7 +283,7 @@ describe('BSON BigInt support', function() {
265
283
expect ( serializedDoc ) . to . deep . equal ( expectedSerialization ) ;
266
284
} ) ;
267
285
268
- it ( 'serializes array of BigInts' , function ( ) {
286
+ it ( 'serializes array of BigInts' , function ( ) {
269
287
const testArr = { a : [ 1n ] } ;
270
288
const serializedArr = BSON . serialize ( testArr ) ;
271
289
const expectedSerialization = bufferFromHexArray ( [
@@ -280,7 +298,7 @@ describe('BSON BigInt support', function() {
280
298
expect ( serializedArr ) . to . deep . equal ( expectedSerialization ) ;
281
299
} ) ;
282
300
283
- it ( 'serializes Map with BigInt values' , function ( ) {
301
+ it ( 'serializes Map with BigInt values' , function ( ) {
284
302
const testMap = new Map ( ) ;
285
303
testMap . set ( 'a' , 1n ) ;
286
304
const serializedMap = getSerializedDocParts ( BSON . serialize ( testMap ) ) ;
@@ -295,7 +313,7 @@ describe('BSON BigInt support', function() {
295
313
} ) ;
296
314
} ) ;
297
315
298
- describe ( 'EJSON.parse()' , function ( ) {
316
+ describe ( 'EJSON.parse()' , function ( ) {
299
317
type ParseOptions = {
300
318
useBigInt64 : boolean | undefined ;
301
319
relaxed : boolean | undefined ;
@@ -352,13 +370,13 @@ describe('BSON BigInt support', function() {
352
370
const condDescription = generateConditionDescription ( entry ) ;
353
371
const behaviourDescription = generateBehaviourDescription ( entry , sampleString ) ;
354
372
355
- describe ( condDescription , function ( ) {
373
+ describe ( condDescription , function ( ) {
356
374
it ( behaviourDescription , test ) ;
357
375
} ) ;
358
376
}
359
377
}
360
378
361
- describe ( 'canonical input' , function ( ) {
379
+ describe ( 'canonical input' , function ( ) {
362
380
const canonicalInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
363
381
return relaxedValues . flatMap ( relaxed => {
364
382
return genTestTable (
@@ -381,7 +399,7 @@ describe('BSON BigInt support', function() {
381
399
createTestsFromTestTable ( canonicalInputTestTable , sampleCanonicalString ) ;
382
400
} ) ;
383
401
384
- describe ( 'relaxed integer input' , function ( ) {
402
+ describe ( 'relaxed integer input' , function ( ) {
385
403
const relaxedIntegerInputTestTable = useBigInt64Values . flatMap ( useBigInt64 => {
386
404
return relaxedValues . flatMap ( relaxed => {
387
405
return genTestTable (
@@ -403,7 +421,7 @@ describe('BSON BigInt support', function() {
403
421
createTestsFromTestTable ( relaxedIntegerInputTestTable , sampleRelaxedIntegerString ) ;
404
422
} ) ;
405
423
406
- describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
424
+ describe ( 'relaxed double input where double is outside of int32 range and useBigInt64 is true' , function ( ) {
407
425
const relaxedDoubleInputTestTable = relaxedValues . flatMap ( relaxed => {
408
426
return genTestTable ( true , relaxed , ( _ , relaxedIsSet : boolean ) =>
409
427
relaxedIsSet ? { a : 2147483647.9 } : { a : new BSON . Double ( 2147483647.9 ) }
@@ -418,15 +436,15 @@ describe('BSON BigInt support', function() {
418
436
} ) ;
419
437
} ) ;
420
438
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 ( ) {
439
+ describe ( 'EJSON.stringify()' , function ( ) {
440
+ context ( 'canonical mode (relaxed=false)' , function ( ) {
441
+ it ( 'truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX]' , function ( ) {
424
442
const numbers = { a : 2n ** 64n + 1n , b : - ( 2n ** 64n ) - 1n } ;
425
443
const serialized = EJSON . stringify ( numbers , { relaxed : false } ) ;
426
444
expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}' ) ;
427
445
} ) ;
428
446
429
- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
447
+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
430
448
const number = { a : 0x1234_5678_1234_5678_9999n } ;
431
449
const stringified = EJSON . stringify ( number , { relaxed : false } ) ;
432
450
const serialized = BSON . serialize ( number ) ;
@@ -446,15 +464,15 @@ describe('BSON BigInt support', function() {
446
464
447
465
expect ( parsed . a . $numberLong ) . to . equal ( serializedValue . toString ( ) ) ;
448
466
} ) ;
449
- it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
467
+ it ( 'serializes bigint values to numberLong in canonical mode' , function ( ) {
450
468
const number = { a : 2n } ;
451
469
const serialized = EJSON . stringify ( number , { relaxed : false } ) ;
452
470
expect ( serialized ) . to . equal ( '{"a":{"$numberLong":"2"}}' ) ;
453
471
} ) ;
454
472
} ) ;
455
473
456
- context ( 'relaxed mode (relaxed=true)' , function ( ) {
457
- it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
474
+ context ( 'relaxed mode (relaxed=true)' , function ( ) {
475
+ it ( 'truncates bigint values in the same way as BSON.serialize' , function ( ) {
458
476
const number = { a : 0x1234_0000_1234_5678_9999n } ; // Ensure that the truncated number can be exactly represented as a JS number
459
477
const stringified = EJSON . stringify ( number , { relaxed : true } ) ;
460
478
const serializedDoc = BSON . serialize ( number ) ;
@@ -473,23 +491,23 @@ describe('BSON BigInt support', function() {
473
491
expect ( parsed . a ) . to . equal ( Number ( dataView . getBigInt64 ( VALUE_OFFSET , true ) ) ) ;
474
492
} ) ;
475
493
476
- it ( 'serializes bigint values to Number' , function ( ) {
494
+ it ( 'serializes bigint values to Number' , function ( ) {
477
495
const number = { a : 10000n } ;
478
496
const serialized = EJSON . stringify ( number , { relaxed : true } ) ;
479
497
expect ( serialized ) . to . equal ( '{"a":10000}' ) ;
480
498
} ) ;
481
499
482
- it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
500
+ it ( 'loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]' , function ( ) {
483
501
const numbers = { a : - ( 2n ** 53n ) - 1n , b : 2n ** 53n + 2n } ;
484
502
const serialized = EJSON . stringify ( numbers , { relaxed : true } ) ;
485
503
expect ( serialized ) . to . equal ( '{"a":-9007199254740992,"b":9007199254740994}' ) ;
486
504
} ) ;
487
505
} ) ;
488
506
489
- context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
507
+ context ( 'when passed bigint values that are 64 bits wide or less' , function ( ) {
490
508
let parsed ;
491
509
492
- before ( function ( ) {
510
+ before ( function ( ) {
493
511
if ( __noBigInt__ ) {
494
512
return ;
495
513
}
@@ -498,20 +516,20 @@ describe('BSON BigInt support', function() {
498
516
parsed = JSON . parse ( serialized ) ;
499
517
} ) ;
500
518
501
- it ( 'passes loose equality checks with native bigint values' , function ( ) {
519
+ it ( 'passes loose equality checks with native bigint values' , function ( ) {
502
520
// eslint-disable-next-line eqeqeq
503
521
expect ( parsed . a . $numberLong == 12345n ) . true ;
504
522
} ) ;
505
523
506
- it ( 'equals the result of BigInt.toString' , function ( ) {
524
+ it ( 'equals the result of BigInt.toString' , function ( ) {
507
525
expect ( parsed . a . $numberLong ) . to . equal ( 12345n . toString ( ) ) ;
508
526
} ) ;
509
527
} ) ;
510
528
511
- context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
529
+ context ( 'when passed bigint values that are more than 64 bits wide' , function ( ) {
512
530
let parsed ;
513
531
514
- before ( function ( ) {
532
+ before ( function ( ) {
515
533
if ( __noBigInt__ ) {
516
534
return ;
517
535
}
@@ -520,12 +538,12 @@ describe('BSON BigInt support', function() {
520
538
parsed = JSON . parse ( serialized ) ;
521
539
} ) ;
522
540
523
- it ( 'fails loose equality checks with native bigint values' , function ( ) {
541
+ it ( 'fails loose equality checks with native bigint values' , function ( ) {
524
542
// eslint-disable-next-line eqeqeq
525
543
expect ( parsed . a . $numberLong == 0x1234_5678_1234_5678_9999n ) . false ;
526
544
} ) ;
527
545
528
- it ( 'not equal to results of BigInt.toString' , function ( ) {
546
+ it ( 'not equal to results of BigInt.toString' , function ( ) {
529
547
expect ( parsed . a . $numberLong ) . to . not . equal ( 0x1234_5678_1234_5678_9999n . toString ( ) ) ;
530
548
} ) ;
531
549
} ) ;
0 commit comments