Skip to content

Commit

Permalink
assert: partialDeepStrictEqual works with ArrayBuffers
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 bbca465
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
22 changes: 21 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'use strict';

const {
ArrayBufferIsView,
ArrayFrom,
ArrayIsArray,
ArrayPrototypeIndexOf,
Expand All @@ -38,6 +39,7 @@ const {
ObjectIs,
ObjectKeys,
ObjectPrototypeIsPrototypeOf,
ObjectPrototypeToString,
ReflectApply,
ReflectHas,
ReflectOwnKeys,
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -369,7 +372,7 @@ function isSpecial(obj) {
}

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

/**
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 17 additions & 2 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 @@ -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([
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(

Check failure on line 102 in test/parallel/test-assert-typedarray-deepequal.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- (node:77069) ExperimentalWarning: assert.partialDeepStrictEqual is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) --- stdout --- β–Ά equalArrayPairs βœ” <anonymous> (1.627959ms) βœ” <anonymous> (0.100583ms) βœ” <anonymous> (0.098208ms) ::debug::starting to run equalArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” <anonymous> (0.132542ms) βœ” <anonymous> (0.072542ms) βœ” <anonymous> (0.079333ms) βœ” <anonymous> (0.233792ms) ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” <anonymous> (3.556083ms) βœ” <anonymous> (5.50125ms) βœ” <anonymous> (0.407292ms) βœ” <anonymous> (0.052667ms) ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” <anonymous> (0.072083ms) βœ” <anonymous> (0.043167ms) βœ” <anonymous> (0.06225ms) βœ” <anonymous> (0.029958ms) ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” equalArrayPairs (12.917041ms) β–Ά looseEqualArrayPairs βœ” <anonymous> (0.949333ms) βœ” <anonymous> (0.241833ms) ::debug::completed running equalArrayPairs ::debug::starting to run looseEqualArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” looseEqualArrayPairs (1.242959ms) ::debug::completed running looseEqualArrayPairs β–Ά notEqualArrayPairs βœ” <anonymous> (1.111333ms) ::debug::starting to run notEqualArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (1.135208ms) βœ” <anonymous> (0.255ms) βœ” <anonymous> (0.139875ms) βœ” <anonymous> (0.22275ms) ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (0.1565ms) βœ” <anonymous> (0.114167ms) βœ” <anonymous> (0.115583ms) βœ” <anonymous> (0.144375ms) ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (0.114792ms) βœ” <anonymous> (0.117917ms) βœ” <anonymous> (0.12375ms) βœ” <anonymous> (0.114791ms) ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (0.11525ms) βœ” <anonymous> (0.123375ms) βœ– <anonymous> (0.371583ms) AssertionError [ERR_ASSERTION]: Missing expected exception (AssertionError). at TestContext.<anonymous> (/Users/runner/work/node/node/test/parallel/test-assert-typedarray-deepequal.js:102:14) at Test.runInAsyncScope (node:async_hooks:211:

Check failure on line 102 in test/parallel/test-assert-typedarray-deepequal.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- (node:137802) ExperimentalWarning: assert.partialDeepStrictEqual is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) --- stdout --- β–Ά equalArrayPairs βœ” <anonymous> (1.762238ms) βœ” <anonymous> (0.190168ms) βœ” <anonymous> (0.289243ms) ::debug::starting to run equalArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” <anonymous> (0.236985ms) βœ” <anonymous> (0.150913ms) βœ” <anonymous> (0.162625ms) βœ” <anonymous> (0.428495ms) ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” <anonymous> (4.62362ms) βœ” <anonymous> (7.603544ms) βœ” <anonymous> (0.37827ms) βœ” <anonymous> (0.126598ms) ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” <anonymous> (0.104035ms) βœ” <anonymous> (0.073177ms) βœ” <anonymous> (0.134313ms) βœ” <anonymous> (0.153899ms) ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” equalArrayPairs (17.987588ms) β–Ά looseEqualArrayPairs βœ” <anonymous> (1.364351ms) βœ” <anonymous> (0.362481ms) ::debug::completed running equalArrayPairs ::debug::starting to run looseEqualArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> βœ” looseEqualArrayPairs (1.856405ms) ::debug::completed running looseEqualArrayPairs β–Ά notEqualArrayPairs βœ” <anonymous> (1.337681ms) ::debug::starting to run notEqualArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (1.89467ms) βœ” <anonymous> (0.58588ms) βœ” <anonymous> (0.466105ms) βœ” <anonymous> (0.583195ms) ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (0.461827ms) βœ” <anonymous> (0.421171ms) βœ” <anonymous> (0.45261ms) βœ” <anonymous> (0.48446ms) ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (0.474079ms) βœ” <anonymous> (0.423225ms) βœ” <anonymous> (0.440608ms) βœ” <anonymous> (0.406513ms) ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (0.431741ms) βœ” <anonymous> (0.484339ms) βœ– <anonymous> (0.865033ms) AssertionError [ERR_ASSERTION]: Missing expected exception (AssertionError). at TestContext.<anonymous> (/home/runner/work/node/node/test/parallel/test-assert-typedarray-deepequal.js:102:14) at Test.runInAsyncScope (node:async_hooks:
makeBlock(assert.partialDeepStrictEqual, arrayPair[0], arrayPair[1]),
assert.AssertionError
);
});
}
});

0 comments on commit bbca465

Please sign in to comment.