如何在 Cucumber 表(多行参数)中使用正则表达式来与表进行比较?

发布于 2024-11-29 08:08:57 字数 1647 浏览 0 评论 0原文

我正在使用场景表(多行步骤参数)来检查一些数据使用 Cucumber 从屏幕上使用内置的 .diff! Cucumber AST 表上的方法。

我想检查内容是否与正则表达式匹配。

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

实际的表格可能看起来像

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

这种情况被翻译为“只要有一些数字,我不在乎”

失败的示例步骤实现:

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

错误:

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)

我尝试使用步骤转换将单元格转换为正则表达式,但它们仍然不相同。

转换代码:

 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

它提供了错误:

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)

有什么想法吗?我被困住了。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

污味仙女 2024-12-06 08:08:57

在场景中使用正则表达式几乎肯定是错误的方法。 Cucumber 功能旨在供以业务为中心的利益相关者阅读和理解。

如何在更高层次上编写该步骤,例如:

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

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
浅唱ヾ落雨殇 2024-12-06 08:08:57

如果不编写自己的 Ast::Table 中的 diff! 方法实现,就无法做到这一点。查看cucumber/lib/ast/table.rb。在内部,它使用 diff-lcs 库进行实际比较,但不支持正则表达式匹配。

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.

旧人九事 2024-12-06 08:08:57

看来您想以提供很酷的 diff 输出的方式编写此代码。否则,我会考虑编写此内容,以便您只需检查行即可。它不会那么漂亮,也不会为您提供整个表的差异,但它是一些东西。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文