class RSpec::Rails::Matchers::BaseMatcher

@api private

Base class to build matchers. Should not be instantiated directly.

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/rails/matchers/base_matcher.rb, line 90
def self.matcher_name
  @matcher_name ||= underscore(name.split('::').last)
end
new(expected = UNDEFINED) click to toggle source
# File lib/rspec/rails/matchers/base_matcher.rb, line 21
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/rails/matchers/base_matcher.rb, line 105
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/rails/matchers/base_matcher.rb, line 85
def actual_formatted
  RSpec::Support::ObjectFormatter.format(@actual)
end
description() click to toggle source

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

# File lib/rspec/rails/matchers/base_matcher.rb, line 53
def description
  desc = RSpec::Matchers::EnglishPhrasing.split_words(self.class.matcher_name)
  desc << RSpec::Matchers::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/rails/matchers/base_matcher.rb, line 62
def diffable?
  false
end
expected_formatted() click to toggle source

@private

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

@api private

# File lib/rspec/rails/matchers/base_matcher.rb, line 75
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/rails/matchers/base_matcher.rb, line 40
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/rails/matchers/base_matcher.rb, line 95
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/rails/matchers/base_matcher.rb, line 29
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/rails/matchers/base_matcher.rb, line 70
def supports_block_expectations?
  false
end

Private Instance Methods

assert_ivars(*expected_ivars) click to toggle source
# File lib/rspec/rails/matchers/base_matcher.rb, line 117
def assert_ivars(*expected_ivars)
  return unless (expected_ivars - present_ivars).any?

  ivar_list = RSpec::Matchers::EnglishPhrasing.list(expected_ivars)
  raise "#{self.class.name} needs to supply#{ivar_list}"
end