How to use regular expressions in a Cucumber table (multi-line argument) to compare with a table?

I use a script table ( multi-line step arguments ) to check some screen data with a cucumber using the built-in .diff! method on the table. Aroma of cucumber.

I would like to check for content matching with regular expressions.

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

The actual table may look something like

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

which this script translates to "as long as there are some numbers, I don't care"

Implementing the sample step:

 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 tried using step transforms to convert cells to regular expressions, but they are still not identical.

Convert 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 

which provides 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.

+6
source share
3 answers

Using regular expressions in a script is almost certainly the wrong approach. The functions of a cucumber are intended to be read and understood by business-oriented stakeholders.

How to write a step at a higher level, for example:

 Then the first three columns of the table should contain a digit 
+4
source

There is no way to do this without writing your own diff! method implementation diff! from Ast :: Table. Take a look at cucumber/lib/ast/table.rb . Internally, it uses the diff-lcs library for actual comparison, which does not support regular expression matching.

0
source

It seems that you want to write this in such a way as to provide a cool diff output. Otherwise, I would look at this so that you simply check the strings. It will not be so beautiful, and it will not help you understand the whole table, but it is 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
source

Source: https://habr.com/ru/post/895044/


All Articles