Skip to content

Commit bbca465

Browse files
committed
assert: partialDeepStrictEqual works with ArrayBuffers
Fixes: nodejs#56097
1 parent 3f9c6c0 commit bbca465

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

lib/assert.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'use strict';
2222

2323
const {
24+
ArrayBufferIsView,
2425
ArrayFrom,
2526
ArrayIsArray,
2627
ArrayPrototypeIndexOf,
@@ -38,6 +39,7 @@ const {
3839
ObjectIs,
3940
ObjectKeys,
4041
ObjectPrototypeIsPrototypeOf,
42+
ObjectPrototypeToString,
4143
ReflectApply,
4244
ReflectHas,
4345
ReflectOwnKeys,
@@ -73,6 +75,7 @@ const {
7375
isDate,
7476
isWeakSet,
7577
isWeakMap,
78+
isSharedArrayBuffer,
7679
} = require('internal/util/types');
7780
const { isError, deprecate, emitExperimentalWarning } = require('internal/util');
7881
const { innerOk } = require('internal/assert/utils');
@@ -369,7 +372,7 @@ function isSpecial(obj) {
369372
}
370373

371374
const typesToCallDeepStrictEqualWith = [
372-
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer,
375+
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer,
373376
];
374377

375378
/**
@@ -406,6 +409,23 @@ function compareBranch(
406409
return true;
407410
}
408411

412+
if (ArrayBufferIsView(actual) && ArrayBufferIsView(expected)) {
413+
if (ObjectPrototypeToString(actual) !== ObjectPrototypeToString(expected)) {
414+
return false;
415+
}
416+
417+
if (expected.byteLength > actual.byteLength) {
418+
return false;
419+
}
420+
421+
for (let i = 0; i < expected.length; i++) {
422+
if (actual[i] !== expected[i]) {
423+
return false;
424+
}
425+
}
426+
return true;
427+
}
428+
409429
for (const type of typesToCallDeepStrictEqualWith) {
410430
if (type(actual) || type(expected)) {
411431
if (isDeepStrictEqual === undefined) lazyLoadComparison();

test/parallel/test-assert-objects.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ describe('Object Comparison Tests', () => {
207207
actual: [1, 2, 3],
208208
expected: ['2'],
209209
},
210+
{
211+
description: 'throws when comparing a ArrayBuffer with a SharedArrayBuffer',
212+
actual: new ArrayBuffer(3),
213+
expected: new SharedArrayBuffer(3),
214+
},
215+
{
216+
description: 'throws when comparing an Int16Array with a Uint16Array',
217+
actual: new Int16Array(3),
218+
expected: new Uint16Array(3),
219+
},
210220
];
211221

212222
if (common.hasCrypto) {
@@ -343,10 +353,15 @@ describe('Object Comparison Tests', () => {
343353
expected: { error: new Error('Test error') },
344354
},
345355
{
346-
description: 'compares two objects with TypedArray instances with the same content',
347-
actual: { typedArray: new Uint8Array([1, 2, 3]) },
356+
description: 'compares two Uint8Array objects',
357+
actual: { typedArray: new Uint8Array([1, 2, 3, 4, 5]) },
348358
expected: { typedArray: new Uint8Array([1, 2, 3]) },
349359
},
360+
{
361+
description: 'compares two Int16Array objects',
362+
actual: { typedArray: new Int16Array([1, 2, 3, 4, 5]) },
363+
expected: { typedArray: new Int16Array([1, 2, 3]) },
364+
},
350365
{
351366
description: 'compares two Map objects with identical entries',
352367
actual: new Map([

test/parallel/test-assert-typedarray-deepequal.js

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ suite('notEqualArrayPairs', () => {
9999
makeBlock(assert.deepStrictEqual, arrayPair[0], arrayPair[1]),
100100
assert.AssertionError
101101
);
102+
assert.throws(
103+
makeBlock(assert.partialDeepStrictEqual, arrayPair[0], arrayPair[1]),
104+
assert.AssertionError
105+
);
102106
});
103107
}
104108
});

0 commit comments

Comments
 (0)