-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathtest-buffer-from.js
139 lines (120 loc) · 3.35 KB
/
test-buffer-from.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
var Buffer = require('../../').Buffer;
const common = require('./common');
const assert = require('assert');
const { deepStrictEqual, throws } = assert;
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
);
[
[{}, 'object'],
[new Boolean(true), 'boolean'],
[{ valueOf() { return null; } }, 'object'],
[{ valueOf() { return undefined; } }, 'object'],
[{ valueOf: null }, 'object'],
[Object.create(null), 'object']
].forEach(([input, actualType]) => {
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. Received ' +
`type ${actualType}`
});
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);
});
{
const u16 = new Uint16Array([0xffff]);
const b16 = Buffer.copyBytesFrom(u16);
u16[0] = 0;
assert.strictEqual(b16.length, 2);
assert.strictEqual(b16[0], 255);
assert.strictEqual(b16[1], 255);
}
{
const u16 = new Uint16Array([0, 0xffff]);
const b16 = Buffer.copyBytesFrom(u16, 1, 5);
u16[0] = 0xffff;
u16[1] = 0;
assert.strictEqual(b16.length, 2);
assert.strictEqual(b16[0], 255);
assert.strictEqual(b16[1], 255);
}
{
const u32 = new Uint32Array([0xffffffff]);
const b32 = Buffer.copyBytesFrom(u32);
u32[0] = 0;
assert.strictEqual(b32.length, 4);
assert.strictEqual(b32[0], 255);
assert.strictEqual(b32[1], 255);
assert.strictEqual(b32[2], 255);
assert.strictEqual(b32[3], 255);
}
assert.throws(() => {
Buffer.copyBytesFrom();
}, TypeError);
{
const dv = new DataView(new ArrayBuffer(1));
assert.throws(() => {
Buffer.copyBytesFrom(dv);
}, TypeError);
}
['', Symbol(), true, false, {}, [], () => {}, 1, 1n, null, undefined].forEach(
notTypedArray => assert.throws(() => {
Buffer.copyBytesFrom(notTypedArray);
}, TypeError)
);
['', Symbol(), true, false, {}, [], () => {}, 1n].forEach(notANumber =>
assert.throws(() => {
Buffer.copyBytesFrom(new Uint8Array(1), notANumber);
}, TypeError)
);
[-1, NaN, 1.1, -Infinity].forEach(outOfRange =>
assert.throws(() => {
Buffer.copyBytesFrom(new Uint8Array(1), outOfRange);
}, RangeError)
);
['', Symbol(), true, false, {}, [], () => {}, 1n].forEach(notANumber =>
assert.throws(() => {
Buffer.copyBytesFrom(new Uint8Array(1), 0, notANumber);
}, TypeError)
);
[-1, NaN, 1.1, -Infinity].forEach(outOfRange =>
assert.throws(() => {
Buffer.copyBytesFrom(new Uint8Array(1), 0, outOfRange);
}, RangeError)
);