0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-25 08:19:38 +01:00

test: split test-whatwg-encoding-textdecoder.js

Split test-whatwg-encoding-textdecoder.js into:

- `test-whatwg-encoding-custom-textdecoder.js` which tests
  Node.js-specific behaviors
- `test-whatwg-encoding-custom-textdecoder-api-invalid-label.js` which
  is a customized version of the WPT counterpart
- `test-whatwg-encoding-custom-api-basics.js` which is the part of
  `test-whatwg-encoding-api-basics.js` that can be run without ICU
- `test-whatwg-encoding-api-basics.js` which can be replaced with WPT
  later.

PR-URL: https://github.com/nodejs/node/pull/25155
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Joyee Cheung 2018-12-20 14:20:31 +08:00
parent 8a60be4f0f
commit 74a9221f6f
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
4 changed files with 178 additions and 102 deletions

View File

@ -0,0 +1,74 @@
'use strict';
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-basics.html
// TODO(joyeecheung): replace this with WPT
const common = require('../common');
if (!common.hasIntl)
common.skip('missing Intl');
const assert = require('assert');
function testDecodeSample(encoding, string, bytes) {
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
string);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
string);
}
// `z` (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
// byte-swapped BOM (non-character U+FFFE)
const sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
{
const encoding = 'utf-8';
const string = sample;
const bytes = [
0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4,
0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF,
0xBF, 0xBE
];
const encoded = new TextEncoder().encode(string);
assert.deepStrictEqual([].slice.call(encoded), bytes);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
string);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
string);
}
testDecodeSample(
'utf-16le',
sample,
[
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
]
);
testDecodeSample(
'utf-16be',
sample,
[
0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34,
0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF,
0xDB, 0xFF, 0xDF, 0xFD, 0xFF, 0xFE
]
);
testDecodeSample(
'utf-16',
sample,
[
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
]
);

View File

@ -0,0 +1,61 @@
'use strict';
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-basics.html
// This is the part that can be run without ICU
require('../common');
const assert = require('assert');
function testDecodeSample(encoding, string, bytes) {
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
string);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
string);
}
// `z` (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
// byte-swapped BOM (non-character U+FFFE)
const sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
{
const encoding = 'utf-8';
const string = sample;
const bytes = [
0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4,
0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF,
0xBF, 0xBE
];
const encoded = new TextEncoder().encode(string);
assert.deepStrictEqual([].slice.call(encoded), bytes);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
string);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
string);
}
testDecodeSample(
'utf-16le',
sample,
[
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
]
);
testDecodeSample(
'utf-16',
sample,
[
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
]
);

View File

@ -0,0 +1,40 @@
'use strict';
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-invalid-label.html
// With the twist that we specifically test for Node.js error codes
const common = require('../common');
[
'utf-8',
'unicode-1-1-utf-8',
'utf8',
'utf-16be',
'utf-16le',
'utf-16'
].forEach((i) => {
['\u0000', '\u000b', '\u00a0', '\u2028', '\u2029'].forEach((ws) => {
common.expectsError(
() => new TextDecoder(`${ws}${i}`),
{
code: 'ERR_ENCODING_NOT_SUPPORTED',
type: RangeError
}
);
common.expectsError(
() => new TextDecoder(`${i}${ws}`),
{
code: 'ERR_ENCODING_NOT_SUPPORTED',
type: RangeError
}
);
common.expectsError(
() => new TextDecoder(`${ws}${i}${ws}`),
{
code: 'ERR_ENCODING_NOT_SUPPORTED',
type: RangeError
}
);
});
});

View File

@ -1,4 +1,7 @@
// Flags: --expose-internals // Flags: --expose-internals
// This tests Node.js-specific behaviors of TextDecoder
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
@ -167,72 +170,6 @@ if (common.hasIntl) {
}); });
} }
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-basics.html
function testDecodeSample(encoding, string, bytes) {
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
string);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
string);
}
// `z` (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
// byte-swapped BOM (non-character U+FFFE)
const sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
{
const encoding = 'utf-8';
const string = sample;
const bytes = [
0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4,
0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF,
0xBF, 0xBE
];
const encoded = new TextEncoder().encode(string);
assert.deepStrictEqual([].slice.call(encoded), bytes);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
string);
assert.strictEqual(
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
string);
}
testDecodeSample(
'utf-16le',
sample,
[
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
]
);
if (common.hasIntl) {
testDecodeSample(
'utf-16be',
sample,
[
0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34,
0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF,
0xDB, 0xFF, 0xDF, 0xFD, 0xFF, 0xFE
]
);
}
testDecodeSample(
'utf-16',
sample,
[
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
]
);
{ {
common.expectsError( common.expectsError(
() => new TextDecoder('utf-8', 1), () => new TextDecoder('utf-8', 1),
@ -242,39 +179,3 @@ testDecodeSample(
} }
); );
} }
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-invalid-label.html
[
'utf-8',
'unicode-1-1-utf-8',
'utf8',
'utf-16be',
'utf-16le',
'utf-16'
].forEach((i) => {
['\u0000', '\u000b', '\u00a0', '\u2028', '\u2029'].forEach((ws) => {
common.expectsError(
() => new TextDecoder(`${ws}${i}`),
{
code: 'ERR_ENCODING_NOT_SUPPORTED',
type: RangeError
}
);
common.expectsError(
() => new TextDecoder(`${i}${ws}`),
{
code: 'ERR_ENCODING_NOT_SUPPORTED',
type: RangeError
}
);
common.expectsError(
() => new TextDecoder(`${ws}${i}${ws}`),
{
code: 'ERR_ENCODING_NOT_SUPPORTED',
type: RangeError
}
);
});
});