Skip to content

Upgrade to Cucumber Expressions 3.0.0 #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
lib/
node_modules
usage.txt
.idea/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if we avoided IDE-specific exclusions.

It's often recommended to do that locally so that we don't have to worry about everyone's preferred IDEs/editors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add it to the repo, then we don't have to worry about some git noob adding their whole IDE config folder in a PR.

Is this really worth worrying about?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a "git noob" sends a PR with the whole .idea folder checked in, it would be a great opportunity to teach them a bit more about Git 👍

But no, it's probably not worth discussing any further. I told you my preference, you decide.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with @jbpros here. I believe anything that is not universal to this project belongs in your local global gitignore.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my two cents: We don't have to "worry" about everyone's preferred IDE, if we just accept their addition to the gitignore whenever it comes. Also, I'm guessing this won't be the last time someone tries to add .idea/ to the gitignore.

But I agree, either way this isn't worth worrying about lol

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove this? I strongly agree with @jbpros and would prefer to take that opportunity to teach people to use their local global gitignore.

15 changes: 8 additions & 7 deletions docs/support_files/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@ The function passed to `defineSupportCode` is called with an object as the first

---

#### `addTransform({captureGroupRegexps, typeName, transformer})`
#### `defineParameterType({regexp, typeName, transformer})`

Add a new transform to convert a capture group into something else.

* `captureGroupRegexps`: An array of regular expressions to apply the transformer to
* `transformer`: A function which transforms the captured group from a string into what is passed to the step definition
* `regexp`: A regular expression (or array of regular expressions) that match the parameter
* `typeName`: string used to refer to this type in cucumber expressions
* `transformer`: An optional function which transforms the captured argument from a string into what is passed to the step definition.
If no transform function is specified, the captured argument is left as a string.

The built in transforms are:

```javascript
// Float
{
captureGroupRegexps: ['-?\\d*\\.?\\d+'],
regexp: /-?\d*\.?\d+/,
transformer: parseFloat,
typeName: 'float'
}

// Int
// Integer
{
captureGroupRegexps: ['-?\\d+'],
regexp: /-?\d+/,
transformer: parseInt,
typeName: 'int'
}

// String in double quotes
{
captureGroupRegexps: ['"[^"]*"'],
regexp: /"[^"]+"/,
transformer: JSON.parse,
typeName: 'stringInDoubleQuotes'
}
Expand Down
69 changes: 69 additions & 0 deletions features/custom_parameter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Feature: Custom Parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use parameter type instead of parameter in this title, the feature description, and the scenario titles.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually looks like this was done in one of the following PRs so this can be ignored.


Users can register their own parameters to be used in Cucumber expressions.
Custom parameters can be used to match certain patterns, and optionally to
transform the matched value into a custom type.

Background:
Given a file named "features/passing_steps.feature" with:
"""
Feature: a feature
Scenario: a scenario
Given a passing step
"""

Scenario: custom parameter
Given a file named "features/step_definitions/passing_steps.js" with:
"""
import assert from 'assert'
import {defineSupportCode} from 'cucumber'
defineSupportCode(({Given, defineParameterType}) => {
defineParameterType({
regexp: /passing|failing|undefined|pending/,
transformer: s => s.toUpperCase(),
typeName: 'status'
})
Given('a {status} step', function(status) {
assert.equal(status, 'PASSING')
})
})
"""
When I run cucumber.js
Then the step "a passing step" has status "passed"

Scenario: custom parameter without transformer
Given a file named "features/step_definitions/passing_steps.js" with:
"""
import assert from 'assert'
import {defineSupportCode} from 'cucumber'
defineSupportCode(({Given, defineParameterType}) => {
defineParameterType({
regexp: /passing|failing|undefined|pending/,
typeName: 'status'
})
Given('a {status} step', function(status) {
assert.equal(status, 'passing')
})
})
"""
When I run cucumber.js
Then the step "a passing step" has status "passed"

Scenario: custom parameter (legacy API)
Given a file named "features/step_definitions/passing_steps.js" with:
"""
import assert from 'assert'
import {defineSupportCode} from 'cucumber'
defineSupportCode(({Given, addTransform}) => {
addTransform({
captureGroupRegexps: /passing|failing|undefined|pending/,
transformer: s => s.toUpperCase(),
typeName: 'status'
})
Given('a {status} step', function(status) {
assert.equal(status, 'PASSING')
})
})
"""
When I run cucumber.js
Then the step "a passing step" has status "passed"
2 changes: 1 addition & 1 deletion features/dryrun_mode.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Feature: Dryrun mode

defineSupportCode(({Given}) => {
Given('a step', function() { });
Given('an? step', function() { });
Given('a(n) step', function() { });
})
"""
When I run cucumber.js with `--dry-run`
Expand Down
28 changes: 20 additions & 8 deletions features/step_definition_snippets.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ Feature: step definition snippets
"""
When I run cucumber-js
Then it fails
And it suggests a "Given" step definition snippet with 1 parameter for:
And the output contains the text:
"""
'a step numbered {arg1:int}'
Given('a step numbered {int}', function (int, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
"""

Scenario: quoted strings
Expand All @@ -23,9 +26,12 @@ Feature: step definition snippets
"""
When I run cucumber-js
Then it fails
And it suggests a "Given" step definition snippet with 1 parameter for:
And the output contains the text:
"""
'a step with {arg1:stringInDoubleQuotes}'
Given('a step with {stringInDoubleQuotes}', function (stringInDoubleQuotes, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
"""

Scenario: multiple quoted strings
Expand All @@ -37,9 +43,12 @@ Feature: step definition snippets
"""
When I run cucumber-js
Then it fails
And it suggests a "Given" step definition snippet with 2 parameters for:
And the output contains the text:
"""
'a step with {arg1:stringInDoubleQuotes} and {arg2:stringInDoubleQuotes}'
Given('a step with {stringInDoubleQuotes} and {stringInDoubleQuotes}', function (stringInDoubleQuotes, stringInDoubleQuotes2, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
"""

Scenario: background step
Expand All @@ -53,7 +62,10 @@ Feature: step definition snippets
"""
When I run cucumber-js
Then it fails
And it suggests a "Given" step definition snippet with 1 parameter for:
And the output contains the text:
"""
'a step with {arg1:stringInDoubleQuotes}'
Given('a step with {stringInDoubleQuotes}', function (stringInDoubleQuotes, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
"""
17 changes: 0 additions & 17 deletions features/step_definitions/cli_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,4 @@ defineSupportCode(function({When, Then}) {
const expectedOutput = 'Usage: cucumber.js'
expect(actualOutput).to.include(expectedOutput)
})

Then(/^it suggests a "([^"]*)" step definition snippet(?: with (\d+) parameters?(?: named "([^"]*)")?)? for:$/, function (step, parameterCount, parameterName, regExp) {
const parameters = []
if (parameterName) {
parameters.push(parameterName)
}
else if (parameterCount) {
const count = parseInt(parameterCount)
for (let i = 1; i <= count; i += 1) {
parameters.push('arg' + i)
}
}
parameters.push('callback')
const expectedOutput = step + '(' + regExp + ', function (' + parameters.join(', ') + ') {\n'
const actualOutput = normalizeText(this.lastRun.output)
expect(actualOutput).to.include(expectedOutput)
})
})
4 changes: 2 additions & 2 deletions features/step_definitions/json_output_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ defineSupportCode(({Then}) => {
expect(step.result.status).to.eql(status)
})

Then('the step {arg1:stringInDoubleQuotes} has the attachment', function (name, table) {
Then('the step {stringInDoubleQuotes} has the attachment', function (name, table) {
const step = findStep(this.lastRun.jsonOutput, _.identity, ['name', name])
const attachment = _.mapKeys(table.hashes()[0], (v, k) => _.snakeCase(k))
expect(step.embeddings[0]).to.eql(attachment)
})

Then('the {arg1:stringInDoubleQuotes} hook has the attachment', function (keyword, table) {
Then('the {stringInDoubleQuotes} hook has the attachment', function (keyword, table) {
const hook = findStep(this.lastRun.jsonOutput, _.identity, ['keyword', keyword])
const attachment = _.mapKeys(table.hashes()[0], (v, k) => _.snakeCase(k))
expect(hook.embeddings[0]).to.eql(attachment)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"cli-table": "^0.3.1",
"colors": "^1.1.2",
"commander": "^2.9.0",
"cucumber-expressions": "^1.0.3",
"cucumber-expressions": "^3.0.0",
"cucumber-tag-expressions": "^1.0.0",
"duration": "^0.2.0",
"figures": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/formatter/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class FormatterBuilder {
}
return new StepDefinitionSnippetBuilder({
snippetSyntax: new Syntax(snippetInterface),
transformLookup: supportCodeLibrary.transformLookup
parameterTypeRegistry: supportCodeLibrary.parameterTypeRegistry
})
}

Expand Down
16 changes: 5 additions & 11 deletions src/formatter/step_definition_snippet_builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import DocString from '../../models/step_arguments/doc_string'
import KeywordType from '../../keyword_type'

export default class StepDefinitionSnippetBuilder {
constructor({snippetSyntax, transformLookup}) {
constructor({snippetSyntax, parameterTypeRegistry}) {
this.snippetSyntax = snippetSyntax
this.cucumberExpressionGenerator = new CucumberExpressionGenerator(transformLookup)
this.cucumberExpressionGenerator = new CucumberExpressionGenerator(parameterTypeRegistry)
}

build(step) {
const functionName = this.getFunctionName(step)
const generatedExpression = this.cucumberExpressionGenerator.generateExpression(step.name, true)
const pattern = generatedExpression.source
const parameters = this.getParameters(step, generatedExpression.transforms)
const parameters = this.getParameters(step, generatedExpression.parameterNames)
const comment = 'Write code here that turns the phrase above into concrete actions'
return this.snippetSyntax.build(functionName, pattern, parameters, comment)
}
Expand All @@ -27,20 +27,14 @@ export default class StepDefinitionSnippetBuilder {
}
}

getParameters(step, expressionTranforms) {
getParameters(step, expressionParameterNames) {
return _.concat(
this.getPatternMatchingGroupParameters(expressionTranforms),
expressionParameterNames,
this.getStepArgumentParameters(step),
'callback'
)
}

getPatternMatchingGroupParameters(expressionTranforms) {
return _.times(expressionTranforms.length, function (n) {
return `arg${n + 1}`
})
}

getStepArgumentParameters(step) {
return step.arguments.map(function (arg) {
if (arg instanceof DataTable) {
Expand Down
18 changes: 9 additions & 9 deletions src/formatter/step_definition_snippet_builder/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import DataTable from '../../models/step_arguments/data_table'
import DocString from '../../models/step_arguments/doc_string'
import KeywordType from '../../keyword_type'
import StepDefinitionSnippetBuilder from './'
import TransformLookupBuilder from '../../support_code_library/transform_lookup_builder'
import TransformLookupBuilder from '../../support_code_library/parameter_type_registry_builder'

describe('StepDefinitionSnippetBuilder', function () {
beforeEach(function () {
this.snippetSyntax = createMock(['build'])
this.transformsLookup = TransformLookupBuilder.build()
this.snippetBuilder = new StepDefinitionSnippetBuilder({
snippetSyntax: this.snippetSyntax,
transformLookup: this.transformsLookup
parameterTypeRegistry: this.transformsLookup
})
})

Expand Down Expand Up @@ -74,8 +74,8 @@ describe('StepDefinitionSnippetBuilder', function () {
})

it('replaces the quoted string with a capture group and adds a parameter', function() {
expect(this.snippetSyntax.build.firstCall.args[1]).to.eql('abc {arg1:stringInDoubleQuotes} ghi')
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['arg1', 'callback'])
expect(this.snippetSyntax.build.firstCall.args[1]).to.eql('abc {stringInDoubleQuotes} ghi')
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['stringInDoubleQuotes', 'callback'])
})
})

Expand All @@ -86,8 +86,8 @@ describe('StepDefinitionSnippetBuilder', function () {
})

it('replaces the quoted strings with capture groups and adds parameters', function() {
expect(this.snippetSyntax.build.firstCall.args[1]).to.eql('abc {arg1:stringInDoubleQuotes} ghi {arg2:stringInDoubleQuotes} mno')
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['arg1', 'arg2', 'callback'])
expect(this.snippetSyntax.build.firstCall.args[1]).to.eql('abc {stringInDoubleQuotes} ghi {stringInDoubleQuotes} mno')
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['stringInDoubleQuotes', 'stringInDoubleQuotes2', 'callback'])
})
})

Expand All @@ -98,8 +98,8 @@ describe('StepDefinitionSnippetBuilder', function () {
})

it('replaces the number with a capture group and adds a parameter', function() {
expect(this.snippetSyntax.build.firstCall.args[1]).to.eql('abc {arg1:int} def')
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['arg1', 'callback'])
expect(this.snippetSyntax.build.firstCall.args[1]).to.eql('abc {int} def')
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['int', 'callback'])
})
})

Expand Down Expand Up @@ -133,7 +133,7 @@ describe('StepDefinitionSnippetBuilder', function () {
})

it('puts the table argument after the capture groups', function() {
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['arg1', 'arg2', 'table', 'callback'])
expect(this.snippetSyntax.build.firstCall.args[2]).to.eql(['stringInDoubleQuotes', 'stringInDoubleQuotes2', 'table', 'callback'])
})
})
})
Expand Down
14 changes: 7 additions & 7 deletions src/models/step_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default class StepDefinition {
return this.buildInvalidCodeLengthMessage(parameters.length, parameters.length + 1)
}

getInvocationParameters({step, transformLookup}) {
const cucumberExpression = this.getCucumberExpression(transformLookup)
getInvocationParameters({step, parameterTypeRegistry}) {
const cucumberExpression = this.getCucumberExpression(parameterTypeRegistry)
const stepNameParameters = _.map(cucumberExpression.match(step.name), 'transformedValue')
const stepArgumentParameters = step.arguments.map(function(arg) {
if (arg instanceof DataTable) {
Expand All @@ -37,20 +37,20 @@ export default class StepDefinition {
return stepNameParameters.concat(stepArgumentParameters)
}

getCucumberExpression(transformLookup) {
getCucumberExpression(parameterTypeRegistry) {
if (typeof(this.pattern) === 'string') {
return new CucumberExpression(this.pattern, [], transformLookup)
return new CucumberExpression(this.pattern, [], parameterTypeRegistry)
} else {
return new RegularExpression(this.pattern, [], transformLookup)
return new RegularExpression(this.pattern, [], parameterTypeRegistry)
}
}

getValidCodeLengths(parameters) {
return [parameters.length, parameters.length + 1]
}

matchesStepName({stepName, transformLookup}) {
const cucumberExpression = this.getCucumberExpression(transformLookup)
matchesStepName({stepName, parameterTypeRegistry}) {
const cucumberExpression = this.getCucumberExpression(parameterTypeRegistry)
return Boolean(cucumberExpression.match(stepName))
}
}
4 changes: 2 additions & 2 deletions src/runtime/scenario_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class ScenarioRunner {
scenarioResult: this.scenarioResult,
step,
stepDefinition,
transformLookup: this.supportCodeLibrary.transformLookup,
parameterTypeRegistry: this.supportCodeLibrary.parameterTypeRegistry,
world: this.world
})
}
Expand Down Expand Up @@ -103,7 +103,7 @@ export default class ScenarioRunner {
const stepDefinitions = this.supportCodeLibrary.stepDefinitions.filter((stepDefinition) => {
return stepDefinition.matchesStepName({
stepName: step.name,
transformLookup: this.supportCodeLibrary.transformLookup
parameterTypeRegistry: this.supportCodeLibrary.parameterTypeRegistry
})
})
if (stepDefinitions.length === 0) {
Expand Down
Loading