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 710b5cd
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-linux

--- stderr --- (node:137455) 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.774892ms) βœ” <anonymous> (0.202737ms) βœ” <anonymous> (0.271836ms) ::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.198039ms) βœ” <anonymous> (0.151061ms) βœ” <anonymous> (0.147034ms) βœ” <anonymous> (0.43513ms) ::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> (5.940213ms) βœ” <anonymous> (12.860599ms) βœ” <anonymous> (0.628038ms) βœ” <anonymous> (0.163013ms) ::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.151111ms) βœ” <anonymous> (0.124923ms) βœ” <anonymous> (0.175807ms) βœ” <anonymous> (0.106598ms) ::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 (25.071218ms) β–Ά looseEqualArrayPairs βœ” <anonymous> (1.846747ms) βœ” <anonymous> (0.473231ms) ::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 (2.483821ms) ::debug::completed running looseEqualArrayPairs β–Ά notEqualArrayPairs βœ” <anonymous> (1.724689ms) ::debug::starting to run notEqualArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (3.017276ms) βœ” <anonymous> (0.4511ms) βœ” <anonymous> (0.282536ms) βœ” <anonymous> (0.490162ms) ::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.277316ms) βœ” <anonymous> (0.250587ms) βœ” <anonymous> (0.251458ms) βœ” <anonymous> (0.427087ms) ::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.382572ms) βœ” <anonymous> (0.363817ms) βœ” <anonymous> (0.386079ms) βœ” <anonymous> (0.28472ms) ::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.304216ms) βœ” <anonymous> (0.397229ms) βœ– <anonymous> (0.715712ms) 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_hoo

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:72702) 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> (2.08825ms) βœ” <anonymous> (0.097292ms) βœ” <anonymous> (0.078958ms) ::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.127708ms) βœ” <anonymous> (0.061209ms) βœ” <anonymous> (0.058584ms) βœ” <anonymous> (0.199583ms) ::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> (8.26275ms) βœ” <anonymous> (4.4575ms) βœ” <anonymous> (0.437458ms) βœ” <anonymous> (0.050084ms) ::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.043041ms) βœ” <anonymous> (0.031416ms) βœ” <anonymous> (0.091667ms) βœ” <anonymous> (0.027583ms) ::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 (16.870208ms) β–Ά looseEqualArrayPairs βœ” <anonymous> (0.901292ms) βœ” <anonymous> (0.214958ms) ::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.164167ms) ::debug::completed running looseEqualArrayPairs β–Ά notEqualArrayPairs βœ” <anonymous> (0.833584ms) ::debug::starting to run notEqualArrayPairs ::debug::starting to run <anonymous> ::debug::completed running <anonymous> ::debug::starting to run <anonymous> βœ” <anonymous> (0.475916ms) βœ” <anonymous> (0.242458ms) βœ” <anonymous> (0.147416ms) βœ” <anonymous> (0.247625ms) ::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.128792ms) βœ” <anonymous> (0.127083ms) βœ” <anonymous> (0.119542ms) βœ” <anonymous> (0.152334ms) ::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.220542ms) βœ” <anonymous> (0.163625ms) βœ” <anonymous> (0.135625ms) βœ” <anonymous> (0.124459ms) ::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.123917ms) βœ” <anonymous> (0.136167ms) βœ– <anonymous> (0.386208ms) 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_hook
makeBlock(assert.partialDeepStrictEqual, arrayPair[0], arrayPair[1]),
assert.AssertionError
);
});
}
});

0 comments on commit 710b5cd

Please sign in to comment.