开发者

How do I use regular expressions in a Cucumber table (multiline argument) to diff against table?

开发者 https://www.devze.com 2023-03-28 15:54 出处:网络
I am using a scenario table (multiline step arguments) to check some data from a screen using cucumber, using the in built .diff! method on the Cucumber AST table.

I am using a scenario table (multiline step arguments) to check some data from a screen using cucumber, using the in built .diff! method on the Cucumber AST table.

I would like to check the content matches against regular expressions.

Scenario: One
    Then the table appears as:
    | One   | Two   | Three |
    | /\d+/ | /\d+/ | /\d+/ |

The actual table could look something like

| One | Two | Three |
| 123 | 456 | 789   |

which this scenario is translated to "as long as there are some digits, I don't care"

An example step implementation that fails:

Then /^the table appears as:$/ do |expected_table|
  actual_table  = [['One','Two', 'Three'],['123', '456', '789']]
  expected_table.diff! actual_table
end

Error:

Then the table appears as: # features/step_definitions/my_steps.rb:230
      | One    | Two    | Three  |
      | /\\d+/ | /\\d+/ | /\\d+/ |
      | 123    | 456    | 789    |
      Tables were not identical (Cucumber::Ast::Table::Different)

I have tried using step transforms to transform the cells into regular expressions, but they still aren't identical.

Transform code:

 expected_table.raw[0].each do |column|
    expected_table.map_column! column do |cell|
      if cell.respond_to? :start_with?
        if cell.start_with? "/"
          cell.to_regexp
        else
          cell
        end
      else
        cell
      end
    end
  end

whi开发者_运维问答ch provides the eror:

Then the table appears as: # features/step_definitions/my_steps.rb:228
      | One          | Two          | Three        |
      | (?-mix:\\d+) | (?-mix:\\d+) | (?-mix:\\d+) |
      | 123          | 456          | 789          |
      Tables were not identical (Cucumber::Ast::Table::Different)

Any ideas? I am stuck.


Using regular expressions in a scenario is almost certainly the wrong approach. Cucumber features are intended to be read and understood by business-focussed stakeholders.

How about writing the step at a higher level, such as as:

Then the first three columns of the table should contain a digit


There is no way to do it without writing your own implementation of diff! method from Ast::Table. Take a look into cucumber/lib/ast/table.rb. Internally it uses diff-lcs library to do an actual comparison which doesn't support regex match.


It seems that you want to write this in a way that provides the cool diff output. Otherwise, I'd look at writing this such that you simply check the rows. It won't be as pretty, and it won't get you the diff of the entire table, but it's something.

Then /^the table appears as:$/ do |expected_table|
  actual_table  = [['One','Two', 'Three'],['123', '456', '789']]

  expected_table.raw.each_with_index { |row, y|
    row.each_with_index { |cell, x| 
      actual_table[x][y].should == cell
    }
  }  
end
0

精彩评论

暂无评论...
验证码 换一张
取 消