class RSpec::Mocks::Proxy

@private

def self.prepended_modules_of(klass)

def self.prepended_modules_of(klass)
  ancestors = klass.ancestors
  # `|| 0` is necessary for Ruby 2.0, where the singleton class
  # is only in the ancestor list when there are prepended modules.
  singleton_index = ancestors.index(klass) || 0
  ancestors[0, singleton_index]
end

def add_message_expectation(method_name, opts=DEFAULT_MESSAGE_EXPECTATION_OPTS, &block)

Other tags:
    Private: -
def add_message_expectation(method_name, opts=DEFAULT_MESSAGE_EXPECTATION_OPTS, &block)
  location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
  meth_double = method_double_for(method_name)
  if null_object? && !block
    meth_double.add_default_stub(@error_generator, @order_group, location, opts) do
      @object
    end
  end
  meth_double.add_expectation @error_generator, @order_group, location, opts, &block
end

def add_simple_expectation(method_name, response, location)

Other tags:
    Private: -
def add_simple_expectation(method_name, response, location)
  method_double_for(method_name).add_simple_expectation method_name, response, @error_generator, location
end

def add_simple_stub(method_name, response)

Other tags:
    Private: -
def add_simple_stub(method_name, response)
  method_double_for(method_name).add_simple_stub method_name, response
end

def add_stub(method_name, opts={}, &implementation)

Other tags:
    Private: -
def add_stub(method_name, opts={}, &implementation)
  location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
  method_double_for(method_name).add_stub @error_generator, @order_group, location, opts, &implementation
end

def as_null_object

Other tags:
    Private: -
def as_null_object
  @null_object = true
  @object
end

def build_expectation(method_name)

Other tags:
    Private: -
def build_expectation(method_name)
  meth_double = method_double_for(method_name)
  meth_double.build_expectation(
    @error_generator,
    @order_group
  )
end

def check_for_unexpected_arguments(expectation)

Other tags:
    Private: -
def check_for_unexpected_arguments(expectation)
  return if @messages_received.empty?
  return if @messages_received.any? { |method_name, args, _| expectation.matches?(method_name, *args) }
  name_but_not_args, others = @messages_received.partition do |(method_name, args, _)|
    expectation.matches_name_but_not_args(method_name, *args)
  end
  return if name_but_not_args.empty? && !others.empty?
  expectation.raise_unexpected_message_args_error(name_but_not_args.map { |args| args[1] })
end

def ensure_implemented(*_args)

Other tags:
    Private: -
def ensure_implemented(*_args)
  # noop for basic proxies, see VerifyingProxy for behaviour.
end

def find_almost_matching_expectation(method_name, *args)

def find_almost_matching_expectation(method_name, *args)
  find_best_matching_expectation_for(method_name) do |expectation|
    expectation.matches_name_but_not_args(method_name, *args)
  end
end

def find_almost_matching_stub(method_name, *args)

def find_almost_matching_stub(method_name, *args)
  method_double_for(method_name).stubs.find { |stub| stub.matches_name_but_not_args(method_name, *args) }
end

def find_best_matching_expectation_for(method_name)

def find_best_matching_expectation_for(method_name)
  first_match = nil
  method_double_for(method_name).expectations.each do |expectation|
    next unless yield expectation
    return expectation unless expectation.called_max_times?
    first_match ||= expectation
  end
  first_match
end

def find_matching_expectation(method_name, *args)

def find_matching_expectation(method_name, *args)
  find_best_matching_expectation_for(method_name) do |expectation|
    expectation.matches?(method_name, *args)
  end
end

def find_matching_method_stub(method_name, *args)

def find_matching_method_stub(method_name, *args)
  method_double_for(method_name).stubs.find { |stub| stub.matches?(method_name, *args) }
end

def has_negative_expectation?(message)

Other tags:
    Private: -
def has_negative_expectation?(message)
  method_double_for(message).expectations.find { |expectation| expectation.negative_expectation_for?(message) }
end

def initialize(object, order_group, options={})

Other tags:
    Private: -
def initialize(object, order_group, options={})
  @object = object
  @order_group = order_group
  @error_generator = ErrorGenerator.new(object)
  @messages_received = []
  @options = options
  @null_object = false
  @method_doubles = Hash.new { |h, k| h[k] = MethodDouble.new(@object, k, self) }
end

def message_received(message, *args, &block)

Other tags:
    Private: -
def message_received(message, *args, &block)
  record_message_received message, *args, &block
  expectation = find_matching_expectation(message, *args)
  stub = find_matching_method_stub(message, *args)
  if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
    expectation.increase_actual_received_count! if expectation && expectation.actual_received_count_matters?
    if (expectation = find_almost_matching_expectation(message, *args))
      expectation.advise(*args) unless expectation.expected_messages_received?
    end
    stub.invoke(nil, *args, &block)
  elsif expectation
    expectation.unadvise(messages_arg_list)
    expectation.invoke(stub, *args, &block)
  elsif (expectation = find_almost_matching_expectation(message, *args))
    expectation.advise(*args) if null_object? unless expectation.expected_messages_received?
    if null_object? || !has_negative_expectation?(message)
      expectation.raise_unexpected_message_args_error([args])
    end
  elsif (stub = find_almost_matching_stub(message, *args))
    stub.advise(*args)
    raise_missing_default_stub_error(stub, [args])
  elsif Class === @object
    @object.superclass.__send__(message, *args, &block)
  else
    @object.__send__(:method_missing, message, *args, &block)
  end
end

def messages_arg_list

Other tags:
    Private: -
def messages_arg_list
  @messages_received.map { |_, args, _| args }
end

def method_double_for(message)

def method_double_for(message)
  @method_doubles[message.to_sym]
end

def method_double_if_exists_for_message(message)

Other tags:
    Private: -
def method_double_if_exists_for_message(message)
  method_double_for(message) if @method_doubles.key?(message.to_sym)
end

def null_object?

Other tags:
    Private: -
def null_object?
  @null_object
end

def original_method_handle_for(_message)

Other tags:
    Private: -
def original_method_handle_for(_message)
  nil
end

def prepended_modules_of_singleton_class

def prepended_modules_of_singleton_class
  @prepended_modules_of_singleton_class ||= RSpec::Mocks::Proxy.prepended_modules_of(@object.singleton_class)
end

def raise_missing_default_stub_error(expectation, args_for_multiple_calls)

Other tags:
    Private: -
def raise_missing_default_stub_error(expectation, args_for_multiple_calls)
  @error_generator.raise_missing_default_stub_error(expectation, args_for_multiple_calls)
end

def raise_unexpected_message_error(method_name, args)

Other tags:
    Private: -
def raise_unexpected_message_error(method_name, args)
  @error_generator.raise_unexpected_message_error method_name, args
end

def received_message?(method_name, *args, &block)

Other tags:
    Private: -
def received_message?(method_name, *args, &block)
  @messages_received.any? { |array| array == [method_name, args, block] }
end

def record_message_received(message, *args, &block)

Other tags:
    Private: -
def record_message_received(message, *args, &block)
  @order_group.invoked SpecificMessage.new(object, message, args)
  @messages_received << [message, args, block]
end

def remove_stub(method_name)

Other tags:
    Private: -
def remove_stub(method_name)
  method_double_for(method_name).remove_stub
end

def remove_stub_if_present(method_name)

Other tags:
    Private: -
def remove_stub_if_present(method_name)
  method_double_for(method_name).remove_stub_if_present
end

def replay_received_message_on(expectation, &block)

Other tags:
    Private: -
def replay_received_message_on(expectation, &block)
  expected_method_name = expectation.message
  meth_double = method_double_for(expected_method_name)
  if meth_double.expectations.any?
    @error_generator.raise_expectation_on_mocked_method(expected_method_name)
  end
  unless null_object? || meth_double.stubs.any?
    @error_generator.raise_expectation_on_unstubbed_method(expected_method_name)
  end
  @messages_received.each do |(actual_method_name, args, received_block)|
    next unless expectation.matches?(actual_method_name, *args)
    expectation.safe_invoke(nil)
    block.call(*args, &received_block) if block
  end
end

def reset

Other tags:
    Private: -
def reset
  @messages_received.clear
end

def verify

Other tags:
    Private: -
def verify
  @method_doubles.each_value { |d| d.verify }
end

def visibility_for(_method_name)

Other tags:
    Private: -
def visibility_for(_method_name)
  # This is the default (for test doubles). Subclasses override this.
  :public
end