timjroberts / cucumber-js-tsflow

Provides 'specflow' like bindings for Cucumber.js in TypeScript 1.7+.
MIT License
133 stars 34 forks source link

Auto-casting regex captured pattern #46

Closed Kaijiro closed 4 years ago

Kaijiro commented 4 years ago

Hi,

Is it possible to "auto-cast" captured step definitions variables to their real types ? With the example in the README :

@given(/I enter '(\d*)' and '(\d*)'"/)
public givenTwoNumbers(num1: string, num2: string): void {
    this.computedResult = parseInt(num1) + parseInt(num2);
}

num1 and num2 are currently string but the Regex makes them explicitly integers (number in Typescript). Is there a way to avoid to use parseInt in the step ?

mikehaas763 commented 4 years ago

I'm not sure I fully follow. Do you want them to be passed as numbers, or strings? Ultimately, what happens here is driven by cucumber-js core, I don't think this lib is doing anything with the params

Kaijiro commented 4 years ago

I would like to avoid the parseInt part, so I would like to have them passed as numbers

kferrone commented 4 years ago

With regex, there isn't much you can do. However with the cucumber style you can define your own types, among the built in ones, which do get type casted. Read more here: https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/api_reference.md

e.g. @when("My number is {int}")

Kaijiro commented 4 years ago

Didn't know the {int} thing would work here. It completely fits my needs. Thank you !