Cucumber Parser Smarter Than Me

So that I don’t lose another 15 minutes… I had a step like this:

Given a valid outpatient A01 Admit message (without extra ZP1 segment) with Patient Num "12345678"

And in the steps.rb file:

Given /^a valid outpatient A01 Admit message (without extra ZP1 segment) with Patient Num "([^"]*)"$/ do |pn|

Which happily kept not working:

You can implement step definitions for undefined steps with these snippets:
And /^a valid outpatient A(d+) Admit message (without extra ZP(d+) segment) with Patient Num "([^"]*)"$/ do |arg1, arg2, arg3|
  pending # express the regexp above with the code you wish you had
end

UGH!

After getting rid of A01 and ZP1, I still had this “error,” but it led me to realize it was ALL in the parentheses in the steps.rb file! Why? Because parentheses are valid regex elements. Duh. Once I escaped those, all worked like a champ:

Given /^a valid outpatient A01 Admit message (without extra ZP1 segment) with Patient Num "([^"]*)"$/ do |pn|

Mystery solved.

2 thoughts on “Cucumber Parser Smarter Than Me

  1. Clifford Heath

    Now that Cucumber has made you smarter, you’ll also have realized that you can do an awful lot by writing generic step patterns, using wildcards, with parenthesis grouping to grab bits and pass them into your step definition. I find that the number of new step definitions I have to write tails off quite sharply after getting into a project, just because I can saw so much with my existing step definitions. Always worthwhile refactoring an existing definition to make it more general, instead of writing a new one.

  2. jon Post author

    Agreed, sometimes you can even do some meta stuff:


    And /^"([^"]*)" should be "([^"]*)"$/ do |field, expected|
    method = field.gsub!(" ", "_").to_sym
    @hl7_msg.send(method).should == expected
    end

    And /^"([^"]*)" should exist$/ do |field|
    method = field.gsub!(" ", "_").to_sym
    @hl7_msg.send(method).should_not be_nil
    end

Leave a Reply