module MuchStub

Constants

NotStubbedError
NullStub
StubArityError
StubError
VERSION

Public Class Methods

arity_matches?(method, args) click to toggle source
# File lib/much-stub.rb, line 17
def self.arity_matches?(method, args)
  # mandatory args
  return true if method.arity == args.size
  # variable args
  return true if method.arity < 0 && args.size >= (method.arity + 1).abs

  false
end
call(*args, &block) click to toggle source
# File lib/much-stub.rb, line 32
def self.call(*args, &block)
  stub(*args, &block)
end
on_call(*args, &on_call_block) click to toggle source
# File lib/much-stub.rb, line 40
def self.on_call(*args, &on_call_block)
  stub_on_call(*args, &on_call_block)
end
spy(obj, *meths, **return_values) click to toggle source
# File lib/much-stub.rb, line 76
def self.spy(obj, *meths, **return_values)
  MuchStub::CallSpy.new(**return_values).call_spy_tap do |spy|
    meths.each do |meth|
      stub(obj, meth) do |*pargs, **kargs, &block|
        spy.__send__(meth, *pargs, **kargs, &block)
      end
    end
  end
end
stub(obj, meth, &block) click to toggle source
# File lib/much-stub.rb, line 26
def self.stub(obj, meth, &block)
  key = stub_key(obj, meth)
  stubs[key] ||= MuchStub::Stub.new(obj, meth, caller_locations)
  stubs[key].tap{ |s| s.do = block }
end
stub_key(obj, meth) click to toggle source
# File lib/much-stub.rb, line 13
def self.stub_key(obj, meth)
  MuchStub::Stub.key(obj, meth)
end
stub_on_call(*args, &on_call_block) click to toggle source
# File lib/much-stub.rb, line 36
def self.stub_on_call(*args, &on_call_block)
  stub(*args).on_call(&on_call_block)
end
stub_send(obj, meth, *pargs, **kargs, &block) click to toggle source
# File lib/much-stub.rb, line 53
def self.stub_send(obj, meth, *pargs, **kargs, &block)
  orig_caller = caller_locations
  stub =
    stubs.fetch(MuchStub::Stub.key(obj, meth)) do
      raise NotStubbedError, "`#{meth}` not stubbed.", orig_caller.map(&:to_s)
    end
  stub.call_method(*pargs, **kargs, &block)
end
stubs() click to toggle source
# File lib/much-stub.rb, line 9
def self.stubs
  @stubs ||= {}
end
tap(obj, meth, &tap_block) click to toggle source
# File lib/much-stub.rb, line 62
def self.tap(obj, meth, &tap_block)
  stub(obj, meth) do |*pargs, **kargs, &block|
    stub_send(obj, meth, *pargs, **kargs, &block).tap do |value|
      tap_block&.call(value, *pargs, **kargs, &block)
    end
  end
end
tap_on_call(obj, meth, &on_call_block) click to toggle source
# File lib/much-stub.rb, line 70
def self.tap_on_call(obj, meth, &on_call_block)
  tap(obj, meth) do |value, *pargs, **kargs, &block|
    on_call_block&.call(value, MuchStub::Call.new(*pargs, **kargs, &block))
  end
end
unstub(obj, meth) click to toggle source
# File lib/much-stub.rb, line 44
def self.unstub(obj, meth)
  key = stub_key(obj, meth)
  (stubs.delete(key) || MuchStub::NullStub.new).teardown
end
unstub!() click to toggle source
# File lib/much-stub.rb, line 49
def self.unstub!
  stubs.keys.each{ |key| stubs.delete(key).teardown }
end