Transforms in Cucumber-JVM?

840 views
Skip to first unread message

Bill Wright

unread,
Mar 5, 2012, 6:31:50 PM3/5/12
to cu...@googlegroups.com
Hi,

I'm avidly reading The Cucumber Book and we are using the Cucumber-JVM here, using Java for our step definitions (though I'd love to mix in Groovy, but I'll save that request for later). I just read about Transforms and I want to write my own transform for our project. One that would parse relative time, like "2 days ago" and "now". I cannot find any examples of creating Transforms in Java for use with the cucumber-jvm, like what is shown in the book for Ruby in section 7.2 (page 117). Are Transforms available in Java for Cucumber-JVM? If so, how do define and use the transform? 

If it is not done, I'd be willing to help out implementing it, but I don't currently have the context/knowledge to do that work. if you can point me in the right direction as far as getting up to speed on this project for contributing, I'd appreciate it.

Thanks,

Bill

aslak hellesoy

unread,
Jun 28, 2012, 8:33:26 AM6/28/12
to cu...@googlegroups.com
On Thu, Jun 28, 2012 at 1:21 PM, Andy Woodly <andy....@gmail.com> wrote:
> I'm facing a similar problem.
> I'd like to parse doubles vs. ints in my step definitions.
> The only solution I've found so far is using a double regexp and converting
> the String argument manually to double.
> In the source code I can see a Transform implementation for Scala but not
> Java.
>

Transformation is completely transparent.

Gherkin:

I have 2 cukes in my belly

Java:

@Given("I have (\d+) ([a-z]*) in my belly")
public void somethingInTheBelly(int count, String what){}

Aslak

> Anyone any ideas?
>
> Thanks,
> Andreas
> -- 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

aslak hellesoy

unread,
Jun 28, 2012, 9:09:39 AM6/28/12
to cu...@googlegroups.com
On Thu, Jun 28, 2012 at 1:33 PM, aslak hellesoy
<aslak.h...@gmail.com> wrote:
> On Thu, Jun 28, 2012 at 1:21 PM, Andy Woodly <andy....@gmail.com> wrote:
>> I'm facing a similar problem.
>> I'd like to parse doubles vs. ints in my step definitions.
>> The only solution I've found so far is using a double regexp and converting
>> the String argument manually to double.
>> In the source code I can see a Transform implementation for Scala but not
>> Java.
>>
>
> Transformation is completely transparent.
>
> Gherkin:
>
> I have 2 cukes in my belly
>
> Java:
>
> @Given("I have (\d+) ([a-z]*) in my belly")
> public void somethingInTheBelly(int count, String what){}
>
> Aslak
>

I was a little quick there. I realised you want custom types. It's
currently possible, but a little clunky. I'm working on an example for
you.

Andy Woodly

unread,
Jul 4, 2012, 6:18:15 AM7/4/12
to cu...@googlegroups.com
Thanks for working on an example.

Looking at the source code a fix (for double argument support) should be fairly easy to implement:

SnippetGenerator.DEFAULT_ARGUMENT_PATTERNS

    private static final ArgumentPattern[] DEFAULT_ARGUMENT_PATTERNS = new ArgumentPattern[]{
            new ArgumentPattern(Pattern.compile("\"([^\"]*)\""), String.class),
            new ArgumentPattern(Pattern.compile("(\\d+)"), Integer.TYPE)
    };

needs to be extended with someting like:

    new ArgumentPattern(Pattern.compile("(\\d+\\.\\d+)"), Double.Type)


aslak hellesoy

unread,
Jul 4, 2012, 6:34:36 AM7/4/12
to cu...@googlegroups.com
On Wed, Jul 4, 2012 at 11:18 AM, Andy Woodly <andy....@gmail.com> wrote:
> Thanks for working on an example.
>
> Looking at the source code a fix (for double argument support) should be
> fairly easy to implement:
>
> SnippetGenerator.DEFAULT_ARGUMENT_PATTERNS
>
> private static final ArgumentPattern[] DEFAULT_ARGUMENT_PATTERNS = new
> ArgumentPattern[]{
> new ArgumentPattern(Pattern.compile("\"([^\"]*)\""),
> String.class),
> new ArgumentPattern(Pattern.compile("(\\d+)"), Integer.TYPE)
> };
>
> needs to be extended with someting like:
>
> new ArgumentPattern(Pattern.compile("(\\d+\\.\\d+)"), Double.Type)
>

It's not that simple. Most European countries (except the UK) use , as
decimal separator and . as thousand separator. Example:

PI
UK: 3.14
Norway: 3,14

1 million
UK: 1,000,000
Norway: 1.000.000

Aslak
>> >> cukes+un...@googlegroups.com. For more options, visit this group
>> >> at
>> >> https://groups.google.com/d/forum/cukes?hl=en
>
> -- 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

aslak hellesoy

unread,
Jul 6, 2012, 7:35:59 AM7/6/12
to cu...@googlegroups.com
On Thu, Jun 28, 2012 at 2:09 PM, aslak hellesoy
<aslak.h...@gmail.com> wrote:
> On Thu, Jun 28, 2012 at 1:33 PM, aslak hellesoy
> <aslak.h...@gmail.com> wrote:
>> On Thu, Jun 28, 2012 at 1:21 PM, Andy Woodly <andy....@gmail.com> wrote:
>>> I'm facing a similar problem.
>>> I'd like to parse doubles vs. ints in my step definitions.
>>> The only solution I've found so far is using a double regexp and converting
>>> the String argument manually to double.
>>> In the source code I can see a Transform implementation for Scala but not
>>> Java.
>>>
>>
>> Transformation is completely transparent.
>>
>> Gherkin:
>>
>> I have 2 cukes in my belly
>>
>> Java:
>>
>> @Given("I have (\d+) ([a-z]*) in my belly")
>> public void somethingInTheBelly(int count, String what){}
>>
>> Aslak
>>
>
> I was a little quick there. I realised you want custom types. It's
> currently possible, but a little clunky. I'm working on an example for
> you.
>

See the announcement of 1.0.11 today for details about the new
Transform feature.

Aslak

le...@aps.org

unread,
Jul 6, 2012, 9:12:57 AM7/6/12
to aslak hellesoy, cu...@googlegroups.com


Sent from my phone

aslak hellesoy

unread,
Jul 6, 2012, 9:21:39 AM7/6/12
to le...@aps.org, cu...@googlegroups.com
On Fri, Jul 6, 2012 at 2:12 PM, le...@aps.org <le...@aps.org> wrote:
>
>
> Sent from my phone
>

What kind of phone do you have?

Lenny Marks

unread,
Jul 6, 2012, 9:30:10 AM7/6/12
to aslak hellesoy, cu...@googlegroups.com

On Jul 6, 2012, at 9:21 AM, aslak hellesoy wrote:

> On Fri, Jul 6, 2012 at 2:12 PM, le...@aps.org <le...@aps.org> wrote:
>>
>>
>> Sent from my phone
>>
>
> What kind of phone do you have?

Hmm. I was surprised to see that. I've been caught before by pocket dialing, but never pocket emailed before. Sorry for the SPAM.

-lenny
Reply all
Reply to author
Forward
0 new messages