Skip to content

Commit

Permalink
assert: make partialDeepStrictEqual throw when comparing [0] with [-0]
Browse files Browse the repository at this point in the history
  • Loading branch information
puskin94 committed Dec 15, 2024
1 parent a1d980c commit 8f997a5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 21 deletions.
78 changes: 57 additions & 21 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@ function partiallyCompareSets(actual, expected, comparedObjects) {
return true;
}

// Helper function to get a unique key for 0, -0 to avoid collisions
function getZeroKey(item) {
if (item === 0) {
if (ObjectIs(item, -0)) return '-0';
return '0';
}
return item;
}

function partiallyCompareArrays(actual, expected, comparedObjects) {
if (expected.length > actual.length) {
return false;
Expand All @@ -506,39 +515,66 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {

// Create a map to count occurrences of each element in the expected array
const expectedCounts = new SafeMap();
for (const expectedItem of expected) {
let found = false;
for (const { 0: key, 1: count } of expectedCounts) {
if (isDeepStrictEqual(key, expectedItem)) {
expectedCounts.set(key, count + 1);
found = true;
break;
const safeExpected = new SafeArrayIterator(expected);

for (const expectedItem of safeExpected) {
// Check if the item is a zero or a -0, as these need to be handled separately
if (expectedItem === 0) {
const zeroKey = getZeroKey(expectedItem);
expectedCounts.set(zeroKey, {
count: (expectedCounts.get(zeroKey)?.count || 0) + 1,
expectedType: typeof expectedItem,
});
} else {
let found = false;
for (const { 0: key, 1: { count, expectedType } } of expectedCounts) {
// eslint-disable-next-line valid-typeof
if (isDeepStrictEqual(key, expectedItem) && expectedType === typeof expectedItem) {
expectedCounts.set(key, { count: count + 1, expectedType });
found = true;
break;
}
}
if (!found) {
expectedCounts.set(expectedItem, { count: 1, expectedType: typeof expectedItem });
}
}
if (!found) {
expectedCounts.set(expectedItem, 1);
}
}

const safeActual = new SafeArrayIterator(actual);

// Create a map to count occurrences of relevant elements in the actual array
for (const actualItem of safeActual) {
for (const { 0: key, 1: count } of expectedCounts) {
if (isDeepStrictEqual(key, actualItem)) {
if (count === 1) {
expectedCounts.delete(key);
} else {
expectedCounts.set(key, count - 1);
// Check if the item is a zero or a -0, as these need to be handled separately
if (actualItem === 0) {
const zeroKey = getZeroKey(actualItem);

if (expectedCounts.has(zeroKey)) {
const { count, expectedType } = expectedCounts.get(zeroKey);
// eslint-disable-next-line valid-typeof
if (expectedType === typeof actualItem) {
if (count === 1) {
expectedCounts.delete(zeroKey);
} else {
expectedCounts.set(zeroKey, { count: count - 1, expectedType });
}
}
}
} else {
for (const { 0: expectedItem, 1: { count, expectedType } } of expectedCounts) {
// eslint-disable-next-line valid-typeof
if (isDeepStrictEqual(expectedItem, actualItem) && expectedType === typeof actualItem) {
if (count === 1) {
expectedCounts.delete(expectedItem);
} else {
expectedCounts.set(expectedItem, { count: count - 1, expectedType });
}
break;
}
break;
}
}
}

const { size } = expectedCounts;
expectedCounts.clear();
return size === 0;
return expectedCounts.size === 0;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-assert-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,41 @@ describe('Object Comparison Tests', () => {
actual: [1, 'two', true],
expected: [1, 'two', false],
},
{
description: 'throws when comparing [0] with [-0]',
actual: [0],
expected: [-0],
},
{
description: 'throws when comparing [0, 0, 0] with [0, -0]',
actual: [0, 0, 0],
expected: [0, -0],
},
{
description: 'throws when comparing [-0] with [0]',
actual: [0],
expected: [-0],
},
{
description: 'throws when comparing ["-0"] with [-0]',
actual: ['-0'],
expected: [-0],
},
{
description: 'throws when comparing [-0] with ["-0"]',
actual: [-0],
expected: ['-0'],
},
{
description: 'throws when comparing ["0"] with [0]',
actual: ['0'],
expected: [0],
},
{
description: 'throws when comparing [0] with ["0"]',
actual: [0],
expected: ['0'],
},
{
description:
'throws when comparing two Date objects with different times',
Expand Down Expand Up @@ -385,6 +420,21 @@ describe('Object Comparison Tests', () => {
actual: [1, 'two', true],
expected: [1, 'two', true],
},
{
description: 'compares [0] with [0]',
actual: [0],
expected: [0],
},
{
description: 'compares [-0] with [-0]',
actual: [-0],
expected: [-0],
},
{
description: 'compares [0, -0, 0] with [0, 0]',
actual: [0, -0, 0],
expected: [0, 0],
},
{
description: 'compares two Date objects with the same time',
actual: new Date(0),
Expand Down

0 comments on commit 8f997a5

Please sign in to comment.