Discussion:
[Cucumber] cucumber expressions; capture optional text or alternation
(too old to reply)
r***@gmail.com
2017-12-07 09:37:26 UTC
Permalink
hello group,

after getting some deprecation messages from cucumber because of still
using Transform, I am trying to modify my code to make use of the new style
parameter types.
In trying to do so, I get some problems I have to resolve.

One is that cucumber expression syntax does not mix with regexp syntax.

I started with

Then /^the string has (not )?(#{DQSRE})$/ do |not, inh|
if not.nil?
expect( @string).to match /#{inh}/
else
expect( @string).not_to match /#{inh}/
end
end



DQSRE being a transform.

With help of the help Aslak's blog 'Upgrading to Cucumber-Ruby 3.0.0' I
changed this to a
ParameterType with name dqs and


Then 'the string has (not ){dqs}' do |not, dqs|


But now the string is matched, but it appears that the optional 'not'
string is not captured anymore, resulting in a message
Your block takes 2 arguments, but the Regexp matched 1 argument.
(Cucumber::Glue::ArityMismatchError)
Changing back to a regular expression
Then /the string has (not )?{dqs}/ do |not, dqs|

does not work because the {dqs} syntax is not a valid regular expression
replacement for using the parametertype.

When I want to have a optional text captured, do I have to create a
parametertype for that too (I guess the same goes for alternation, which is
not captured either)?
Or am I overlooking something?

thanks in advance, Ruud
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chuck van der Linden
2017-12-13 18:15:58 UTC
Permalink
Post by r***@gmail.com
hello group,
after getting some deprecation messages from cucumber because of still
using Transform, I am trying to modify my code to make use of the new style
parameter types.
In trying to do so, I get some problems I have to resolve.
One is that cucumber expression syntax does not mix with regexp syntax.
I started with
Then /^the string has (not )?(#{DQSRE})$/ do |not, inh|
if not.nil?
else
end
end
DQSRE being a transform.
With help of the help Aslak's blog 'Upgrading to Cucumber-Ruby 3.0.0' I
changed this to a
ParameterType with name dqs and
Then 'the string has (not ){dqs}' do |not, dqs|
But now the string is matched, but it appears that the optional 'not'
string is not captured anymore, resulting in a message
Your block takes 2 arguments, but the Regexp matched 1 argument.
(Cucumber::Glue::ArityMismatchError)
Changing back to a regular expression
Then /the string has (not )?{dqs}/ do |not, dqs|
does not work because the {dqs} syntax is not a valid regular expression
replacement for using the parametertype.
When I want to have a optional text captured, do I have to create a
parametertype for that too (I guess the same goes for alternation, which is
not captured either)?
Or am I overlooking something?
thanks in advance, Ruud
I've used optional capture groups as you are doing above, (Picked it up
from this great blog posting from Cory Schires
<http://coryschires.com/ten-tips-for-writing-better-cucumber-steps/>) and I
have to agree that it would be dissapointing to have to discontinue doing
that when using the new alternatives to transforms.

Although I think there is also an argument to be made that in this
particular instance, doing this as two separate steps results in easier to
read code, and in this case even one fewer line of code

Then 'the string has {dqs}' do |dqs|
expect( @string).to match /#{inh}/
end


Then 'the string does not have {dqs}' do |dqs|
expect( @string).not_to match /#{inh}/
end

which might be your best workaround until we see if someone has a better
way to do optional capture groups in cucumber expression syntax. (I'd also
like to do non-capture groups as well,. Good uses for which are also
documented in the blog posting referenced above. )
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
aslak hellesoy
2017-12-13 21:02:20 UTC
Permalink
Haha, I just read your response after replying myself, and saw that you
beat me to it!
Post by Chuck van der Linden
Post by r***@gmail.com
hello group,
after getting some deprecation messages from cucumber because of still
using Transform, I am trying to modify my code to make use of the new style
parameter types.
In trying to do so, I get some problems I have to resolve.
One is that cucumber expression syntax does not mix with regexp syntax.
I started with
Then /^the string has (not )?(#{DQSRE})$/ do |not, inh|
if not.nil?
else
end
end
DQSRE being a transform.
With help of the help Aslak's blog 'Upgrading to Cucumber-Ruby 3.0.0' I
changed this to a
ParameterType with name dqs and
Then 'the string has (not ){dqs}' do |not, dqs|
But now the string is matched, but it appears that the optional 'not'
string is not captured anymore, resulting in a message
Your block takes 2 arguments, but the Regexp matched 1 argument.
(Cucumber::Glue::ArityMismatchError)
Changing back to a regular expression
Then /the string has (not )?{dqs}/ do |not, dqs|
does not work because the {dqs} syntax is not a valid regular expression
replacement for using the parametertype.
When I want to have a optional text captured, do I have to create a
parametertype for that too (I guess the same goes for alternation, which is
not captured either)?
Or am I overlooking something?
thanks in advance, Ruud
I've used optional capture groups as you are doing above, (Picked it up
from this great blog posting from Cory Schires
<http://coryschires.com/ten-tips-for-writing-better-cucumber-steps/>) and
I have to agree that it would be dissapointing to have to discontinue doing
that when using the new alternatives to transforms.
Although I think there is also an argument to be made that in this
particular instance, doing this as two separate steps results in easier to
read code, and in this case even one fewer line of code
Then 'the string has {dqs}' do |dqs|
end
Then 'the string does not have {dqs}' do |dqs|
end
which might be your best workaround until we see if someone has a better
way to do optional capture groups in cucumber expression syntax. (I'd also
like to do non-capture groups as well,. Good uses for which are also
documented in the blog posting referenced above. )
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chuck van der Linden
2017-12-14 18:21:12 UTC
Permalink
Post by aslak hellesoy
Haha, I just read your response after replying myself, and saw that you
beat me to it!
HAH "ninja a cucumber dev with the correct answer" Achievement
Unlocked! HAR!

so'kay, validates my answer to see it also coming from you...

and... it's NOT the same code inside each definition, so it is not
violating DRY if you ask me.
and I agree about simplicity and clarity being paramount.

So what about the second aspect to this I raised in my answer? how do we
express non capture groups in the new expression syntax?
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
aslak hellesoy
2017-12-14 19:57:52 UTC
Permalink
Post by Chuck van der Linden
Post by aslak hellesoy
Haha, I just read your response after replying myself, and saw that you
beat me to it!
HAH "ninja a cucumber dev with the correct answer" Achievement
Unlocked! HAR!
so'kay, validates my answer to see it also coming from you...
and... it's NOT the same code inside each definition, so it is not
violating DRY if you ask me.
and I agree about simplicity and clarity being paramount.
So what about the second aspect to this I raised in my answer? how do we
express non capture groups in the new expression syntax?
Can you give (me )an example of a regexp?
I’ll translate it for you.
Post by Chuck van der Linden
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chuck van der Linden
2017-12-15 18:15:56 UTC
Permalink
Post by aslak hellesoy
Post by Chuck van der Linden
Post by aslak hellesoy
Haha, I just read your response after replying myself, and saw that you
beat me to it!
HAH "ninja a cucumber dev with the correct answer" Achievement
Unlocked! HAR!
so'kay, validates my answer to see it also coming from you...
and... it's NOT the same code inside each definition, so it is not
violating DRY if you ask me.
and I agree about simplicity and clarity being paramount.
So what about the second aspect to this I raised in my answer? how do we
express non capture groups in the new expression syntax?
Can you give (me )an example of a regexp?
I’ll translate it for you.
Sure thing... So here's an example both of non-capturing groups combined
with alternation, also with a single character that may or may not occur

And /^once the files? (?:have|has) finished processing$/ do

That would match both of the following

- And once the file has finished processing
- And once the files have finished processing


Another example of one I use a lot

I (?:have |)(?:log|logged) in as: (\w+)

That's a step I use both as a Given or a When, hence the change in tense..
so that capture would match both

- Given I have logged in as: fizz
- When I log in as: buzz

(If we have nothing in the docs showing how to do those sorts of step
definitions via the new expression, I think it would be good to include
them..

--Chuck
--
Post by aslak hellesoy
Post by Chuck van der Linden
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
aslak hellesoy
2017-12-18 13:17:53 UTC
Permalink
See replies inline
Post by Chuck van der Linden
Post by aslak hellesoy
Post by Chuck van der Linden
Post by aslak hellesoy
Haha, I just read your response after replying myself, and saw that you
beat me to it!
HAH "ninja a cucumber dev with the correct answer" Achievement
Unlocked! HAR!
so'kay, validates my answer to see it also coming from you...
and... it's NOT the same code inside each definition, so it is not
violating DRY if you ask me.
and I agree about simplicity and clarity being paramount.
So what about the second aspect to this I raised in my answer? how do
we express non capture groups in the new expression syntax?
Can you give (me )an example of a regexp?
I’ll translate it for you.
Sure thing... So here's an example both of non-capturing groups combined
with alternation, also with a single character that may or may not occur
And /^once the files? (?:have|has) finished processing$/ do
Corresponding Cucumber Expression:

"once the file(s) have/has finished processing"
Post by Chuck van der Linden
That would match both of the following
- And once the file has finished processing
- And once the files have finished processing
Another example of one I use a lot
I (?:have |)(?:log|logged) in as: (\w+)
Corresponding Cucumber Expression:

"I (have )log/logged in as: {word}"
Post by Chuck van der Linden
That's a step I use both as a Given or a When, hence the change in tense..
so that capture would match both
- Given I have logged in as: fizz
- When I log in as: buzz
(If we have nothing in the docs showing how to do those sorts of step
definitions via the new expression, I think it would be good to include
them..
https://docs.cucumber.io/cucumber-expressions/#optional-text
https://docs.cucumber.io/cucumber-expressions/#alternative-text

Aslak
Post by Chuck van der Linden
--Chuck
--
Post by aslak hellesoy
Post by Chuck van der Linden
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google
Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chuck van der Linden
2017-12-18 18:00:11 UTC
Permalink
Post by aslak hellesoy
See replies inline
Thank you very much, I'm starting to be a convert to this new syntax
Post by aslak hellesoy
Post by Chuck van der Linden
Post by aslak hellesoy
Post by Chuck van der Linden
Post by aslak hellesoy
Haha, I just read your response after replying myself, and saw that
you beat me to it!
HAH "ninja a cucumber dev with the correct answer" Achievement
Unlocked! HAR!
so'kay, validates my answer to see it also coming from you...
and... it's NOT the same code inside each definition, so it is not
violating DRY if you ask me.
and I agree about simplicity and clarity being paramount.
So what about the second aspect to this I raised in my answer? how do
we express non capture groups in the new expression syntax?
Can you give (me )an example of a regexp?
I’ll translate it for you.
Sure thing... So here's an example both of non-capturing groups combined
with alternation, also with a single character that may or may not occur
And /^once the files? (?:have|has) finished processing$/ do
"once the file(s) have/has finished processing"
Post by Chuck van der Linden
That would match both of the following
- And once the file has finished processing
- And once the files have finished processing
Another example of one I use a lot
I (?:have |)(?:log|logged) in as: (\w+)
"I (have )log/logged in as: {word}"
Post by Chuck van der Linden
That's a step I use both as a Given or a When, hence the change in
tense.. so that capture would match both
- Given I have logged in as: fizz
- When I log in as: buzz
(If we have nothing in the docs showing how to do those sorts of step
definitions via the new expression, I think it would be good to include
them..
https://docs.cucumber.io/cucumber-expressions/#optional-text
https://docs.cucumber.io/cucumber-expressions/#alternative-text
Aslak
Post by Chuck van der Linden
--Chuck
--
Post by aslak hellesoy
Post by Chuck van der Linden
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google
Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
aslak hellesoy
2017-12-13 21:00:16 UTC
Permalink
See replies inline
Post by r***@gmail.com
hello group,
after getting some deprecation messages from cucumber because of still
using Transform, I am trying to modify my code to make use of the new style
parameter types.
In trying to do so, I get some problems I have to resolve.
One is that cucumber expression syntax does not mix with regexp syntax.
I started with
Then /^the string has (not )?(#{DQSRE})$/ do |not, inh|
if not.nil?
else
end
end
DQSRE being a transform.
With help of the help Aslak's blog 'Upgrading to Cucumber-Ruby 3.0.0' I
changed this to a
ParameterType with name dqs and
Then 'the string has (not ){dqs}' do |not, dqs|
But now the string is matched, but it appears that the optional 'not'
string is not captured anymore, resulting in a message
Your block takes 2 arguments, but the Regexp matched 1 argument.
(Cucumber::Glue::ArityMismatchError)
Changing back to a regular expression
Then /the string has (not )?{dqs}/ do |not, dqs|
does not work because the {dqs} syntax is not a valid regular expression
replacement for using the parametertype.
When I want to have a optional text captured, do I have to create a
parametertype for that too (I guess the same goes for alternation, which is
not captured either)?
Or am I overlooking something?
You *could* use the following cucumber expression: ''the string has
{neg}{dqs}"

And a parameter type for {neg} with:

name: 'neg',
regexp: /(not )?/,
transformer: -> (s) { s == 'not '}

However, I wouldn't do that. The added complexity isn't worth it, and I
always try to only define parameter types that represent something from my
problem domain.
I'd do this instead:

Then 'the string has {dqs}' do |inh|
expect(@string).to match /#{inh}/
end

Then 'the string has not {dqs}' do |inh|
expect(@string).not_to match /#{inh}/
end

"Duplication! Heresy!" I hear some people cry. I don't care. Clarity and
simplicity trumps DRY.

Aslak

thanks in advance, Ruud
Post by r***@gmail.com
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
r***@gmail.com
2017-12-14 12:48:39 UTC
Permalink
hi Chuck and Aslak,
thanks for your explanation.
In de meantime, rewrote the step using plain regular expression and not
using a transform at al. But you convinced me that it is better to have
readable code and to use TWO definition to do the same.
And it is good to get some experience with Cucumber expressions as well.

Ruud
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...