2022-11-22 20:16:01 +01:00
|
|
|
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
|
|
|
|
// This code is governed by the BSD license found in the LICENSE file.
|
|
|
|
/*---
|
|
|
|
description: |
|
|
|
|
A collection of assertion and wrapper functions for testing asynchronous built-ins.
|
|
|
|
defines: [asyncTest]
|
|
|
|
---*/
|
|
|
|
|
2024-05-19 18:43:58 +02:00
|
|
|
/**
|
|
|
|
* Defines the **sole** asynchronous test of a file.
|
|
|
|
* @see {@link ../docs/rfcs/async-helpers.md} for background.
|
|
|
|
*
|
|
|
|
* @param {Function} testFunc a callback whose returned promise indicates test results
|
|
|
|
* (fulfillment for success, rejection for failure)
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-11-22 20:16:01 +01:00
|
|
|
function asyncTest(testFunc) {
|
|
|
|
if (!Object.hasOwn(globalThis, "$DONE")) {
|
|
|
|
throw new Test262Error("asyncTest called without async flag");
|
|
|
|
}
|
|
|
|
if (typeof testFunc !== "function") {
|
|
|
|
$DONE(new Test262Error("asyncTest called with non-function argument"));
|
2022-12-31 01:06:33 +01:00
|
|
|
return;
|
2022-11-22 20:16:01 +01:00
|
|
|
}
|
|
|
|
try {
|
2022-12-31 01:06:33 +01:00
|
|
|
testFunc().then(
|
2022-11-22 20:16:01 +01:00
|
|
|
function () {
|
|
|
|
$DONE();
|
|
|
|
},
|
|
|
|
function (error) {
|
|
|
|
$DONE(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (syncError) {
|
|
|
|
$DONE(syncError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 18:43:58 +02:00
|
|
|
/**
|
|
|
|
* Asserts that a callback asynchronously throws an instance of a particular
|
|
|
|
* error (i.e., returns a promise whose rejection value is an object referencing
|
|
|
|
* the constructor).
|
|
|
|
*
|
|
|
|
* @param {Function} expectedErrorConstructor the expected constructor of the
|
|
|
|
* rejection value
|
|
|
|
* @param {Function} func the callback
|
|
|
|
* @param {string} [message] the prefix to use for failure messages
|
|
|
|
* @returns {Promise<void>} fulfills if the expected error is thrown,
|
|
|
|
* otherwise rejects
|
|
|
|
*/
|
2024-05-17 18:13:21 +02:00
|
|
|
assert.throwsAsync = function (expectedErrorConstructor, func, message) {
|
|
|
|
return new Promise(function (resolve) {
|
2024-05-19 19:41:38 +02:00
|
|
|
var expectedName = expectedErrorConstructor.name;
|
|
|
|
var fail = function (detail) {
|
|
|
|
if (message === undefined) {
|
|
|
|
throw new Test262Error(detail);
|
|
|
|
}
|
|
|
|
throw new Test262Error(message + " " + detail);
|
|
|
|
};
|
2024-05-17 18:13:21 +02:00
|
|
|
var innerThenable;
|
2024-05-19 19:18:45 +02:00
|
|
|
if (typeof func !== "function") {
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("assert.throwsAsync called with an argument that is not a function");
|
2024-05-19 19:18:45 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
innerThenable = func();
|
|
|
|
} catch (thrown) {
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("Expected a " +
|
|
|
|
expectedName +
|
|
|
|
" to be thrown asynchronously but the function threw synchronously");
|
2022-11-22 20:16:01 +01:00
|
|
|
}
|
2024-05-19 19:26:31 +02:00
|
|
|
if (
|
|
|
|
innerThenable === null ||
|
|
|
|
typeof innerThenable !== "object" ||
|
|
|
|
typeof innerThenable.then !== "function"
|
|
|
|
) {
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("Expected to obtain a promise that would reject with a " +
|
|
|
|
expectedName +
|
|
|
|
" but result was not a thenable");
|
2024-05-19 19:26:31 +02:00
|
|
|
}
|
2022-11-22 20:16:01 +01:00
|
|
|
|
2024-05-17 18:13:21 +02:00
|
|
|
try {
|
|
|
|
resolve(innerThenable.then(
|
|
|
|
function () {
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("Expected a " +
|
|
|
|
expectedName +
|
|
|
|
" to be thrown asynchronously but no exception was thrown at all");
|
2024-05-17 18:13:21 +02:00
|
|
|
},
|
|
|
|
function (thrown) {
|
2024-05-19 19:41:38 +02:00
|
|
|
var actualName;
|
2024-05-17 18:13:21 +02:00
|
|
|
if (typeof thrown !== "object" || thrown === null) {
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("Thrown value was not an object!");
|
2024-05-17 18:13:21 +02:00
|
|
|
} else if (thrown.constructor !== expectedErrorConstructor) {
|
|
|
|
actualName = thrown.constructor.name;
|
|
|
|
if (expectedName === actualName) {
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("Expected a " +
|
2024-05-17 18:13:21 +02:00
|
|
|
expectedName +
|
2024-05-19 19:41:38 +02:00
|
|
|
" but got a different error constructor with the same name");
|
2024-05-17 18:13:21 +02:00
|
|
|
}
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("Expected a " + expectedName + " but got a " + actualName);
|
2022-11-22 20:16:01 +01:00
|
|
|
}
|
|
|
|
}
|
2024-05-17 18:13:21 +02:00
|
|
|
));
|
|
|
|
} catch (thrown) {
|
2024-05-19 19:41:38 +02:00
|
|
|
fail("Expected a " +
|
|
|
|
expectedName +
|
|
|
|
" to be thrown asynchronously but .then threw synchronously");
|
2022-11-22 20:16:01 +01:00
|
|
|
}
|
2024-05-17 18:13:21 +02:00
|
|
|
});
|
2022-11-22 20:16:01 +01:00
|
|
|
};
|