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]
subject_double_collection[R]

Public Class Methods

new(delegating_method) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 159
def initialize(delegating_method)
  @delegating_method = delegating_method

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

  @delegated_arguments = []
  @delegate_object_reader_method = nil
  @subject = nil
  @subject_double_collection = nil
end

Public Instance Methods

as(delegate_method) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 206
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 223
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 186
def description
  string = "delegate #{formatted_delegating_method_name} to " +
    "#{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

  string
end
failure_message() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 230
def failure_message
  "Expected #{class_under_test} to #{description}\n" +
    "Method calls sent to " +
    "#{formatted_delegate_object_reader_method_name(include_module: true)}:" +
    formatted_calls_on_delegate_object
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 237
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 171
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 176
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?
end
to(delegate_object_reader_method) click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 201
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 211
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 216
def with_prefix(prefix = nil)
  @delegating_method =
    :"#{build_delegating_method_prefix(prefix)}_#{delegate_method}"
    delegate_method
  self
end

Protected Instance Methods

calls_on_delegate_object() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 358
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 354
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 299
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 265
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 307
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 311
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 325
def ensure_delegate_object_has_been_specified!
  if delegate_object_reader_method.to_s.empty?
    raise DelegateObjectNotSpecified
  end
end
formatted_calls_on_delegate_object() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 362
def formatted_calls_on_delegate_object
  string = ""

  if calls_on_delegate_object.any?
    string << "\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 273
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 281
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 277
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 285
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 291
def possible_class_under_test(options)
  if options[:include_module]
    class_under_test.to_s
  else
    ""
  end
end
register_subject_double_collection() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 345
def register_subject_double_collection
  double_collection =
    Doublespeak.double_collection_for(subject.singleton_class)
  double_collection.register_stub(delegate_object_reader_method).
    to_return(delegate_object)

  @subject_double_collection = double_collection
end
subject() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 253
def subject
  @subject
end
subject_delegates_to_delegate_object_correctly?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 331
def subject_delegates_to_delegate_object_correctly?
  register_subject_double_collection

  Doublespeak.with_doubles_activated do
    subject.public_send(delegating_method, *delegated_arguments)
  end

  if delegated_arguments.any?
    delegate_object_received_call_with_delegated_arguments?
  else
    delegate_object_received_call?
  end
end
subject_has_delegate_object_reader_method?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_method_matcher.rb, line 321
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 317
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 257
def subject_is_a_class?
  if @subject
    @subject.is_a?(Class)
  else
    context.subject_is_a_class?
  end
end