class RSpec::SleepingKingStudios::Matchers::Core::HaveAliasedMethodMatcher

Matcher for testing whether an object aliases a specified method using the specified other method name.

@since 2.2.0

@note Prior to 2.7.0, this was named AliasMethodMatcher.

Attributes

aliased_name[R]
original_name[R]

Public Class Methods

new(original_name) click to toggle source

@param [String, Symbol] original_name The name of the method that is

expected to have an alias.
# File lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb, line 16
def initialize(original_name)
  @original_name = original_name.intern
end

Public Instance Methods

as(aliased_name) click to toggle source

Specifies the name of the new method.

@param [String, Symbol] aliased_name The method name.

@return [AliasMethodMatcher] self

# File lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb, line 25
def as(aliased_name)
  @aliased_name = aliased_name

  self
end
description() click to toggle source

(see BaseMatcher#description)

# File lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb, line 32
def description
  str = "alias :#{original_name}"

  str += " as #{aliased_name.inspect}" if aliased_name

  str
end
failure_message() click to toggle source

(see BaseMatcher#failure_message)

# File lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb, line 41
def failure_message
  message = "expected #{@actual.inspect} to alias :#{original_name}"

  message += " as #{aliased_name.inspect}" if aliased_name

  if @errors[:does_not_respond_to_old_method]
    message += ", but did not respond to :#{original_name}"

    return message
  end

  if @errors[:does_not_respond_to_new_method]
    message += ", but did not respond to :#{aliased_name}"

    return message
  end

  if @errors[:does_not_alias_method]
    message +=
      ", but :#{original_name} and :#{aliased_name} are different "\
      "methods"

    return message
  end

  message
end
matches?(actual) click to toggle source

(see BaseMatcher#matches?)

# File lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb, line 70
def matches?(actual)
  super

  @errors = {}

  if aliased_name.nil?
    raise ArgumentError.new('must specify a new method name')
  end

  responds_to_methods? && aliases_method?
end

Private Instance Methods

aliases_method?() click to toggle source
# File lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb, line 88
def aliases_method?
  unless @actual.method(original_name) == @actual.method(aliased_name)
    @errors[:does_not_alias_method] = true

    return false
  end

  true
end
responds_to_methods?() click to toggle source
# File lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb, line 98
def responds_to_methods?
  unless @actual.respond_to?(original_name)
    @errors[:does_not_respond_to_old_method] = true

    return false
  end

  unless @actual.respond_to?(aliased_name)
    @errors[:does_not_respond_to_new_method] = true

    return false
  end

  true
end