UNPKG

typespec-bdd

Version:

BDD framework for TypeScript.

341 lines 12.1 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", "./Keyword", "./Steps"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Keyword_1 = require("./Keyword"); const Steps_1 = require("./Steps"); class Scenario { constructor(priorState) { this.givens = []; this.whens = []; this.thens = []; this.featureDescription = []; this.tags = []; this.tagsToExclude = []; this.tableHeaders = []; this.tableRows = []; if (priorState !== null) { this.featureTitle = priorState.featureTitle; this.featureDescription = priorState.featureDescription; this.scenarioTitle = priorState.scenarioTitle; this.tags = priorState.tags; this.tagsToExclude = priorState.tagsToExclude; this.tableHeaders = priorState.tableHeaders; this.tableRows = priorState.tableRows; this.givens = priorState.givens; this.whens = priorState.whens; this.thens = priorState.thens; } } getAllConditions() { const conditions = []; for (const given of this.givens) { conditions.push({ condition: given, type: Steps_1.StepType.Given }); } for (const when of this.whens) { conditions.push({ condition: when, type: Steps_1.StepType.When }); } for (const then of this.thens) { conditions.push({ condition: then, type: Steps_1.StepType.Then }); } return conditions; } prepareCondition(condition, index) { if (this.tableRows.length > index) { const data = this.tableRows[index]; for (const prop in data) { const token = Keyword_1.Keyword.getToken(prop); condition = condition.replace(token, data[prop]); } } return condition; } process(line) { line = line.trim(); if (!line) { // Skip empty lines return this; } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Feature)) { return this.feature(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Tag)) { return this.tag(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Scenario)) { return this.scenario(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Outline)) { return this.outline(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Given)) { return this.given(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.When)) { return this.when(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Then)) { return this.then(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.And)) { return this.and(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Examples)) { return this.examples(line); } if (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Table)) { return this.table(line); } return this.unknown(line); } isTagExcluded(tag) { for (const excludedTag of this.tagsToExclude) { if (tag === excludedTag) { return true; } } return false; } isNewScenario(line) { return false; } unknown(line) { throw new Error(`Unknown line ${line}`); } feature(line) { throw new Error(this.unexpectedLine(line)); } tag(line) { throw new Error(this.unexpectedLine(line)); } scenario(line) { throw new Error(this.unexpectedLine(line)); } outline(line) { throw new Error(this.unexpectedLine(line)); } given(line) { throw new Error(this.unexpectedLine(line)); } when(line) { throw new Error(this.unexpectedLine(line)); } then(line) { throw new Error(this.unexpectedLine(line)); } and(line) { throw new Error(this.unexpectedLine(line)); } examples(line) { throw new Error(this.unexpectedLine(line)); } table(line) { throw new Error(this.unexpectedLine(line)); } unexpectedLine(line) { return `Did not expect line: ${line}`; } } exports.Scenario = Scenario; /* Each state objects only has the methods it allows. This makes it easy to see which methods are allowed in any given state */ class InitializedState extends Scenario { constructor(tagsToExclude = []) { super(null); this.tagsToExclude = tagsToExclude; } feature(line) { this.featureTitle = Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.Feature); return new FeatureState(this); } } exports.InitializedState = InitializedState; class FeatureState extends Scenario { constructor(priorState) { super(priorState); } unknown(line) { this.featureDescription.push(line); return this; } tag(line) { const tags = Keyword_1.Keyword.getTags(line); let trimmedTags = []; for (let i = 0; i < tags.length; i++) { const trimmedTag = tags[i].trim().toLowerCase(); if (trimmedTag) { if (this.isTagExcluded(trimmedTag)) { // Exclude this scenario... return new ExcludedScenarioState(this); } trimmedTags.push(trimmedTag); } } this.tags.push.apply(this.tags, trimmedTags); return this; } scenario(line) { this.scenarioTitle = Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.Scenario); return new ScenarioState(this); } outline(line) { this.scenarioTitle = Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.Scenario); return new ScenarioState(this); } } exports.FeatureState = FeatureState; class ExcludedScenarioState extends Scenario { constructor(priorState) { super(priorState); this.hasScenario = false; } isNewScenario(line) { return this.hasScenario && (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Scenario) || Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Outline) || Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Tag)); } tag(line) { // Discard return this; } scenario(line) { // Discard this.hasScenario = true; return this; } outline(line) { // Discard this.hasScenario = true; return this; } given(line) { // Discard return this; } when(line) { // Discard return this; } then(line) { // Discard return this; } and(line) { // Discard return this; } examples(line) { // Discard return this; } table(line) { // Discard return this; } } class ScenarioState extends Scenario { constructor(priorState) { super(priorState); } given(line) { this.givens.push(Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.Given)); return new GivenState(this); } } class GivenState extends Scenario { constructor(priorState) { super(priorState); } when(line) { this.whens.push(Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.When)); return new WhenState(this); } then(line) { this.thens.push(Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.Then)); return new ThenState(this); } and(line) { this.givens.push(Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.And)); return this; } } class WhenState extends Scenario { constructor(priorState) { super(priorState); } then(line) { this.thens.push(Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.Then)); return new ThenState(this); } and(line) { this.whens.push(Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.And)); return this; } } class ThenState extends Scenario { constructor(priorState) { super(priorState); } isNewScenario(line) { return (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Scenario) || Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Outline) || Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Tag)); } and(line) { this.thens.push(Keyword_1.Keyword.trimKeyword(line, Keyword_1.KeywordType.And)); return this; } examples(line) { return new ExampleState(this); } } class ExampleState extends Scenario { constructor(priorState) { super(priorState); } table(line) { const headings = Keyword_1.Keyword.getTableRow(line); for (const heading of headings) { const trimmedHeading = heading.trim(); this.tableHeaders.push(trimmedHeading); } return new TableState(this); } } class TableState extends Scenario { constructor(priorState) { super(priorState); } isNewScenario(line) { return (Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Scenario) || Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Outline) || Keyword_1.Keyword.is(line, Keyword_1.KeywordType.Tag)); } table(line) { const data = Keyword_1.Keyword.getTableRow(line); let row = {}; for (let i = 0; i < data.length; i++) { const trimmedData = data[i].trim(); if (this.tableHeaders[i]) { row[this.tableHeaders[i]] = trimmedData; } } this.tableRows.push(row); return this; } } }); //# sourceMappingURL=State.js.map