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;
2 changes: 1 addition & 1 deletion third_party/test262/harness/asyncHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defines: [asyncTest, assert.throwsAsync]

/**
* Defines the **sole** asynchronous test of a file.
* @see {@link ../docs/rfcs/async-helpers.md} for background.
* @see {@link ../rfcs/async-helpers.md} for background.
*
* @param {Function} testFunc a callback whose returned promise indicates test results
* (fulfillment for success, rejection for failure)
Expand Down
52 changes: 52 additions & 0 deletions third_party/test262/harness/nativeErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (C) 2026 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Arrays of language-specified Error constructors, plus a helper that
constructs a sample instance with appropriate arguments for the
constructor's signature.

`nativeErrors` contains every Error constructor whose first argument
is a `message` string: %Error% and the six NativeErrors.

`allErrorConstructors` additionally includes %AggregateError% and
%SuppressedError% when present in the host. Their constructors have
different signatures (`(errors, message)` and
`(error, suppressed, message)` respectively), so tests that just
iterate as `new Ctor(message)` should prefer `nativeErrors`; tests
that need to cover every Error-like constructor should use
`allErrorConstructors` together with `makeNativeError`.
defines: [nativeErrors, allErrorConstructors, makeNativeError]
---*/

var nativeErrors = [
Error,
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError
];

var allErrorConstructors = nativeErrors.slice();
if (typeof AggregateError !== 'undefined') {
allErrorConstructors.push(AggregateError);
}
if (typeof SuppressedError !== 'undefined') {
allErrorConstructors.push(SuppressedError);
}

function makeNativeError(Ctor, useNew) {
if (typeof AggregateError !== 'undefined' && Ctor === AggregateError) {
return useNew
? new AggregateError([new Error('inner')], 'msg')
: AggregateError([new Error('inner')], 'msg');
}
if (typeof SuppressedError !== 'undefined' && Ctor === SuppressedError) {
return useNew
? new SuppressedError(new Error('inner'), new Error('suppressed'), 'msg')
: SuppressedError(new Error('inner'), new Error('suppressed'), 'msg');
}
return useNew ? new Ctor('msg') : Ctor('msg');
}
Loading