0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-whatwg-encoding-textencoder-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

53 lines
1.1 KiB
JavaScript

'use strict';
// From: https://github.com/w3c/web-platform-tests/blob/fa9436d12c/encoding/textencoder-utf16-surrogates.html
require('../common');
const assert = require('assert');
const {
TextDecoder,
TextEncoder
} = require('util');
const bad = [
{
input: '\uD800',
expected: '\uFFFD',
name: 'lone surrogate lead'
},
{
input: '\uDC00',
expected: '\uFFFD',
name: 'lone surrogate trail'
},
{
input: '\uD800\u0000',
expected: '\uFFFD\u0000',
name: 'unmatched surrogate lead'
},
{
input: '\uDC00\u0000',
expected: '\uFFFD\u0000',
name: 'unmatched surrogate trail'
},
{
input: '\uDC00\uD800',
expected: '\uFFFD\uFFFD',
name: 'swapped surrogate pair'
},
{
input: '\uD834\uDD1E',
expected: '\uD834\uDD1E',
name: 'properly encoded MUSICAL SYMBOL G CLEF (U+1D11E)'
}
];
bad.forEach((t) => {
const encoded = new TextEncoder().encode(t.input);
const decoded = new TextDecoder().decode(encoded);
assert.strictEqual(decoded, t.expected);
});
assert.strictEqual(new TextEncoder().encode().length, 0);