Skip to content

Commit

Permalink
+Table.rows_hash+ method. Take two
Browse files Browse the repository at this point in the history
  • Loading branch information
torbjornvatn committed Mar 26, 2009
1 parent 366844e commit 0e3eb52
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ doc
.DS_Store
*.swp
target
*.tmproj
21 changes: 21 additions & 0 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ def hashes
row.to_hash
end
end

# Converts this table into a Hash where the first column is
# used as keys and the second column is used as values
#
# | a | 2 |
# | b | 3 |
#
# Gets converted into the following:
#
# {'a' => '2', 'b' => '3'}
#
# The table must be exactly two columns wide
#
def rows_hash
verify_table_width(2)
@rows_hash = self.transpose.hashes[0]
end

# Gets the raw data of this table. For example, a Table built from
# the following plain text:
Expand Down Expand Up @@ -160,6 +177,10 @@ def index(cells) #:nodoc:
def verify_column(column_name)
raise %{The column named "#{column_name}" does not exist} unless @raw[0].include?(column_name)
end

def verify_table_width(width)
raise %{The table must have exactly #{width} columns} unless @raw[0].size == width
end

def arguments_replaced(arguments) #:nodoc:
raw_with_replaced_args = raw.map do |row|
Expand Down
21 changes: 21 additions & 0 deletions spec/cucumber/ast/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ def @table.columns; super; end
@table.transpose.hashes[0].should == {'one' => '1111', 'two' => '22222'}
end
end

describe ".rows_hash" do

it "should return a hash of the rows" do
table = Table.new([
%w{one 1111},
%w{two 22222}
])
table.rows_hash.should == {'one' => '1111', 'two' => '22222'}
end

it "should fail if the table doesn't have two columns" do
faulty_table = Table.new([
%w{one 1111 abc},
%w{two 22222 def}
])
lambda {
faulty_table.rows_hash
}.should raise_error('The table must have exactly 2 columns')
end
end

it "should allow renaming columns" do
table2 = @table.map_headers('one' => :three)
Expand Down

0 comments on commit 0e3eb52

Please sign in to comment.