This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
forked from Arthy000/gherkin-testcafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolveStepDefinition.js
63 lines (52 loc) · 1.77 KB
/
resolveStepDefinition.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
const glob = require('glob');
const path = require('path');
const {CucumberExpression, ParameterTypeRegistry} = require('cucumber-expressions');
const {steps} = require('./args').argv;
const stepDefinitionsPaths = [].concat(...steps.map(pattern => glob.sync(pattern)));
const parameterTypeRegistry = new ParameterTypeRegistry();
//
// A global registry to load load and resolve all step definitions
//
class StepDefinitionRegistry {
constructor() {
this.definitions = {};
this.runtime = {};
this.latestType = '';
['given', 'when', 'then'].forEach(keyword => {
this.definitions[keyword] = [];
this.runtime[keyword] = (expression, implementation) => this.definitions[keyword].push({
implementation,
expression: new CucumberExpression(expression, parameterTypeRegistry)
});
})
this.load = definitionFactoryFunction => definitionFactoryFunction(this.runtime);
}
resolve(type, text) {
if (type === 'and') {
type = this.latestType;
}
if (this.definitions[type]) {
this.latestType = type;
return this.definitions[type].filter(({expression}) => expression.match(text))[0]
}
}
};
//
// Initialize a sole instance of the registry
//
const stepDefinitionRegistry = new StepDefinitionRegistry();
//
// Load all step definitions
//
stepDefinitionsPaths.forEach(stepDefinitionsPath => {
const definitionFactoryFunction = require(path.resolve(stepDefinitionsPath));
stepDefinitionRegistry.load(definitionFactoryFunction);
});
//
// Given a step from a gherkin AST, this will find the corresponding step definition or
// return an empty object, if there is none
//
module.exports = function resolveStepDefinition(step, predecessor) {
const stepDefinition = stepDefinitionRegistry.resolve(step.keyword.toLowerCase().trim(), step.text);
return stepDefinition || {};
};