Sha256: 3af0fd618b161f71f5d1cd416e319e475bf20106ef31d74d3625c0766eef45f9

Contents?: true

Size: 1.51 KB

Versions: 33

Compression:

Stored size: 1.51 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) && no_numbers?(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

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

Version data entries

33 entries across 33 versions & 4 rubygems

Version Path
messagebus_ruby_api-0.4.7 spec/ruby/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/fail_with.rb
messagebus_ruby_api-0.4.4 spec/ruby/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/fail_with.rb
rspec-expectations-2.6.0 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.6.0.rc6 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.6.0.rc4 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.6.0.rc2 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.5.0 lib/rspec/expectations/fail_with.rb
vim-jar-0.1.2.0001 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/fail_with.rb
rspec-expectations-2.4.0 lib/rspec/expectations/fail_with.rb
vim-jar-0.1.2 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/fail_with.rb
vim-jar-0.1.1 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/fail_with.rb
vim-jar-0.1.0 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/fail_with.rb
rspec-expectations-2.3.0 lib/rspec/expectations/fail_with.rb
vim-jar-0.0.3 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/fail_with.rb
vim-jar-0.0.2 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/fail_with.rb
vim-jar-0.0.1 bundler/ruby/1.8/gems/rspec-expectations-2.1.0/lib/rspec/expectations/fail_with.rb
rspec-expectations-2.2.0 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.1.0 lib/rspec/expectations/fail_with.rb
rspec-expectations-2.0.1 lib/rspec/expectations/fail_with.rb
gemrage-1.0.0 vendor/ruby/1.8/gems/rspec-expectations-2.0.0/lib/rspec/expectations/fail_with.rb