• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-basics.html
4// This is the part that can be run without ICU
5
6require('../common');
7
8const assert = require('assert');
9
10function testDecodeSample(encoding, string, bytes) {
11  assert.strictEqual(
12    new TextDecoder(encoding).decode(new Uint8Array(bytes)),
13    string);
14  assert.strictEqual(
15    new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
16    string);
17}
18
19// `z` (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
20// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
21// byte-swapped BOM (non-character U+FFFE)
22const sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
23
24{
25  const encoding = 'utf-8';
26  const string = sample;
27  const bytes = [
28    0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4,
29    0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
30    0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF,
31    0xBF, 0xBE,
32  ];
33  const encoded = new TextEncoder().encode(string);
34  assert.deepStrictEqual([].slice.call(encoded), bytes);
35  assert.strictEqual(
36    new TextDecoder(encoding).decode(new Uint8Array(bytes)),
37    string);
38  assert.strictEqual(
39    new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
40    string);
41}
42
43testDecodeSample(
44  'utf-16le',
45  sample,
46  [
47    0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
48    0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
49    0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF,
50  ]
51);
52
53testDecodeSample(
54  'utf-16',
55  sample,
56  [
57    0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
58    0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
59    0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF,
60  ]
61);
62