-
-
Save iamkenos/c42546239c0d9ae393403c4c00d52a61 to your computer and use it in GitHub Desktop.
Cucumber.js step definition transforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# features/my.feature | |
Feature: | |
Scenario: cukes | |
Given I have 10 cucumbers in my BaG | |
Given I don't have 20 cucumbers in my bAg | |
Given I have 98775655 cucumbers in my bag |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// features/step_definitions/stepdefs.js | |
var transform = require('./support/transform.js'); | |
module.exports = function () { | |
this.Transform(/^(\d+)$/, function (matches) { return parseInt(matches[0]); }); | |
this.Transform(/^(have|don't have)$/, function (matches) { return matches[0] == 'have'; }); | |
this.Transform(/^([bB][aA][gG])$/, function () { return 'bag'; }); | |
this.Given(/^I (have|don't have) (\d+) cucumbers in my (.*)$/, function (has, count, where) { | |
console.log('Given...', has, typeof count, where) | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// features/support/transform.js | |
var transforms = []; | |
function cast(value) { | |
for (var i = 0; i < transforms.length; i++) { | |
var transform = transforms[i]; | |
var matches = transform.pattern.exec(value); | |
if (matches) { | |
return transform.fn(matches); | |
} | |
} | |
return value; | |
} | |
function t(fn) { | |
var args = []; | |
var body = 'var args = [];'; | |
for (var i = 0; i < fn.length; i++) { | |
args.push('arg' + i); | |
body += 'args.push(this.cast(arg' + i + '));\n'; | |
} | |
body += 'return this.fn.apply(this, args);'; | |
var transformedFn = Function.apply(null, args.concat([body])).bind({fn: fn, cast: cast}); | |
return transformedFn; | |
} | |
module.exports = function () { | |
var _defineStep = this.defineStep; | |
this.defineStep = function (pattern, fn) { | |
_defineStep(pattern, t(fn)); | |
}; | |
this.Given = this.When = this.Then = this.defineStep; | |
this.Transform = function (pattern, fn) { | |
transforms.push({ pattern: pattern, fn: fn }); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment