Skip to content

Commit

Permalink
add diff! method to DataTable
Browse files Browse the repository at this point in the history
  • Loading branch information
adbatista authored and mattwynne committed Jul 6, 2014
1 parent 9df4382 commit 0241b77
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion features/docs/defining_steps/table_diffing.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Feature: Table diffing
| x | y |
| a | b |
| a | c |
Tables were not identical (Cucumber::Ast::Table::Different)
Tables were not identical (Cucumber::MultilineArgument::DataTable::Different)
./features/step_definitions/steps.rb:2:in `/the table should be:/'
features/tables.feature:3:in `Then the table should be:'
Expand Down
65 changes: 64 additions & 1 deletion lib/cucumber/multiline_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,70 @@ def append_to(array)
def to_json(options)
raw.to_json(options)
end

def diff!(other_table)
other_table = ensure_table(other_table)
other_table_cell_matrix = other_table.cell_matrix

ensure_green!

require_diff_lcs
cell_matrix.extend(Diff::LCS)

changes = cell_matrix.diff(other_table_cell_matrix).flatten

return if changes.empty?

inserted = 0
missing = 0

changes.each do |change|
if(change.action == '-')
missing_row_pos = change.position + inserted
cell_matrix[missing_row_pos].each{|cell| cell.status = :undefined}

missing += 1
else # '+'
inserted_row = change.element
inserted_row.each{|cell| cell.status = :comment}

insert_row_pos = change.position + missing
cell_matrix.insert(insert_row_pos, inserted_row)

inserted += 1
end
end

raise Different.new(self)
end

def ensure_green! #:nodoc:
each_cell{|cell| cell.status = :passed}
end

private

def ensure_table(table_or_array) #:nodoc:
return table_or_array if DataTable === table_or_array
DataTable.new(table_or_array)
end

def require_diff_lcs #:nodoc:
begin
require 'diff/lcs'
rescue LoadError => e
e.message << "\n Please gem install diff-lcs\n"
raise e
end
end

class Different < StandardError
attr_reader :table
def initialize(table)
@table = table
super("Tables were not identical")
end
end
end

class None
Expand All @@ -46,7 +110,6 @@ def append_to(array)
def describe_to(visitor)
end
end

end
end

0 comments on commit 0241b77

Please sign in to comment.