Skip to content

Commit

Permalink
assert: partialDeepStrictEqual compares SharedArrayBuffers, Int16Arrays
Browse files Browse the repository at this point in the history
Fixes: #56097
  • Loading branch information
puskin94 committed Dec 1, 2024
1 parent 3f9c6c0 commit a5f1059
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const {
StringPrototypeSlice,
StringPrototypeSplit,
SymbolIterator,
Uint8Array,
} = primordials;

const {
Expand All @@ -73,6 +74,9 @@ const {
isDate,
isWeakSet,
isWeakMap,
isSharedArrayBuffer,
isArrayBuffer,
isInt16Array,
} = require('internal/util/types');
const { isError, deprecate, emitExperimentalWarning } = require('internal/util');
const { innerOk } = require('internal/assert/utils');
Expand Down Expand Up @@ -369,7 +373,7 @@ function isSpecial(obj) {
}

const typesToCallDeepStrictEqualWith = [
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer,
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, isInt16Array,
];

/**
Expand Down Expand Up @@ -406,6 +410,22 @@ function compareBranch(
return true;
}

if (isArrayBuffer(actual) && isArrayBuffer(expected)) {
if (actual.byteLength !== expected.byteLength) {
return false;
}

const actualView = new Uint8Array(actual);
const expectedView = new Uint8Array(expected);

for (let i = 0; i < actualView.length; i++) {
if (actualView[i] !== expectedView[i]) {
return false;
}
}
return true;
}

for (const type of typesToCallDeepStrictEqualWith) {
if (type(actual) || type(expected)) {
if (isDeepStrictEqual === undefined) lazyLoadComparison();
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-assert-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -347,6 +357,11 @@ describe('Object Comparison Tests', () => {
actual: { typedArray: new Uint8Array([1, 2, 3]) },
expected: { typedArray: new Uint8Array([1, 2, 3]) },
},
{
description: 'compares two objects with TypedArray instances with the same content',
actual: { typedArray: new Int16Array([1, 2, 3]) },
expected: { typedArray: new Int16Array([1, 2, 3]) },
},
{
description: 'compares two Map objects with identical entries',
actual: new Map([
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-assert-typedarray-deepequal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
}
});

0 comments on commit a5f1059

Please sign in to comment.