Skip to content

Commit 9ac22cd

Browse files
hiroppyMylesBorins
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 cda5937 commit 9ac22cd

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ var assert = require('assert');
88
var SD = require('string_decoder').StringDecoder;
99
var encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];
1010

11-
var 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.
16-
for (var i = 1; i <= 16; i++) {
17-
var bytes = new Array(i).join('.').split('.').map(function(_, j) {
18-
return j + 0x78;
19-
});
14+
for (let i = 1; i <= 16; i++) {
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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
107107
assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
108108
assert.strictEqual(decoder.end(), '\ud83d');
109109

110+
assert.throws(() => {
111+
new StringDecoder(1);
112+
}, /^Error: Unknown encoding: 1$/);
113+
114+
assert.throws(() => {
115+
new StringDecoder('test');
116+
}, /^Error: Unknown encoding: test$/);
117+
110118
// test verifies that StringDecoder will correctly decode the given input
111119
// buffer with the given encoding to the expected output. It will attempt all
112120
// possible ways to write() the input buffer, see writeSequences(). The
@@ -119,10 +127,10 @@ function test(encoding, input, expected, singleSequence) {
119127
} else {
120128
sequences = [singleSequence];
121129
}
122-
sequences.forEach(function(sequence) {
123-
var decoder = new StringDecoder(encoding);
124-
var output = '';
125-
sequence.forEach(function(write) {
130+
sequences.forEach((sequence) => {
131+
const decoder = new StringDecoder(encoding);
132+
let output = '';
133+
sequence.forEach((write) => {
126134
output += decoder.write(input.slice(write[0], write[1]));
127135
});
128136
output += decoder.end();

0 commit comments

Comments
 (0)