forked from aluzardo/gherkin-definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (118 loc) · 4.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var escapeStringRegexp = require('escape-string-regexp');
var SECTION_START_REGEX = /^\s*\w+:/;
var DEFINITION_START_REGEX = /^\s*Definition:(.*)$/;
var STEP_REGEX = /^(\s*)(?:GIVEN|WHEN|THEN|AND|BUT)(.*)$/i;
var PLACEHOLDER_GROUP_REGEX = /<.*?>/g;
var definitions = [];
function pullDefinitions(inputString) {
var inputLines = inputString.split('\n');
var definitionLineSets = [];
var definitionLines = null;
var inDefinition = false;
var outputLines = inputLines.filter(function getDefinitionLine(line) {
var sectionStarts = SECTION_START_REGEX.test(line);
if (sectionStarts) {
inDefinition = false;
}
var definitionStarts = DEFINITION_START_REGEX.test(line);
if (definitionStarts) {
definitionLines = [];
definitionLineSets.push(definitionLines);
inDefinition = true;
}
if (inDefinition) {
definitionLines.push(line);
}
return !inDefinition;
});
var newDefinitions = definitionLineSets.map(definitionFromLines);
definitions = definitions.concat(newDefinitions);
return outputLines.join('\n');
}
function definitionFromLines(lines) {
var headerLine = lines.shift();
var header = headerLine.match(DEFINITION_START_REGEX)[1].trim();
var definitionPlaceholders = header.match(PLACEHOLDER_GROUP_REGEX) || [];
var matcher = new RegExp('^' + escapeStringRegexp(header).replace(PLACEHOLDER_GROUP_REGEX, '(.*?)') + '$');
var steps = lines
.filter(function removeEmptyLines(stepLine) {
return (stepLine !== '');
})
.map(function trimLines(stepLine) {
stepLine = stepLine.trim();
var stepPlaceholders = stepLine.match(PLACEHOLDER_GROUP_REGEX) || [];
stepPlaceholders.forEach(validateStepPlaceholder.bind(null, definitionPlaceholders));
return stepLine.trim();
});
var definition = {
header: header,
placeholders: definitionPlaceholders,
matcher: matcher,
steps: steps
};
return definition;
}
function validateStepPlaceholder(definitionPlaceholders, stepPlaceholder) {
var validPlaceholder = (definitionPlaceholders.indexOf(stepPlaceholder) !== -1);
if (!validPlaceholder) {
throw new Error('Placeholder ' + stepPlaceholder +
' must be included in definition header to be used in its steps.');
}
}
function insertDefinitions(inputString) {
var inputLines = inputString.split('\n');
var outputLines = [];
inputLines.forEach(function maybeReplaceStep(line) {
var expandedLines = maybeExpandLine(line);
if (!expandedLines) {
//It's not a line that matches a definition.
return outputLines.push(line);
}
outputLines = outputLines.concat(expandedLines);
});
return outputLines.join('\n');
}
function maybeExpandLine(line) {
var stepMatch = STEP_REGEX.exec(line);
if (!stepMatch) {
//It's not a step.
return null;
}
//Search through the definitions in order of decreasing header length.
//This is a basic way to help prioritizing specific steps over less specific ones.
var sortedDefinitions = definitions.sort(function compareDefinitions(definitionA, definitionB) {
return definitionB.header.length - definitionA.header.length;
});
var indentation = stepMatch[1];
var stepBody = stepMatch[2].trim();
var definitionMatch = null;
var definition = null;
var i = 0, len = definitions.length;
while (!definitionMatch && i < len) {
definition = sortedDefinitions[i];
definitionMatch = definition.matcher.exec(stepBody);
i++;
}
if (!definitionMatch) {
//It's not a definition step.
return null;
}
var placeholders = definition.placeholders;
var replacers = definitionMatch.slice(1);
var expandedLines = definition.steps.map(function expandStep(step) {
var expandedStep = step;
placeholders.forEach(function replacePlaceholder(placeholder, index) {
expandedStep = expandedStep.replace(placeholder, replacers[index]);
});
return indentation + expandedStep;
});
return expandedLines;
}
function expandDefinitions(input) {
var pulledDefinitions = pullDefinitions(input);
var output = insertDefinitions(pulledDefinitions);
return output;
}
module.exports = expandDefinitions;
module.exports.pullDefinitions = pullDefinitions;
module.exports.insertDefinitions = insertDefinitions;