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

actual[R]

@private

expected[R]

@private

matcher_name[W]

@private

rescued_exception[R]

@private

Public Class Methods

matcher_name() click to toggle source

@private

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

Private Class Methods

underscore(camel_cased_word) click to toggle source

@private Borrowed from ActiveSupport.

# File lib/rspec/matchers/built_in/base_matcher.rb, line 117
def self.underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end

Public Instance Methods

actual_formatted() click to toggle source

@private

# File lib/rspec/matchers/built_in/base_matcher.rb, line 97
def actual_formatted
  RSpec::Support::ObjectFormatter.format(@actual)
end
description() click to toggle source

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

# File 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
diffable?() click to toggle source

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

# File lib/rspec/matchers/built_in/base_matcher.rb, line 69
def diffable?
  false
end
expected_formatted() click to toggle source

@private

# File lib/rspec/matchers/built_in/base_matcher.rb, line 92
def expected_formatted
  RSpec::Support::ObjectFormatter.format(@expected)
end
expects_call_stack_jump?() click to toggle source

@api private

# File lib/rspec/matchers/built_in/base_matcher.rb, line 87
def expects_call_stack_jump?
  false
end
match_unless_raises(*exceptions) { || ... } click to toggle source

@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 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
matcher_name() click to toggle source

@private

# File 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
matches?(actual) click to toggle source

@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 lib/rspec/matchers/built_in/base_matcher.rb, line 36
def matches?(actual)
  @actual = actual
  match(expected, actual)
end
supports_block_expectations?() click to toggle source

@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 lib/rspec/matchers/built_in/base_matcher.rb, line 77
def supports_block_expectations?
  false
end
supports_value_expectations?() click to toggle source

@private

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

Private Instance Methods

assert_ivars(*expected_ivars) click to toggle source
# File lib/rspec/matchers/built_in/base_matcher.rb, line 129
def assert_ivars(*expected_ivars)
  return unless (expected_ivars - present_ivars).any?
  ivar_list = EnglishPhrasing.list(expected_ivars)
  raise "#{self.class.name} needs to supply#{ivar_list}"
end
present_ivars() click to toggle source

:nocov:

# File lib/rspec/matchers/built_in/base_matcher.rb, line 137
def present_ivars
  instance_variables.map(&:to_sym)
end