Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change the differ for arrays of strings with newlines in them.

Signed-off-by: Sam Phippen <samphippen@googlemail.com>
  • Loading branch information...
commit d72a1d7b80bd2c5b30a212ba4e0cf3c6fc235b7f 1 parent 82f05dd
@samphippen samphippen authored
View
12 lib/rspec/expectations/fail_with.rb
@@ -52,7 +52,17 @@ def no_numbers?(*args)
def coerce_to_string(string_or_array)
return string_or_array unless Array === string_or_array
- string_or_array.join(',')
+ diffably_stringify(string_or_array).join("\n")
+ end
+
+ def diffably_stringify(array)
+ array.map do |entry|
+ if Array === entry
+ entry.inspect
+ else
+ entry.to_s.gsub("\n", "\\n")
+ end
+ end
end
if String.method_defined?(:encoding)
View
19 spec/rspec/expectations/fail_with_spec.rb
@@ -1,6 +1,25 @@
# encoding: utf-8
require 'spec_helper'
+
+describe RSpec::Expectations, "#fail_with with diff of arrays" do
+ before { RSpec::Matchers.configuration.stub(:color? => false) }
+
+ it "splits items with newlines" do
+ expected_diff = "\nDiff:\n@@ -1 +1,3 @@\n+a\\nb\n+c\\nd\n"
+ expect {
+ RSpec::Expectations.fail_with("", [], ["a\nb", "c\nd"])
+ }.to fail_with(expected_diff)
+ end
+
+ it "shows inner arrays on a single line" do
+ expected_diff = "\nDiff:\n@@ -1 +1,3 @@\n+a\\nb\n+[\"c\\nd\"]\n"
+ expect {
+ RSpec::Expectations.fail_with("", [], ["a\nb", ["c\nd"]])
+ }.to fail_with(expected_diff)
+ end
+end
+
describe RSpec::Expectations, "#fail_with with diff" do
let(:differ) { double("differ") }
Please sign in to comment.
Something went wrong with that request. Please try again.