1
- 'use strict' ;
1
+ import { BSON } from '../register-bson' ;
2
+ import { expect } from 'chai' ;
3
+ import * as corpus from './tools/bson_corpus_test_loader' ;
2
4
3
- const BSON = require ( '../register-bson' ) ;
4
5
const Decimal128 = BSON . Decimal128 ;
5
6
6
- var NAN = Buffer . from (
7
+ const NAN = Buffer . from (
7
8
[
8
9
0x7c , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
9
10
] . reverse ( )
10
11
) ;
11
- var INF_NEGATIVE_BUFFER = Buffer . from (
12
+ const INF_NEGATIVE_BUFFER = Buffer . from (
12
13
[
13
14
0xf8 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
14
15
] . reverse ( )
15
16
) ;
16
- var INF_POSITIVE_BUFFER = Buffer . from (
17
+ const INF_POSITIVE_BUFFER = Buffer . from (
17
18
[
18
19
0x78 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
19
20
] . reverse ( )
@@ -70,7 +71,7 @@ describe('Decimal128', function () {
70
71
} ) ;
71
72
72
73
it ( 'fromString NaN input' , function ( done ) {
73
- var result = Decimal128 . fromString ( 'NaN' ) ;
74
+ let result = Decimal128 . fromString ( 'NaN' ) ;
74
75
expect ( NAN ) . to . deep . equal ( result . bytes ) ;
75
76
result = Decimal128 . fromString ( '+NaN' ) ;
76
77
expect ( NAN ) . to . deep . equal ( result . bytes ) ;
@@ -92,7 +93,7 @@ describe('Decimal128', function () {
92
93
} ) ;
93
94
94
95
it ( 'fromString infinity input' , function ( done ) {
95
- var result = Decimal128 . fromString ( 'Infinity' ) ;
96
+ let result = Decimal128 . fromString ( 'Infinity' ) ;
96
97
expect ( INF_POSITIVE_BUFFER ) . to . deep . equal ( result . bytes ) ;
97
98
result = Decimal128 . fromString ( '+Infinity' ) ;
98
99
expect ( INF_POSITIVE_BUFFER ) . to . deep . equal ( result . bytes ) ;
@@ -107,8 +108,8 @@ describe('Decimal128', function () {
107
108
108
109
it ( 'fromString simple' , function ( done ) {
109
110
// Create decimal from string value 1
110
- var result = Decimal128 . fromString ( '1' ) ;
111
- var bytes = Buffer . from (
111
+ let result = Decimal128 . fromString ( '1' ) ;
112
+ let bytes = Buffer . from (
112
113
[
113
114
0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
114
115
0x01
@@ -210,8 +211,8 @@ describe('Decimal128', function () {
210
211
211
212
it ( 'fromString scientific format' , function ( done ) {
212
213
// Create decimal from string value 10e0
213
- var result = Decimal128 . fromString ( '10e0' ) ;
214
- var bytes = Buffer . from (
214
+ let result = Decimal128 . fromString ( '10e0' ) ;
215
+ let bytes = Buffer . from (
215
216
[
216
217
0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
217
218
0x0a
@@ -283,8 +284,8 @@ describe('Decimal128', function () {
283
284
284
285
it ( 'fromString large format' , function ( done ) {
285
286
// Create decimal from string value 12345689012345789012345
286
- var result = Decimal128 . fromString ( '12345689012345789012345' ) ;
287
- var bytes = Buffer . from (
287
+ let result = Decimal128 . fromString ( '12345689012345789012345' ) ;
288
+ let bytes = Buffer . from (
288
289
[
289
290
0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x02 , 0x9d , 0x42 , 0xda , 0x3a , 0x76 , 0xf9 , 0xe0 , 0xd9 ,
290
291
0x79
@@ -337,8 +338,8 @@ describe('Decimal128', function () {
337
338
it ( 'fromString exponent normalization' , function ( done ) {
338
339
// Create decimal from string value 1000000000000000000000000000000000000000
339
340
340
- result = Decimal128 . fromString ( '1000000000000000000000000000000000000000' ) ;
341
- bytes = Buffer . from (
341
+ let result = Decimal128 . fromString ( '1000000000000000000000000000000000000000' ) ;
342
+ let bytes = Buffer . from (
342
343
[
343
344
0x30 , 0x4c , 0x31 , 0x4d , 0xc6 , 0x44 , 0x8d , 0x93 , 0x38 , 0xc1 , 0x5b , 0x0a , 0x00 , 0x00 , 0x00 ,
344
345
0x00
@@ -366,7 +367,7 @@ describe('Decimal128', function () {
366
367
) ;
367
368
expect ( bytes ) . to . deep . equal ( result . bytes ) ;
368
369
369
- var str =
370
+ const str =
370
371
'100000000000000000000000000000000000000000000000000000000000000000000' +
371
372
'000000000000000000000000000000000000000000000000000000000000000000000' +
372
373
'000000000000000000000000000000000000000000000000000000000000000000000' +
@@ -385,8 +386,8 @@ describe('Decimal128', function () {
385
386
386
387
// Create decimal from string value str
387
388
388
- var result = Decimal128 . fromString ( str ) ;
389
- var bytes = Buffer . from (
389
+ result = Decimal128 . fromString ( str ) ;
390
+ bytes = Buffer . from (
390
391
[
391
392
0x37 , 0xcc , 0x31 , 0x4d , 0xc6 , 0x44 , 0x8d , 0x93 , 0x38 , 0xc1 , 0x5b , 0x0a , 0x00 , 0x00 , 0x00 ,
392
393
0x00
@@ -424,8 +425,8 @@ describe('Decimal128', function () {
424
425
425
426
it ( 'fromString from string zeros' , function ( done ) {
426
427
// Create decimal from string value 0
427
- var result = Decimal128 . fromString ( '0' ) ;
428
- var bytes = Buffer . from (
428
+ let result = Decimal128 . fromString ( '0' ) ;
429
+ let bytes = Buffer . from (
429
430
[
430
431
0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
431
432
0x00
@@ -467,8 +468,8 @@ describe('Decimal128', function () {
467
468
468
469
it ( 'fromString from string round' , function ( done ) {
469
470
// Create decimal from string value 10E-6177
470
- var result = Decimal128 . fromString ( '10E-6177' ) ;
471
- var bytes = Buffer . from (
471
+ let result = Decimal128 . fromString ( '10E-6177' ) ;
472
+ const bytes = Buffer . from (
472
473
[
473
474
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
474
475
0x01
@@ -809,7 +810,7 @@ describe('Decimal128', function () {
809
810
} ) ;
810
811
811
812
it ( 'toString infinity' , function ( done ) {
812
- var decimal = new Decimal128 (
813
+ let decimal = new Decimal128 (
813
814
Buffer . from (
814
815
[
815
816
0x78 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -832,7 +833,7 @@ describe('Decimal128', function () {
832
833
} ) ;
833
834
834
835
it ( 'toString NaN' , function ( done ) {
835
- var decimal = new Decimal128 (
836
+ let decimal = new Decimal128 (
836
837
Buffer . from (
837
838
[
838
839
0x7c , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -885,7 +886,7 @@ describe('Decimal128', function () {
885
886
} ) ;
886
887
887
888
it ( 'toString regular' , function ( done ) {
888
- var decimal = new Decimal128 (
889
+ let decimal = new Decimal128 (
889
890
Buffer . from (
890
891
[
891
892
0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -988,7 +989,7 @@ describe('Decimal128', function () {
988
989
} ) ;
989
990
990
991
it ( 'toString scientific' , function ( done ) {
991
- var decimal = new Decimal128 (
992
+ let decimal = new Decimal128 (
992
993
Buffer . from (
993
994
[
994
995
0x5f , 0xfe , 0x31 , 0x4d , 0xc6 , 0x44 , 0x8d , 0x93 , 0x38 , 0xc1 , 0x5b , 0x0a , 0x00 , 0x00 , 0x00 ,
@@ -1111,7 +1112,7 @@ describe('Decimal128', function () {
1111
1112
} ) ;
1112
1113
1113
1114
it ( 'toString zeros' , function ( done ) {
1114
- var decimal = new Decimal128 (
1115
+ let decimal = new Decimal128 (
1115
1116
Buffer . from (
1116
1117
[
1117
1118
0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -1145,10 +1146,10 @@ describe('Decimal128', function () {
1145
1146
1146
1147
it ( 'Serialize and Deserialize tests' , function ( done ) {
1147
1148
// Test all methods around a simple serialization at object top level
1148
- var doc = { value : Decimal128 . fromString ( '1' ) } ;
1149
- var buffer = BSON . serialize ( doc ) ;
1150
- var size = BSON . calculateObjectSize ( doc ) ;
1151
- var back = BSON . deserialize ( buffer ) ;
1149
+ let doc = { value : Decimal128 . fromString ( '1' ) } ;
1150
+ let buffer = BSON . serialize ( doc ) ;
1151
+ let size = BSON . calculateObjectSize ( doc ) ;
1152
+ let back = BSON . deserialize ( buffer ) ;
1152
1153
1153
1154
expect ( buffer . length ) . to . equal ( size ) ;
1154
1155
expect ( doc ) . to . deep . equal ( back ) ;
@@ -1193,6 +1194,7 @@ describe('Decimal128', function () {
1193
1194
it ( 'throws correct error for invalid constructor argument type' , ( ) => {
1194
1195
const constructorArgErrMsg = 'Decimal128 must take a Buffer or string' ;
1195
1196
1197
+ // ts-ignore
1196
1198
expect ( ( ) => new Decimal128 ( - 0 ) ) . to . throw ( constructorArgErrMsg ) ;
1197
1199
expect ( ( ) => new Decimal128 ( - 1 ) ) . to . throw ( constructorArgErrMsg ) ;
1198
1200
expect ( ( ) => new Decimal128 ( 10 ) ) . to . throw ( constructorArgErrMsg ) ;
@@ -1235,5 +1237,75 @@ describe('Decimal128', function () {
1235
1237
} ) ;
1236
1238
}
1237
1239
) ;
1240
+
1241
+ describe ( 'fromStringWithRounding' , function ( ) {
1242
+ context ( 'Corpus tests' , function ( ) {
1243
+ // Filter for only Decimal128 tests
1244
+ for ( const { description, valid, _filename, parseErrors } of corpus . filter ( s =>
1245
+ / D e c i m a l 1 2 8 / . test ( s . description )
1246
+ ) ) {
1247
+ const scenarioName = `${ description } (${ _filename } )` ;
1248
+ describe ( scenarioName , function ( ) {
1249
+ if ( valid ) {
1250
+ // We only care about the extended json inputs because the bson inputs will not test the
1251
+ // fromString or fromStringWithRounding code paths
1252
+ const inputs = [
1253
+ 'canonical_extjson' ,
1254
+ 'degenerate_extjson' ,
1255
+ 'relaxed_extjson' ,
1256
+ 'converted_extjson'
1257
+ ] ;
1258
+ for ( const validTest of valid ) {
1259
+ context ( `Valid Test: ${ validTest . description } ` , function ( ) {
1260
+ for ( const input of inputs ) {
1261
+ if ( validTest [ input ] ) {
1262
+ describe ( `with ${ input } input` , function ( ) {
1263
+ it ( 'has same output as fromString' , function ( ) {
1264
+ const extJSONString : string = JSON . parse ( validTest [ input ] ) . d
1265
+ . $numberDecimal ;
1266
+ expect ( Decimal128 . fromStringWithRounding ( extJSONString ) ) . to . deep . equal (
1267
+ Decimal128 . fromString ( extJSONString )
1268
+ ) ;
1269
+ } ) ;
1270
+ } ) ;
1271
+ }
1272
+ }
1273
+ } ) ;
1274
+ }
1275
+ }
1276
+ if ( parseErrors ) {
1277
+ // Filter out the inexact rounding tests
1278
+ for ( const parseErrorTest of parseErrors . filter (
1279
+ p => ! / I n e x a c t / . test ( p . description )
1280
+ ) ) {
1281
+ context ( `ParseError - ${ parseErrorTest . description } ` , function ( ) {
1282
+ it ( 'emits the same error as fromString' , function ( ) {
1283
+ const errorOrNull = ( f : ( ) => void ) => {
1284
+ try {
1285
+ f ( ) ;
1286
+ } catch ( err ) {
1287
+ return err ;
1288
+ }
1289
+ return null ;
1290
+ } ;
1291
+ const fromStringError = errorOrNull ( ( ) =>
1292
+ Decimal128 . fromString ( parseErrorTest . string )
1293
+ ) ;
1294
+ const fromStringWithRoundingError = errorOrNull ( ( ) =>
1295
+ Decimal128 . fromStringWithRounding ( parseErrorTest . string )
1296
+ ) ;
1297
+
1298
+ expect ( fromStringError ) . to . be . instanceOf ( Error ) ;
1299
+ expect ( fromStringWithRoundingError ) . to . be . instanceOf ( Error ) ;
1300
+
1301
+ expect ( fromStringWithRoundingError ) . to . deep . equal ( fromStringError ) ;
1302
+ } ) ;
1303
+ } ) ;
1304
+ }
1305
+ }
1306
+ } ) ;
1307
+ }
1308
+ } ) ;
1309
+ } ) ;
1238
1310
} ) ;
1239
1311
} ) ;
0 commit comments