0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-buffer-from.js
starkwang dbfe8c4ea2
errors,buffer: port errors to internal/errors
PR-URL: https://github.com/nodejs/node/pull/13976
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2017-07-12 17:00:30 -04:00

66 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
const { deepStrictEqual, throws } = require('assert');
const { Buffer } = require('buffer');
const { runInNewContext } = require('vm');
const checkString = 'test';
const check = Buffer.from(checkString);
class MyString extends String {
constructor() {
super(checkString);
}
}
class MyPrimitive {
[Symbol.toPrimitive]() {
return checkString;
}
}
class MyBadPrimitive {
[Symbol.toPrimitive]() {
return 1;
}
}
deepStrictEqual(Buffer.from(new String(checkString)), check);
deepStrictEqual(Buffer.from(new MyString()), check);
deepStrictEqual(Buffer.from(new MyPrimitive()), check);
deepStrictEqual(Buffer.from(
runInNewContext('new String(checkString)', {checkString})),
check);
[
{},
new Boolean(true),
{ valueOf() { return null; } },
{ valueOf() { return undefined; } },
{ valueOf: null },
Object.create(null)
].forEach((input) => {
const err = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The first argument must be one of type string, buffer, ' +
'arrayBuffer, array, or array-like object'
});
throws(() => Buffer.from(input), err);
});
[
new Number(true),
new MyBadPrimitive()
].forEach((input) => {
const errMsg = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "value" argument must not be of type number. ' +
'Received type number'
});
throws(() => Buffer.from(input), errMsg);
});