Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map_headers refactored #528

Merged
merged 4 commits into from
Sep 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,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