Class: RSpec::SleepingKingStudios::Matchers::Core::AliasMethodMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/alias_method_matcher.rb

Overview

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

Since:

  • 2.2.0

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(expected) ⇒ AliasMethodMatcher

Returns a new instance of AliasMethodMatcher.

Parameters:

  • expected (String, Symbol)

    The name of the method that is expected to be aliased.

Since:

  • 2.2.0



14
15
16
17
# File 'lib/rspec/sleeping_king_studios/matchers/core/alias_method_matcher.rb', line 14

def initialize expected
  @old_method_name = @expected = expected.intern
  @errors          = {}
end

Instance Method Details

#as(new_method_name) ⇒ AliasMethodMatcher

Specifies the name of the new method.

Parameters:

  • new_method_name (String, Symbol)

    The method name.

Returns:

Since:

  • 2.2.0



24
25
26
27
28
# File 'lib/rspec/sleeping_king_studios/matchers/core/alias_method_matcher.rb', line 24

def as new_method_name
  @new_method_name = new_method_name

  self
end

#descriptionObject

Since:

  • 2.2.0



31
32
33
34
35
36
37
# File 'lib/rspec/sleeping_king_studios/matchers/core/alias_method_matcher.rb', line 31

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

  str << " as #{new_method_name.inspect}" if new_method_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



40
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
# File 'lib/rspec/sleeping_king_studios/matchers/core/alias_method_matcher.rb', line 40

def failure_message
  message = "expected #{@actual.inspect} to alias :#{old_method_name}"

  message << " as #{new_method_name.inspect}" if new_method_name

  if @errors[:does_not_respond_to_old_method]
    message << ", but did not respond to :#{old_method_name}"

    return message
  end # if

  if @errors[:does_not_respond_to_new_method]
    message << ", but did not respond to :#{new_method_name}"

    return message
  end # if

  if @errors[:does_not_alias_method]
    message <<
      ", but :#{old_method_name} and :#{new_method_name} are different "\
      "methods"

    return message
  end # if

  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



69
70
71
72
73
74
75
# File 'lib/rspec/sleeping_king_studios/matchers/core/alias_method_matcher.rb', line 69

def matches? actual
  super

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

  responds_to_methods? && aliases_method?
end