Skip to content

Commit

Permalink
test: split test-whatwg-encoding-textdecoder.js
Browse files Browse the repository at this point in the history
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: #25155
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung authored and targos committed Jan 1, 2019
1 parent bc66356 commit a8f5191
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 102 deletions.
74 changes: 74 additions & 0 deletions test/parallel/test-whatwg-encoding-api-basics.js
Original file line number Diff line number Diff line change
@@ -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
]
);
61 changes: 61 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-api-basics.js
Original file line number Diff line number Diff line change
@@ -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
]
);
Original file line number Diff line number Diff line change
@@ -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
}
);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Flags: --expose-internals

// This tests Node.js-specific behaviors of TextDecoder

'use strict';

const common = require('../common');
Expand Down Expand Up @@ -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(
() => new TextDecoder('utf-8', 1),
Expand All @@ -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
}
);
});
});

0 comments on commit a8f5191

Please sign in to comment.