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

Diff tables with misplaced columns #212

Merged
merged 1 commit into from
Apr 21, 2012
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
16 changes: 13 additions & 3 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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