Skip to content

Commit

Permalink
test: increase coverage for buffer.js
Browse files Browse the repository at this point in the history
Add coverage for non-numeric byteOffset and length when using
Buffer.from() with an ArrayBuffer.

PR-URL: #12476
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
Trott authored and evanlucas committed May 2, 2017
1 parent 38278db commit a75fbe0
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/parallel/test-buffer-arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,41 @@ b.writeDoubleBE(11.11, 0, true);
return true;
});
}

{
// If byteOffset is not numeric, it defaults to 0.
const ab = new ArrayBuffer(10);
const expected = Buffer.from(ab, 0);
assert.deepStrictEqual(Buffer.from(ab, 'fhqwhgads'), expected);
assert.deepStrictEqual(Buffer.from(ab, NaN), expected);
assert.deepStrictEqual(Buffer.from(ab, {}), expected);
assert.deepStrictEqual(Buffer.from(ab, []), expected);

// If byteOffset can be converted to a number, it will be.
assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1));

// If byteOffset is Infinity, throw.
assert.throws(
() => { Buffer.from(ab, Infinity); },
/^RangeError: 'offset' is out of bounds$/
);
}

{
// If length is not numeric, it defaults to 0.
const ab = new ArrayBuffer(10);
const expected = Buffer.from(ab, 0, 0);
assert.deepStrictEqual(Buffer.from(ab, 0, 'fhqwhgads'), expected);
assert.deepStrictEqual(Buffer.from(ab, 0, NaN), expected);
assert.deepStrictEqual(Buffer.from(ab, 0, {}), expected);
assert.deepStrictEqual(Buffer.from(ab, 0, []), expected);

// If length can be converted to a number, it will be.
assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1));

//If length is Infinity, throw.
assert.throws(
() => { Buffer.from(ab, 0, Infinity); },
/^RangeError: 'length' is out of bounds$/
);
}

0 comments on commit a75fbe0

Please sign in to comment.