Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Diff string arrays like other arrays #519

Closed
wants to merge 2 commits into from
Closed
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: 5 additions & 1 deletion lib/rspec/support/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def diff(actual, expected)

unless actual.nil? || expected.nil?
if all_strings?(actual, expected)
if any_multiline_strings?(actual, expected)
if all_arrays?(actual, expected) || any_multiline_strings?(actual, expected)
diff = diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
end
elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
Expand Down Expand Up @@ -71,6 +71,10 @@ def initialize(opts={})

private

def all_arrays?(*args)
args.all? { |a| Array === a }
end

def no_procs?(*args)
safely_flatten(args).none? { |a| Proc === a }
end
Expand Down
41 changes: 41 additions & 0 deletions spec/rspec/support/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,47 @@ def inspect; "<BrokenObject>"; end
expect(diff).to be_diffed_as(expected_diff)
end

it 'outputs unified diff message of two string arrays' do
expected = ['foo', 'bar', 'baz', 'quux']
actual = ['foo', 'bar', 'corge', 'quux', 'garply']

added_line_index = ::Diff::LCS::VERSION.to_f < 1.4 ? 7 : 5
expected_diff = dedent(<<-EOD)
|
|
|@@ -1,6 +1,#{added_line_index} @@
| foo
| bar
|-corge
|+baz
| quux
|-garply
|
EOD
diff = differ.diff(expected,actual)
expect(diff).to be_diffed_as(expected_diff)
end

it 'outputs unified diff message of nested string arrays' do
expected = ['foo', ['bar', 'baz'], 'quux']
actual = ['foo', ['bar', 'corge'], 'quux', 'garply']

added_line_index = ::Diff::LCS::VERSION.to_f < 1.4 ? 6 : 4
expected_diff = dedent(<<-EOD)
|
|
|@@ -1,5 +1,#{added_line_index} @@
| foo
|-["bar", "corge"]
|+["bar", "baz"]
| quux
|-garply
|
EOD
diff = differ.diff(expected,actual)
expect(diff).to be_diffed_as(expected_diff)
end

it "outputs unified diff message of two hashes" do
expected = { :foo => 'bar', :baz => 'quux', :metasyntactic => 'variable', :delta => 'charlie', :width =>'quite wide' }
actual = { :foo => 'bar', :metasyntactic => 'variable', :delta => 'charlotte', :width =>'quite wide' }
Expand Down