UNPKG

2.79 kBJavaScriptView Raw
1const fs = require('fs');
2const givenSteps = require('../uiTestHelpers/stepDefinitions/commonGivenSteps');
3const whenSteps = require('../uiTestHelpers/stepDefinitions/commonWhenSteps');
4const thenSteps = require('../uiTestHelpers/stepDefinitions/commonThenSteps');
5const generateSnippetsSublime = require('./generateSnippetsSublime');
6
7// console.log(generateSnippetsSublime);
8
9const createStepDefLine = (matcher, code, notes) =>
10 [`${matcher.replace(/\((.*)\)/g, '$1')}`, code, notes || ''];
11
12const padLongestMatcher = (stepDefLns) => {
13 const stepDefLines = stepDefLns;
14 let longestMatcherLength = 0;
15 let longestIndex;
16
17 stepDefLines.forEach((stepDefLine, i) => {
18 const len = stepDefLine[0].length;
19 if (len > longestMatcherLength && len < 60) {
20 longestIndex = i;
21 longestMatcherLength = stepDefLine[0].length;
22 }
23 });
24
25 stepDefLines[longestIndex][0] = stepDefLines[longestIndex][0].replace(/ /g, '&nbsp;');
26
27 return stepDefLines;
28};
29
30const createStepDefLines = (steps, type) => {
31 let stepDefNum = 0;
32 const newStepDefLines = [];
33 steps.forEach((step) => {
34 const code = generateSnippetsSublime[type][stepDefNum++]; // eslint-disable-line no-plusplus
35 const zeroOrManyNotMatcher = /\((.*not.*)\)\*/;
36 const newMatcher = step.matcher
37 .replace(/\(\?\:(.*)\)\?/g, (match, p1) => p1.replace(/([^ ]+)/, '_$1_'))
38 .replace(/\|/g, ' OR ')
39 .replace(/\(\?\:(.*)\)/g, '$1');
40
41 const matcher = newMatcher.replace(zeroOrManyNotMatcher, '');
42 newStepDefLines.push(createStepDefLine(matcher, code, step.notes));
43
44 if (newMatcher.match(zeroOrManyNotMatcher)) {
45 const matcher2 = newMatcher.replace(zeroOrManyNotMatcher, '$1');
46 const code2 = generateSnippetsSublime[type][stepDefNum++]; // eslint-disable-line no-plusplus
47 newStepDefLines.push(createStepDefLine(matcher2, code2, step.notes));
48 }
49 });
50
51 padLongestMatcher(newStepDefLines);
52
53 return newStepDefLines
54 .map((newStepDefLine) => `| ${newStepDefLine.join(' | ')} |`);
55};
56
57const givenStepDefLines = [
58 '## Step Definitions',
59 '',
60 'Note that the words in italics are optional.',
61 '',
62 '### Given...',
63 '',
64 '| Step definition | Snippet Code | Notes |',
65 '| --- | --- | --- |',
66].concat(createStepDefLines(givenSteps, 'given'));
67
68const whenStepDefLines = [
69 '',
70 '### When...',
71 '',
72 '| Step definition | Snippet Code | Notes |',
73 '| --- | --- | --- |',
74].concat(createStepDefLines(whenSteps, 'when'));
75
76const thenStepDefLines = [
77 '',
78 '### Then...',
79 '',
80 '| Step definition | Snippet Code | Notes |',
81 '| --- | --- | --- |',
82].concat(createStepDefLines(thenSteps, 'then'));
83
84const fileContents = [].concat(givenStepDefLines, whenStepDefLines, thenStepDefLines).join('\n');
85fs.writeFileSync('./STEP_DEFINITIONS.md', fileContents);