Skip to content

Commit

Permalink
test: complete coverage of buffer
Browse files Browse the repository at this point in the history
PR-URL: #12831
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Alexey Orlenko <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
DavidCai1111 authored and addaleax committed May 7, 2017
1 parent 7e5f500 commit a710e44
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
9 changes: 9 additions & 0 deletions test/parallel/test-buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
13 changes: 11 additions & 2 deletions test/parallel/test-buffer-tostring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()), [])
Expand All @@ -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);
}
9 changes: 9 additions & 0 deletions test/parallel/test-buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.