1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

LibJS: Add JSDoc to test-common.js

This commit is contained in:
Linus Groh 2020-04-20 16:23:07 +01:00 committed by Andreas Kling
parent e578b7884b
commit cff68af965

View file

@ -1,4 +1,9 @@
/**
* Custom error for failed assertions.
* @constructor
* @param {string} message Error message
* @returns Error
*/
function AssertionError(message) { function AssertionError(message) {
var instance = new Error(message); var instance = new Error(message);
instance.name = 'AssertionError'; instance.name = 'AssertionError';
@ -6,16 +11,32 @@ function AssertionError(message) {
return instance; return instance;
} }
/**
* Throws an `AssertionError` if `value` is not truthy.
* @param {*} value Value to be tested
*/
function assert(value) { function assert(value) {
if (!value) if (!value)
throw new AssertionError("The assertion failed!"); throw new AssertionError("The assertion failed!");
} }
/**
* Throws an `AssertionError` when called.
* @throws {AssertionError}
*/
function assertNotReached() { function assertNotReached() {
throw new AssertionError("assertNotReached() was reached!"); throw new AssertionError("assertNotReached() was reached!");
} }
function assertThrowsError(testFunction, options) { /**
* Ensures the provided functions throws a specific error.
* @param {Function} testFunction Function executing the throwing code
* @param {object} [options]
* @param {Error} [options.error] Expected error type
* @param {string} [options.name] Expected error name
* @param {string} [options.message] Expected error message
*/
function assertThrowsError(testFunction, options = {}) {
try { try {
testFunction(); testFunction();
assertNotReached(); assertNotReached();