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.



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

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
  @expects_private_delegation = false
end

Instance Method Details

#allow_nilObject



277
278
279
280
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 277

def allow_nil
  @expects_to_allow_nil_delegate_object = true
  self
end

#as(delegate_method) ⇒ Object



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

def as(delegate_method)
  @delegate_method = delegate_method
  self
end

#build_delegating_method_prefix(prefix) ⇒ Object



287
288
289
290
291
292
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 287

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

#descriptionObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 229

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

  if expects_private_delegation?
    string << ' privately'
  end

  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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 294

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

  if failed_to_allow_nil_delegate_object? || failed_to_handle_private_delegation?
    message << formatted_delegating_method_name(include_module: true)
    message << ' did delegate to '
    message << formatted_delegate_object_reader_method_name
  end

  if failed_to_allow_nil_delegate_object?
    message << ' when it was non-nil, but it failed to account '
    message << 'for when '
    message << formatted_delegate_object_reader_method_name
    message << ' *was* nil.'
  elsif failed_to_handle_private_delegation?
    message << ", but 'private: true' is missing."
  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



321
322
323
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 321

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

#in_context(context) ⇒ Object



212
213
214
215
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 212

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
220
221
222
223
224
225
226
227
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 217

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? &&
    subject_handles_private_delegation?
end

#to(delegate_object_reader_method) ⇒ Object



255
256
257
258
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 255

def to(delegate_object_reader_method)
  @delegate_object_reader_method = delegate_object_reader_method
  self
end

#with_arguments(*arguments) ⇒ Object



265
266
267
268
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 265

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

#with_prefix(prefix = nil) ⇒ Object



270
271
272
273
274
275
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 270

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

#with_privateObject



282
283
284
285
# File 'lib/shoulda/matchers/independent/delegate_method_matcher.rb', line 282

def with_private
  @expects_private_delegation = true
  self
end