Skip to content

Commit

Permalink
test: updating error message
Browse files Browse the repository at this point in the history
  • Loading branch information
larissayvette authored and Trott committed May 28, 2017
1 parent ee453f3 commit c12569a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 39 deletions.
2 changes: 1 addition & 1 deletion doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ found [here][online].
<a id="ERR_INDEX_OUT_OF_RANGE"></a>
### ERR_INDEX_OUT_OF_RANGE

The `'ERR_INDEX_OUT_OF_RANGE'` error code is used to alert user that index parsed is out of range
The `'ERR_INDEX_OUT_OF_RANGE'` error code is used when a given index is out of the accepted range.


<a id="nodejs-error-codes"></a>
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,8 @@ assert.throws(() => {
const a = Buffer.alloc(1);
const b = Buffer.alloc(1);
a.copy(b, 0, 0x100000000, 0x100000001);
}, /out of range index/);
}, common.expectsError(
{code: undefined, type: RangeError, message: 'Index out of range'}));

// Unpooled buffer (replaces SlowBuffer)
{
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-buffer-compare-offset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');

const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
Expand Down Expand Up @@ -57,7 +57,7 @@ assert.strictEqual(1, a.compare(b, Infinity, -Infinity));
// zero length target because default for targetEnd <= targetSource
assert.strictEqual(1, a.compare(b, '0xff'));

const oor = /out of range index/;
const oor = common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'});

assert.throws(() => a.compare(b, 0, 100, 0), oor);
assert.throws(() => a.compare(b, 0, 1, 0, 100), oor);
Expand Down
77 changes: 44 additions & 33 deletions test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Flags: --expose-internals
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const errors = require('internal/errors');
const SIZE = 28;

const buf1 = Buffer.allocUnsafe(SIZE);
Expand Down Expand Up @@ -191,25 +192,30 @@ deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);


// Check exceptions
assert.throws(() => buf1.fill(0, -1), /^RangeError: Out of range index$/);
assert.throws(() =>
buf1.fill(0, 0, buf1.length + 1),
/^RangeError: Out of range index$/);
assert.throws(() => buf1.fill('', -1), /^RangeError: Out of range index$/);
assert.throws(() =>
buf1.fill('', 0, buf1.length + 1),
/^RangeError: Out of range index$/);
assert.throws(() =>
buf1.fill('a', 0, buf1.length, 'node rocks!'),
/^TypeError: Unknown encoding: node rocks!$/);
assert.throws(() =>
buf1.fill('a', 0, 0, NaN),
/^TypeError: encoding must be a string$/);
assert.throws(() =>
buf1.fill('a', 0, 0, null),
/^TypeError: encoding must be a string$/);
assert.throws(() =>
buf1.fill('a', 0, 0, 'foo'), /^TypeError: Unknown encoding: foo$/);
assert.throws(
() => buf1.fill(0, -1),
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
assert.throws(
() => buf1.fill(0, 0, buf1.length + 1),
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
assert.throws(
() => buf1.fill('', -1),
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
assert.throws(
() => buf1.fill('', 0, buf1.length + 1),
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
assert.throws(
() => buf1.fill('a', 0, buf1.length, 'node rocks!'),
/^TypeError: Unknown encoding: node rocks!$/);
assert.throws(
() => buf1.fill('a', 0, 0, NaN),
/^TypeError: encoding must be a string$/);
assert.throws(
() => buf1.fill('a', 0, 0, null),
/^TypeError: encoding must be a string$/);
assert.throws(
() => buf1.fill('a', 0, 0, 'foo'),
/^TypeError: Unknown encoding: foo$/);


function genBuffer(size, args) {
Expand Down Expand Up @@ -241,7 +247,7 @@ function writeToFill(string, offset, end, encoding) {
}

if (offset < 0 || end > buf2.length)
throw new RangeError('Out of range index');
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');

if (end <= offset)
return buf2;
Expand Down Expand Up @@ -279,12 +285,12 @@ function testBufs(string, offset, length, encoding) {
}

// Make sure these throw.
assert.throws(() =>
Buffer.allocUnsafe(8).fill('a', -1),
/^RangeError: Out of range index$/);
assert.throws(() =>
Buffer.allocUnsafe(8).fill('a', 0, 9),
/^RangeError: Out of range index$/);
assert.throws(
() => Buffer.allocUnsafe(8).fill('a', -1),
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
assert.throws(
() => Buffer.allocUnsafe(8).fill('a', 0, 9),
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));

// Make sure this doesn't hang indefinitely.
Buffer.allocUnsafe(8).fill('');
Expand Down Expand Up @@ -350,7 +356,8 @@ Buffer.alloc(8, '');
}
};
Buffer.alloc(1).fill(Buffer.alloc(1), start, 1);
}, /out of range index/);
}, common.expectsError(
{code: undefined, type: RangeError, message: 'Index out of range'}));
// Make sure -1 is making it to Buffer::Fill().
assert.ok(elseWasLast,
'internal API changed, -1 no longer in correct location');
Expand All @@ -360,7 +367,8 @@ Buffer.alloc(8, '');
// around.
assert.throws(() => {
process.binding('buffer').fill(Buffer.alloc(1), 1, -1, 0, 1);
}, /out of range index/);
}, common.expectsError(
{code: undefined, type: RangeError, message: 'Index out of range'}));

// Make sure "end" is properly checked, even if it's magically mangled using
// Symbol.toPrimitive.
Expand All @@ -383,7 +391,8 @@ assert.throws(() => {
}
};
Buffer.alloc(1).fill(Buffer.alloc(1), 0, end);
}, /^RangeError: out of range index$/);
}, common.expectsError(
{code: undefined, type: RangeError, message: 'Index out of range'}));
// Make sure -1 is making it to Buffer::Fill().
assert.ok(elseWasLast,
'internal API changed, -1 no longer in correct location');
Expand All @@ -393,7 +402,8 @@ assert.throws(() => {
// around.
assert.throws(() => {
process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1);
}, /out of range index/);
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range'}));

// Test that bypassing 'length' won't cause an abort.
assert.throws(() => {
Expand All @@ -403,7 +413,8 @@ assert.throws(() => {
enumerable: true
});
buf.fill('');
}, /^RangeError: out of range index$/);
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range'}));

assert.deepStrictEqual(
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function read(buff, funx, args, expected) {
assert.strictEqual(buff[funx](...args), expected);
assert.throws(
() => buff[funx](-1),
common.expectsError('ERR_INDEX_OUT_OF_RANGE')
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})
);

assert.doesNotThrow(
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-write-noassert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert');

// testing buffer write functions

const outOfRange = /^RangeError: (?:Index )?out of range(?: index)?$/;
const outOfRange = /^RangeError\b.*\bIndex out of range$/;

function write(funx, args, result, res) {
{
Expand Down

0 comments on commit c12569a

Please sign in to comment.