Don't require registration

This commit is contained in:
Michael Miller 2019-11-09 09:15:41 -07:00
parent fc0b46caca
commit 5a072301af

View file

@ -15,30 +15,25 @@ module Spectator::Mocks
@entries.clear @entries.clear
end end
def register(object) : Nil
key = unique_key(object)
@@entries[key] = Entry.new
end
def add_stub(object, stub : MethodStub) : Nil def add_stub(object, stub : MethodStub) : Nil
key = unique_key(object) fetch(object).stubs << stub
@@entries[key].stubs << stub
rescue KeyError
raise "Cannot stub unregistered mock"
end end
def find_stub(object, call : GenericMethodCall(T, NT)) forall T, NT def find_stub(object, call : GenericMethodCall(T, NT)) forall T, NT
key = unique_key(object) fetch(object).stubs.find(&.callable?(call))
@@entries[key].stubs.find(&.callable?(call))
rescue KeyError
raise "Cannot stub unregistered mock"
end end
def record_call(object, call : MethodCall) : Nil def record_call(object, call : MethodCall) : Nil
fetch(object).calls << call
end
private def fetch(object)
key = unique_key(object) key = unique_key(object)
@@entries[key].calls << call if @@entries.has_key?(key)
rescue KeyError @@entries[key]
raise "Cannot record call for unregistered mock" else
@@entries[key] = Entry.new
end
end end
private def unique_key(reference : Reference) private def unique_key(reference : Reference)