UNPKG

soft-assertion

Version:
888 lines 29.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Assert = void 0; const LogHelper_js_1 = require("./helper/LogHelper.js"); const lodash_1 = __importDefault(require("lodash")); /** * * Assertion - A class for performing soft assertions. * * This class provides methods to perform soft assertions, which do not throw errors immediately * but instead collect them and throw them all at once when `assertAll` is called. * */ class Assert { constructor() { // To hold assertion errors temporarily this.assertionErrors = []; } /** * Asserts that two values are strictly equal.\ * If they are not, an error is thrown and captured in the assertion errors list. * * @param actual { any } - The actual value to test. * @param expected { any } - The expected value to compare against. * @param message { string } - A descriptive message for the assertion. * * --- * * Examples: * ```ts * equals("foo", "foo", "Oh no"); // Pass * equals("foo", "doo", "Oh no"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored * */ equals(actual, expected, message) { if (!lodash_1.default.isEqual(actual, expected)) { const error = new Error(`[Assertion failed: equals] ${message}\n Expected: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts that actual string contains expected string.\ * If they are not, an error is thrown and captured in the assertion errors list. * * --- * * --- * @param actual {any} - The actual value to be checked. * @param expected {any} - The expected value to check for. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * * ```ts * includes('Lazy fox!', 'y f', 'Oh no!'); // Pass * includes('Lazy fox!', ' ', 'Oh no!'); // Pass * includes('Lazy fox!', 'Y f', 'Oh no!'); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored */ includes(actual, expected, message) { const condition = lodash_1.default.isObject(actual) && !lodash_1.default.isArray(actual) ? lodash_1.default.has(actual, expected) : lodash_1.default.includes(actual, expected); if (!condition) { const error = new Error(`[Assertion failed: includes] ${message}\n Expected: ${JSON.stringify(expected)}\n Actual: ${JSON.stringify(actual)}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is strictly true.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {boolean} - The value for the assertion * @param message {string} -The message to be displayed if the assertion fails * * --- * * Examples: * ```ts * isTrue(false, "Oh no"); // Fail * isTrue(true, "Oh no"); // Pass * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored */ isTrue(value, message) { if (!value || typeof value !== "boolean") { const error = new Error(`[Assertion failed: isTrue] ${message}\n Expected: true\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is strictly false.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {boolean} - The value for the assertion * @param message {string} - The message to be displayed if the assertion fails * * --- * * Examples: * ```ts * isFalse(false, "Oh no"); // Pass * isFalse(true, "Oh no"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ isFalse(value, message) { if (value) { const error = new Error(`[Assertion failed: isFalse] ${message}\n Expected: false\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts that two values are strictly not equal.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param actual { any } - The actual value to be compared. * @param expected { any } - The expected value to compare against. * @param message { string } - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * notEqual("foo", "foo", "Oh no!"); // Pass * notEqual("foo", "doo", "Oh no!"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored */ notEqual(actual, expected, message) { if (lodash_1.default.isEqual(actual, expected)) { const error = new Error(`[Assertion failed: notEqual] ${message}\n Value should not be equal to: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the actual value is greater than the expected value.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param actual {number} - The actual value to be compared. * @param expected {number} - The expected value to compare against. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * greaterThan(11, 10, 'Oh no!'); // Pass * * greaterThan(1, 10, 'Oh no!'); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. * */ greaterThan(actual, expected, message) { if (actual <= expected) { const error = new Error(`[Assertion failed: greaterThan] ${message}\n Expected a value greater than: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the actual value is less than the expected value.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param actual {number} - The actual value to be compared. * @param expected {number} - The expected value to compare against. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * isLessThan(1, 10, 'Oh no!'); // Pass * isLessThan(-11, 0, 'Oh no!'); // Pass * isLessThan(11, 0, 'Oh no!'); // Fail * * isLessThan(Infinity, 0, "Oh no!"); // Fail * isLessThan(Infinity, Infinity, "Oh no!"); // Fail * isLessThan(0, 0, "Oh no!"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ isLessThan(actual, expected, message) { if (actual >= expected) { const error = new Error(`[Assertion failed: isLessThan] ${message}\n Expected a value less than: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is not null.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * notNull(1, 'Oh no!'); // Pass * * notNull(null, 'Oh no!'); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ notNull(value, message) { if (value === null) { const error = new Error(`[Assertion failed: notNull] ${message}\n Expected a non-null value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is null.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * isNull(null, "Oh no!"); // Pass * * isNull([1,3,'foo'], "Oh no!"); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ isNull(value, message) { if (value !== null) { const error = new Error(`[Assertion failed: isNull] ${message}\n Expected a null value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts the value should not be undefined.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * isDefined(Infinity, "Oh no!"); // Pass * isDefined(null, "Oh no!"); // Pass * isDefined(1, "Oh no!"); // Pass * * isDefined(undefined, "Oh no!"); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ isDefined(value, message) { if (value === undefined) { const error = new Error(`[Assertion failed: isDefined] ${message}\n Expected a defined value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is undefined.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * isUndefined(undefined, 'Oh no!'); // Pass * * isUndefined(1, 'Oh no!'); // Fail * isUndefined({obj: 'foo'}, 'Oh no!'); // Fail * isUndefined(1, 'Oh no!'); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ isUndefined(value, message) { if (value !== undefined) { const error = new Error(`[Assertion failed: isUndefined] ${message}\n Expected an undefined value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is a number.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * * ```ts * isNumber(1/0, "Oh no!"); // Fail * isNumber(NaN, "Oh no!"); // Fail * isNumber(Infinity, "Oh no!"); // Fail * isNumber(-Infinity, "Oh no!"); // Fail * isNumber(null, "Oh no!"); // Fail * isNumber(undefined, "Oh no!"); // Fail * * isNumber(42, "All good!"); // Pass * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ isNumber(value, message) { if (lodash_1.default.isNaN(value) // typeof value !== "number" || // Number.isNaN(value) || // !Number.isFinite(value) ) { const error = new Error(`[Assertion failed: isNumber] ${message}\n Expected a number\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is a string.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * * ```ts * isString(123, "Oh no!"); // Fail * isString(null, "Oh no!"); // Fail * isString(undefined, "Oh no!"); // Fail * isString(true, "Oh no!"); // Fail * isString(false, "Oh no!"); // Fail * isString(Infinity, "Oh no!"); // Fail * isString(NaN, "Oh no!"); // Fail * * isString("foo", "Oh no!"); // Pass * ``` * * --- * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. * */ isString(value, message) { if (!lodash_1.default.isString(value)) { const error = new Error(`[Assertion failed: isString] ${message}\n Expected a string\n Actual: ${value} (type: ${typeof value})`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts that two values are strictly equal.\ * If they are not, an error is thrown and captured in the assertion errors list. * * @param actual { any } - The actual value to test. * @param expected { any } - The expected value to compare against. * @param message { string } - A descriptive message for the assertion. * * --- * * Examples: * ```ts * strictEquals("foo", "foo", "Oh no"); // Pass * strictEquals("foo", "doo", "Oh no"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored * */ strictEquals(actual, expected, message) { if (!lodash_1.default.isEqual(actual, expected)) { const error = new Error(`[Assertion failed: strictEquals] ${message}\n Expected: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts that actual string contains expected string.\ * If they are not, an error is thrown and captured in the assertion errors list. * * --- * * --- * @param actual {any} - The actual value to be checked. * @param expected {any} - The expected value to check for. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * * ```ts * strictIncludes('Lazy fox!', 'y f', 'Oh no!'); // Pass * strictIncludes('Lazy fox!', ' ', 'Oh no!'); // Pass * strictIncludes('Lazy fox!', 'Y f', 'Oh no!'); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored */ strictIncludes(actual, expected, message) { const condition = lodash_1.default.isObject(actual) && !lodash_1.default.isArray(actual) ? lodash_1.default.has(actual, expected) : lodash_1.default.includes(actual, expected); if (!condition) { const error = new Error(`[Assertion failed: strictIncludes] ${message}\n Expected: ${JSON.stringify(expected)}\n Actual: ${JSON.stringify(actual)}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the value is strictly true.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {boolean} - The value for the assertion * @param message {string} -The message to be displayed if the assertion fails * * --- * * Examples: * ```ts * strictIsTrue(false, "Oh no"); // Fail * strictIsTrue(true, "Oh no"); // Pass * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored */ strictIsTrue(value, message) { if (!value || typeof value !== "boolean") { const error = new Error(`[Assertion failed: strictIsTrue] ${message}\n Expected: true\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the value is strictly false.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {boolean} - The value for the assertion * @param message {string} - The message to be displayed if the assertion fails * * --- * * Examples: * ```ts * strictIsFalse(false, "Oh no"); // Pass * strictIsFalse(true, "Oh no"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ strictIsFalse(value, message) { if (value) { const error = new Error(`[Assertion failed: strictIsFalse] ${message}\n Expected: false\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts that two values are strictly not equal.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param actual { any } - The actual value to be compared. * @param expected { any } - The expected value to compare against. * @param message { string } - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * strictNotEqual("foo", "foo", "Oh no!"); // Pass * strictNotEqual("foo", "doo", "Oh no!"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored */ strictNotEqual(actual, expected, message) { if (lodash_1.default.isEqual(actual, expected)) { const error = new Error(`[Assertion failed: strictNotEqual] ${message}\n Value should not be equal to: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the actual value is greater than the expected value.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param actual {number} - The actual value to be compared. * @param expected {number} - The expected value to compare against. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * strictGreaterThan(11, 10, 'Oh no!'); // Pass * * strictGreaterThan(1, 10, 'Oh no!'); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. * */ strictGreaterThan(actual, expected, message) { if (actual <= expected) { const error = new Error(`[Assertion failed: strictGreaterThan] ${message}\n Expected a value greater than: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the actual value is less than the expected value.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param actual {number} - The actual value to be compared. * @param expected {number} - The expected value to compare against. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * strictIsLessThan(1, 10, 'Oh no!'); // Pass * strictIsLessThan(-11, 0, 'Oh no!'); // Pass * strictIsLessThan(11, 0, 'Oh no!'); // Fail * * strictIsLessThan(Infinity, 0, "Oh no!"); // Fail * strictIsLessThan(Infinity, Infinity, "Oh no!"); // Fail * strictIsLessThan(0, 0, "Oh no!"); // Fail * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ strictIsLessThan(actual, expected, message) { if (actual >= expected) { const error = new Error(`[Assertion failed: strictIsLessThan] ${message}\n Expected a value less than: ${expected}\n Actual: ${actual}`); this.assertionErrors.push({ message: `${error.stack}`, }); } } /** * Asserts if the value is not null.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * strictNotNull(1, 'Oh no!'); // Pass * * strictNotNull(null, 'Oh no!'); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ strictNotNull(value, message) { if (value === null) { const error = new Error(`[Assertion failed: strictNotNull] ${message}\n Expected a non-null value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the value is null.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * strictIsNull(null, "Oh no!"); // Pass * * strictIsNull([1,3,'foo'], "Oh no!"); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ strictIsNull(value, message) { if (value !== null) { const error = new Error(`[Assertion failed: strictIsNull] ${message}\n Expected a null value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts the value should not be undefined.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * strictIsDefined(Infinity, "Oh no!"); // Pass * strictIsDefined(null, "Oh no!"); // Pass * strictIsDefined(1, "Oh no!"); // Pass * * strictIsDefined(undefined, "Oh no!"); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ strictIsDefined(value, message) { if (value === undefined) { const error = new Error(`[Assertion failed: strictIsDefined] ${message}\n Expected a defined value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the value is undefined.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * ```ts * strictIsUndefined(undefined, 'Oh no!'); // Pass * * strictIsUndefined(1, 'Oh no!'); // Fail * strictIsUndefined({obj: 'foo'}, 'Oh no!'); // Fail * strictIsUndefined(1, 'Oh no!'); // Fail * ``` * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ strictIsUndefined(value, message) { if (value !== undefined) { const error = new Error(`[Assertion failed: strictIsUndefined] ${message}\n Expected an undefined value\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the value is a number.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * * ```ts * strictIsNumber(1/0, "Oh no!"); // Fail * strictIsNumber(NaN, "Oh no!"); // Fail * strictIsNumber(Infinity, "Oh no!"); // Fail * strictIsNumber(-Infinity, "Oh no!"); // Fail * strictIsNumber(null, "Oh no!"); // Fail * strictIsNumber(undefined, "Oh no!"); // Fail * * strictIsNumber(42, "All good!"); // Pass * ``` * * --- * * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. */ strictIsNumber(value, message) { if (lodash_1.default.isNaN(value) // typeof value !== "number" || // Number.isNaN(value) || // !Number.isFinite(value) ) { const error = new Error(`[Assertion failed: strictIsNumber] ${message}\n Expected a number\n Actual: ${value}`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * Asserts if the value is a string.\ * If condition is failed, an error is thrown and captured in the assertion errors list. * * @param value {any} - The value to be checked. * @param message {string} - The message to be displayed if the assertion fails. * * --- * * Examples: * * ```ts * strictIsString(123, "Oh no!"); // Fail * strictIsString(null, "Oh no!"); // Fail * strictIsString(undefined, "Oh no!"); // Fail * strictIsString(true, "Oh no!"); // Fail * strictIsString(false, "Oh no!"); // Fail * strictIsString(Infinity, "Oh no!"); // Fail * strictIsString(NaN, "Oh no!"); // Fail * * strictIsString("foo", "Oh no!"); // Pass * ``` * * --- * ### Case * - Pass: No error would be stored. * - Fail: Assertion error will be stored. * */ strictIsString(value, message) { if (!lodash_1.default.isString(value)) { const error = new Error(`[Assertion failed: strictIsString] ${message}\n Expected a string\n Actual: ${value} (type: ${typeof value})`); this.assertionErrors.push({ message: `${error.stack}`, }); this.assertAll(); } } /** * assertAll - Throw error (if any) for all assertions */ assertAll() { if (this.assertionErrors.length) { const errorMessageData = this.assertionErrors; this.assertionErrors = []; (0, LogHelper_js_1.throwAssertionErrors)(errorMessageData); } } } exports.Assert = Assert; //# sourceMappingURL=assert.js.map