UNPKG

@drozdik.m/unit-test

Version:

Unit test with test cases with Assert functions. Simple and easy.

173 lines (172 loc) 6.38 kB
exports.__esModule = true; var AssertFailedError_1 = require("./Errors/AssertFailedError"); var Assert = /** @class */ (function () { function Assert() { } /** * Returns error count * */ Assert.ErrorCount = function () { return Assert.errorCount; }; /** * Resets error count * */ Assert.ResetErrorCount = function () { Assert.errorCount = 0; }; /** * Handles occured error * */ Assert.ErrorOccured = function () { Assert.errorCount += 1; }; //-------------------------------------------------- //----------ASSERT---------------------------------- //-------------------------------------------------- /** * Testing method. If input is false, error is thrown. * @param input Testing sample (boolean) */ Assert.Assert = function (input) { if (!input) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Input is false"); } }; //-------------------------------------------------- //----------EQUAL----------------------------------- //-------------------------------------------------- /** * Testing method. If parameter1 == parameter2 is false, error is thrown. * @param expected Expected result * @param actual Actual result */ Assert.AreEqual = function (expected, actual) { if (expected != actual) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Expected: " + expected + ", actual: " + actual); } }; /** * Testing method. If parameter1 != parameter2 is false, error is thrown. * @param notExpected Not expected result * @param actual Actual result */ Assert.AreNotEqual = function (notExpected, actual) { if (notExpected == actual) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Not expected: " + notExpected + ", actual: " + actual); } }; //-------------------------------------------------- //----------SAME------------------------------------ //-------------------------------------------------- /** * Testing method. If type of parameter 1 == type of parameter 2 is false, error is thrown. * @param expected Expected type * @param actual Actual type */ Assert.AreSame = function (expected, actual) { if (typeof expected != typeof actual) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Not expected: " + expected + ", actual: " + actual); } }; /** * Testing method. If type of parameter 1 != type of parameter 2 is false, error is thrown. * @param notExpected Not expected type * @param actual Actual type */ Assert.AreNotSame = function (notExpected, actual) { if (typeof notExpected == typeof actual) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Not expected: " + notExpected + ", actual: " + actual); } }; //-------------------------------------------------- //----------NULL------------------------------------ //-------------------------------------------------- /** * Testing method. If the input is not null, error is thrown. * @param input Input */ Assert.IsNull = function (input) { if (input != null) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Input is not null, expected not null"); } }; /** * Testing method. If the input is null, error is thrown. * @param input Input */ Assert.IsNotNull = function (input) { if (input == null) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Input is null, expected not null"); } }; //-------------------------------------------------- //----------DEFINED--------------------------------- //-------------------------------------------------- /** * Testing method. If the input is not undefined, error is thrown. * @param input */ Assert.IsUndefined = function (input) { if (typeof input != "undefined") { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Input is defined, expected undefined"); } }; /** * Testing method. If the input is undefined, error is thrown. * @param input */ Assert.IsDefined = function (input) { if (typeof input == "undefined") { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Input is undefined, expected defined"); } }; //-------------------------------------------------- //----------BOOLEAN--------------------------------- //-------------------------------------------------- /** * Testing method. If the input is false, error is called. * @param input Input */ Assert.IsTrue = function (input) { if (!input) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Input is false, expected true"); } }; /** * Testing method. If the input is true, error is called. * @param input Input */ Assert.IsFalse = function (input) { if (input) { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Input is true, expected false"); } }; //-------------------------------------------------- //----------FAIL------------------------------------ //-------------------------------------------------- /** * Fails an assert. * */ Assert.Fail = function () { Assert.ErrorOccured(); throw new AssertFailedError_1.AssertFailedError("Assert fail"); }; //-------------------------------------------------- //----------COUNT---------------------------------- //-------------------------------------------------- Assert.errorCount = 0; return Assert; }()); exports.Assert = Assert;