forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-util-text-decoder.js
More file actions
63 lines (53 loc) · 2.32 KB
/
test-util-text-decoder.js
File metadata and controls
63 lines (53 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
const common = require('../common');
const test = require('node:test');
const assert = require('node:assert');
test('TextDecoder correctly decodes windows-1252 encoded data', { skip: !common.hasIntl }, () => {
const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]);
const expectedString = 'Áéó';
const decoder = new TextDecoder('windows-1252');
const decodedString = decoder.decode(latin1Bytes);
assert.strictEqual(decodedString, expectedString);
});
// Test for the difference between Latin1 and Windows-1252 in the 128-159
// range
// Ref: https://github.com/nodejs/node/issues/60888
test('TextDecoder correctly decodes windows-1252 special characters in ' +
'128-159 range', { skip: !common.hasIntl }, () => {
const decoder = new TextDecoder('windows-1252');
// Test specific characters that differ between Latin1 and Windows-1252.
// € Euro sign
assert.strictEqual(decoder.decode(Uint8Array.of(128)).codePointAt(0),
8364);
// ‚ Single low-9 quotation mark
assert.strictEqual(decoder.decode(Uint8Array.of(130)).codePointAt(0),
8218);
// Latin small letter f with hook (ƒ)
assert.strictEqual(decoder.decode(Uint8Array.of(131)).codePointAt(0),
402);
// Ÿ Latin capital letter Y with diaeresis
assert.strictEqual(decoder.decode(Uint8Array.of(159)).codePointAt(0),
376);
// Test the full range to ensure no character is treated as Latin1
// Directly.
const expectedMappings = [
[128, 8364], [129, 129], [130, 8218], [131, 402], [132, 8222],
[133, 8230], [134, 8224], [135, 8225], [136, 710], [137, 8240],
[138, 352], [139, 8249], [140, 338], [141, 141], [142, 381],
[143, 143], [144, 144], [145, 8216], [146, 8217], [147, 8220],
[148, 8221], [149, 8226], [150, 8211], [151, 8212], [152, 732],
[153, 8482], [154, 353], [155, 8250], [156, 339], [157, 157],
[158, 382], [159, 376],
];
for (const [byte, expectedCodePoint] of expectedMappings) {
const result = decoder.decode(Uint8Array.of(byte));
const actualCodePoint = result.codePointAt(0);
assert.strictEqual(
actualCodePoint,
expectedCodePoint,
`Byte 0x${byte.toString(16)} should decode to ` +
`U+${expectedCodePoint.toString(16)} but got ` +
`U+${actualCodePoint.toString(16)}`
);
}
});