Re: [Cucumber] [JVM] using ( not)? in regular expressions

2,121 views
Skip to first unread message

Matt Wynne

unread,
Sep 20, 2012, 6:43:13 AM9/20/12
to cu...@googlegroups.com
Hi,

On 19 Sep 2012, at 17:26, Rob McDiarmid wrote:

Hi,

I'm trying to use a step definition like this:

@Then("^I should( not)? see the error message \"([^\"]*)\"$")
public void I_should_see_the_error_message(boolean errorMessageShouldNotExist, String errorMessage) { ... }

If I have a cucumber step like - Then I should see the error message "Some error Message"
Then errorMessageShouldNotExist is false as I would expect but the console only outputs "Then" instead of the whole cucumber step.

I don't know much about how Cucumber-JVM matches steps, but it looks like your regular expression is wrong. You are capturing the pattern ( not) optionally, and I'd be surprised if Cucumber-JVM were clever enough to turn that into a boolean. My guess is that you've confused it.

I would suggest doing something more like this:

@Then("^I (should|should not) see the error message \"([^\"]*)\"$")
public void I_should_see_the_error_message(String maybe, String errorMessage) { ... }

Then check the value of the maybe string to see whether it contains 'should' or 'should not'. 


If I have a cucumber step like - Then I should not see the error message "Some error Message"
Then errorMessageShouldNotExist is still false which I would not expect and the console outputs the expected step.

Anyone know how to accomplish this sort of functionality in cucumber JVM?
Thanks!

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
 
 


Rob McDiarmid

unread,
Sep 20, 2012, 9:21:00 AM9/20/12
to cu...@googlegroups.com
Whoops....I thought I deleted this post because I figured it out right after I posted it.

Your right the problem was just that cucumber doesn't know how to convert that into a boolean so I just wrote a transformer to do it. If I use "( not)?" then the string parameter will be null if "not" isn't in the step and will be "not" if "not" is in the step.

The only problem that remains is that when the step is finished it only outputs "Then" to standard out. Changing the regular expression to the one you suggested may fix this issue but it would be nice if this sort of regular expression was supported in JVM. I think it's supported in other version of cucumber as I got the idea from this article http://coryschires.com/ten-tips-for-writing-better-cucumber-steps/

Matt Wynne

unread,
Sep 21, 2012, 11:52:22 AM9/21/12
to cu...@googlegroups.com
On 20 Sep 2012, at 14:21, Rob McDiarmid wrote:

Whoops....I thought I deleted this post because I figured it out right after I posted it.

Your right the problem was just that cucumber doesn't know how to convert that into a boolean so I just wrote a transformer to do it. If I use "( not)?" then the string parameter will be null if "not" isn't in the step and will be "not" if "not" is in the step.

The only problem that remains is that when the step is finished it only outputs "Then" to standard out. Changing the regular expression to the one you suggested may fix this issue but it would be nice if this sort of regular expression was supported in JVM. I think it's supported in other version of cucumber as I got the idea from this article http://coryschires.com/ten-tips-for-writing-better-cucumber-steps/

I'd have to try it, but I'm surprised about the optional group example in that article. I didn't think Cucumber-Ruby worked like that.

Rob McDiarmid

unread,
Sep 21, 2012, 4:21:59 PM9/21/12
to cu...@googlegroups.com
Alright I'll just use the regex you suggested. Thanks for the help!

Matt Wynne

unread,
Sep 22, 2012, 5:58:41 PM9/22/12
to cu...@googlegroups.com
On 21 Sep 2012, at 16:52, Matt Wynne wrote:


On 20 Sep 2012, at 14:21, Rob McDiarmid wrote:

Whoops....I thought I deleted this post because I figured it out right after I posted it.

Your right the problem was just that cucumber doesn't know how to convert that into a boolean so I just wrote a transformer to do it. If I use "( not)?" then the string parameter will be null if "not" isn't in the step and will be "not" if "not" is in the step.

The only problem that remains is that when the step is finished it only outputs "Then" to standard out. Changing the regular expression to the one you suggested may fix this issue but it would be nice if this sort of regular expression was supported in JVM. I think it's supported in other version of cucumber as I got the idea from this article http://coryschires.com/ten-tips-for-writing-better-cucumber-steps/

I'd have to try it, but I'm surprised about the optional group example in that article. I didn't think Cucumber-Ruby worked like that.

I just tried it, and it does. Amazed.
Reply all
Reply to author
Forward
0 new messages