typespec-bdd
Version:
BDD framework for TypeScript.
193 lines • 8.35 kB
JavaScript
// Assertions lifted from tsUnit and made static here. Any improvements ought to be passed back to tsUnit too.
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function printVariable(variable) {
if (variable === null) {
return '"null"';
}
if (typeof variable === 'object') {
return `{object: ${getNameOfClass(variable)}}`;
}
return `{${typeof variable}} "${variable}"`;
}
function getNameOfClass(inputClass) {
return inputClass.constructor.name;
}
class Assert {
static areIdentical(expected, actual, message = '') {
if (expected !== actual) {
throw this.getError(`areIdentical failed when given ${printVariable(expected)} and ${printVariable(actual)}`, message);
}
}
static areNotIdentical(expected, actual, message = '') {
if (expected === actual) {
throw this.getError(`areNotIdentical failed when given ${printVariable(expected)} and ${printVariable(actual)}`, message);
}
}
static areCollectionsIdentical(expected, actual, message = '') {
function resultToString(result) {
let msg = '';
while (result.length > 0) {
msg = '[' + result.pop() + ']' + msg;
}
return msg;
}
let compareArray = (expected, actual, result) => {
let indexString = '';
if (expected === null) {
if (actual !== null) {
indexString = resultToString(result);
throw this.getError(`areCollectionsIdentical failed when array a${indexString} is null and b${indexString} is not null`, message);
}
return; // correct: both are nulls
}
else if (actual === null) {
indexString = resultToString(result);
throw this.getError(`areCollectionsIdentical failed when array a${indexString} is not null and b${indexString} is null`, message);
}
if (expected.length !== actual.length) {
indexString = resultToString(result);
throw this.getError(`areCollectionsIdentical failed when length of ` +
`array a${indexString} (length: ${expected.length})` +
`is different of length of ` +
`array b${indexString} (length: ${actual.length})`, message);
}
for (let i = 0; i < expected.length; i++) {
if ((expected[i] instanceof Array) && (actual[i] instanceof Array)) {
result.push(i);
compareArray(expected[i], actual[i], result);
result.pop();
}
else if (expected[i] !== actual[i]) {
result.push(i);
indexString = resultToString(result);
throw this.getError(`areCollectionsIdentical failed when ` +
`element a${indexString} (${printVariable(expected[i])})` +
`is different than ` +
`element b${indexString} (${printVariable(actual[i])})`, message);
}
}
return;
};
compareArray(expected, actual, []);
}
static areCollectionsNotIdentical(expected, actual, message = '') {
try {
this.areCollectionsIdentical(expected, actual);
}
catch (ex) {
return;
}
throw this.getError('areCollectionsNotIdentical failed when both collections are identical', message);
}
static isTrue(actual, message = '') {
if (!actual) {
throw this.getError(`isTrue failed when given ${printVariable(actual)}`, message);
}
}
static isFalse(actual, message = '') {
if (actual) {
throw this.getError(`isFalse failed when given ${printVariable(actual)}`, message);
}
}
static isTruthy(actual, message = '') {
if (!actual) {
throw this.getError(`isTruthy failed when given ${printVariable(actual)}`, message);
}
}
static isFalsey(actual, message = '') {
if (actual) {
throw this.getError(`isFalsey failed when given ${printVariable(actual)}`, message);
}
}
static isString(actual, message = '') {
if (typeof actual !== 'string') {
throw this.getError(`isString failed when given ${printVariable(actual)}`, message);
}
}
static isNumber(actual, message = '') {
if (typeof actual !== 'number') {
throw this.getError(`isNumber failed when given ${printVariable(actual)}`, message);
}
}
static isBoolean(actual, message = '') {
if (typeof actual !== 'boolean') {
throw this.getError(`isBoolean failed when given ${printVariable(actual)}`, message);
}
}
static throws(a, message = '', errorString = '') {
let actual = () => { return; };
if (typeof a === 'function') {
actual = a;
}
else if (a.fn) {
actual = a.fn;
message = a.message;
errorString = a.exceptionString;
}
let isThrown = false;
try {
actual();
}
catch (ex) {
if (!errorString || ex.message === errorString) {
isThrown = true;
}
if (errorString && ex.message !== errorString) {
throw this.getError('different error string than supplied');
}
}
if (!isThrown) {
throw this.getError('did not throw an error', message || '');
}
}
static doesNotThrow(actual, message) {
try {
actual();
}
catch (ex) {
throw this.getError('threw an error ' + ex, message || '');
}
}
static executesWithin(actual, timeLimit, message = '') {
function getTime() {
return window.performance.now();
}
function timeToString(value) {
return Math.round(value * 100) / 100;
}
const startOfExecution = getTime();
try {
actual();
}
catch (ex) {
throw this.getError(`isExecuteTimeLessThanLimit fails when given code throws an exception: "${ex}"`, message);
}
const executingTime = getTime() - startOfExecution;
if (executingTime > timeLimit) {
throw this.getError(`isExecuteTimeLessThanLimit fails when execution time of given code (${timeToString(executingTime)} ms) ` +
`exceed the given limit(${timeToString(timeLimit)} ms)`, message);
}
}
static fail(message = '') {
throw this.getError('fail', message);
}
static getError(resultMessage, message = '') {
if (message) {
return new Error(resultMessage + '. ' + message);
}
return new Error(resultMessage);
}
}
exports.Assert = Assert;
});
//# sourceMappingURL=Assertions.js.map