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 <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent cda5937 commit 9ac22cd

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎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
varSD=require('string_decoder').StringDecoder;
99
varencodings=['base64','hex','utf8','utf16le','ucs2'];
1010

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

1513
// also test just arbitrary bytes from 0-15.
16-
for(vari=1;i<=16;i++){
17-
varbytes=newArray(i).join('.').split('.').map(function(_,j){
18-
returnj+0x78;
19-
});
14+
for(leti=1;i<=16;i++){
15+
constbytes=newArray(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
functiontestEncoding(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+
newStringDecoder(1);
112+
},/^Error:Unknownencoding:1$/);
113+
114+
assert.throws(()=>{
115+
newStringDecoder('test');
116+
},/^Error:Unknownencoding: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-
vardecoder=newStringDecoder(encoding);
124-
varoutput='';
125-
sequence.forEach(function(write){
130+
sequences.forEach((sequence)=>{
131+
constdecoder=newStringDecoder(encoding);
132+
letoutput='';
133+
sequence.forEach((write)=>{
126134
output+=decoder.write(input.slice(write[0],write[1]));
127135
});
128136
output+=decoder.end();

0 commit comments

Comments
 (0)