lib/rspec/expectations/fail_with.rb in rspec-expectations-2.0.0.beta.11 vs lib/rspec/expectations/fail_with.rb in rspec-expectations-2.0.0.beta.12

- old
+ new

@@ -6,24 +6,42 @@ end # raises a RSpec::Expectations::ExpectationNotMetError with message # # When a differ has been assigned and fail_with is passed - # <code>expected</code> and <code>target</code>, passes them + # <code>expected</code> and <code>actual</code>, passes them # to the differ to append a diff message to the failure message. - def fail_with(message, expected=nil, target=nil) # :nodoc: - if message.nil? + def fail_with(message, expected=nil, actual=nil) # :nodoc: + if !message raise ArgumentError, "Failure message is nil. Does your matcher define the " + "appropriate failure_message_for_* method to return a string?" end - unless (differ.nil? || expected.nil? || target.nil?) - if expected.is_a?(String) - message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected) - elsif !target.is_a?(Proc) - message << "\nDiff:" << self.differ.diff_as_object(target, expected) + + if actual && expected + if all_strings?(actual, expected) + if any_multiline_strings?(actual, expected) + message << "\nDiff:" << self.differ.diff_as_string(actual, expected) + end + elsif no_procs?(actual, expected) + message << "\nDiff:" << self.differ.diff_as_object(actual, expected) end end - Kernel::raise(RSpec::Expectations::ExpectationNotMetError.new(message)) + + raise(RSpec::Expectations::ExpectationNotMetError.new(message)) + end + + private + + def no_procs?(*args) + args.none? {|a| Proc === a} + end + + def all_strings?(*args) + args.all? {|a| String === a} + end + + def any_multiline_strings?(*args) + all_strings?(*args) && args.any? {|a| a =~ /\n/} end end end end