如何在 Cucumber 表(多行参数)中使用正则表达式来与表进行比较?
我正在使用场景表(多行步骤参数)来检查一些数据使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在场景中使用正则表达式几乎肯定是错误的方法。 Cucumber 功能旨在供以业务为中心的利益相关者阅读和理解。
如何在更高层次上编写该步骤,例如:
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:
如果不编写自己的 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 intocucumber/lib/ast/table.rb
. Internally it uses diff-lcs library to do an actual comparison which doesn't support regex match.看来您想以提供很酷的 diff 输出的方式编写此代码。否则,我会考虑编写此内容,以便您只需检查行即可。它不会那么漂亮,也不会为您提供整个表的差异,但它是一些东西。
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.