Skip to content
Permalink
Browse files

test: complete coverage of buffer

PR-URL: #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>
  • Loading branch information...
DavidCai1993 authored and addaleax committed May 5, 2017
1 parent 7e5f500 commit a710e443a28552dece3453ab7b7b8306489ecf85
@@ -106,3 +106,12 @@ assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14);
// Test that ArrayBuffer from a different context is detected correctly
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
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');

// utf8, ucs2, ascii, latin1, utf16le
const encodings = ['utf8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary',
'utf16le', 'utf-16le'];
const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
'binary', 'utf16le', 'utf-16le'];

encodings
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
@@ -23,3 +23,12 @@ encodings
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
'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.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);
}

0 comments on commit a710e44

Please sign in to comment.
You can’t perform that action at this time.