class RSpec::Matchers::BuiltIn::BaseMatcher

@api private

Used internally as a base class for matchers that ship with rspec-expectations and rspec-rails.

### 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.

Constants

UNDEFINED

@api private Used to detect when no arg is passed to ‘initialize`. `nil` cannot be used because it’s a valid value to pass.

Attributes

@private

Public Class Methods

@private

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 102
def self.matcher_name
  @matcher_name ||= underscore(name.split('::').last)
end
# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 28
def initialize(expected=UNDEFINED)
  @expected = expected unless UNDEFINED.equal?(expected)
end

Public Instance Methods

@private

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 97
def actual_formatted
  RSpec::Support::ObjectFormatter.format(@actual)
end

@api private Generates a description using {EnglishPhrasing}. @return [String]

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 60
def description
  desc = EnglishPhrasing.split_words(self.class.matcher_name)
  desc << EnglishPhrasing.list(@expected) if defined?(@expected)
  desc
end

@api private Matchers are not diffable by default. Override this to make your subclass diffable.

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 69
def diffable?
  false
end

@private

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 92
def expected_formatted
  RSpec::Support::ObjectFormatter.format(@expected)
end

@api private

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 87
def expects_call_stack_jump?
  false
end

@api private Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

This is used by rspec-rails for some of its matchers that wrap rails’ assertions.

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 47
def match_unless_raises(*exceptions)
  exceptions.unshift Exception if exceptions.empty?
  begin
    yield
    true
  rescue *exceptions => @rescued_exception
    false
  end
end

@private

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 107
def matcher_name
  if defined?(@matcher_name)
    @matcher_name
  else
    self.class.matcher_name
  end
end

@api private Indicates if the match is successful. Delegates to ‘match`, which should be defined on a subclass. Takes care of consistently initializing the `actual` attribute.

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 36
def matches?(actual)
  @actual = actual
  match(expected, actual)
end

@api private Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false. Block matchers must override this to return true.

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 77
def supports_block_expectations?
  false
end

@private

# File rspec-expectations/lib/rspec/matchers/built_in/base_matcher.rb, line 82
def supports_value_expectations?
  true
end