Harness tests for asyncHelpers.js' asyncTest function

This commit is contained in:
Cam Tenny 2022-11-22 16:31:03 -08:00 committed by Philip Chimento
parent c056d31b9b
commit cb9b45ded2
7 changed files with 212 additions and 0 deletions

View File

@ -0,0 +1,16 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The 'asyncTest' helper reports synchronous errors via $DONE.
includes: [asyncHelpers.js]
---*/
var called = false;
function $DONE(error) {
called = true;
assert(error instanceof Test262Error);
}
asyncTest(function () {
throw new Test262Error("Should not be evaluated");
});
assert(called, "asyncTest called $DONE with a synchronously thrown error");

View File

@ -0,0 +1,19 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The 'asyncTest' helper rejects non-callable test functions.
includes: [asyncHelpers.js, compareArray.js]
---*/
const doneValues = [];
function $DONE(error) {
doneValues.push(error instanceof Test262Error);
}
asyncTest(null);
asyncTest({});
asyncTest("string");
asyncTest(42);
asyncTest(undefined);
asyncTest();
assert.compareArray(doneValues, [true, true, true, true, true, true]);

View File

@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The 'asyncTest' helper rejects test functions that do not return a thenable.
includes: [asyncHelpers.js, compareArray.js]
---*/
const doneValues = [];
function $DONE(error) {
doneValues.push(error instanceof TypeError);
}
asyncTest(function () {
return null;
});
asyncTest(function () {
return {};
});
asyncTest(function () {
return "string";
});
asyncTest(function () {
return 42;
});
asyncTest(function () {});
asyncTest(function () {
return function () {};
});
assert.compareArray(doneValues, [true, true, true, true, true, true]);

View File

@ -0,0 +1,45 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The 'asyncTest' helper when called with async flag always returns a promise that resolves to undefined.
flags: [async]
includes: [asyncHelpers.js]
---*/
var realDone = $DONE;
var doneCalls = 0;
globalThis.$DONE = function () {
doneCalls++;
};
async function assertPromiseUndefined(returnedPromise) {
assert(
returnedPromise instanceof Promise,
"returned value should be a Promise"
);
assert.sameValue(
await returnedPromise,
undefined,
"returned Promise should resolve to undefined"
);
}
(async function () {
await assertPromiseUndefined(asyncTest({}));
await assertPromiseUndefined(
asyncTest(function () {
return "non-thenable";
})
);
await assertPromiseUndefined(
asyncTest(function () {
return Promise.resolve(true);
})
);
await assertPromiseUndefined(
asyncTest(function () {
return Promise.reject(new Test262Error("oh no"));
})
);
assert.sameValue(doneCalls, 4, "asyncTest must call $DONE");
})().then(realDone, realDone);

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The 'asyncTest' helper calls $DONE with the rejection value if the test function rejects.
flags: [async]
includes: [asyncHelpers.js, compareArray.js]
---*/
const rejectionValues = [];
var realDone = $DONE;
globalThis.$DONE = function (mustBeDefined) {
rejectionValues.push(mustBeDefined);
};
const someObject = {};
(async function () {
await asyncTest(function () {
return Promise.reject(null);
});
await asyncTest(function () {
return Promise.reject(someObject);
});
await asyncTest(function () {
return Promise.reject("hi");
});
await asyncTest(function () {
return Promise.reject(10);
});
await asyncTest(function () {
return {
then(res, rej) {
rej(true);
},
};
});
assert.compareArray(rejectionValues, [null, someObject, "hi", 10, true]);
})().then(realDone, realDone);

View File

@ -0,0 +1,45 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The 'asyncTest' helper calls $DONE with undefined, regardless of what value the promise resolves with
flags: [async]
includes: [asyncHelpers.js]
---*/
var doneCalls = 0;
var realDone = $DONE;
globalThis.$DONE = function (noError) {
doneCalls++;
assert.sameValue(
noError,
undefined,
"asyncTest should discard promise's resolved value"
);
};
(async function () {
await asyncTest(function () {
return Promise.resolve(null);
});
assert.sameValue(doneCalls, 1, "asyncTest called $DONE with undefined");
await asyncTest(function () {
return Promise.resolve({});
});
assert.sameValue(doneCalls, 2, "asyncTest called $DONE with undefined");
await asyncTest(function () {
return Promise.resolve("hi");
});
assert.sameValue(doneCalls, 3, "asyncTest called $DONE with undefined");
await asyncTest(function () {
return Promise.resolve(10);
});
assert.sameValue(doneCalls, 4, "asyncTest called $DONE with undefined");
await asyncTest(function () {
return {
then(res, rej) {
res(true);
},
};
});
assert.sameValue(doneCalls, 5, "asyncTest called $DONE with undefined");
})().then(realDone, realDone);

View File

@ -0,0 +1,21 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The 'asyncTest' helper checks that it is called with the 'async' flag.
includes: [asyncHelpers.js]
---*/
function makePromise() {
return {
then(res, rej) {
throw new Test262Error("Should not be evaluated");
},
};
}
assert(
!Object.hasOwn(globalThis, "$DONE"),
"Without 'async' flag, $DONE should not be defined"
);
assert.throws(Test262Error, function () {
asyncTest(makePromise);
});