@@ -1205,7 +1205,7 @@ describe('Decimal128', function () {
12051205 done ( ) ;
12061206 } ) ;
12071207
1208- it ( 'accepts strings in the constructor' , function ( done ) {
1208+ it ( 'accepts strings in the constructor' , ( ) => {
12091209 expect ( new Decimal128 ( '0' ) . toString ( ) ) . to . equal ( '0' ) ;
12101210 expect ( new Decimal128 ( '00' ) . toString ( ) ) . to . equal ( '0' ) ;
12111211 expect ( new Decimal128 ( '0.5' ) . toString ( ) ) . to . equal ( '0.5' ) ;
@@ -1216,6 +1216,30 @@ describe('Decimal128', function () {
12161216 expect ( new Decimal128 ( '0.0011' ) . toString ( ) ) . to . equal ( '0.0011' ) ;
12171217 expect ( new Decimal128 ( '0.00110' ) . toString ( ) ) . to . equal ( '0.00110' ) ;
12181218 expect ( new Decimal128 ( '-1e400' ) . toString ( ) ) . to . equal ( '-1E+400' ) ;
1219- done ( ) ;
1219+ } ) ;
1220+
1221+ it ( 'throws correct error for invalid constructor argument type' , ( ) => {
1222+ const constructorArgErrMsg = 'Decimal128 must take a Buffer or string' ;
1223+
1224+ expect ( ( ) => new Decimal128 ( - 0 ) ) . to . throw ( constructorArgErrMsg ) ;
1225+ expect ( ( ) => new Decimal128 ( - 1 ) ) . to . throw ( constructorArgErrMsg ) ;
1226+ expect ( ( ) => new Decimal128 ( 10 ) ) . to . throw ( constructorArgErrMsg ) ;
1227+ expect ( ( ) => new Decimal128 ( 1111111111111111 ) ) . to . throw ( constructorArgErrMsg ) ;
1228+ } ) ;
1229+
1230+ it ( 'throws correct error for an invalid Buffer constructor argument' , ( ) => {
1231+ const byteLengthErrMsg = 'Decimal128 must take a Buffer of 16 bytes' ;
1232+
1233+ expect ( ( ) => new Decimal128 ( new Uint8Array ( 0 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1234+ expect ( ( ) => new Decimal128 ( Buffer . alloc ( 0 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1235+ expect ( ( ) => new Decimal128 ( new Uint8Array ( 3 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1236+ expect ( ( ) => new Decimal128 ( Buffer . alloc ( 3 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1237+ expect ( ( ) => new Decimal128 ( new Uint8Array ( 17 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1238+ expect ( ( ) => new Decimal128 ( Buffer . alloc ( 17 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1239+ } ) ;
1240+
1241+ it ( 'does not throw error for an empty Buffer of correct length constructor argument' , ( ) => {
1242+ expect ( ( ) => new Decimal128 ( Buffer . alloc ( 16 ) ) ) . to . not . throw ( ) ;
1243+ expect ( ( ) => new Decimal128 ( new Uint8Array ( 16 ) ) ) . to . not . throw ( ) ;
12201244 } ) ;
12211245} ) ;
0 commit comments