tia
Version:
Time is All (logs driven test engine with ExtJs support)
315 lines (314 loc) • 10.3 kB
JavaScript
;
/**
* Test description.
* **[[GlobalTiaObjects|gT]].a**
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Assertions can use results accumulators.
* I.e. the map which remembers if there was some error for current name.
*/
const resultAccumulators = {};
function checkAccName(name) {
if (typeof name !== 'string') {
throw new Error('Use string type for accumulator name');
}
}
function initResultsAccumulator(name) {
checkAccName(name);
resultAccumulators[name] = true;
}
exports.initResultsAccumulator = initResultsAccumulator;
function getResultFromAccumulator(name) {
checkAccName(name);
return resultAccumulators[name];
}
exports.getResultFromAccumulator = getResultFromAccumulator;
function deleteResultAccumulator(name) {
checkAccName(name);
delete resultAccumulators[name];
}
exports.deleteResultAccumulator = deleteResultAccumulator;
function mergeResultToAccumulator(res, name) {
checkAccName(name);
resultAccumulators[name] = resultAccumulators[name] && res;
}
exports.mergeResultToAccumulator = mergeResultToAccumulator;
/**
* For shortening the code.
* Adds result accumulators usage to fail call.
* @param msg
*/
function failWrapper(msg, mode) {
gT.l.fail(msg);
if (mode && mode.accName) {
checkAccName(mode.accName);
resultAccumulators[mode.accName] = false;
}
}
/**
* The parameter for all assertions.
* @param {Object} [mode] the mode for pass case.
* @param {Boolean} [mode.passSilently] - do not show message.
* @param {Boolean} [mode.noPassIncrement] - do not increment pass counter.
* @param {Boolean} [mode.accName] - the name for result accumulator which will be falsed
* if some assertion is failed.
* /
/**
* Checks that specified condition is true.
* @param condition
* @param msg - message to describe the entity which you expect.
*/
function checkIfTrue(condition, msg, mode) {
if (condition) {
gT.l.pass(msg, mode);
return true;
}
failWrapper(msg, mode);
return false;
}
exports.true = checkIfTrue;
/**
* Checks that specified condition is false.
* @param condition
* @param msg - message to describe the entity which you expect.
* param {Object} mode - see 'true' assertion description.
*/
function checkIfFalse(condition, msg, mode) {
return checkIfTrue(!condition, msg, mode);
}
exports.false = checkIfFalse;
/**
* Checks that value equals to expected value.
* @param {*} actVal - actual value.
* @param {*} expVal - expected value.
* @param {String} [msg] - message to describe the entity which you expect.
* @returns {Boolean} comparision result.
*/
function value(actVal, expVal, msg, mode) {
if (typeof msg !== 'undefined') {
if (actVal === expVal) {
gT.l.pass(`${msg}: ${actVal}`, mode);
return true;
}
msg += `\nAct: ${actVal}\nExp: ${expVal}`;
failWrapper(msg, mode);
return false;
}
if (actVal === expVal) {
msg = `Act: "${actVal}" = Exp: "${expVal}"`;
// TODO: (Yellow color ??) It is strange situation to compare smth without message.
gT.l.pass(msg, mode);
return true;
}
msg = `Equality checking:\nAct: ${actVal}\nExp: ${expVal}`;
failWrapper(msg, mode);
return false;
}
exports.value = value;
/**
* Checks that string value representation equals to expected string.
* @param {*} actVal - actual value.
* @param {*} expVal - expected value.
* @param {String} [msg] - message to describe the entity which you expect.
* @returns {Boolean} comparision result.
*/
function valueStr(actVal, expVal, msg, mode) {
actVal = String(actVal);
expVal = String(expVal);
return value(actVal, expVal, msg, mode);
}
exports.valueStr = valueStr;
/**
* Checks that number value representation equals to expected number.
* @param {*} actVal - actual value.
* @param {*} expVal - expected value.
* @param {String} [msg] - message to describe the entity which you expect.
* @returns {Boolean} comparision result.
*/
function valueNumber(actVal, expVal, msg, mode) {
actVal = Number(actVal);
expVal = Number(expVal);
return value(actVal, expVal, msg, mode);
}
exports.valueNumber = valueNumber;
/**
* Checks that bool value representation equals to expected bool value.
* @param {*} actVal - actual value.
* @param {*} expVal - expected value.
* @param {String} [msg] - message to describe the entity which you expect.
* @returns {Boolean} comparision result.
*/
function valueBool(actVal, expVal, msg, mode) {
actVal = Boolean(actVal);
expVal = Boolean(expVal);
return value(actVal, expVal, msg, mode);
}
exports.valueBool = valueBool;
/**
* Checks that two objects or values are equal.
* Functions are not supported.
* @param actVal - actual value.
* @param expVal - expected value.
* @param msg - message to describe the entity which you expect.
* @returns {boolean}
*/
function valueDeep(actVal, expVal, msg, mode) {
function handleVals(actualValue, expectedValue, propPath) {
const actType = typeof actualValue;
const expType = typeof expectedValue;
if (actType !== expType) {
msg += `\nPath: '${propPath}', Act type: ${actType}, 'Exp type: ${expType}`;
failWrapper(msg, mode);
return false;
}
if (actType === 'object' && actualValue !== null && expectedValue !== null) {
const actProps = Object.getOwnPropertyNames(actualValue).sort();
const expProps = Object.getOwnPropertyNames(expectedValue).sort();
if (actProps.length !== expProps.length) {
// eslint-disable-next-line max-len
msg += `\nDifferent property counts, \nPath: '${propPath}', \nAct props: ${actProps}\nExp props: ${expProps}`;
failWrapper(msg, mode);
return false;
}
for (let i = 0, len = actProps.length; i < len; i++) {
const actSubProp = actProps[i];
const expSubProp = expProps[i];
if (actSubProp !== expSubProp) {
msg += `\nPath: '${propPath}', Property names are different: Act : ${actSubProp}, Exp : ${expSubProp}`;
failWrapper(msg, mode);
return false;
}
if (!handleVals(actualValue[actSubProp], expectedValue[expSubProp], `${propPath}/${actSubProp}`)) {
return false;
}
}
}
else if (actualValue !== expectedValue) {
msg += `\nPath: ${propPath}, \nAct val: ${actualValue}\nExp val: ${expectedValue}`;
failWrapper(msg, mode);
return false;
}
return true;
}
const res = handleVals(actVal, expVal, '');
if (res) {
gT.l.pass(msg, mode); // There is no sense to print [object Object].
return true;
}
return false;
}
exports.valueDeep = valueDeep;
/**
* Checks that given func will throw given exception.
* @param func
* @param expExc
* @param mode
* @return {boolean}
*/
function exception(func, expExc, mode) {
try {
func();
let msg;
if (typeof expExc === 'undefined') {
msg = 'There is not any exception';
}
else {
msg = `There is not expected exception: '${expExc}'`;
}
failWrapper(msg, mode);
return false;
}
catch (e) {
const str = e.toString();
if (typeof expExc === 'undefined') {
gT.l.pass(`Expected any exception: '${str}'`, mode);
return true;
}
if (str === expExc) {
gT.l.pass(`Expected exception: '${expExc}'`, mode);
return true;
}
failWrapper(`Actual Exception vs Expected one:\n'${str}'\n'${expExc}'`, mode);
return false;
}
}
exports.exception = exception;
// TODO exception in async func.
/**
* Checks that given async func will throw given exception.
* @param asyncFunc
* @param expExc
* @param mode
* @return {Promise}
*/
function exceptionAsync(asyncFunc, expExc, mode) {
return asyncFunc().then(() => {
let msg;
if (typeof expExc === 'undefined') {
msg = 'There is not any exception';
}
else {
msg = `There is not expected exception: '${expExc}'`;
}
failWrapper(msg, mode);
return false;
}, (e) => {
const str = e.toString();
if (typeof expExc === 'undefined') {
gT.l.pass(`Expected any exception: '${str}'`, mode);
return true;
}
if (str === expExc) {
gT.l.pass(`Expected exception: '${expExc}'`, mode);
return true;
}
failWrapper(`Actual Exception vs Expected one:\n'${str}'\n'${expExc}'`, mode);
return false;
});
}
exports.exceptionAsync = exceptionAsync;
function equal(val1, val2, msg1, msg2, doNotShowValues, mode) {
if (val1 === val2) {
if (doNotShowValues) {
gT.l.pass(`${msg1} === ${msg2}`, mode);
return true;
}
gT.l.pass(`${msg1}: ${val1} === ${msg2}: ${val2}`, mode);
return true;
}
failWrapper(`${msg1}: ${val1} !== ${msg2}: ${val2}`, mode);
return false;
}
exports.equal = equal;
function equalBool(val1, val2, msg1, msg2, doNotShowValues, mode) {
val1 = Boolean(val1);
val2 = Boolean(val2);
if (val1 === val2) {
if (doNotShowValues) {
gT.l.pass(`${msg1} === ${msg2}`, mode);
return true;
}
gT.l.pass(`${msg1} === ${msg2} === ${val1}`, mode);
return true;
}
failWrapper(`${msg1}: ${val1} !== ${msg2}: ${val2}`, mode);
return false;
}
exports.equalBool = equalBool;
function notEqualBool(val1, val2, msg1, msg2, doNotShowValues, mode) {
val1 = Boolean(val1);
val2 = Boolean(val2);
if (val1 !== val2) {
if (doNotShowValues) {
gT.l.pass(`${msg1} !== ${msg2}`, mode);
return true;
}
gT.l.pass(`${msg1}: ${val1} !== ${msg2}: ${val2}`, mode);
return true;
}
failWrapper(`${msg1} === ${msg2} === ${val1}`, mode);
return false;
}
exports.notEqualBool = notEqualBool;
//# sourceMappingURL=assertions.js.map