-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Harness tests for asyncHelpers.js' assert.throwsAsync function
- Loading branch information
Showing
13 changed files
with
538 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
63 changes: 63 additions & 0 deletions
63
test/harness/asyncHelpers-throwsAsync-funcOrThenable-throws-sync.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
test/harness/asyncHelpers-throwsAsync-invalid-funcOrThenable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.