0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/parallel/test-whatwg-encoding-textdecoder-utf16-surrogates.js
James M Snell 7f9eb4c29c util: graduate TextEncoder/TextDecoder, tests
Add tests ported from Web Platform Tests.

Graduate TextEncoder / TextDecoder from experimental

PR-URL: https://github.com/nodejs/node/pull/15743
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
2017-10-23 21:12:56 -07:00

64 lines
1.3 KiB
JavaScript

'use strict';
// From: https://github.com/w3c/web-platform-tests/blob/39a67e2fff/encoding/textdecoder-utf16-surrogates.html
const common = require('../common');
if (!common.hasIntl)
common.skip('missing Intl');
const assert = require('assert');
const {
TextDecoder
} = require('util');
const bad = [
{
encoding: 'utf-16le',
input: [0x00, 0xd8],
expected: '\uFFFD',
name: 'lone surrogate lead'
},
{
encoding: 'utf-16le',
input: [0x00, 0xdc],
expected: '\uFFFD',
name: 'lone surrogate trail'
},
{
encoding: 'utf-16le',
input: [0x00, 0xd8, 0x00, 0x00],
expected: '\uFFFD\u0000',
name: 'unmatched surrogate lead'
},
{
encoding: 'utf-16le',
input: [0x00, 0xdc, 0x00, 0x00],
expected: '\uFFFD\u0000',
name: 'unmatched surrogate trail'
},
{
encoding: 'utf-16le',
input: [0x00, 0xdc, 0x00, 0xd8],
expected: '\uFFFD\uFFFD',
name: 'swapped surrogate pair'
}
];
bad.forEach((t) => {
assert.strictEqual(
new TextDecoder(t.encoding).decode(new Uint8Array(t.input)),
t.expected);
common.expectsError(
() => {
new TextDecoder(t.encoding, { fatal: true })
.decode(new Uint8Array(t.input));
}, {
code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
type: TypeError
}
);
});