1
1
'use strict' ;
2
2
3
+ // const { BSONTypeError } = require('../register-bson');
3
4
const BSON = require ( '../register-bson' ) ;
4
5
const Decimal128 = BSON . Decimal128 ;
5
6
@@ -1205,7 +1206,7 @@ describe('Decimal128', function () {
1205
1206
done ( ) ;
1206
1207
} ) ;
1207
1208
1208
- it ( 'accepts strings in the constructor' , function ( done ) {
1209
+ it ( 'accepts strings in the constructor' , ( ) => {
1209
1210
expect ( new Decimal128 ( '0' ) . toString ( ) ) . to . equal ( '0' ) ;
1210
1211
expect ( new Decimal128 ( '00' ) . toString ( ) ) . to . equal ( '0' ) ;
1211
1212
expect ( new Decimal128 ( '0.5' ) . toString ( ) ) . to . equal ( '0.5' ) ;
@@ -1216,6 +1217,24 @@ describe('Decimal128', function () {
1216
1217
expect ( new Decimal128 ( '0.0011' ) . toString ( ) ) . to . equal ( '0.0011' ) ;
1217
1218
expect ( new Decimal128 ( '0.00110' ) . toString ( ) ) . to . equal ( '0.00110' ) ;
1218
1219
expect ( new Decimal128 ( '-1e400' ) . toString ( ) ) . to . equal ( '-1E+400' ) ;
1219
- done ( ) ;
1220
+ } ) ;
1221
+
1222
+ it . only ( 'throws appropriate error for invalid constructor arguments' , ( ) => {
1223
+ const byteLengthErrMsg = 'Decimal128 must take a Buffer of 16 bytes' ;
1224
+ const constructorArgErrMsg = 'Decimal128 must take a Buffer or string' ;
1225
+
1226
+ expect ( ( ) => new Decimal128 ( - 0 ) ) . to . throw ( constructorArgErrMsg ) ;
1227
+ expect ( ( ) => new Decimal128 ( - 1 ) ) . to . throw ( constructorArgErrMsg ) ;
1228
+ expect ( ( ) => new Decimal128 ( 10 ) ) . to . throw ( constructorArgErrMsg ) ;
1229
+ expect ( ( ) => new Decimal128 ( 1111111111111111 ) ) . to . throw ( constructorArgErrMsg ) ;
1230
+
1231
+ expect ( ( ) => new Decimal128 ( new Uint8Array ( 0 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1232
+ expect ( ( ) => new Decimal128 ( Buffer . alloc ( 0 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1233
+ expect ( ( ) => new Decimal128 ( new Uint8Array ( 3 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1234
+ expect ( ( ) => new Decimal128 ( Buffer . alloc ( 3 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1235
+ expect ( ( ) => new Decimal128 ( new Uint8Array ( 17 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1236
+ expect ( ( ) => new Decimal128 ( Buffer . alloc ( 17 ) ) ) . to . throw ( byteLengthErrMsg ) ;
1237
+ new Decimal128 ( Buffer . alloc ( 16 ) ) ;
1238
+ new Decimal128 ( new Uint8Array ( 16 ) ) ;
1220
1239
} ) ;
1221
1240
} ) ;
0 commit comments