From a8f5191eb9563922248ce1c496c9a56237258a6e Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 20 Dec 2018 14:20:31 +0800 Subject: [PATCH] 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 --- .../test-whatwg-encoding-api-basics.js | 74 ++++++++++++ .../test-whatwg-encoding-custom-api-basics.js | 61 ++++++++++ ...ng-custom-textdecoder-api-invalid-label.js | 40 +++++++ ...est-whatwg-encoding-custom-textdecoder.js} | 105 +----------------- 4 files changed, 178 insertions(+), 102 deletions(-) create mode 100644 test/parallel/test-whatwg-encoding-api-basics.js create mode 100644 test/parallel/test-whatwg-encoding-custom-api-basics.js create mode 100644 test/parallel/test-whatwg-encoding-custom-textdecoder-api-invalid-label.js rename test/parallel/{test-whatwg-encoding-textdecoder.js => test-whatwg-encoding-custom-textdecoder.js} (69%) diff --git a/test/parallel/test-whatwg-encoding-api-basics.js b/test/parallel/test-whatwg-encoding-api-basics.js new file mode 100644 index 00000000000000..8abaee817156f4 --- /dev/null +++ b/test/parallel/test-whatwg-encoding-api-basics.js @@ -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 + ] +); diff --git a/test/parallel/test-whatwg-encoding-custom-api-basics.js b/test/parallel/test-whatwg-encoding-custom-api-basics.js new file mode 100644 index 00000000000000..c39bce5d74ee99 --- /dev/null +++ b/test/parallel/test-whatwg-encoding-custom-api-basics.js @@ -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 + ] +); diff --git a/test/parallel/test-whatwg-encoding-custom-textdecoder-api-invalid-label.js b/test/parallel/test-whatwg-encoding-custom-textdecoder-api-invalid-label.js new file mode 100644 index 00000000000000..aabd3fe5c4de5d --- /dev/null +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder-api-invalid-label.js @@ -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 + } + ); + }); +}); diff --git a/test/parallel/test-whatwg-encoding-textdecoder.js b/test/parallel/test-whatwg-encoding-custom-textdecoder.js similarity index 69% rename from test/parallel/test-whatwg-encoding-textdecoder.js rename to test/parallel/test-whatwg-encoding-custom-textdecoder.js index 55afd34a02b11e..ceee5b9cbbde30 100644 --- a/test/parallel/test-whatwg-encoding-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder.js @@ -1,4 +1,7 @@ // Flags: --expose-internals + +// This tests Node.js-specific behaviors of TextDecoder + 'use strict'; 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( () => 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 - } - ); - }); -});