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

Allow free-form text in optionals #166

Open
rdeltour opened this issue Sep 27, 2022 · 1 comment
Open

Allow free-form text in optionals #166

rdeltour opened this issue Sep 27, 2022 · 1 comment

Comments

@rdeltour
Copy link

🤔 What's the problem you're trying to solve?

I'd like to have optional free-form text in my steps:

For instance:

Then error 123 is reported (because reasons)

The "because reasons" string isn't fixed, the content of the parenthesis varies depending on the scenario.

✨ What's your proposed solution?

It would be nice to either:

  • have a way to use some kind of "catch-all" structure (here .*) in optionals:
    error {int} is reported( \\(.*)
  • or allow parameters in optionals
    error {int} is reported( \\({})

⛏ Have you considered any alternatives or workarounds?

I tried using an anonymous parameter:

error {int} is reported( \\(){}

and ignore the related parameter in the step definition.

but this is not possible in my case, since I also have a step for:

error {int} is reported {int} times( \\(){}

which causes an ambiguous step definition.

This is why I'd need to put the anonymous part within the optional:

error {int} is reported( \\({})

But then it is an illegal expression (parameters are not allowed in optionals).

I also tried using a comment at the end of the expression:

Then error 123 is reported #(because reasons)

but comments are only allowed as a start of a new line.

The only resort is to use a regex. But the expressions then get quite ugly and unreadable (the examples above are simplified, my real expressions are more complex).
Or move the free-form text to its own line, but it is definitely less concise and sort of breaks the scenario flow.

These two workarounds work, but are suboptimal as they unfortunately make for less readable code.

📚 Any additional context?

I recently used cucumber v4.5.4 (cucumber-java), and the following expressions just worked for my use case:

error {int} is reported( \\(.*)`
error {int} is reported {int} time(s)( \\(.*)

This stopped working when migrating to cucumber v7.8.0!

@mattwynne
Copy link
Member

Would another workaround be to use a custom parameter type for your optional text? That way you get to control the regex used for matching it.

error {int} is reported{becauseReasons}

Then just ignore that parameter when it comes through to your step function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants