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

Inherits:
BaseMatcher
  • Object
show all
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



16
17
18
# 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

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



25
26
27
28
29
# 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

#descriptionObject

Since:

  • 2.2.0



32
33
34
35
36
37
38
# 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_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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ 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

Since:

  • 2.2.0



70
71
72
73
74
75
76
77
78
79
80
# 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