Sha256: 3e64bff01ea5897e949e6e8230730fb0f304044b7055bff4a11ad5ac80c6eded

Contents?: true

Size: 1.59 KB

Versions: 126

Compression:

Stored size: 1.59 KB

Contents

module RSpec
  module Expectations
    class << self
      # @private
      def differ
        @differ ||= Differ.new
      end

      # Raises an RSpec::Expectations::ExpectationNotMetError with message.
      # @param [String] message
      # @param [Object] expected
      # @param [Object] actual
      #
      # Adds a diff to the failure message when `expected` and `actual` are
      # both present.
      def fail_with(message, expected=nil, actual=nil)
        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)
              expected = expected.join(',') if Array === expected
              message << "\nDiff:" << differ.diff_as_string(actual, expected)
            end
          elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
            message << "\nDiff:" << differ.diff_as_object(actual, expected)
          end
        end

        raise(RSpec::Expectations::ExpectationNotMetError.new(message))
      end

    private

      def no_procs?(*args)
        args.flatten.none? {|a| Proc === a}
      end

      def all_strings?(*args)
        args.flatten.all? {|a| String === a}
      end

      def any_multiline_strings?(*args)
        all_strings?(*args) && args.any? {|a| a =~ /\n/}
      end

      def no_numbers?(*args)
        args.flatten.none? {|a| Numeric === a}
      end
    end
  end
end

Version data entries

126 entries across 75 versions & 12 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb
classiccms-0.7.5 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/expectations/fail_with.rb
classiccms-0.7.4 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb
classiccms-0.7.4 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/expectations/fail_with.rb
classiccms-0.7.3 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb
classiccms-0.7.3 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/expectations/fail_with.rb
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/expectations/fail_with.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/expectations/fail_with.rb
classiccms-0.7.2 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb
classiccms-0.7.2 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/expectations/fail_with.rb
classiccms-0.7.1 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb
classiccms-0.7.1 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/expectations/fail_with.rb
classiccms-0.7.0 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb
classiccms-0.7.0 vendor/bundle/gems/rspec-expectations-2.9.1/lib/rspec/expectations/fail_with.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb
remq-0.0.4 vendor/bundle/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb
remq-0.0.3 vendor/bundle/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb
classiccms-0.6.9 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/expectations/fail_with.rb