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 getter calls = Deque(MethodCall).new
end end
@entries = {} of Key => Entry @@entries = {} of Key => Entry
def reset def reset : Nil
@entries.clear @entries.clear
end end
def register(object) def register(object) : Nil
key = unique_key(object) key = unique_key(object)
@entries[key] = Entry.new @@entries[key] = Entry.new
end end
def stub(object, stub : MethodStub) def add_stub(object, stub : MethodStub) : Nil
key = unique_key(object) key = unique_key(object)
@entries[key].stubs << stub @@entries[key].stubs << stub
rescue KeyError rescue KeyError
raise "Cannot stub unregistered mock" raise "Cannot stub unregistered mock"
end end
def record_call(object, call : MethodCall) def find_stub(object, call : GenericMethodCall(T, NT)) forall T, NT
key = unique_key(object) 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 rescue KeyError
raise "Cannot record call for unregistered mock" raise "Cannot record call for unregistered mock"
end end

View file

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