gherkin
Version:
164 lines • 6.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var countSymbols_1 = __importDefault(require("../countSymbols"));
var cucumber_messages_1 = require("cucumber-messages");
function compile(gherkinDocument) {
var pickles = [];
if (gherkinDocument.feature == null) {
return pickles;
}
var feature = gherkinDocument.feature;
var language = feature.language;
var featureTags = feature.tags;
var backgroundSteps = [];
feature.children.forEach(function (stepsContainer) {
if (stepsContainer.background) {
backgroundSteps = pickleSteps(stepsContainer.background);
}
else if (stepsContainer.rule) {
compileRule(featureTags, backgroundSteps, stepsContainer.rule, language, pickles);
}
else if (stepsContainer.scenario.examples.length === 0) {
compileScenario(featureTags, backgroundSteps, stepsContainer.scenario, language, pickles);
}
else {
compileScenarioOutline(featureTags, backgroundSteps, stepsContainer.scenario, language, pickles);
}
});
return pickles;
}
exports.default = compile;
function compileRule(featureTags, inheritedBackgroundSteps, rule, language, pickles) {
var backgroundSteps = [].concat(inheritedBackgroundSteps);
rule.children.forEach(function (stepsContainer) {
if (stepsContainer.background) {
backgroundSteps = backgroundSteps.concat(pickleSteps(stepsContainer.background));
}
else if (stepsContainer.scenario.examples.length === 0) {
compileScenario(featureTags, backgroundSteps, stepsContainer.scenario, language, pickles);
}
else {
compileScenarioOutline(featureTags, backgroundSteps, stepsContainer.scenario, language, pickles);
}
});
}
function compileScenario(featureTags, backgroundSteps, scenario, language, pickles) {
var steps = scenario.steps.length === 0 ? [] : [].concat(backgroundSteps);
var tags = [].concat(featureTags).concat(scenario.tags);
scenario.steps.forEach(function (step) { return steps.push(pickleStep(step)); });
var pickle = cucumber_messages_1.messages.Pickle.fromObject({
tags: pickleTags(tags),
name: scenario.name,
language: language,
locations: [scenario.location],
steps: steps,
});
pickles.push(pickle);
}
function compileScenarioOutline(featureTags, backgroundSteps, scenario, language, pickles) {
scenario.examples
.filter(function (e) { return e.tableHeader !== null; })
.forEach(function (examples) {
var variableCells = examples.tableHeader.cells;
examples.tableBody.forEach(function (values) {
var valueCells = values.cells;
var steps = scenario.steps.length === 0 ? [] : [].concat(backgroundSteps);
var tags = []
.concat(featureTags)
.concat(scenario.tags)
.concat(examples.tags);
scenario.steps.forEach(function (scenarioOutlineStep) {
var stepText = interpolate(scenarioOutlineStep.text, variableCells, valueCells);
var args = createPickleArguments(scenarioOutlineStep, variableCells, valueCells);
steps.push(cucumber_messages_1.messages.Pickle.PickleStep.fromObject({
text: stepText,
argument: args,
locations: [
pickleStepLocation(scenarioOutlineStep),
values.location,
],
}));
});
pickles.push(cucumber_messages_1.messages.Pickle.fromObject({
name: interpolate(scenario.name, variableCells, valueCells),
language: language,
steps: steps,
tags: pickleTags(tags),
locations: [scenario.location, values.location],
}));
});
});
}
function createPickleArguments(step, variableCells, valueCells) {
if (step.dataTable) {
var argument = step.dataTable;
var table = {
rows: argument.rows.map(function (row) {
return {
cells: row.cells.map(function (cell) {
return {
location: cell.location,
value: interpolate(cell.value, variableCells, valueCells),
};
}),
};
}),
};
return {
dataTable: table,
};
}
else if (step.docString) {
var argument = step.docString;
var docString = cucumber_messages_1.messages.PickleStepArgument.PickleDocString.fromObject({
location: argument.location,
content: interpolate(argument.content, variableCells, valueCells),
});
if (argument.contentType) {
docString.contentType = interpolate(argument.contentType, variableCells, valueCells);
}
return {
docString: docString,
};
}
}
function interpolate(name, variableCells, valueCells) {
variableCells.forEach(function (variableCell, n) {
var valueCell = valueCells[n];
var search = new RegExp('<' + variableCell.value + '>', 'g');
// JS Specific - dollar sign needs to be escaped with another dollar sign
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
var replacement = valueCell.value.replace(new RegExp('\\$', 'g'), '$$$$');
name = name.replace(search, replacement);
});
return name;
}
function pickleSteps(scenarioDefinition) {
return scenarioDefinition.steps.map(pickleStep);
}
function pickleStep(step) {
return cucumber_messages_1.messages.Pickle.PickleStep.fromObject({
text: step.text,
argument: createPickleArguments(step, [], []),
locations: [pickleStepLocation(step)],
});
}
function pickleStepLocation(step) {
return cucumber_messages_1.messages.Location.fromObject({
line: step.location.line,
column: step.location.column + (step.keyword ? countSymbols_1.default(step.keyword) : 0),
});
}
function pickleTags(tags) {
return tags.map(pickleTag);
}
function pickleTag(tag) {
return cucumber_messages_1.messages.Pickle.PickleTag.fromObject({
name: tag.name,
location: tag.location,
});
}
//# sourceMappingURL=compile.js.map