UNPKG

typespec-bdd

Version:

BDD framework for TypeScript.

98 lines 4.07 kB
(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", "./RegEx"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const RegEx_1 = require("./RegEx"); class StepDefinition { constructor(expression, step, isAsync, type) { this.expression = expression; this.step = step; this.isAsync = isAsync; this.type = type; } } exports.StepDefinition = StepDefinition; class StepExecution { constructor(method, isAsync, parameters) { this.method = method; this.isAsync = isAsync; this.parameters = parameters; } } exports.StepExecution = StepExecution; var StepType; (function (StepType) { StepType[StepType["Given"] = 1] = "Given"; StepType[StepType["When"] = 2] = "When"; StepType[StepType["Then"] = 4] = "Then"; })(StepType = exports.StepType || (exports.StepType = {})); class StepCollection { constructor() { this.steps = []; this.anyStepType = StepType.Given | StepType.When | StepType.Then; } add(expression, step, isAsync = false, type = this.anyStepType) { this.steps.push(new StepDefinition(expression, step, isAsync, type)); } find(text, type) { let foundStepTypes = []; for (const step of this.steps) { if (text.match(step.expression)) { if (!((type & step.type) === type)) { foundStepTypes.push(step.type); continue; } let params = this.getParams(text, RegEx_1.ExpressionLibrary.defaultStepRegExp, step.expression); return new StepExecution(step.step, step.isAsync, params); } } if (foundStepTypes.length > 0) { let error = 'Found matching steps, but of type(s): '; for (const foundStepType of foundStepTypes) { error += StepType[foundStepType] + ', '; } error += ' but not ' + StepType[type]; throw new Error(error); } return null; } getParams(text, parameterExpression, findExpression) { if (parameterExpression) { const typeIndicators = findExpression.source.toString().match(RegEx_1.ExpressionLibrary.regexFinderRegExp) || []; const matches = text.match(findExpression); if (!matches) { return []; } let result = []; for (let i = 1; i < matches.length; i++) { let match = matches[i]; match = match.replace(/^"(.+(?="$))"$/, '$1'); match = match.replace(/^'(.+(?='$))'$/, '$1'); const paramIndex = i - 1; const indicator = typeIndicators[i - 1] || ''; switch (indicator) { case "\\d+": result[paramIndex] = parseFloat(match); break; case "(\\\"true\\\"|\\\"false\\\")": result[paramIndex] = (match.toLowerCase() === 'true'); break; default: result[paramIndex] = match; } } return result; } return []; } } exports.StepCollection = StepCollection; }); //# sourceMappingURL=Steps.js.map