Class: RSpec::SleepingKingStudios::Matchers::Core::HaveAliasedMethodMatcher

Inherits:
BaseMatcher
  • Object
show all
Includes:
Matchers::Composable
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb

Overview

Note:

Prior to 2.7.0, this was named AliasMethodMatcher.

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

Since:

  • 2.2.0

Direct Known Subclasses

AliasMethodMatcher

Constant Summary

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual

Instance Method Summary collapse

Methods inherited from BaseMatcher

#does_not_match?, #failure_message_when_negated

Constructor Details

#initialize(original_name) ⇒ HaveAliasedMethodMatcher

Returns a new instance of HaveAliasedMethodMatcher.

Parameters:

  • original_name (String, Symbol)

    The name of the method that is expected to have an alias.

Since:

  • 2.2.0



18
19
20
21
22
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb', line 18

def initialize(original_name)
  super()

  @original_name = original_name.intern
end

Instance Method Details

#as(aliased_name) ⇒ AliasMethodMatcher

Specifies the name of the new method.

Parameters:

  • aliased_name (String, Symbol)

    The method name.

Returns:

Since:

  • 2.2.0



29
30
31
32
33
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb', line 29

def as(aliased_name)
  @aliased_name = aliased_name

  self
end

#descriptionObject

Since:

  • 2.2.0



36
37
38
39
40
41
42
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb', line 36

def description
  str = "alias :#{original_name}"

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

  str
end

#failure_messageObject

Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.

Since:

  • 2.2.0



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb', line 45

def failure_message # rubocop:disable Metrics/MethodLength
  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) ⇒ Boolean

Tests the actual object to see if it matches the defined condition(s). Invoked by RSpec expectations.

Parameters:

  • actual (Object)

    the object to test against the matcher

Returns:

  • (Boolean)

    true if the object matches, otherwise false

Raises:

  • (ArgumentError)

Since:

  • 2.2.0



74
75
76
77
78
79
80
81
82
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_aliased_method_matcher.rb', line 74

def matches?(actual)
  super

  @errors = {}

  raise ArgumentError, 'must specify a new method name' if aliased_name.nil?

  responds_to_methods? && aliases_method?
end