Sha256: f3ecc0a4a050cd8400c7e25f9cab7b3e86875ef0a60b64fe61a890c9445c9122

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      # @api private
      #
      # Used _internally_ as a base class for matchers that ship with
      # rspec-expectations.
      #
      # ### Warning:
      #
      # This class is for internal use, and subject to change without notice.  We
      # strongly recommend that you do not base your custom matchers on this
      # class. If/when this changes, we will announce it and remove this warning.
      class BaseMatcher
        include RSpec::Matchers::Pretty
        include RSpec::Matchers::MatchAliases

        UNDEFINED = Object.new.freeze

        attr_reader :actual, :expected, :rescued_exception

        def initialize(expected = UNDEFINED)
          @expected = expected unless UNDEFINED.equal?(expected)
        end

        def matches?(actual)
          @actual = actual
          match(expected, actual)
        end

        def match_unless_raises(*exceptions)
          exceptions.unshift Exception if exceptions.empty?
          begin
            yield
            true
          rescue *exceptions => @rescued_exception
            false
          end
        end

        def failure_message_for_should
          assert_ivars :@actual
          "expected #{@actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
        end

        def failure_message_for_should_not
          assert_ivars :@actual
          "expected #{@actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
        end

        def description
          defined?(@expected) ? "#{name_to_sentence} #{@expected.inspect}" : name_to_sentence
        end

        def diffable?
          false
        end

        private

        def assert_ivars *ivars
          raise "#{self.class.name} needs to supply #{to_sentence ivars}" unless ivars.all? { |v| instance_variables.map(&:intern).include? v }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-expectations-2.99.0.beta2 lib/rspec/matchers/built_in/base_matcher.rb