Sha256: 8f5749b632ead3c1d06cf31c52a5af1445d83bf04213fda6821492417f74541c

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

module RSpec
  module Expectations
    class << self
      def differ
        @differ ||= Differ.new
      end
      
      # raises a RSpec::Expectations::ExpectationNotMetError with message
      #
      # When a differ has been assigned and fail_with is passed
      # <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, 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

        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

        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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rspec-expectations-2.0.0.beta.20 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.19 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.18 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.17 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.16 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.15 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.14 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.13 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.0.beta.12 lib/rspec/expectations/fail_with.rb