Skip to content

Commit 4296837

Browse files
aeisenbergjasnell
authored andcommitted
test: replace assert.throws w/ common.expectsError
Converts RangeError assertions to use common.expectsError and includes an assertion for the error code. PR-URL: #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>
1 parent 6855b61 commit 4296837

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

test/parallel/test-buffer-alloc.js

+34-42
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,22 @@ new Buffer('', 'latin1');
7474
new Buffer('', 'binary');
7575
Buffer(0);
7676

77+
const outOfBoundsError = {
78+
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
79+
type: RangeError
80+
};
81+
7782
// try to write a 0-length string beyond the end of b
78-
common.expectsError(
79-
() => b.write('', 2048),
80-
{
81-
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
82-
type: RangeError
83-
}
84-
);
83+
common.expectsError(() => b.write('', 2048), outOfBoundsError);
8584

8685
// throw when writing to negative offset
87-
common.expectsError(
88-
() => b.write('a', -1),
89-
{
90-
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
91-
type: RangeError
92-
}
93-
);
86+
common.expectsError(() => b.write('a', -1), outOfBoundsError);
9487

9588
// throw when writing past bounds from the pool
96-
common.expectsError(
97-
() => b.write('a', 2048),
98-
{
99-
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
100-
type: RangeError
101-
}
102-
);
89+
common.expectsError(() => b.write('a', 2048), outOfBoundsError);
10390

10491
// throw when writing to negative offset
105-
common.expectsError(
106-
() => b.write('a', -1),
107-
{
108-
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
109-
type: RangeError
110-
}
111-
);
92+
common.expectsError(() => b.write('a', -1), outOfBoundsError);
11293

11394
// try to copy 0 bytes worth of data into an empty buffer
11495
b.copy(Buffer.alloc(0), 0, 0, 0);
@@ -804,20 +785,34 @@ assert.strictEqual(Buffer.from('13.37').length, 5);
804785
Buffer.from(Buffer.allocUnsafe(0), 0, 0);
805786

806787
// issue GH-5587
807-
assert.throws(() => Buffer.alloc(8).writeFloatLE(0, 5), RangeError);
808-
assert.throws(() => Buffer.alloc(16).writeDoubleLE(0, 9), RangeError);
788+
common.expectsError(
789+
() => Buffer.alloc(8).writeFloatLE(0, 5),
790+
outOfBoundsError
791+
);
792+
common.expectsError(
793+
() => Buffer.alloc(16).writeDoubleLE(0, 9),
794+
outOfBoundsError
795+
);
809796

810797
// attempt to overflow buffers, similar to previous bug in array buffers
811-
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
812-
RangeError);
813-
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
814-
RangeError);
815-
798+
common.expectsError(
799+
() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
800+
outOfBoundsError
801+
);
802+
common.expectsError(
803+
() => Buffer.allocUnsafe(8).writeFloatLE(0.0, 0xffffffff),
804+
outOfBoundsError
805+
);
816806

817807
// ensure negative values can't get past offset
818-
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
819-
assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
820-
808+
common.expectsError(
809+
() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1),
810+
outOfBoundsError
811+
);
812+
common.expectsError(
813+
() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1),
814+
outOfBoundsError
815+
);
821816

822817
// test for common write(U)IntLE/BE
823818
{
@@ -1010,10 +1005,7 @@ common.expectsError(() => {
10101005
const a = Buffer.alloc(1);
10111006
const b = Buffer.alloc(1);
10121007
a.copy(b, 0, 0x100000000, 0x100000001);
1013-
}, {
1014-
code: 'ERR_OUT_OF_RANGE',
1015-
type: RangeError
1016-
});
1008+
}, outOfBoundsError);
10171009

10181010
// Unpooled buffer (replaces SlowBuffer)
10191011
{

0 commit comments

Comments
 (0)