Skip to content
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

ParameterType transformer runs in World #948

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions docs/support_files/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ The function passed to `defineSupportCode` is called with an object as the first

#### `defineParameterType({name, preferForRegexpMatch, regexp, transformer, useForSnippets})`

Add a new transform to convert a capture group into something else.
Define a new parameter type and optionally convert an output parameter into something else.

* `name`: string used to refer to this type in cucumber expressions
* `regexp`: A regular expression (or array of regular expressions) that match the parameter
* `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 function can be synchronous or return a `Promise` of the transformed value.
The function can be synchronous or return a `Promise` of the transformed value. The value of `this` is the current world, so the function can delegate to world functions. World delegation does not work with arrow functions.
* `useForSnippets`: Defaults to `true`. That means this parameter type will be used to generate snippets for undefined steps. If the `regexp` frequently matches text you don't intend to be used as arguments, disable its use for snippets with `false`.
* `preferForRegexpMatch`: Defaults to `false`. Set to true if you use regular expressions and you want this parameter type's `regexp` to take precedence over others during a match.

Expand Down
23 changes: 23 additions & 0 deletions features/parameter_types.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ Feature: Parameter types
})
"""

Scenario: delegate transform to world
Given a file named "features/support/transforms.js" with:
"""
import {setWorldConstructor, defineParameterType} from 'cucumber'

defineParameterType({
regexp: /particular/,
transformer(s) {
return this.upcase(s)
},
name: 'param'
})

class MyWorld {
upcase(s) {
return s.toUpperCase()
}
}
setWorldConstructor(MyWorld)
"""
When I run cucumber.js
Then the step "a particular step" has status "passed"

Scenario: sync transform (success)
Given a file named "features/support/transforms.js" with:
"""
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"cli-table": "^0.3.1",
"colors": "^1.1.2",
"commander": "^2.9.0",
"cucumber-expressions": "^4.0.4",
"cucumber-expressions": "^5.0.0",
"cucumber-tag-expressions": "^1.0.0",
"duration": "^0.2.0",
"escape-string-regexp": "^1.0.5",
Expand Down
10 changes: 4 additions & 6 deletions src/models/step_definition.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash'
import { CucumberExpression, RegularExpression } from 'cucumber-expressions'
import DataTable from './data_table'
import { buildStepArgumentIterator } from '../step_arguments'
Expand Down Expand Up @@ -33,12 +32,11 @@ export default class StepDefinition {
)
}

getInvocationParameters({ step, parameterTypeRegistry }) {
getInvocationParameters({ step, parameterTypeRegistry, world }) {
const cucumberExpression = this.getCucumberExpression(parameterTypeRegistry)
const stepNameParameters = _.map(
cucumberExpression.match(step.text),
'value'
)
const stepNameParameters = cucumberExpression
.match(step.text)
.map(arg => arg.getValue(world))
const iterator = buildStepArgumentIterator({
dataTable: arg => new DataTable(arg),
docString: arg => arg.content
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/step_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ async function run({
stepDefinition.getInvocationParameters({
hookParameter,
parameterTypeRegistry,
step
step,
world
})
)
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1244,9 +1244,9 @@ crypto-browserify@^3.0.0:
public-encrypt "^4.0.0"
randombytes "^2.0.0"

cucumber-expressions@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/cucumber-expressions/-/cucumber-expressions-4.0.4.tgz#64d7ae485b941261b0582b0f8f02037f0818bf14"
cucumber-expressions@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/cucumber-expressions/-/cucumber-expressions-5.0.0.tgz#69332ccd38b8a06c57a7ecc38dfd7f75efc98f65"
dependencies:
becke-ch--regex--s0-0-v1--base--pl--lib "^1.2.0"

Expand Down