Skip to content

Commit

Permalink
harness/deepEqual.js: Leverage base assert
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Oct 9, 2024
1 parent 8f1354a commit ea656f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
22 changes: 17 additions & 5 deletions harness/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,30 @@ assert.throws = function (expectedErrorConstructor, func, message) {
throw new Test262Error(message);
};

assert._formatIdentityFreeValue = function formatIdentityFreeValue(value) {
switch (typeof value) {
case 'string':
return typeof JSON !== "undefined" ? JSON.stringify(value) : `"${value}"`;
case 'bigint':
return `${value}n`;
case 'boolean':
case 'undefined':
case 'number':
return value === 0 && 1 / value === -Infinity ? '-0' : String(value);
default:
if (value === null) return 'null';
}
};

assert._toString = function (value) {
var basic = assert._formatIdentityFreeValue(value);
if (basic) return basic;
try {
if (value === 0 && 1 / value === -Infinity) {
return '-0';
}

return String(value);
} catch (err) {
if (err.name === 'TypeError') {
return Object.prototype.toString.call(value);
}

throw err;
}
};
6 changes: 3 additions & 3 deletions harness/deepEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ assert.deepEqual.format = (function () {
let renderUsage = usage => usage.used ? ` as #${usage.id}` : '';

return function format(value, seen) {
let basic = assert._formatIdentityFreeValue(value);
if (basic) return basic;
switch (typeof value) {
case 'string':
return typeof JSON !== "undefined" ? JSON.stringify(value) : `"${value}"`;
case 'bigint':
return `${value}n`;
case 'boolean':
case 'undefined':
case 'number':
return value === 0 && 1 / value === -Infinity ? '-0' : String(value);
assert(false, 'values without identity should use basic formatting');
case 'symbol':
case 'function':
case 'object':
Expand Down

0 comments on commit ea656f8

Please sign in to comment.