Class: Shoulda::Matchers::Independent::DelegateMethodMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/independent/delegate_method_matcher.rb,
lib/shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error.rb

Defined Under Namespace

Classes: DelegateObjectNotSpecified

Instance Method Summary collapse

Constructor Details

#initialize(delegating_method) ⇒ DelegateMethodMatcher

Returns a new instance of DelegateMethodMatcher.



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 179

def initialize(delegating_method)
  @delegating_method = delegating_method

  @delegate_method = @delegating_method
  @delegate_object = Doublespeak::ObjectDouble.new

  @context = nil
  @subject = nil
  @delegate_object_reader_method = nil
  @delegated_arguments = []
  @expects_to_allow_nil_delegate_object = false
end

Instance Method Details

#allow_nilObject



252
253
254
255
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 252

def allow_nil
  @expects_to_allow_nil_delegate_object = true
  self
end

#as(delegate_method) ⇒ Object



235
236
237
238
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 235

def as(delegate_method)
  @delegate_method = delegate_method
  self
end

#build_delegating_method_prefix(prefix) ⇒ Object



257
258
259
260
261
262
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 257

def build_delegating_method_prefix(prefix)
  case prefix
  when true, nil then delegate_object_reader_method
  else prefix
  end
end

#descriptionObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 208

def description
  string =
    "delegate #{formatted_delegating_method_name} to the " +
    "#{formatted_delegate_object_reader_method_name} object"

  if delegated_arguments.any?
    string << " passing arguments #{delegated_arguments.inspect}"
  end

  if delegate_method != delegating_method
    string << " as #{formatted_delegate_method}"
  end

  if expects_to_allow_nil_delegate_object?
    string << ', allowing '
    string << formatted_delegate_object_reader_method_name
    string << ' to return nil'
  end

  string
end

#failure_messageObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 264

def failure_message
  message = "Expected #{class_under_test} to #{description}.\n\n"

  if failed_to_allow_nil_delegate_object?
    message << formatted_delegating_method_name(include_module: true)
    message << ' did delegate to '
    message << formatted_delegate_object_reader_method_name
    message << ' when it was non-nil, but it failed to account '
    message << 'for when '
    message << formatted_delegate_object_reader_method_name
    message << ' *was* nil.'
  else
    message << 'Method calls sent to '
    message << formatted_delegate_object_reader_method_name(
      include_module: true,
    )
    message << ": #{formatted_calls_on_delegate_object}"
  end

  Shoulda::Matchers.word_wrap(message)
end

#failure_message_when_negatedObject



286
287
288
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 286

def failure_message_when_negated
  "Expected #{class_under_test} not to #{description}, but it did."
end

#in_context(context) ⇒ Object



192
193
194
195
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 192

def in_context(context)
  @context = MatcherContext.new(context)
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
202
203
204
205
206
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 197

def matches?(subject)
  @subject = subject

  ensure_delegate_object_has_been_specified!

  subject_has_delegating_method? &&
    subject_has_delegate_object_reader_method? &&
    subject_delegates_to_delegate_object_correctly? &&
    subject_handles_nil_delegate_object?
end

#to(delegate_object_reader_method) ⇒ Object



230
231
232
233
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 230

def to(delegate_object_reader_method)
  @delegate_object_reader_method = delegate_object_reader_method
  self
end

#with_arguments(*arguments) ⇒ Object



240
241
242
243
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 240

def with_arguments(*arguments)
  @delegated_arguments = arguments
  self
end

#with_prefix(prefix = nil) ⇒ Object



245
246
247
248
249
250
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 245

def with_prefix(prefix = nil)
  @delegating_method =
    :"#{build_delegating_method_prefix(prefix)}_#{delegate_method}"
  delegate_method
  self
end