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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
0c58193d1b
commit
492163c74c
@ -8,15 +8,11 @@ const assert = require('assert');
|
|||||||
const SD = require('string_decoder').StringDecoder;
|
const SD = require('string_decoder').StringDecoder;
|
||||||
const encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];
|
const encodings = ['base64', 'hex', 'utf8', 'utf16le', 'ucs2'];
|
||||||
|
|
||||||
const bufs = [ '☃💩', 'asdf' ].map(function(b) {
|
const bufs = [ '☃💩', 'asdf' ].map((b) => Buffer.from(b));
|
||||||
return Buffer.from(b);
|
|
||||||
});
|
|
||||||
|
|
||||||
// also test just arbitrary bytes from 0-15.
|
// also test just arbitrary bytes from 0-15.
|
||||||
for (let i = 1; i <= 16; i++) {
|
for (let i = 1; i <= 16; i++) {
|
||||||
const bytes = new Array(i).join('.').split('.').map(function(_, j) {
|
const bytes = new Array(i).join('.').split('.').map((_, j) => j + 0x78);
|
||||||
return j + 0x78;
|
|
||||||
});
|
|
||||||
bufs.push(Buffer.from(bytes));
|
bufs.push(Buffer.from(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,7 +21,7 @@ encodings.forEach(testEncoding);
|
|||||||
console.log('ok');
|
console.log('ok');
|
||||||
|
|
||||||
function testEncoding(encoding) {
|
function testEncoding(encoding) {
|
||||||
bufs.forEach(function(buf) {
|
bufs.forEach((buf) => {
|
||||||
testBuf(encoding, buf);
|
testBuf(encoding, buf);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,14 @@ assert.strictEqual(decoder.write(Buffer.from('3DD8', 'hex')), '');
|
|||||||
assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
|
assert.strictEqual(decoder.write(Buffer.from('4D', 'hex')), '');
|
||||||
assert.strictEqual(decoder.end(), '\ud83d');
|
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
|
// test verifies that StringDecoder will correctly decode the given input
|
||||||
// buffer with the given encoding to the expected output. It will attempt all
|
// buffer with the given encoding to the expected output. It will attempt all
|
||||||
// possible ways to write() the input buffer, see writeSequences(). The
|
// possible ways to write() the input buffer, see writeSequences(). The
|
||||||
@ -116,10 +124,10 @@ function test(encoding, input, expected, singleSequence) {
|
|||||||
} else {
|
} else {
|
||||||
sequences = [singleSequence];
|
sequences = [singleSequence];
|
||||||
}
|
}
|
||||||
sequences.forEach(function(sequence) {
|
sequences.forEach((sequence) => {
|
||||||
const decoder = new StringDecoder(encoding);
|
const decoder = new StringDecoder(encoding);
|
||||||
let output = '';
|
let output = '';
|
||||||
sequence.forEach(function(write) {
|
sequence.forEach((write) => {
|
||||||
output += decoder.write(input.slice(write[0], write[1]));
|
output += decoder.write(input.slice(write[0], write[1]));
|
||||||
});
|
});
|
||||||
output += decoder.end();
|
output += decoder.end();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user