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 string arrays like other arrays #519

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
41 changes: 41 additions & 0 deletions spec/rspec/support/differ_spec.rb
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