diff --git a/lib/rspec/support/differ.rb b/lib/rspec/support/differ.rb index 9488296e..02af8b95 100644 --- a/lib/rspec/support/differ.rb +++ b/lib/rspec/support/differ.rb @@ -13,11 +13,9 @@ class Differ def diff(actual, expected) diff = "" - unless actual.nil? || expected.nil? + unless actual.nil? || expected.nil? || pointless_diff?(actual, expected) if all_strings?(actual, expected) - if any_multiline_strings?(actual, expected) - diff = diff_as_string(coerce_to_string(actual), coerce_to_string(expected)) - end + diff = diff_as_string(coerce_to_string(actual), coerce_to_string(expected)) elsif no_procs?(actual, expected) && no_numbers?(actual, expected) diff = diff_as_object(actual, expected) end @@ -81,8 +79,8 @@ def all_strings?(*args) safely_flatten(args).all? { |a| String === a } end - def any_multiline_strings?(*args) - all_strings?(*args) && safely_flatten(args).any? { |a| multiline?(a) } + def pointless_diff?(actual, expected) + String === actual && String === expected && !multiline?(actual) && !multiline?(expected) end def no_numbers?(*args) diff --git a/spec/rspec/support/differ_spec.rb b/spec/rspec/support/differ_spec.rb index cbd1ed28..3d84b3c0 100644 --- a/spec/rspec/support/differ_spec.rb +++ b/spec/rspec/support/differ_spec.rb @@ -116,6 +116,36 @@ module Support expect { differ.diff(actual, expected) }.not_to change { differ_ivars } end + it 'returns the expected diff for arrays of single-line strings' do + expected = ["foo"] + actual = ["foo", "bar"] + + expected_diff = dedent(<<-'EOS') + | + |@@ -1,2 +1,3 @@ + | foo + |+bar + | + EOS + diff = differ.diff(actual, expected) + expect(diff).to eq(expected_diff) + end + + it 'does not give an empty string for single-element arrays of single-line strings' do + expected = ["foo"] + actual = ["bar"] + + expected_diff = dedent(<<-'EOS') + | + |@@ -1 +1 @@ + |-foo + |+bar + | + EOS + diff = differ.diff(actual, expected) + expect(diff).to eq(expected_diff) + end + def differ_ivars Hash[ differ.instance_variables.map do |ivar| [ivar, differ.instance_variable_get(ivar)]