node/test/parallel/test-buffer-write-fast.js
Anna Henningsen 4b289d047d
test: expand linting rules around assert w literal messages
Expands the existing restrictions around not using literal messages
to the `not` variants of `strictEqual` and `deepStrictEqual`, and
forbids `assert()` where the second argument is a non-string literal,
which almost always is a mis-typed `assert.strictEquals()`.

PR-URL: https://github.com/nodejs/node/pull/59147
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2025-07-23 16:28:06 +00:00

45 lines
1.4 KiB
JavaScript

// Flags: --expose-internals --no-warnings --allow-natives-syntax
'use strict';
const common = require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
function testFastUtf8Write() {
{
const buf = Buffer.from('\x80');
assert.strictEqual(buf[0], 194);
assert.strictEqual(buf[1], 128);
}
{
const buf = Buffer.alloc(64);
const newBuf = buf.subarray(0, buf.write('éñüç߯'));
assert.deepStrictEqual(newBuf, Buffer.from([195, 169, 195, 177, 195, 188, 195, 167, 195, 159, 195, 134]));
}
{
const buf = Buffer.alloc(64);
const newBuf = buf.subarray(0, buf.write('¿'));
assert.deepStrictEqual(newBuf, Buffer.from([194, 191]));
}
{
const buf = Buffer.from(new ArrayBuffer(34), 0, 16);
const str = Buffer.from([50, 83, 127, 39, 104, 8, 74, 65, 108, 123, 5, 4, 82, 10, 7, 53]).toString();
const newBuf = buf.subarray(0, buf.write(str));
assert.deepStrictEqual(newBuf, Buffer.from([ 50, 83, 127, 39, 104, 8, 74, 65, 108, 123, 5, 4, 82, 10, 7, 53]));
}
}
eval('%PrepareFunctionForOptimization(Buffer.prototype.utf8Write)');
testFastUtf8Write();
eval('%OptimizeFunctionOnNextCall(Buffer.prototype.utf8Write)');
testFastUtf8Write();
if (common.isDebug) {
const { getV8FastApiCallCount } = internalBinding('debug');
assert.strictEqual(getV8FastApiCallCount('buffer.writeString'), 4);
}