test: complete coverage of buffer
PR-URL: https://github.com/nodejs/node/pull/12831 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
7e5f500c98
commit
a710e443a2
@ -106,3 +106,12 @@ assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14);
|
|||||||
// Test that ArrayBuffer from a different context is detected correctly
|
// Test that ArrayBuffer from a different context is detected correctly
|
||||||
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
|
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
|
||||||
assert.strictEqual(Buffer.byteLength(arrayBuf), 0);
|
assert.strictEqual(Buffer.byteLength(arrayBuf), 0);
|
||||||
|
|
||||||
|
// Verify that invalid encodings are treated as utf8
|
||||||
|
for (let i = 1; i < 10; i++) {
|
||||||
|
const encoding = String(i).repeat(i);
|
||||||
|
|
||||||
|
assert.ok(!Buffer.isEncoding(encoding));
|
||||||
|
assert.strictEqual(Buffer.byteLength('foo', encoding),
|
||||||
|
Buffer.byteLength('foo', 'utf8'));
|
||||||
|
}
|
||||||
|
@ -4,8 +4,8 @@ require('../common');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
// utf8, ucs2, ascii, latin1, utf16le
|
// utf8, ucs2, ascii, latin1, utf16le
|
||||||
const encodings = ['utf8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary',
|
const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
|
||||||
'utf16le', 'utf-16le'];
|
'binary', 'utf16le', 'utf-16le'];
|
||||||
|
|
||||||
encodings
|
encodings
|
||||||
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
|
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
|
||||||
@ -23,3 +23,12 @@ encodings
|
|||||||
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
|
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
|
||||||
'666f6f');
|
'666f6f');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Invalid encodings
|
||||||
|
for (let i = 1; i < 10; i++) {
|
||||||
|
const encoding = String(i).repeat(i);
|
||||||
|
const error = new RegExp(`^TypeError: Unknown encoding: ${encoding}$`);
|
||||||
|
|
||||||
|
assert.ok(!Buffer.isEncoding(encoding));
|
||||||
|
assert.throws(() => Buffer.from('foo').toString(encoding), error);
|
||||||
|
}
|
||||||
|
@ -53,3 +53,12 @@ encodings
|
|||||||
assert.strictEqual(buf.write('666f6f', 0, len, encoding), len);
|
assert.strictEqual(buf.write('666f6f', 0, len, encoding), len);
|
||||||
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
|
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Invalid encodings
|
||||||
|
for (let i = 1; i < 10; i++) {
|
||||||
|
const encoding = String(i).repeat(i);
|
||||||
|
const error = new RegExp(`^TypeError: Unknown encoding: ${encoding}$`);
|
||||||
|
|
||||||
|
assert.ok(!Buffer.isEncoding(encoding));
|
||||||
|
assert.throws(() => Buffer.alloc(9).write('foo', encoding), error);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user