class RSpec::Mocks::MethodDouble

@private

def add_default_stub(*args, &implementation)

Other tags:
    Private: -
def add_default_stub(*args, &implementation)
  return if stubs.any?
  add_stub(*args, &implementation)
end

def add_expectation(error_generator, expectation_ordering, expected_from, opts, &implementation)

Other tags:
    Private: -
def add_expectation(error_generator, expectation_ordering, expected_from, opts, &implementation)
  configure_method
  expectation = message_expectation_class.new(error_generator, expectation_ordering,
                                       expected_from, self, 1, opts, &implementation)
  expectations << expectation
  expectation
end

def add_simple_expectation(method_name, response, error_generator, backtrace_line)

Other tags:
    Private: -
def add_simple_expectation(method_name, response, error_generator, backtrace_line)
  setup_simple_method_double method_name, response, expectations, error_generator, backtrace_line
end

def add_simple_stub(method_name, response)

Other tags:
    Private: -
def add_simple_stub(method_name, response)
  setup_simple_method_double method_name, response, stubs
end

def add_stub(error_generator, expectation_ordering, expected_from, opts={}, &implementation)

Other tags:
    Private: -
def add_stub(error_generator, expectation_ordering, expected_from, opts={}, &implementation)
  configure_method
  stub = message_expectation_class.new(error_generator, expectation_ordering, expected_from,
                                self, :any, opts, &implementation)
  stubs.unshift stub
  stub
end

def build_expectation(error_generator, expectation_ordering)

Other tags:
    Private: -
def build_expectation(error_generator, expectation_ordering)
  expected_from = IGNORED_BACKTRACE_LINE
  message_expectation_class.new(error_generator, expectation_ordering, expected_from, self)
end

def clear

Other tags:
    Private: -
def clear
  expectations.clear
  stubs.clear
end

def configure_method

Other tags:
    Private: -
def configure_method
  @original_visibility = [visibility, method_name]
  @method_stasher.stash unless @method_is_proxied
  define_proxy_method
end

def define_proxy_method

Other tags:
    Private: -
def define_proxy_method
  return if @method_is_proxied
  save_original_method!
  object_singleton_class.class_exec(self, method_name, visibility) do |method_double, method_name, visibility|
    define_method(method_name) do |*args, &block|
      method_double.proxy_method_invoked(self, *args, &block)
    end
    self.__send__ visibility, method_name
  end
  @method_is_proxied = true
end

def initialize(object, method_name, proxy)

Other tags:
    Private: -
def initialize(object, method_name, proxy)
  @method_name = method_name
  @object = object
  @proxy = proxy
  @original_visibility = nil
  @method_stasher = InstanceMethodStasher.new(object, method_name)
  @method_is_proxied = false
  @expectations = []
  @stubs = []
end

def message_expectation_class

Other tags:
    Private: -
def message_expectation_class
  MessageExpectation
end

def object_singleton_class

Other tags:
    Private: -
def object_singleton_class
  class << @object; self; end
end

def original_method

def original_method
  # If original method is not present, uses the `method_missing`
  # handler of the object. This accounts for cases where the user has not
  # correctly defined `respond_to?`, and also 1.8 which does not provide
  # method handles for missing methods even if `respond_to?` is correct.
  @original_method ||=
    @method_stasher.original_method ||
    @proxy.method_handle_for(method_name) ||
    Proc.new do |*args, &block|
      @object.__send__(:method_missing, @method_name, *args, &block)
    end
end

def proxy_method_invoked(obj, *args, &block)

Other tags:
    Private: -
def proxy_method_invoked(obj, *args, &block)
  @proxy.message_received method_name, *args, &block
end

def raise_method_not_stubbed_error

Other tags:
    Private: -
def raise_method_not_stubbed_error
  raise MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed"
end

def remove_single_stub(stub)

Other tags:
    Private: -
def remove_single_stub(stub)
  stubs.delete(stub)
  restore_original_method if stubs.empty? && expectations.empty?
end

def remove_stub

Other tags:
    Private: -
def remove_stub
  raise_method_not_stubbed_error if stubs.empty?
  expectations.empty? ? reset : stubs.clear
end

def reset

Other tags:
    Private: -
def reset
  restore_original_method
  clear
end

def restore_original_method

Other tags:
    Private: -
def restore_original_method
  return unless @method_is_proxied
  object_singleton_class.__send__(:remove_method, @method_name)
  if @method_stasher.method_is_stashed?
    @method_stasher.restore
  end
  restore_original_visibility
  @method_is_proxied = false
end

def restore_original_visibility

Other tags:
    Private: -
def restore_original_visibility
  return unless @original_visibility &&
    (object_singleton_class.method_defined?(@method_name) ||
     object_singleton_class.private_method_defined?(@method_name))
  object_singleton_class.__send__(*@original_visibility)
end

def setup_simple_method_double(method_name, response, collection, error_generator = nil, backtrace_line = nil)

Other tags:
    Private: -
def setup_simple_method_double(method_name, response, collection, error_generator = nil, backtrace_line = nil)
  define_proxy_method
  me = SimpleMessageExpectation.new(method_name, response, error_generator, backtrace_line)
  collection.unshift me
  me
end

def verify

Other tags:
    Private: -
def verify
  expectations.each {|e| e.verify_messages_received}
end

def visibility

Other tags:
    Private: -
def visibility
  if TestDouble === @object
    'public'
  elsif object_singleton_class.private_method_defined?(@method_name)
    'private'
  elsif object_singleton_class.protected_method_defined?(@method_name)
    'protected'
  else
    'public'
  end
end