From bbca4657a3cea3579026df5ad672ecd728842870 Mon Sep 17 00:00:00 2001 From: Giovanni Date: Sun, 1 Dec 2024 15:11:44 +0100 Subject: [PATCH] assert: partialDeepStrictEqual works with ArrayBuffers Fixes: /~https://github.com/nodejs/node/issues/56097 --- lib/assert.js | 22 ++++++++++++++++++- test/parallel/test-assert-objects.js | 19 ++++++++++++++-- .../test-assert-typedarray-deepequal.js | 4 ++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 3e212a1c3aebbe..3f57df96b5751f 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -21,6 +21,7 @@ 'use strict'; const { + ArrayBufferIsView, ArrayFrom, ArrayIsArray, ArrayPrototypeIndexOf, @@ -38,6 +39,7 @@ const { ObjectIs, ObjectKeys, ObjectPrototypeIsPrototypeOf, + ObjectPrototypeToString, ReflectApply, ReflectHas, ReflectOwnKeys, @@ -73,6 +75,7 @@ const { isDate, isWeakSet, isWeakMap, + isSharedArrayBuffer, } = require('internal/util/types'); const { isError, deprecate, emitExperimentalWarning } = require('internal/util'); const { innerOk } = require('internal/assert/utils'); @@ -369,7 +372,7 @@ function isSpecial(obj) { } const typesToCallDeepStrictEqualWith = [ - isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, + isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, ]; /** @@ -406,6 +409,23 @@ function compareBranch( return true; } + if (ArrayBufferIsView(actual) && ArrayBufferIsView(expected)) { + if (ObjectPrototypeToString(actual) !== ObjectPrototypeToString(expected)) { + return false; + } + + if (expected.byteLength > actual.byteLength) { + return false; + } + + for (let i = 0; i < expected.length; i++) { + if (actual[i] !== expected[i]) { + return false; + } + } + return true; + } + for (const type of typesToCallDeepStrictEqualWith) { if (type(actual) || type(expected)) { if (isDeepStrictEqual === undefined) lazyLoadComparison(); diff --git a/test/parallel/test-assert-objects.js b/test/parallel/test-assert-objects.js index d1c8bb854babb0..8e137b042ab1e1 100644 --- a/test/parallel/test-assert-objects.js +++ b/test/parallel/test-assert-objects.js @@ -207,6 +207,16 @@ describe('Object Comparison Tests', () => { actual: [1, 2, 3], expected: ['2'], }, + { + description: 'throws when comparing a ArrayBuffer with a SharedArrayBuffer', + actual: new ArrayBuffer(3), + expected: new SharedArrayBuffer(3), + }, + { + description: 'throws when comparing an Int16Array with a Uint16Array', + actual: new Int16Array(3), + expected: new Uint16Array(3), + }, ]; if (common.hasCrypto) { @@ -343,10 +353,15 @@ describe('Object Comparison Tests', () => { expected: { error: new Error('Test error') }, }, { - description: 'compares two objects with TypedArray instances with the same content', - actual: { typedArray: new Uint8Array([1, 2, 3]) }, + description: 'compares two Uint8Array objects', + actual: { typedArray: new Uint8Array([1, 2, 3, 4, 5]) }, expected: { typedArray: new Uint8Array([1, 2, 3]) }, }, + { + description: 'compares two Int16Array objects', + actual: { typedArray: new Int16Array([1, 2, 3, 4, 5]) }, + expected: { typedArray: new Int16Array([1, 2, 3]) }, + }, { description: 'compares two Map objects with identical entries', actual: new Map([ diff --git a/test/parallel/test-assert-typedarray-deepequal.js b/test/parallel/test-assert-typedarray-deepequal.js index 1c1c4c030a267e..243b51de1cd4a3 100644 --- a/test/parallel/test-assert-typedarray-deepequal.js +++ b/test/parallel/test-assert-typedarray-deepequal.js @@ -99,6 +99,10 @@ suite('notEqualArrayPairs', () => { makeBlock(assert.deepStrictEqual, arrayPair[0], arrayPair[1]), assert.AssertionError ); + assert.throws( + makeBlock(assert.partialDeepStrictEqual, arrayPair[0], arrayPair[1]), + assert.AssertionError + ); }); } });