class Object

:nodoc:

def stub name, val_or_callable, *block_args, &block

def stub name, val_or_callable, *block_args, &block
  new_name = "__minitest_stub__#{name}"
  metaclass = class << self; self; end
  if respond_to? name and not methods.map(&:to_s).include? name.to_s then
    metaclass.send :define_method, name do |*args|
      super(*args)
    end
  end
  metaclass.send :alias_method, new_name, name
  metaclass.send :define_method, name do |*args, &blk|
    ret = if val_or_callable.respond_to? :call then
      val_or_callable.call(*args)
    else
      val_or_callable
    end
    blk.call(*block_args) if blk
    ret
  end
  yield self
ensure
  metaclass.send :undef_method, name
  metaclass.send :alias_method, name, new_name
  metaclass.send :undef_method, new_name
end