Skip to content

Commit a5f1059

Browse files
committed
assert: partialDeepStrictEqual compares SharedArrayBuffers, Int16Arrays
Fixes: nodejs#56097
1 parent 3f9c6c0 commit a5f1059

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/assert.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050
StringPrototypeSlice,
5151
StringPrototypeSplit,
5252
SymbolIterator,
53+
Uint8Array,
5354
} = primordials;
5455

5556
const {
@@ -73,6 +74,9 @@ const {
7374
isDate,
7475
isWeakSet,
7576
isWeakMap,
77+
isSharedArrayBuffer,
78+
isArrayBuffer,
79+
isInt16Array,
7680
} = require('internal/util/types');
7781
const { isError, deprecate, emitExperimentalWarning } = require('internal/util');
7882
const { innerOk } = require('internal/assert/utils');
@@ -369,7 +373,7 @@ function isSpecial(obj) {
369373
}
370374

371375
const typesToCallDeepStrictEqualWith = [
372-
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer,
376+
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, isInt16Array,
373377
];
374378

375379
/**
@@ -406,6 +410,22 @@ function compareBranch(
406410
return true;
407411
}
408412

413+
if (isArrayBuffer(actual) && isArrayBuffer(expected)) {
414+
if (actual.byteLength !== expected.byteLength) {
415+
return false;
416+
}
417+
418+
const actualView = new Uint8Array(actual);
419+
const expectedView = new Uint8Array(expected);
420+
421+
for (let i = 0; i < actualView.length; i++) {
422+
if (actualView[i] !== expectedView[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

Lines changed: 15 additions & 0 deletions
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) {
@@ -347,6 +357,11 @@ describe('Object Comparison Tests', () => {
347357
actual: { typedArray: new Uint8Array([1, 2, 3]) },
348358
expected: { typedArray: new Uint8Array([1, 2, 3]) },
349359
},
360+
{
361+
description: 'compares two objects with TypedArray instances with the same content',
362+
actual: { typedArray: new Int16Array([1, 2, 3]) },
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

Lines changed: 4 additions & 0 deletions
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)