Skip to content

Commit

Permalink
Merge pull request #528 from adbatista/refactor_map_headers
Browse files Browse the repository at this point in the history
map_headers refactored
  • Loading branch information
os97673 committed Sep 3, 2013
2 parents 1e13ab2 + 1dbfe09 commit 3ca8609
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
6 changes: 2 additions & 4 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,8 @@ def map_headers!(mappings={}, &block)
end

# Returns a new Table where the headers are redefined. See #map_headers!
def map_headers(mappings={})
table = self.dup
table.map_headers!(mappings)
table
def map_headers(mappings={}, &block)
self.class.new raw.dup, @conversion_procs.dup, mappings, block
end

# Change how #hashes converts column values. The +column_name+ argument identifies the column
Expand Down
78 changes: 43 additions & 35 deletions spec/cucumber/ast/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def @table.columns; super; end
@table.map_column!('one') { |v| v.to_i }
@table.hashes.first['one'].should == 4444
end

it "applies the block once to each value" do
headers = ['header']
rows = ['value']
Expand Down Expand Up @@ -158,75 +158,84 @@ def @table.columns; super; end
end
end

describe '#map_headers' do
describe '#map_headers!' do
let(:table) do
Table.new([
%w{HELLO WORLD},
%w{4444 55555}
])
end

it "renames the columns to the specified values in the provided hash" do
table2 = @table.map_headers('one' => :three)
table2.hashes.first[:three].should == '4444'
@table.map_headers!('one' => :three)
@table.hashes.first[:three].should == '4444'
end

it "allows renaming columns using regexp" do
table2 = @table.map_headers(/one|uno/ => :three)
table2.hashes.first[:three].should == '4444'
@table.map_headers!(/one|uno/ => :three)
@table.hashes.first[:three].should == '4444'
end

it "copies column mappings" do
@table.map_column!('one') { |v| v.to_i }
table2 = @table.map_headers('one' => 'three')
table2.hashes.first['three'].should == 4444
@table.map_headers!('one' => 'three')
@table.hashes.first['three'].should == 4444
end

it "takes a block and operates on all the headers with it" do
table = Table.new([
['HELLO', 'WORLD'],
%w{4444 55555}
])

table.map_headers! do |header|
header.downcase
end

table.hashes.first.keys.should =~ %w[hello world]
end

it "treats the mappings in the provided hash as overrides when used with a block" do
table = Table.new([
['HELLO', 'WORLD'],
%w{4444 55555}
])

table.map_headers!('WORLD' => 'foo') do |header|
header.downcase
end

table.hashes.first.keys.should =~ %w[hello foo]
end
end

it "should allow mapping of headers before table.hashes has been accessed" do
table = Table.new([
['HELLO', 'WORLD'],
describe '#map_headers' do
let(:table) do
Table.new([
%w{HELLO WORLD},
%w{4444 55555}
])
end

table.map_headers! do |header|
header.downcase
end
it "renames the columns to the specified values in the provided hash" do
table2 = @table.map_headers('one' => :three)
table2.hashes.first[:three].should == '4444'
end

table.hashes.first.keys.should =~ %w[hello world]
it "allows renaming columns using regexp" do
table2 = @table.map_headers(/one|uno/ => :three)
table2.hashes.first[:three].should == '4444'
end

it "should allow mapping of headers after table.hashes has been accessed" do
table = Table.new([
['HELLO', 'WORLD'],
%w{4444 55555}
])
it "copies column mappings" do
@table.map_column!('one') { |v| v.to_i }
table2 = @table.map_headers('one' => 'three')
table2.hashes.first['three'].should == 4444
end

dev_null = table.hashes.size
it "takes a block and operates on all the headers with it" do
table2 = table.map_headers do |header|
header.downcase
end

table.map_headers! do |header|
table2.hashes.first.keys.should =~ %w[hello world]
end

it "treats the mappings in the provided hash as overrides when used with a block" do
table2 = table.map_headers('WORLD' => 'foo') do |header|
header.downcase
end

table.hashes.first.keys.should =~ %w[hello world]
table2.hashes.first.keys.should =~ %w[hello foo]
end
end

Expand Down Expand Up @@ -289,7 +298,6 @@ def @table.columns; super; end
table.arguments_replaced({'<book>' => nil, '<qty>' => '5'})
}.should_not raise_error
end

end

describe "diff!" do
Expand Down

0 comments on commit 3ca8609

Please sign in to comment.