From c300f81c4ac1a7f86e62e924d3489fb6e2a3a530 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 16 Feb 2018 00:17:47 -0800 Subject: [PATCH] Fix error messages for invalid sizes (test-buffer-slow.js) --- index.js | 4 ++-- test/node/test-buffer-slow.js | 40 ++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 1c783ef..8fdb388 100644 --- a/index.js +++ b/index.js @@ -69,7 +69,7 @@ Object.defineProperty(Buffer.prototype, 'offset', { function createBuffer (length) { if (length > K_MAX_LENGTH) { - throw new RangeError('Invalid typed array length') + throw new RangeError('The value "' + length + '" is invalid for option "size"') } // Return an augmented `Uint8Array` instance var buf = new Uint8Array(length) @@ -153,7 +153,7 @@ function assertSize (size) { if (typeof size !== 'number') { throw new TypeError('"size" argument must be of type number') } else if (size < 0) { - throw new RangeError('"size" argument must not be negative') + throw new RangeError('The value "' + size + '" is invalid for option "size"') } } diff --git a/test/node/test-buffer-slow.js b/test/node/test-buffer-slow.js index 65cea21..4d73993 100644 --- a/test/node/test-buffer-slow.js +++ b/test/node/test-buffer-slow.js @@ -1,21 +1,19 @@ 'use strict'; var Buffer = require('../../').Buffer; +const common = require('./common'); +const assert = require('assert'); +const buffer = require('../../'); +const SlowBuffer = buffer.SlowBuffer; - -var assert = require('assert'); -var buffer = require('../../'); -var Buffer = buffer.Buffer; -var SlowBuffer = buffer.SlowBuffer; - -var ones = [1, 1, 1, 1]; +const ones = [1, 1, 1, 1]; // should create a Buffer -var sb = SlowBuffer(4); +let sb = SlowBuffer(4); assert(sb instanceof Buffer); assert.strictEqual(sb.length, 4); sb.fill(1); -for (var [key, value] of sb.entries()) { +for (const [key, value] of sb.entries()) { assert.deepStrictEqual(value, ones[key]); } @@ -27,7 +25,7 @@ sb = SlowBuffer(4); assert(sb instanceof Buffer); assert.strictEqual(sb.length, 4); sb.fill(1); -for (var [key, value] of sb.entries()) { +for (const [key, value] of sb.entries()) { assert.deepStrictEqual(value, ones[key]); } @@ -37,7 +35,9 @@ try { assert.strictEqual( SlowBuffer(buffer.kMaxLength).length, buffer.kMaxLength); } catch (e) { - assert.equal(e.message, 'Array buffer allocation failed'); + // Don't match on message as it is from the JavaScript engine. V8 and + // ChakraCore provide different messages. + assert.strictEqual(e.name, 'RangeError'); } // should work with number-coercible values @@ -51,13 +51,23 @@ assert.strictEqual(SlowBuffer({}).length, 0); assert.strictEqual(SlowBuffer('string').length, 0); // should throw with invalid length +const bufferMaxSizeMsg = common.expectsError({ + code: 'ERR_INVALID_OPT_VALUE', + type: RangeError, + message: /^The value "[^"]*" is invalid for option "size"$/ +}, 2); assert.throws(function() { SlowBuffer(Infinity); -}, 'invalid Buffer length'); -assert.throws(function() { +}, bufferMaxSizeMsg); +common.expectsError(function() { SlowBuffer(-1); -}, 'invalid Buffer length'); +}, { + code: 'ERR_INVALID_OPT_VALUE', + type: RangeError, + message: 'The value "-1" is invalid for option "size"' +}); + assert.throws(function() { SlowBuffer(buffer.kMaxLength + 1); -}, 'invalid Buffer length'); +}, bufferMaxSizeMsg); -- 2.34.1