Class: RSpec::SleepingKingStudios::Matchers::Core::DelegateMethodMatcher

Inherits:
BaseMatcher
  • Object
show all
Includes:
Mocks::ExampleMethods
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb

Overview

Matcher for testing whether an object delegates a method to the specified other object.

Since:

  • 2.2.0

Constant Summary collapse

DEFAULT_EXPECTED_RETURN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 2.2.0

Object.new.freeze

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Attribute Summary collapse

Attributes inherited from BaseMatcher

#actual

Instance Method Summary collapse

Methods inherited from BaseMatcher

#does_not_match?

Constructor Details

#initialize(expected) ⇒ DelegateMethodMatcher

Returns a new instance of DelegateMethodMatcher.

Parameters:

  • expected (Array<String, Symbol>)

    The names of the methods that should be delegated to the target.

Since:

  • 2.2.0



20
21
22
23
24
25
26
27
28
29
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 20

def initialize expected
  @expected               = expected
  @expected_arguments     = []
  @expected_keywords      = {}
  @expected_block         = false
  @received_block         = false
  @expected_return_values = []
  @received_return_values = []
  @errors                 = {}
end

Instance Attribute Details

#expectedObject (readonly) Also known as: method_name

method initialize

Since:

  • 2.2.0



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

def expected
  @expected
end

Instance Method Details

#and_return(*return_values) ⇒ DelegateMethodMatcher

Specifies that the actual method, when called, will return the specified value. If more than one return value is specified, the method will be called one time for each return value, and on the Nth call must return the Nth specified return value.

from calling the method on the actual object.

Parameters:

  • return_values (Array<Object>)

    The expected values to be returned

Returns:

Since:

  • 2.2.0



43
44
45
46
47
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 43

def and_return *return_values
  @expected_return_values = return_values

  self
end

#descriptionObject

Since:

  • 2.2.0



50
51
52
53
54
55
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 50

def description
  str = 'delegate method'
  str << " :#{@expected}" if @expected
  str << " to #{@target.inspect}" if @target
  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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 58

def failure_message
  message =
    "expected #{@actual.inspect} to delegate :#{@expected} to "\
    "#{@target.inspect}"

  if @errors.key?(:actual_does_not_respond_to)
    message << ", but #{@actual.inspect} does not respond to :#{@expected}"

    return message
  end # if

  if @errors.key?(:target_does_not_respond_to)
    message << ", but #{@target.inspect} does not respond to :#{@expected}"

    return message
  end # if

  if @errors.key?(:raised_exception)
    exception = @errors[:raised_exception]

    message << format_arguments << format_return_values <<
      ", but ##{@expected} raised #{exception.class.name}: "\
      "#{exception.message}"

    return message
  end # if

  if @errors.key?(:actual_does_not_delegate_to_target)
    message <<
      ", but calling ##{@expected} on the object does not call "\
      "##{@expected} on the delegate"

    return message
  end # if

  message << format_arguments << format_return_values

  if @errors.key?(:unexpected_arguments)
    message << ", but #{@errors[:unexpected_arguments]}"

    return message
  end # if

  if @errors.key?(:block_not_received)
    message << ', but the block was not passed to the delegate'

    return message
  end # if

  if @errors.key?(:unexpected_return)
    values = @errors[:unexpected_return].map &:inspect

    message << ', but returned ' <<
      SleepingKingStudios::Tools::ArrayTools.humanize_list(values)
  end # if

  message
end

#failure_message_when_negatedObject

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

Since:

  • 2.2.0



118
119
120
121
122
123
124
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 118

def failure_message_when_negated
  message =
    "expected #{@actual.inspect} not to delegate :#{@expected} to "\
    "#{@target.inspect}"

  message << format_arguments << format_return_values
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



127
128
129
130
131
132
133
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 127

def matches? actual
  super

  raise ArgumentError.new('must specify a target') if @target.nil?

  responds_to_method? && delegates_method?
end

#to(target) ⇒ DelegateMethodMatcher

Specifies the target object. The expected methods should be delegated from the actual object to the target.

Parameters:

  • target (Object)

    The target object.

Returns:

Since:

  • 2.2.0



141
142
143
144
145
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 141

def to target
  @target = target

  self
end

#with_a_blockDelegateMethodMatcher Also known as: and_a_block

Specifies that a block argument must be passed in to the target when calling the method on the actual object.

Returns:

Since:

  • 2.2.0



151
152
153
154
155
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 151

def with_a_block
  @expected_block = true

  self
end

#with_arguments(*arguments) ⇒ DelegateMethodMatcher Also known as: and_arguments

Specifies a list of arguments. The provided arguments are passed in to the method call when calling the method on the actual object, and must be passed on to the target.

Parameters:

  • arguments (Array<Object>)

    The arguments to be passed in to the method and which must be forwarded to the target.

Returns:

Since:

  • 2.2.0



166
167
168
169
170
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 166

def with_arguments *arguments
  @expected_arguments = arguments

  self
end

#with_keywords(**keywords) ⇒ DelegateMethodMatcher Also known as: and_keywords

Specifies a hash of keywords and values. The provided keywords are passed in to the method call when calling the method on the actual object, and must be passed on to the target.

Parameters:

  • keywords (Hash)

    The keywords to be passed in to the method and which must be forwarded to the target.

Returns:

Since:

  • 2.2.0



181
182
183
184
185
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 181

def with_keywords **keywords
  @expected_keywords = keywords

  self
end