1'use strict';
2
3// From: https://github.com/w3c/web-platform-tests/blob/39a67e2fff/encoding/textdecoder-utf16-surrogates.html
4// With the twist that we specifically test for Node.js error codes
5
6const common = require('../common');
7
8if (!common.hasIntl)
9  common.skip('missing Intl');
10
11const assert = require('assert');
12
13const bad = [
14  {
15    encoding: 'utf-16le',
16    input: [0x00, 0xd8],
17    expected: '\uFFFD',
18    name: 'lone surrogate lead'
19  },
20  {
21    encoding: 'utf-16le',
22    input: [0x00, 0xdc],
23    expected: '\uFFFD',
24    name: 'lone surrogate trail'
25  },
26  {
27    encoding: 'utf-16le',
28    input: [0x00, 0xd8, 0x00, 0x00],
29    expected: '\uFFFD\u0000',
30    name: 'unmatched surrogate lead'
31  },
32  {
33    encoding: 'utf-16le',
34    input: [0x00, 0xdc, 0x00, 0x00],
35    expected: '\uFFFD\u0000',
36    name: 'unmatched surrogate trail'
37  },
38  {
39    encoding: 'utf-16le',
40    input: [0x00, 0xdc, 0x00, 0xd8],
41    expected: '\uFFFD\uFFFD',
42    name: 'swapped surrogate pair'
43  },
44];
45
46bad.forEach((t) => {
47  assert.throws(
48    () => {
49      new TextDecoder(t.encoding, { fatal: true })
50        .decode(new Uint8Array(t.input));
51    }, {
52      code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
53      name: 'TypeError'
54    }
55  );
56});
57