Skip to content

Commit

Permalink
Harness tests for asyncHelpers.js' assert.throwsAsync function
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtenny authored and ptomato committed Feb 21, 2023
1 parent cb9b45d commit 0294bea
Show file tree
Hide file tree
Showing 13 changed files with 538 additions and 0 deletions.
72 changes: 72 additions & 0 deletions test/harness/asyncHelpers-throwsAsync-custom-typeerror.js
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions test/harness/asyncHelpers-throwsAsync-custom.js
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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);
33 changes: 33 additions & 0 deletions test/harness/asyncHelpers-throwsAsync-incorrect-ctor.js
Original file line number Diff line number Diff line change
@@ -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);
71 changes: 71 additions & 0 deletions test/harness/asyncHelpers-throwsAsync-invalid-funcOrThenable.js
Original file line number Diff line number Diff line change
@@ -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);
47 changes: 47 additions & 0 deletions test/harness/asyncHelpers-throwsAsync-native.js
Original file line number Diff line number Diff line change
@@ -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);
32 changes: 32 additions & 0 deletions test/harness/asyncHelpers-throwsAsync-no-arg.js
Original file line number Diff line number Diff line change
@@ -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);
32 changes: 32 additions & 0 deletions test/harness/asyncHelpers-throwsAsync-no-error.js
Original file line number Diff line number Diff line change
@@ -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);
Loading

0 comments on commit 0294bea

Please sign in to comment.