|
| 1 | +// Copyright (C) 2022 Igalia, S.L. All rights reserved. |
| 2 | +// This code is governed by the BSD license found in the LICENSE file. |
| 3 | +/*--- |
| 4 | +description: | |
| 5 | + A collection of assertion and wrapper functions for testing asynchronous built-ins. |
| 6 | +defines: [asyncTest] |
| 7 | +---*/ |
| 8 | + |
| 9 | +function asyncTest(testFunc) { |
| 10 | + if (!Object.hasOwn(globalThis, "$DONE")) { |
| 11 | + throw new Test262Error("asyncTest called without async flag"); |
| 12 | + } |
| 13 | + if (typeof testFunc !== "function") { |
| 14 | + $DONE(new Test262Error("asyncTest called with non-function argument")); |
| 15 | + return; |
| 16 | + } |
| 17 | + try { |
| 18 | + testFunc().then( |
| 19 | + function () { |
| 20 | + $DONE(); |
| 21 | + }, |
| 22 | + function (error) { |
| 23 | + $DONE(error); |
| 24 | + } |
| 25 | + ); |
| 26 | + } catch (syncError) { |
| 27 | + $DONE(syncError); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +assert.throwsAsync = function (expectedErrorConstructor, func, message) { |
| 32 | + return new Promise(function (resolve) { |
| 33 | + var innerThenable; |
| 34 | + if (message === undefined) { |
| 35 | + message = ""; |
| 36 | + } else { |
| 37 | + message += " "; |
| 38 | + } |
| 39 | + if (typeof func === "function") { |
| 40 | + try { |
| 41 | + innerThenable = func(); |
| 42 | + if ( |
| 43 | + innerThenable === null || |
| 44 | + typeof innerThenable !== "object" || |
| 45 | + typeof innerThenable.then !== "function" |
| 46 | + ) { |
| 47 | + message += |
| 48 | + "Expected to obtain an inner promise that would reject with a" + |
| 49 | + expectedErrorConstructor.name + |
| 50 | + " but result was not a thenable"; |
| 51 | + throw new Test262Error(message); |
| 52 | + } |
| 53 | + } catch (thrown) { |
| 54 | + message += |
| 55 | + "Expected a " + |
| 56 | + expectedErrorConstructor.name + |
| 57 | + " to be thrown asynchronously but an exception was thrown synchronously while obtaining the inner promise"; |
| 58 | + throw new Test262Error(message); |
| 59 | + } |
| 60 | + } else { |
| 61 | + message += |
| 62 | + "assert.throwsAsync called with an argument that is not a function"; |
| 63 | + throw new Test262Error(message); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + resolve(innerThenable.then( |
| 68 | + function () { |
| 69 | + message += |
| 70 | + "Expected a " + |
| 71 | + expectedErrorConstructor.name + |
| 72 | + " to be thrown asynchronously but no exception was thrown at all"; |
| 73 | + throw new Test262Error(message); |
| 74 | + }, |
| 75 | + function (thrown) { |
| 76 | + var expectedName, actualName; |
| 77 | + if (typeof thrown !== "object" || thrown === null) { |
| 78 | + message += "Thrown value was not an object!"; |
| 79 | + throw new Test262Error(message); |
| 80 | + } else if (thrown.constructor !== expectedErrorConstructor) { |
| 81 | + expectedName = expectedErrorConstructor.name; |
| 82 | + actualName = thrown.constructor.name; |
| 83 | + if (expectedName === actualName) { |
| 84 | + message += |
| 85 | + "Expected a " + |
| 86 | + expectedName + |
| 87 | + " but got a different error constructor with the same name"; |
| 88 | + } else { |
| 89 | + message += |
| 90 | + "Expected a " + expectedName + " but got a " + actualName; |
| 91 | + } |
| 92 | + throw new Test262Error(message); |
| 93 | + } |
| 94 | + } |
| 95 | + )); |
| 96 | + } catch (thrown) { |
| 97 | + if (typeof thrown !== "object" || thrown === null) { |
| 98 | + message += |
| 99 | + "Expected a " + |
| 100 | + expectedErrorConstructor.name + |
| 101 | + " to be thrown asynchronously but innerThenable synchronously threw a value that was not an object "; |
| 102 | + } else { |
| 103 | + message += |
| 104 | + "Expected a " + |
| 105 | + expectedErrorConstructor.name + |
| 106 | + " to be thrown asynchronously but a " + |
| 107 | + thrown.constructor.name + |
| 108 | + " was thrown synchronously"; |
| 109 | + } |
| 110 | + throw new Test262Error(message); |
| 111 | + } |
| 112 | + }); |
| 113 | +}; |
0 commit comments