Harness tests for asyncHelpers.js' assert.throwsAsync function

This commit is contained in:
Cam Tenny 2022-11-22 18:07:14 -08:00 committed by Philip Chimento
parent cb9b45ded2
commit 0294bea997
13 changed files with 538 additions and 0 deletions

View File

@ -0,0 +1,72 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that reject with instances of the specified constructor function
satisfy the assertion, without collision with error constructors of the same name.
flags: [async]
includes: [asyncHelpers.js]
---*/
var intrinsicTypeError = TypeError;
(async function () {
function TypeError() {}
var caught = false;
var p = assert.throwsAsync(
TypeError,
async function () {
throw new TypeError();
},
"Throws an instance of the matching custom TypeError"
);
assert(p instanceof Promise);
await p;
p = assert.throwsAsync(intrinsicTypeError, async function () {
throw new TypeError();
});
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject a collision of constructor names"
);
}
caught = false;
p = assert.throwsAsync(TypeError, async function () {
throw new intrinsicTypeError();
});
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject a collision of constructor names"
);
}
})().then($DONE, $DONE);

View File

@ -0,0 +1,17 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that reject with values of the specified constructor function
satisfy the assertion.
flags: [async]
includes: [asyncHelpers.js]
---*/
function MyError() {}
(async function () {
const p = assert.throwsAsync(MyError, Promise.reject(new MyError()));
assert(p instanceof Promise);
await p;
})().then($DONE, $DONE);

View File

@ -0,0 +1,63 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
assert.throwsAsync returns a promise that rejects if funcOrThenable or the inner thenable synchronously throws.
flags: [async]
includes: [asyncHelpers.js]
---*/
async function checkRejects(funcOrThenable) {
var caught = false;
const p = assert.throwsAsync(Test262Error, funcOrThenable);
assert(p instanceof Promise, "assert.throwsAsync should return a promise");
try {
await p;
} catch (e) {
caught = true;
assert.sameValue(
e.constructor,
Test262Error,
"throwsAsync should reject improper funcOrThenable with a Test262Error"
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject improper funcOrThenable " +
funcOrThenable
);
}
}
(async function () {
await checkRejects(function () {
throw new Error();
});
await checkRejects(function () {
throw new Test262Error();
});
await checkRejects({
then: function () {
throw new Error();
},
});
await checkRejects({
then: function () {
throw new Test262Error();
},
});
await checkRejects(function () {
return {
then: function () {
throw new Error();
},
};
});
await checkRejects(function () {
return {
then: function () {
throw new Test262Error();
},
};
});
})().then($DONE, $DONE);

View File

@ -0,0 +1,33 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that reject with values whose constructor does not match the specified
constructor do not satisfy the assertion.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var caught = false;
const p = assert.throwsAsync(Error, Promise.reject(new TypeError()));
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when a value with incorrect constructor was thrown"
);
}
})().then($DONE, $DONE);

View File

@ -0,0 +1,71 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
assert.throwsAsync returns a promise that rejects if funcOrThenable is not a function returning a thenable or a thenable.
flags: [async]
includes: [asyncHelpers.js]
---*/
async function checkRejects(funcOrThenable) {
var caught = false;
const p = assert.throwsAsync(Test262Error, funcOrThenable);
assert(p instanceof Promise, "assert.throwsAsync should return a promise");
try {
await p;
} catch (e) {
caught = true;
assert.sameValue(
e.constructor,
Test262Error,
"throwsAsync should reject improper funcOrThenable with a Test262Error"
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject improper funcOrThenable " +
funcOrThenable
);
}
}
(async function () {
await checkRejects(null);
await checkRejects({});
await checkRejects("string");
await checkRejects(10);
await checkRejects();
await checkRejects({ then: null });
await checkRejects({ then: {} });
await checkRejects({ then: "string" });
await checkRejects({ then: 10 });
await checkRejects({ then: undefined });
await checkRejects(function () {
return null;
});
await checkRejects(function () {
return {};
});
await checkRejects(function () {
return "string";
});
await checkRejects(function () {
return 10;
});
await checkRejects(function () {});
await checkRejects(function () {
return { then: null };
});
await checkRejects(function () {
return { then: {} };
});
await checkRejects(function () {
return { then: "string" };
});
await checkRejects(function () {
return { then: 10 };
});
await checkRejects(function () {
return { then: undefined };
});
})().then($DONE, $DONE);

View File

@ -0,0 +1,47 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that reject with instances of the specified native Error constructor
satisfy the assertion.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var p = assert.throwsAsync(Error, async function () {
throw new Error();
});
assert(p instanceof Promise);
await p;
p = assert.throwsAsync(EvalError, async function () {
throw new EvalError();
});
assert(p instanceof Promise);
await p;
p = assert.throwsAsync(RangeError, async function () {
throw new RangeError();
});
assert(p instanceof Promise);
await p;
p = assert.throwsAsync(ReferenceError, async function () {
throw new ReferenceError();
});
assert(p instanceof Promise);
await p;
p = assert.throwsAsync(SyntaxError, async function () {
throw new SyntaxError();
});
assert(p instanceof Promise);
await p;
p = assert.throwsAsync(TypeError, async function () {
throw new TypeError();
});
assert(p instanceof Promise);
await p;
p = assert.throwsAsync(URIError, async function () {
throw new URIError();
});
assert(p instanceof Promise);
await p;
})().then($DONE, $DONE);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
The assertion fails when invoked without arguments.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var caught = false;
const p = assert.throwsAsync();
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when invoked without arguments"
);
}
})().then($DONE, $DONE);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that do not reject do not satisfy the assertion.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var caught = false;
const p = assert.throwsAsync(Error, async function () {});
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when the thenable did not reject"
);
}
})().then($DONE, $DONE);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that reject with the `null` value do not satisfy the assertion.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var caught = false;
const p = assert.throwsAsync(Error, async function () {
throw null;
});
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when null was thrown"
);
}
})().then($DONE, $DONE);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that reject with primitive values do not satisfy the assertion.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var caught = false;
const p = assert.throwsAsync(Error, async function () {
throw 3;
});
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when a primitive was thrown"
);
}
})().then($DONE, $DONE);

View File

@ -0,0 +1,35 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Thenables that resolve with an error do not satisfy the assertion.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var caught = false;
const p = assert.throwsAsync(
Error,
Promise.resolve(new Error("it's-a-me, Chris Pratt"))
);
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when the thenable resolved with an error"
);
}
})().then($DONE, $DONE);

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: |
Thenables that reject with instances of the realm specified constructor function
satisfy the assertion, without cross realms collisions.
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var intrinsicTypeError = TypeError;
var caught = false;
var realmGlobal = $262.createRealm().global;
const p = assert.throwsAsync(TypeError, async function () {
throw new realmGlobal.TypeError();
});
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when a different realm's error was thrown"
);
}
})().then($DONE, $DONE);

View File

@ -0,0 +1,31 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
assert.throwsAsync returns a promise that rejects when invoked with a single argument
flags: [async]
includes: [asyncHelpers.js]
---*/
(async function () {
var caught = false;
const p = assert.throwsAsync(function () {});
assert(p instanceof Promise);
try {
await p;
} catch (err) {
caught = true;
assert.sameValue(
err.constructor,
Test262Error,
"Expected a Test262Error, but a '" +
err.constructor.name +
"' was thrown."
);
} finally {
assert(
caught,
"assert.throwsAsync did not reject when invoked with a single argumemnt"
);
}
})().then($DONE, $DONE);