@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
238 lines • 10.6 kB
JavaScript
;
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertEx = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importStar(require("assert"));
var node_1 = require("./node");
var nodeVersion = node_1.getNodeVersion();
function getNode8AndBelowErrorObject(error) {
return {
message: error.message,
name: error.name
};
}
/**
* A collection of additional assertion checks on top of the standard assert checks.
*/
var assertEx;
(function (assertEx) {
/**
* Check that the provided text contains the provided substring.
* @param value The text to look in.
* @param substring The substring to look for.
*/
function contains(value, substring) {
var errorMessage = Array.isArray(value)
? "Expected " + JSON.stringify(value, undefined, 2) + " to contain\n " + JSON.stringify(substring) + "."
: "Expected\n " + JSON.stringify(value) + "\nto contain\n " + JSON.stringify(substring) + ".";
assert_1.default(value && substring && value.indexOf(substring) !== -1, errorMessage);
}
assertEx.contains = contains;
/**
* Check that the provided text contains the all of the provided substrings.
* @param value The text to look in.
* @param substrings The substring to look for.
*/
function containsAll(value, substrings) {
var e_1, _a;
if (substrings) {
try {
for (var substrings_1 = tslib_1.__values(substrings), substrings_1_1 = substrings_1.next(); !substrings_1_1.done; substrings_1_1 = substrings_1.next()) {
var substring = substrings_1_1.value;
contains(value, substring);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (substrings_1_1 && !substrings_1_1.done && (_a = substrings_1.return)) _a.call(substrings_1);
}
finally { if (e_1) throw e_1.error; }
}
}
}
assertEx.containsAll = containsAll;
/**
* Check that the provided text does not contain the provided substring.
* @param value The text to look in.
* @param substring The substring to look for.
*/
function doesNotContain(value, substring) {
var errorMessage = Array.isArray(value)
? "Expected " + JSON.stringify(value, undefined, 2) + " to not contain\n " + JSON.stringify(substring) + "."
: "Expected\n " + JSON.stringify(value) + "\nto not contain\n " + JSON.stringify(substring) + ".";
assert_1.default(value && substring && value.indexOf(substring) === -1, errorMessage);
}
assertEx.doesNotContain = doesNotContain;
/**
* Check that the provided text doesn't contain any of the provided substrings.
* @param value The text to look in.
* @param substrings The substrings to look for.
*/
function doesNotContainAny(value, substrings) {
var e_2, _a;
if (substrings) {
try {
for (var substrings_2 = tslib_1.__values(substrings), substrings_2_1 = substrings_2.next(); !substrings_2_1.done; substrings_2_1 = substrings_2.next()) {
var substring = substrings_2_1.value;
doesNotContain(value, substring);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (substrings_2_1 && !substrings_2_1.done && (_a = substrings_2.return)) _a.call(substrings_2);
}
finally { if (e_2) throw e_2.error; }
}
}
}
assertEx.doesNotContainAny = doesNotContainAny;
/**
* Check that the two errors are equal (except for their stack property).
* @param actualError The actual error.
* @param expectedError The expected error.
* @param message The optional message to output if this check fails.
*/
function equalErrors(actualError, expectedError, message) {
actualError.stack = undefined;
expectedError.stack = undefined;
if (nodeVersion.major <= 8) {
actualError = getNode8AndBelowErrorObject(actualError);
expectedError = getNode8AndBelowErrorObject(expectedError);
}
assert_1.default.deepEqual(actualError, expectedError, message);
}
assertEx.equalErrors = equalErrors;
function validateThrownError(thrownError, expectedError) {
if (!thrownError) {
throw new assert_1.AssertionError({ message: "Missing expected exception.", operator: "throws" });
}
else if (expectedError instanceof Error) {
equalErrors(thrownError, expectedError);
}
else if (expectedError) {
expectedError(thrownError);
}
return thrownError;
}
/**
* Assert that the provided syncFunction throws an Error. If the expectedError is undefined, then
* this function will just assert that an Error was thrown. If the expectedError is defined, then
* this function will assert that the Error that was thrown is equal to the provided expectedError.
* @param syncFunction The synchronous function that is expected to thrown an Error.
* @param expectedError The Error that is expected to be thrown.
*/
function throws(syncFunction, expectedError) {
var thrownError;
try {
syncFunction();
}
catch (error) {
thrownError = error;
}
return validateThrownError(thrownError, expectedError);
}
assertEx.throws = throws;
/**
* Assert that the provided asyncFunction throws an Error. If the expectedError is undefined, then
* this function will just assert that an Error was thrown. If the expectedError is defined, then
* this function will assert that the Error that was thrown is equal to the provided expectedError.
* @param asyncFunction The asynchronous function that is expected to thrown an Error.
* @param expectedError The Error that is expected to be thrown.
*/
function throwsAsync(asyncFunction, expectedError) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var thrownError, error_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, (typeof asyncFunction === "function" ? asyncFunction() : asyncFunction)];
case 1:
_a.sent();
return [3 /*break*/, 3];
case 2:
error_1 = _a.sent();
thrownError = error_1;
return [3 /*break*/, 3];
case 3: return [2 /*return*/, validateThrownError(thrownError, expectedError)];
}
});
});
}
assertEx.throwsAsync = throwsAsync;
/**
* Assert that the provided value starts with the provided prefix.
* @param value The value to check.
* @param prefix The prefix to look for.
* @param expressionName The name of the expression that provided the value.
*/
function startsWith(value, prefix, expressionName) {
defined(value, expressionName);
defined(prefix, "prefix");
assert_1.default(value.startsWith(prefix), (expressionName || "value") + " (" + value + ") must start with the provided prefix (" + prefix + ").");
}
assertEx.startsWith = startsWith;
/**
* Assert that the provided value is defined. If it is, return the defined value.
* @param value The value to check.
* @param expressionName The name of the expression that provided the value.
*/
function defined(value, expressionName) {
assert_1.default(value != undefined, (expressionName || "value") + " must be defined.");
return value;
}
assertEx.defined = defined;
function definedAndNotEmpty(value, expressionName) {
expressionName = expressionName || "value";
defined(value, expressionName);
assert_1.default(value, expressionName + " cannot be empty.");
}
assertEx.definedAndNotEmpty = definedAndNotEmpty;
function definedAndNotStrictEqual(actualValue, expectedValue, expressionName) {
expressionName = expressionName || "actualValue";
defined(actualValue, expressionName);
assert_1.default.notStrictEqual(actualValue, expectedValue, expressionName + " (" + actualValue + ") cannot be strictly equal to " + expectedValue + ".");
}
assertEx.definedAndNotStrictEqual = definedAndNotStrictEqual;
function greaterThan(value, expectedLowerBound, expressionName) {
defined(value, expressionName);
defined(expectedLowerBound, "expectedLowerBound");
assert_1.default(value > expectedLowerBound, (expressionName || "value") + " (" + value + ") must be greater than " + expectedLowerBound + ".");
}
assertEx.greaterThan = greaterThan;
/**
* Assert that the provided value is equal to one of the provided expectedValues.
* @param value The value to look for.
* @param expectedValues The expected values that value could be.
*/
function oneOf(value, expectedValues) {
var e_3, _a;
var found = false;
try {
for (var expectedValues_1 = tslib_1.__values(expectedValues), expectedValues_1_1 = expectedValues_1.next(); !expectedValues_1_1.done; expectedValues_1_1 = expectedValues_1.next()) {
var expectedValue = expectedValues_1_1.value;
if (value === expectedValue) {
found = true;
break;
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (expectedValues_1_1 && !expectedValues_1_1.done && (_a = expectedValues_1.return)) _a.call(expectedValues_1);
}
finally { if (e_3) throw e_3.error; }
}
assert_1.default(found, "Expected " + value + " to be one of the following values: " + JSON.stringify(expectedValues));
}
assertEx.oneOf = oneOf;
})(assertEx = exports.assertEx || (exports.assertEx = {}));
//# sourceMappingURL=assertEx.js.map