Skip to content

Commit

Permalink
Merge pull request cucumber#212 from laserlemon/master
Browse files Browse the repository at this point in the history
Diff tables with misplaced columns
  • Loading branch information
mattwynne committed Apr 21, 2012
2 parents 5efda78 + 400ff32 commit 3390561
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/cucumber/ast/table.rb
Expand Up @@ -285,14 +285,16 @@ def map_column!(column_name, strict=true, &conversion_proc)
# #diff!. You can use #map_column! on either of the tables.
#
# A Different error is raised if there are missing rows or columns, or
# surplus rows. An error is <em>not</em> raised for surplus columns.
# surplus rows. An error is <em>not</em> raised for surplus columns. An
# error is <em>not</em> raised for misplaced (out of sequence) columns.
# Whether to raise or not raise can be changed by setting values in
# +options+ to true or false:
#
# * <tt>missing_row</tt> : Raise on missing rows (defaults to true)
# * <tt>surplus_row</tt> : Raise on surplus rows (defaults to true)
# * <tt>missing_col</tt> : Raise on missing columns (defaults to true)
# * <tt>surplus_col</tt> : Raise on surplus columns (defaults to false)
# * <tt>misplaced_col</tt> : Raise on misplaced columns (defaults to false)
#
# The +other_table+ argument can be another Table, an Array of Array or
# an Array of Hash (similar to the structure returned by #hashes).
Expand All @@ -301,7 +303,13 @@ def map_column!(column_name, strict=true, &conversion_proc)
# a Table argument, if you want to compare that table to some actual values.
#
def diff!(other_table, options={})
options = {:missing_row => true, :surplus_row => true, :missing_col => true, :surplus_col => false}.merge(options)
options = {
:missing_row => true,
:surplus_row => true,
:missing_col => true,
:surplus_col => false,
:misplaced_col => false
}.merge(options)

other_table = ensure_table(other_table)
other_table.convert_headers!
Expand All @@ -317,6 +325,7 @@ def diff!(other_table, options={})

missing_col = cell_matrix[0].detect{|cell| cell.status == :undefined}
surplus_col = padded_width > original_width
misplaced_col = cell_matrix[0] != other_table.cell_matrix[0]

require_diff_lcs
cell_matrix.extend(Diff::LCS)
Expand Down Expand Up @@ -365,7 +374,8 @@ def diff!(other_table, options={})
missing_row_pos && options[:missing_row] ||
insert_row_pos && options[:surplus_row] ||
missing_col && options[:missing_col] ||
surplus_col && options[:surplus_col]
surplus_col && options[:surplus_col] ||
misplaced_col && options[:misplaced_col]
raise Different.new(self) if should_raise
end

Expand Down
9 changes: 9 additions & 0 deletions spec/cucumber/ast/table_spec.rb
Expand Up @@ -474,6 +474,15 @@ def @table.columns; super; end
lambda { @t.dup.diff!(t) }.should_not raise_error
lambda { @t.dup.diff!(t, :surplus_col => true) }.should raise_error
end

it "should not raise on misplaced columns" do
t = table(%{
| b | a |
| d | c |
}, __FILE__, __LINE__)
lambda { @t.dup.diff!(t) }.should_not raise_error
lambda { @t.dup.diff!(t, :misplaced_col => true) }.should raise_error
end
end

def table(text, file, offset)
Expand Down

0 comments on commit 3390561

Please sign in to comment.