Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
85 changes: 49 additions & 36 deletions third_party/test262/harness/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,52 @@
/*---
description: |
Collection of assertion functions used throughout test262
defines: [assert]
defines:
- assert
- formatIdentityFreeValue
- formatSimpleValue
- isNegativeZero
- isPrimitive
---*/


function isNegativeZero(value) {
return value === 0 && 1 / value === -Infinity;
}

function isPrimitive(value) {
return !value || (typeof value !== 'object' && typeof value !== 'function');
}

function formatIdentityFreeValue(value) {
switch (value === null ? 'null' : typeof value) {
case 'string':
return typeof JSON !== "undefined" ? JSON.stringify(value) : '"' + value + '"';
case 'bigint':
return String(value) + "n";
case 'number':
if (isNegativeZero(value)) return '-0';
// falls through
case 'boolean':
case 'undefined':
case 'null':
return String(value);
}
}

function formatSimpleValue(value) {
var basic = formatIdentityFreeValue(value);
if (basic) return basic;
try {
return String(value);
} catch (err) {
if (err.name === 'TypeError') {
return Object.prototype.toString.call(value);
}
throw err;
}
}

function assert(mustBeTrue, message) {
if (mustBeTrue === true) {
return;
Expand Down Expand Up @@ -101,10 +143,6 @@ assert.throws = function (expectedErrorConstructor, func, message) {
throw new Test262Error(message);
};

function isPrimitive(value) {
return !value || (typeof value !== 'object' && typeof value !== 'function');
}

assert.compareArray = function (actual, expected, message) {
message = message === undefined ? '' : message;

Expand All @@ -113,15 +151,15 @@ assert.compareArray = function (actual, expected, message) {
}

if (isPrimitive(actual)) {
assert(false, `Actual argument [${actual}] shouldn't be primitive. ${message}`);
assert(false, "Actual argument [" + actual + "] shouldn't be primitive. " + String(message));
} else if (isPrimitive(expected)) {
assert(false, `Expected argument [${expected}] shouldn't be primitive. ${message}`);
assert(false, "Expected argument [" + expected + "] shouldn't be primitive. " + String(message));
}
var result = compareArray(actual, expected);
if (result) return;

var format = compareArray.format;
assert(false, `Actual ${format(actual)} and expected ${format(expected)} should have the same contents. ${message}`);
assert(false, "Actual " + format(actual) + " and expected " + format(expected) + " should have the same contents. " + String(message));
};

function compareArray(a, b) {
Expand All @@ -137,34 +175,9 @@ function compareArray(a, b) {
}

compareArray.format = function (arrayLike) {
return `[${Array.prototype.map.call(arrayLike, String).join(', ')}]`;
return "[" + Array.prototype.map.call(arrayLike, String).join(", ") + "]";
};

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

assert._toString = function (value) {
var basic = assert._formatIdentityFreeValue(value);
if (basic) return basic;
try {
return String(value);
} catch (err) {
if (err.name === 'TypeError') {
return Object.prototype.toString.call(value);
}
throw err;
}
};
assert._toString = formatSimpleValue;
Loading