Permalink
Browse files

Merge pull request #213 from samphippen/differ-newlines

Change the differ for arrays of strings with newlines in them.
  • Loading branch information...
myronmarston committed Mar 28, 2013
2 parents 16f3de3 + e0d885c commit b3899f1c757ef205ac8275e96091a90da52ceccd
Showing with 32 additions and 1 deletion.
  1. +2 −0 Changelog.md
  2. +11 −1 lib/rspec/expectations/fail_with.rb
  3. +19 −0 spec/rspec/expectations/fail_with_spec.rb
View
@@ -7,6 +7,8 @@ Enhancements
number of times: `expect { }.to yield_control.exactly(3).times`,
`expect { }.to yield_control.at_least(2).times`, etc (Bartek
Borkowski).
+* Make the differ that is used when an expectation fails better handle arrays
+ by splitting each element of the array onto its own line. (Sam Phippen)
Bug fixes
@@ -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)
@@ -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") }

0 comments on commit b3899f1

Please sign in to comment.