From 3c87795a0099e66ea476fc1384de9d0ea1c4c57f Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 15 Feb 2018 23:52:19 -0800 Subject: [PATCH] Update buf.compare() error message (test-buffer-compare-offset.js) --- index.js | 5 ++- test/node/test-buffer-compare-offset.js | 60 ++++++++++++++----------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 8ab3fff..9d0f800 100644 --- a/index.js +++ b/index.js @@ -580,7 +580,10 @@ Buffer.prototype.inspect = function inspect () { Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { if (!Buffer.isBuffer(target)) { - throw new TypeError('Argument must be a Buffer') + throw new TypeError( + 'The "target" argument must be one of type Buffer or Uint8Array. ' + + 'Received type ' + (typeof target) + ) } if (start === undefined) { diff --git a/test/node/test-buffer-compare-offset.js b/test/node/test-buffer-compare-offset.js index f1984ed..f7a76d3 100644 --- a/test/node/test-buffer-compare-offset.js +++ b/test/node/test-buffer-compare-offset.js @@ -1,66 +1,76 @@ 'use strict'; var Buffer = require('../../').Buffer; +const common = require('./common'); +const assert = require('assert'); +const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); +const b = Buffer.from([5, 6, 7, 8, 9, 0, 1, 2, 3, 4]); -var assert = require('assert'); - -var a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); -var b = Buffer.from([5, 6, 7, 8, 9, 0, 1, 2, 3, 4]); - -assert.equal(-1, a.compare(b)); +assert.strictEqual(-1, a.compare(b)); // Equivalent to a.compare(b). -assert.equal(-1, a.compare(b, 0)); -assert.equal(-1, a.compare(b, '0')); +assert.strictEqual(-1, a.compare(b, 0)); +assert.strictEqual(-1, a.compare(b, '0')); +assert.strictEqual(-1, a.compare(b, undefined)); // Equivalent to a.compare(b). -assert.equal(-1, a.compare(b, 0, undefined, 0)); +assert.strictEqual(-1, a.compare(b, 0, undefined, 0)); -// Zero-length targer, return 1 -assert.equal(1, a.compare(b, 0, 0, 0)); -assert.equal(1, a.compare(b, '0', '0', '0')); +// Zero-length target, return 1 +assert.strictEqual(1, a.compare(b, 0, 0, 0)); +assert.strictEqual(1, a.compare(b, '0', '0', '0')); // Equivalent to Buffer.compare(a, b.slice(6, 10)) -assert.equal(1, a.compare(b, 6, 10)); +assert.strictEqual(1, a.compare(b, 6, 10)); // Zero-length source, return -1 -assert.equal(-1, a.compare(b, 6, 10, 0, 0)); +assert.strictEqual(-1, a.compare(b, 6, 10, 0, 0)); + +// Zero-length source and target, return 0 +assert.strictEqual(0, a.compare(b, 0, 0, 0, 0)); +assert.strictEqual(0, a.compare(b, 1, 1, 2, 2)); // Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5)) -assert.equal(1, a.compare(b, 0, 5, 4)); +assert.strictEqual(1, a.compare(b, 0, 5, 4)); // Equivalent to Buffer.compare(a.slice(1), b.slice(5)) -assert.equal(1, a.compare(b, 5, undefined, 1)); +assert.strictEqual(1, a.compare(b, 5, undefined, 1)); // Equivalent to Buffer.compare(a.slice(2), b.slice(2, 4)) -assert.equal(-1, a.compare(b, 2, 4, 2)); +assert.strictEqual(-1, a.compare(b, 2, 4, 2)); // Equivalent to Buffer.compare(a.slice(4), b.slice(0, 7)) -assert.equal(-1, a.compare(b, 0, 7, 4)); +assert.strictEqual(-1, a.compare(b, 0, 7, 4)); // Equivalent to Buffer.compare(a.slice(4, 6), b.slice(0, 7)); -assert.equal(-1, a.compare(b, 0, 7, 4, 6)); +assert.strictEqual(-1, a.compare(b, 0, 7, 4, 6)); // zero length target -assert.equal(1, a.compare(b, 0, null)); +assert.strictEqual(1, a.compare(b, 0, null)); // coerces to targetEnd == 5 -assert.equal(-1, a.compare(b, 0, {valueOf: () => 5})); +assert.strictEqual(-1, a.compare(b, 0, { valueOf: () => 5 })); // zero length target -assert.equal(1, a.compare(b, Infinity, -Infinity)); +assert.strictEqual(1, a.compare(b, Infinity, -Infinity)); // zero length target because default for targetEnd <= targetSource -assert.equal(1, a.compare(b, '0xff')); +assert.strictEqual(1, a.compare(b, '0xff')); -var oor = /out of range index/; +const oor = common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }, 7); assert.throws(() => a.compare(b, 0, 100, 0), oor); assert.throws(() => a.compare(b, 0, 1, 0, 100), oor); assert.throws(() => a.compare(b, -1), oor); assert.throws(() => a.compare(b, 0, '0xff'), oor); assert.throws(() => a.compare(b, 0, Infinity), oor); +assert.throws(() => a.compare(b, 0, 1, -1), oor); assert.throws(() => a.compare(b, -Infinity, Infinity), oor); -assert.throws(() => a.compare(), /Argument must be a Buffer/); +common.expectsError(() => a.compare(), { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "target" argument must be one of ' + + 'type Buffer or Uint8Array. Received type undefined' +}); -- 2.34.1