test: convert test_encoding_binding.cc to a JS test · nodejs/node@86c199b
1+// Flags: --expose-internals
2+3+'use strict';
4+5+require('../common');
6+7+const assert = require('node:assert');
8+const { internalBinding } = require('internal/test/binding');
9+const binding = internalBinding('encoding_binding');
10+11+{
12+// Valid input
13+const buf = Uint8Array.from([0xC1, 0xE9, 0xF3]);
14+assert.strictEqual(binding.decodeLatin1(buf, false, false), 'Áéó');
15+}
16+17+{
18+// Empty input
19+const buf = Uint8Array.from([]);
20+assert.strictEqual(binding.decodeLatin1(buf, false, false), '');
21+}
22+23+{
24+// Invalid input, but Latin1 has no invalid chars and should never throw.
25+const buf = new TextEncoder().encode('Invalid Latin1 🧑🧑🧒🧒');
26+assert.strictEqual(
27+binding.decodeLatin1(buf, false, false),
28+'Invalid Latin1 ð\x9F§\x91â\x80\x8Dð\x9F§\x91â\x80\x8Dð\x9F§\x92â\x80\x8Dð\x9F§\x92'
29+);
30+}
31+32+{
33+// IgnoreBOM with BOM
34+const buf = Uint8Array.from([0xFE, 0xFF, 0xC1, 0xE9, 0xF3]);
35+assert.strictEqual(binding.decodeLatin1(buf, true, false), 'þÿÁéó');
36+}
37+38+{
39+// Fatal and InvalidInput, but Latin1 has no invalid chars and should never throw.
40+const buf = Uint8Array.from([0xFF, 0xFF, 0xFF]);
41+assert.strictEqual(binding.decodeLatin1(buf, false, true), 'ÿÿÿ');
42+}
43+44+{
45+// IgnoreBOM and Fatal, but Latin1 has no invalid chars and should never throw.
46+const buf = Uint8Array.from([0xFE, 0xFF, 0xC1, 0xE9, 0xF3]);
47+assert.strictEqual(binding.decodeLatin1(buf, true, true), 'þÿÁéó');
48+}