node/test/parallel/test-internal-encoding-binding.js
Chengzhong Wu 3c105b6e21 test: convert test_encoding_binding.cc to a JS test
The cctest file `test_encoding_binding.cc` is never tested and it is
not a valid test. Binding functions should never be tested with V8 API
circumvented. A binding function should only be tested with JS calls.

PR-URL: https://github.com/nodejs/node/pull/56791
Refs: https://github.com/nodejs/node/pull/55275
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2025-01-29 21:55:04 +00:00

49 lines
1.4 KiB
JavaScript

// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('node:assert');
const { internalBinding } = require('internal/test/binding');
const binding = internalBinding('encoding_binding');
{
// Valid input
const buf = Uint8Array.from([0xC1, 0xE9, 0xF3]);
assert.strictEqual(binding.decodeLatin1(buf, false, false), 'Áéó');
}
{
// Empty input
const buf = Uint8Array.from([]);
assert.strictEqual(binding.decodeLatin1(buf, false, false), '');
}
{
// Invalid input, but Latin1 has no invalid chars and should never throw.
const buf = new TextEncoder().encode('Invalid Latin1 🧑‍🧑‍🧒‍🧒');
assert.strictEqual(
binding.decodeLatin1(buf, false, false),
'Invalid Latin1 ð\x9F§\x91â\x80\x8Dð\x9F§\x91â\x80\x8Dð\x9F§\x92â\x80\x8Dð\x9F§\x92'
);
}
{
// IgnoreBOM with BOM
const buf = Uint8Array.from([0xFE, 0xFF, 0xC1, 0xE9, 0xF3]);
assert.strictEqual(binding.decodeLatin1(buf, true, false), 'þÿÁéó');
}
{
// Fatal and InvalidInput, but Latin1 has no invalid chars and should never throw.
const buf = Uint8Array.from([0xFF, 0xFF, 0xFF]);
assert.strictEqual(binding.decodeLatin1(buf, false, true), 'ÿÿÿ');
}
{
// IgnoreBOM and Fatal, but Latin1 has no invalid chars and should never throw.
const buf = Uint8Array.from([0xFE, 0xFF, 0xC1, 0xE9, 0xF3]);
assert.strictEqual(binding.decodeLatin1(buf, true, true), 'þÿÁéó');
}