class Shoulda::Matchers::Independent::DelegateMethodMatcher

@private

Attributes

context[R]
delegate_method[R]
delegate_object[R]
delegate_object_reader_method[R]
delegated_arguments[R]
delegating_method[R]
method[R]

Public Class Methods

new(delegating_method) click to toggle source
# 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

Public Instance Methods

allow_nil() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
description() click to toggle source
# 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_message() click to toggle source
# 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_negated() click to toggle source
# 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) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 192
def in_context(context)
  @context = MatcherContext.new(context)
  self
end
matches?(subject) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 240
def with_arguments(*arguments)
  @delegated_arguments = arguments
  self
end
with_prefix(prefix = nil) click to toggle source
# 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

Protected Instance Methods

call_delegating_method_with_delegate_method_returning(value) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 418
def call_delegating_method_with_delegate_method_returning(value)
  register_subject_double_collection_to(value)

  Doublespeak.with_doubles_activated do
    subject.public_send(delegating_method, *delegated_arguments)
  end
end
calls_on_delegate_object() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 437
def calls_on_delegate_object
  delegate_object.calls
end
calls_to_delegate_method() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 433
def calls_to_delegate_method
  delegate_object.calls_to(delegate_method)
end
class_or_instance_method_indicator() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 351
def class_or_instance_method_indicator
  if subject_is_a_class?
    '.'
  else
    '#'
  end
end
class_under_test() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 313
def class_under_test
  if subject_is_a_class?
    subject
  else
    subject.class
  end
end
delegate_object_received_call?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 359
def delegate_object_received_call?
  calls_to_delegate_method.any?
end
delegate_object_received_call_with_delegated_arguments?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 363
def delegate_object_received_call_with_delegated_arguments?
  calls_to_delegate_method.any? do |call|
    call.args == delegated_arguments
  end
end
ensure_delegate_object_has_been_specified!() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 377
def ensure_delegate_object_has_been_specified!
  if delegate_object_reader_method.to_s.empty?
    raise DelegateObjectNotSpecified
  end
end
expects_to_allow_nil_delegate_object?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 321
def expects_to_allow_nil_delegate_object?
  @expects_to_allow_nil_delegate_object
end
failed_to_allow_nil_delegate_object?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 413
def failed_to_allow_nil_delegate_object?
  expects_to_allow_nil_delegate_object? &&
    !@subject_handled_nil_delegate_object
end
formatted_calls_on_delegate_object() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 441
def formatted_calls_on_delegate_object
  string = ""

  if calls_on_delegate_object.any?
    string << "\n\n"
    calls_on_delegate_object.each_with_index do |call, i|
      name = call.method_name
      args = call.args.map { |arg| arg.inspect }.join(', ')
      string << "#{i+1}) #{name}(#{args})\n"
    end
  else
    string << " (none)"
  end

  string.rstrip!

  string
end
formatted_delegate_method(options = {}) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 325
def formatted_delegate_method(options = {})
  formatted_method_name_for(delegate_method, options)
end
formatted_delegate_object_reader_method_name(options = {}) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 333
def formatted_delegate_object_reader_method_name(options = {})
  formatted_method_name_for(delegate_object_reader_method, options)
end
formatted_delegating_method_name(options = {}) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 329
def formatted_delegating_method_name(options = {})
  formatted_method_name_for(delegating_method, options)
end
formatted_method_name_for(method_name, options) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 337
def formatted_method_name_for(method_name, options)
  possible_class_under_test(options) +
    class_or_instance_method_indicator +
    method_name.to_s
end
possible_class_under_test(options) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 343
def possible_class_under_test(options)
  if options[:include_module]
    class_under_test.to_s
  else
    ""
  end
end
register_subject_double_collection_to(returned_value) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 426
def register_subject_double_collection_to(returned_value)
  double_collection =
    Doublespeak.double_collection_for(subject.singleton_class)
  double_collection.register_stub(delegate_object_reader_method).
    to_return(returned_value)
end
subject() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 301
def subject
  @subject
end
subject_delegates_to_delegate_object_correctly?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 383
def subject_delegates_to_delegate_object_correctly?
  call_delegating_method_with_delegate_method_returning(delegate_object)

  if delegated_arguments.any?
    delegate_object_received_call_with_delegated_arguments?
  else
    delegate_object_received_call?
  end
end
subject_handles_nil_delegate_object?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 393
def subject_handles_nil_delegate_object?
  @subject_handled_nil_delegate_object =
    if expects_to_allow_nil_delegate_object?
      begin
        call_delegating_method_with_delegate_method_returning(nil)
        true
      rescue Module::DelegationError
        false
      rescue NoMethodError => error
        if error.message =~ /undefined method `#{delegate_method}' for nil:NilClass/
          false
        else
          raise error
        end
      end
    else
      true
    end
end
subject_has_delegate_object_reader_method?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 373
def subject_has_delegate_object_reader_method?
  subject.respond_to?(delegate_object_reader_method, true)
end
subject_has_delegating_method?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 369
def subject_has_delegating_method?
  subject.respond_to?(delegating_method)
end
subject_is_a_class?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 305
def subject_is_a_class?
  if @subject
    @subject.is_a?(Class)
  else
    context.subject_is_a_class?
  end
end