Skip to content

Commit

Permalink
Diff string arrays like other arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoles committed Oct 20, 2021
1 parent 2852617 commit 8eabbcd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rspec/support/differ.rb
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
37 changes: 37 additions & 0 deletions spec/rspec/support/differ_spec.rb
Expand Up @@ -270,6 +270,43 @@ 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']
expected_diff = dedent(<<-EOD)
|
|
|@@ -1,6 +1,5 @@
| 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']
expected_diff = dedent(<<-EOD)
|
|
|@@ -1,5 +1,4 @@
| 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

0 comments on commit 8eabbcd

Please sign in to comment.