1- 'use strict' ;
1+ import { BSON } from '../register-bson' ;
2+ import { expect } from 'chai' ;
3+ import * as corpus from './tools/bson_corpus_test_loader' ;
24
3- const BSON = require ( '../register-bson' ) ;
45const Decimal128 = BSON . Decimal128 ;
56
6- var NAN = Buffer . from (
7+ const NAN = Buffer . from (
78 [
89 0x7c , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
910 ] . reverse ( )
1011) ;
11- var INF_NEGATIVE_BUFFER = Buffer . from (
12+ const INF_NEGATIVE_BUFFER = Buffer . from (
1213 [
1314 0xf8 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
1415 ] . reverse ( )
1516) ;
16- var INF_POSITIVE_BUFFER = Buffer . from (
17+ const INF_POSITIVE_BUFFER = Buffer . from (
1718 [
1819 0x78 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
1920 ] . reverse ( )
@@ -70,7 +71,7 @@ describe('Decimal128', function () {
7071 } ) ;
7172
7273 it ( 'fromString NaN input' , function ( done ) {
73- var result = Decimal128 . fromString ( 'NaN' ) ;
74+ let result = Decimal128 . fromString ( 'NaN' ) ;
7475 expect ( NAN ) . to . deep . equal ( result . bytes ) ;
7576 result = Decimal128 . fromString ( '+NaN' ) ;
7677 expect ( NAN ) . to . deep . equal ( result . bytes ) ;
@@ -92,7 +93,7 @@ describe('Decimal128', function () {
9293 } ) ;
9394
9495 it ( 'fromString infinity input' , function ( done ) {
95- var result = Decimal128 . fromString ( 'Infinity' ) ;
96+ let result = Decimal128 . fromString ( 'Infinity' ) ;
9697 expect ( INF_POSITIVE_BUFFER ) . to . deep . equal ( result . bytes ) ;
9798 result = Decimal128 . fromString ( '+Infinity' ) ;
9899 expect ( INF_POSITIVE_BUFFER ) . to . deep . equal ( result . bytes ) ;
@@ -107,8 +108,8 @@ describe('Decimal128', function () {
107108
108109 it ( 'fromString simple' , function ( done ) {
109110 // 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 (
112113 [
113114 0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
114115 0x01
@@ -210,8 +211,8 @@ describe('Decimal128', function () {
210211
211212 it ( 'fromString scientific format' , function ( done ) {
212213 // 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 (
215216 [
216217 0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
217218 0x0a
@@ -283,8 +284,8 @@ describe('Decimal128', function () {
283284
284285 it ( 'fromString large format' , function ( done ) {
285286 // 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 (
288289 [
289290 0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x02 , 0x9d , 0x42 , 0xda , 0x3a , 0x76 , 0xf9 , 0xe0 , 0xd9 ,
290291 0x79
@@ -337,8 +338,8 @@ describe('Decimal128', function () {
337338 it ( 'fromString exponent normalization' , function ( done ) {
338339 // Create decimal from string value 1000000000000000000000000000000000000000
339340
340- result = Decimal128 . fromString ( '1000000000000000000000000000000000000000' ) ;
341- bytes = Buffer . from (
341+ let result = Decimal128 . fromString ( '1000000000000000000000000000000000000000' ) ;
342+ let bytes = Buffer . from (
342343 [
343344 0x30 , 0x4c , 0x31 , 0x4d , 0xc6 , 0x44 , 0x8d , 0x93 , 0x38 , 0xc1 , 0x5b , 0x0a , 0x00 , 0x00 , 0x00 ,
344345 0x00
@@ -366,7 +367,7 @@ describe('Decimal128', function () {
366367 ) ;
367368 expect ( bytes ) . to . deep . equal ( result . bytes ) ;
368369
369- var str =
370+ const str =
370371 '100000000000000000000000000000000000000000000000000000000000000000000' +
371372 '000000000000000000000000000000000000000000000000000000000000000000000' +
372373 '000000000000000000000000000000000000000000000000000000000000000000000' +
@@ -385,8 +386,8 @@ describe('Decimal128', function () {
385386
386387 // Create decimal from string value str
387388
388- var result = Decimal128 . fromString ( str ) ;
389- var bytes = Buffer . from (
389+ result = Decimal128 . fromString ( str ) ;
390+ bytes = Buffer . from (
390391 [
391392 0x37 , 0xcc , 0x31 , 0x4d , 0xc6 , 0x44 , 0x8d , 0x93 , 0x38 , 0xc1 , 0x5b , 0x0a , 0x00 , 0x00 , 0x00 ,
392393 0x00
@@ -424,8 +425,8 @@ describe('Decimal128', function () {
424425
425426 it ( 'fromString from string zeros' , function ( done ) {
426427 // 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 (
429430 [
430431 0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
431432 0x00
@@ -467,8 +468,8 @@ describe('Decimal128', function () {
467468
468469 it ( 'fromString from string round' , function ( done ) {
469470 // 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 (
472473 [
473474 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
474475 0x01
@@ -809,7 +810,7 @@ describe('Decimal128', function () {
809810 } ) ;
810811
811812 it ( 'toString infinity' , function ( done ) {
812- var decimal = new Decimal128 (
813+ let decimal = new Decimal128 (
813814 Buffer . from (
814815 [
815816 0x78 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -832,7 +833,7 @@ describe('Decimal128', function () {
832833 } ) ;
833834
834835 it ( 'toString NaN' , function ( done ) {
835- var decimal = new Decimal128 (
836+ let decimal = new Decimal128 (
836837 Buffer . from (
837838 [
838839 0x7c , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -885,7 +886,7 @@ describe('Decimal128', function () {
885886 } ) ;
886887
887888 it ( 'toString regular' , function ( done ) {
888- var decimal = new Decimal128 (
889+ let decimal = new Decimal128 (
889890 Buffer . from (
890891 [
891892 0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -988,7 +989,7 @@ describe('Decimal128', function () {
988989 } ) ;
989990
990991 it ( 'toString scientific' , function ( done ) {
991- var decimal = new Decimal128 (
992+ let decimal = new Decimal128 (
992993 Buffer . from (
993994 [
994995 0x5f , 0xfe , 0x31 , 0x4d , 0xc6 , 0x44 , 0x8d , 0x93 , 0x38 , 0xc1 , 0x5b , 0x0a , 0x00 , 0x00 , 0x00 ,
@@ -1111,7 +1112,7 @@ describe('Decimal128', function () {
11111112 } ) ;
11121113
11131114 it ( 'toString zeros' , function ( done ) {
1114- var decimal = new Decimal128 (
1115+ let decimal = new Decimal128 (
11151116 Buffer . from (
11161117 [
11171118 0x30 , 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -1145,10 +1146,10 @@ describe('Decimal128', function () {
11451146
11461147 it ( 'Serialize and Deserialize tests' , function ( done ) {
11471148 // 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 ) ;
11521153
11531154 expect ( buffer . length ) . to . equal ( size ) ;
11541155 expect ( doc ) . to . deep . equal ( back ) ;
@@ -1193,6 +1194,7 @@ describe('Decimal128', function () {
11931194 it ( 'throws correct error for invalid constructor argument type' , ( ) => {
11941195 const constructorArgErrMsg = 'Decimal128 must take a Buffer or string' ;
11951196
1197+ // ts-ignore
11961198 expect ( ( ) => new Decimal128 ( - 0 ) ) . to . throw ( constructorArgErrMsg ) ;
11971199 expect ( ( ) => new Decimal128 ( - 1 ) ) . to . throw ( constructorArgErrMsg ) ;
11981200 expect ( ( ) => new Decimal128 ( 10 ) ) . to . throw ( constructorArgErrMsg ) ;
@@ -1235,5 +1237,75 @@ describe('Decimal128', function () {
12351237 } ) ;
12361238 }
12371239 ) ;
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+ } ) ;
12381310 } ) ;
12391311} ) ;
0 commit comments