Skip to content

Commit 537d954

Browse files
hiroppyitaloacasas
authored andcommitted
test: increase coverage of string-decoder
Make use of Arrow Function. Add normalizeencoding's test. normalizeEncoding: https://github.com/nodejs/node/blob/master/lib/string_decoder.js#L9 PR-URL: #10863 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 659428f commit 537d954

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

test/parallel/test-string-decoder-end.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ const assert = require('assert');
88
const SD = require('string_decoder').StringDecoder;
99
const encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];
1010

11-
const bufs = [ '☃💩', 'asdf' ].map(function(b) {
12-
return Buffer.from(b);
13-
});
11+
const bufs = [ '☃💩', 'asdf' ].map((b) => Buffer.from(b));
1412

1513
// also test just arbitrary bytes from 0-15.
1614
for (let i = 1; i <= 16; i++) {
17-
const bytes = new Array(i).join('.').split('.').map(function(_, j) {
18-
return j + 0x78;
19-
});
15+
const bytes = new Array(i).join('.').split('.').map((_, j) => j + 0x78);
2016
bufs.push(Buffer.from(bytes));
2117
}
2218

@@ -25,7 +21,7 @@ encodings.forEach(testEncoding);
2521
console.log('ok');
2622

2723
function testEncoding(encoding) {
28-
bufs.forEach(function(buf) {
24+
bufs.forEach((buf) => {
2925
testBuf(encoding, buf);
3026
});
3127
}

test/parallel/test-string-decoder.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
104104
assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
105105
assert.strictEqual(decoder.end(), '\ud83d');
106106

107+
assert.throws(() => {
108+
new StringDecoder(1);
109+
}, /^Error: Unknown encoding: 1$/);
110+
111+
assert.throws(() => {
112+
new StringDecoder('test');
113+
}, /^Error: Unknown encoding: test$/);
114+
107115
// test verifies that StringDecoder will correctly decode the given input
108116
// buffer with the given encoding to the expected output. It will attempt all
109117
// possible ways to write() the input buffer, see writeSequences(). The
@@ -116,10 +124,10 @@ function test(encoding, input, expected, singleSequence) {
116124
} else {
117125
sequences = [singleSequence];
118126
}
119-
sequences.forEach(function(sequence) {
127+
sequences.forEach((sequence) => {
120128
const decoder = new StringDecoder(encoding);
121129
let output = '';
122-
sequence.forEach(function(write) {
130+
sequence.forEach((write) => {
123131
output += decoder.write(input.slice(write[0], write[1]));
124132
});
125133
output += decoder.end();

0 commit comments

Comments
 (0)