test: replace assert.throws w/ common.expectsError

Converts RangeError assertions to use common.expectsError and includes
an assertion for the error code.

PR-URL: https://github.com/nodejs/node/pull/23454
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
Andrew Eisenberg 2018-10-12 09:33:37 -07:00 committed by Rich Trott
parent c301518a45
commit 0005846f03

View File

@ -74,41 +74,22 @@ new Buffer('', 'latin1');
new Buffer('', 'binary'); new Buffer('', 'binary');
Buffer(0); Buffer(0);
const outOfBoundsError = {
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError
};
// try to write a 0-length string beyond the end of b // try to write a 0-length string beyond the end of b
common.expectsError( common.expectsError(() => b.write('', 2048), outOfBoundsError);
() => b.write('', 2048),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError
}
);
// throw when writing to negative offset // throw when writing to negative offset
common.expectsError( common.expectsError(() => b.write('a', -1), outOfBoundsError);
() => b.write('a', -1),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError
}
);
// throw when writing past bounds from the pool // throw when writing past bounds from the pool
common.expectsError( common.expectsError(() => b.write('a', 2048), outOfBoundsError);
() => b.write('a', 2048),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError
}
);
// throw when writing to negative offset // throw when writing to negative offset
common.expectsError( common.expectsError(() => b.write('a', -1), outOfBoundsError);
() => b.write('a', -1),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError
}
);
// try to copy 0 bytes worth of data into an empty buffer // try to copy 0 bytes worth of data into an empty buffer
b.copy(Buffer.alloc(0), 0, 0, 0); b.copy(Buffer.alloc(0), 0, 0, 0);
@ -804,20 +785,34 @@ assert.strictEqual(Buffer.from('13.37').length, 5);
Buffer.from(Buffer.allocUnsafe(0), 0, 0); Buffer.from(Buffer.allocUnsafe(0), 0, 0);
// issue GH-5587 // issue GH-5587
assert.throws(() => Buffer.alloc(8).writeFloatLE(0, 5), RangeError); common.expectsError(
assert.throws(() => Buffer.alloc(16).writeDoubleLE(0, 9), RangeError); () => Buffer.alloc(8).writeFloatLE(0, 5),
outOfBoundsError
);
common.expectsError(
() => Buffer.alloc(16).writeDoubleLE(0, 9),
outOfBoundsError
);
// attempt to overflow buffers, similar to previous bug in array buffers // attempt to overflow buffers, similar to previous bug in array buffers
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), common.expectsError(
RangeError); () => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff), outOfBoundsError
RangeError); );
common.expectsError(
() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
outOfBoundsError
);
// ensure negative values can't get past offset // ensure negative values can't get past offset
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError); common.expectsError(
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError); () => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1),
outOfBoundsError
);
common.expectsError(
() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1),
outOfBoundsError
);
// test for common write(U)IntLE/BE // test for common write(U)IntLE/BE
{ {
@ -1010,10 +1005,7 @@ common.expectsError(() => {
const a = Buffer.alloc(1); const a = Buffer.alloc(1);
const b = Buffer.alloc(1); const b = Buffer.alloc(1);
a.copy(b, 0, 0x100000000, 0x100000001); a.copy(b, 0, 0x100000000, 0x100000001);
}, { }, outOfBoundsError);
code: 'ERR_OUT_OF_RANGE',
type: RangeError
});
// Unpooled buffer (replaces SlowBuffer) // Unpooled buffer (replaces SlowBuffer)
{ {