Skip to content

Commit

Permalink
Fix map_headers when headers have the same prefix (#1598)
Browse files Browse the repository at this point in the history
* Add test case for headers with same prefix when using map_headers

* Fix map_headers when multiple headers with same prefix

* Update changelog

* Use match? instead of match.nil?

Co-authored-by: Aurélien Reeves <aurelien.reeves@smartbear.com>
  • Loading branch information
valerianb and aurelien-reeves committed Dec 27, 2021
1 parent 58a558a commit 8040625
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Please visit [cucumber/CONTRIBUTING.md](https://github.com/cucumber/cucumber/blo
([PR#1589](https://github.com/cucumber/cucumber-ruby/pull/1589)
[Issue#1583](https://github.com/cucumber/cucumber-ruby/issues/1583))

- Fixed `DataTable#map_headers` when headers have the same prefix.
([PR#1598](https://github.com/cucumber/cucumber-ruby/pull/1598)
[Issue#1450](https://github.com/cucumber/cucumber-ruby/issues/1450))

### Changed

- In `DataTable#map_column`, Changed the `strict` argument into a keyword argument.
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/multiline_argument/data_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def convert_headers! # :nodoc:
end

@header_mappings.each_pair do |pre, post|
mapped_cells = header_cells.reject { |cell| cell.value.match(pre).nil? }
mapped_cells = header_cells.select { |cell| pre.is_a?(Regexp) ? cell.value.match?(pre) : cell.value == pre }
raise "No headers matched #{pre.inspect}" if mapped_cells.empty?
raise "#{mapped_cells.length} headers matched #{pre.inspect}: #{mapped_cells.map(&:value).inspect}" if mapped_cells.length > 1

Expand Down
8 changes: 4 additions & 4 deletions spec/cucumber/multiline_argument/data_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ module MultilineArgument
describe '#map_headers' do
let(:table) do
DataTable.from([
%w[HELLO WORLD],
%w[ANT ANTEATER],
%w[4444 55555]
])
end
Expand All @@ -193,13 +193,13 @@ module MultilineArgument
it 'takes a block and operates on all the headers with it' do
table2 = table.map_headers(&:downcase)

expect(table2.hashes.first.keys).to match %w[hello world]
expect(table2.hashes.first.keys).to match %w[ant anteater]
end

it 'treats the mappings in the provided hash as overrides when used with a block' do
table2 = table.map_headers('WORLD' => 'foo', &:downcase)
table2 = table.map_headers('ANT' => 'foo', &:downcase)

expect(table2.hashes.first.keys).to match %w[hello foo]
expect(table2.hashes.first.keys).to match %w[foo anteater]
end
end

Expand Down

0 comments on commit 8040625

Please sign in to comment.