Skip to content

Commit

Permalink
test: increase coverage of string-decoder
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
hiroppy authored and jasnell committed Mar 8, 2017
1 parent 60785e6 commit 3a6fd1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
12 changes: 4 additions & 8 deletions test/parallel/test-string-decoder-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ var assert = require('assert');
var SD = require('string_decoder').StringDecoder;
var encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];

var bufs = [ '☃💩', 'asdf' ].map(function(b) {
return Buffer.from(b);
});
const bufs = [ '☃💩', 'asdf' ].map((b) => Buffer.from(b));

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

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

function testEncoding(encoding) {
bufs.forEach(function(buf) {
bufs.forEach((buf) => {
testBuf(encoding, buf);
});
}
Expand Down
16 changes: 12 additions & 4 deletions test/parallel/test-string-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
assert.strictEqual(decoder.end(), '\ud83d');

assert.throws(() => {
new StringDecoder(1);
}, /^Error: Unknown encoding: 1$/);

assert.throws(() => {
new StringDecoder('test');
}, /^Error: Unknown encoding: test$/);

// test verifies that StringDecoder will correctly decode the given input
// buffer with the given encoding to the expected output. It will attempt all
// possible ways to write() the input buffer, see writeSequences(). The
Expand All @@ -119,10 +127,10 @@ function test(encoding, input, expected, singleSequence) {
} else {
sequences = [singleSequence];
}
sequences.forEach(function(sequence) {
var decoder = new StringDecoder(encoding);
var output = '';
sequence.forEach(function(write) {
sequences.forEach((sequence) => {
const decoder = new StringDecoder(encoding);
let output = '';
sequence.forEach((write) => {
output += decoder.write(input.slice(write[0], write[1]));
});
output += decoder.end();
Expand Down

0 comments on commit 3a6fd1d

Please sign in to comment.