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)
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"')
}
}
'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]);
}
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]);
}
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
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);