-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #25155 Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
bc66356
commit a8f5191
Showing
4 changed files
with
178 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
); |
40 changes: 40 additions & 0 deletions
40
test/parallel/test-whatwg-encoding-custom-textdecoder-api-invalid-label.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters