Switch to using Registry for mocks and fix various issues

This commit is contained in:
Michael Miller 2019-11-09 09:03:36 -07:00
parent 8c2f8d973b
commit 0698ed655d
2 changed files with 19 additions and 12 deletions

View file

@ -9,27 +9,34 @@ module Spectator::Mocks
getter calls = Deque(MethodCall).new
end
@entries = {} of Key => Entry
@@entries = {} of Key => Entry
def reset
def reset : Nil
@entries.clear
end
def register(object)
def register(object) : Nil
key = unique_key(object)
@entries[key] = Entry.new
@@entries[key] = Entry.new
end
def stub(object, stub : MethodStub)
def add_stub(object, stub : MethodStub) : Nil
key = unique_key(object)
@entries[key].stubs << stub
@@entries[key].stubs << stub
rescue KeyError
raise "Cannot stub unregistered mock"
end
def record_call(object, call : MethodCall)
def find_stub(object, call : GenericMethodCall(T, NT)) forall T, NT
key = unique_key(object)
@entries[key].calls << call
@@entries[key].stubs.find(&.callable?(call))
rescue KeyError
raise "Cannot stub unregistered mock"
end
def record_call(object, call : MethodCall) : Nil
key = unique_key(object)
@@entries[key].calls << call
rescue KeyError
raise "Cannot record call for unregistered mock"
end

View file

@ -30,8 +30,8 @@ module Spectator::Mocks
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
@spectator_stub_calls << %call
if (%stub = @spectator_stubs.find(&.callable?(%call)))
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
%stub.call(%args, typeof(previous_def({{args.splat}})))
else
previous_def({{args.splat}})
@ -41,8 +41,8 @@ module Spectator::Mocks
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
@spectator_stub_calls << %call
if (%stub = @spectator_stubs.find(&.callable?(%call)))
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
%stub.call(%args, typeof(previous_def({{args.splat}}) { |*%ya| yield *%ya }))
else
previous_def({{args.splat}}) do |*%yield_args|