Skip to content

Commit 04796ee

Browse files
committed
test: increase coverage of buffer
Increase coverage of lib/buffer.js. PR-URL: #12714 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 133fb0c commit 04796ee

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

test/parallel/test-buffer-bytelength.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ assert.throws(() => { Buffer.byteLength({}, 'latin1'); },
1616
assert.throws(() => { Buffer.byteLength(); },
1717
/"string" must be a string, Buffer, or ArrayBuffer/);
1818

19+
assert.strictEqual(Buffer.byteLength('', undefined, true), -1);
20+
1921
assert(ArrayBuffer.isView(new Buffer(10)));
2022
assert(ArrayBuffer.isView(new SlowBuffer(10)));
2123
assert(ArrayBuffer.isView(Buffer.alloc(10)));
@@ -76,6 +78,7 @@ assert.strictEqual(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10);
7678

7779
// base64
7880
assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11);
81+
assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'BASE64'), 11);
7982
assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14);
8083
assert.strictEqual(Buffer.byteLength('aGkk', 'base64'), 3);
8184
assert.strictEqual(
@@ -87,12 +90,18 @@ assert.strictEqual(Buffer.byteLength('aaaa==', 'base64'), 3);
8790

8891
assert.strictEqual(Buffer.byteLength('Il était tué'), 14);
8992
assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14);
90-
assert.strictEqual(Buffer.byteLength('Il était tué', 'ascii'), 12);
91-
assert.strictEqual(Buffer.byteLength('Il était tué', 'latin1'), 12);
92-
assert.strictEqual(Buffer.byteLength('Il était tué', 'binary'), 12);
93-
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
94-
assert.strictEqual(24, Buffer.byteLength('Il était tué', encoding));
95-
});
93+
94+
['ascii', 'latin1', 'binary']
95+
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
96+
.forEach((encoding) => {
97+
assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12);
98+
});
99+
100+
['ucs2', 'ucs-2', 'utf16le', 'utf-16le']
101+
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
102+
.forEach((encoding) => {
103+
assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24);
104+
});
96105

97106
// Test that ArrayBuffer from a different context is detected correctly
98107
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');

test/parallel/test-buffer-tostring.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
// utf8, ucs2, ascii, latin1, utf16le
7+
const encodings = ['utf8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary',
8+
'utf16le', 'utf-16le'];
9+
10+
encodings
11+
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
12+
.forEach((encoding) => {
13+
assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo');
14+
});
15+
16+
// base64
17+
['base64', 'BASE64'].forEach((encoding) => {
18+
assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v');
19+
});
20+
21+
// hex
22+
['hex', 'HEX'].forEach((encoding) => {
23+
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
24+
'666f6f');
25+
});

test/parallel/test-buffer-write.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
const outsideBounds = /^RangeError: Attempt to write outside buffer bounds$/;
7+
8+
assert.throws(() => Buffer.alloc(9).write('foo', -1), outsideBounds);
9+
assert.throws(() => Buffer.alloc(9).write('foo', 10), outsideBounds);
10+
11+
const resultMap = new Map([
12+
['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
13+
['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
14+
['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
15+
['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
16+
['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
17+
['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
18+
['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
19+
['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])]
20+
]);
21+
22+
// utf8, ucs2, ascii, latin1, utf16le
23+
const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
24+
'binary', 'utf16le', 'utf-16le'];
25+
26+
encodings
27+
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
28+
.forEach((encoding) => {
29+
const buf = Buffer.alloc(9);
30+
const len = Buffer.byteLength('foo', encoding);
31+
assert.strictEqual(buf.write('foo', 0, len, encoding), len);
32+
33+
if (encoding.indexOf('-') !== -1)
34+
encoding = encoding.replace('-', '');
35+
36+
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
37+
});
38+
39+
// base64
40+
['base64', 'BASE64'].forEach((encoding) => {
41+
const buf = Buffer.alloc(9);
42+
const len = Buffer.byteLength('Zm9v', encoding);
43+
44+
assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len);
45+
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
46+
});
47+
48+
// hex
49+
['hex', 'HEX'].forEach((encoding) => {
50+
const buf = Buffer.alloc(9);
51+
const len = Buffer.byteLength('666f6f', encoding);
52+
53+
assert.strictEqual(buf.write('666f6f', 0, len, encoding), len);
54+
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
55+
});

0 commit comments

Comments
 (0)