Move double info to registry

This commit is contained in:
Michael Miller 2019-11-09 09:18:34 -07:00
parent 5a072301af
commit 9e8286f892

View file

@ -3,9 +3,6 @@ require "./generic_method_stub"
module Spectator::Mocks module Spectator::Mocks
abstract class Double abstract class Double
@spectator_stubs = Deque(MethodStub).new
@spectator_stub_calls = Deque(MethodCall).new
def initialize(@spectator_double_name : Symbol) def initialize(@spectator_double_name : Symbol)
end end
@ -39,8 +36,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(%method({{args.splat}}))) %stub.call(%args, typeof(%method({{args.splat}})))
else else
%method({{args.splat}}) %method({{args.splat}})
@ -50,8 +47,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(%method({{args.splat}}) { |*%ya| yield *%ya })) %stub.call(%args, typeof(%method({{args.splat}}) { |*%ya| yield *%ya }))
else else
%method({{args.splat}}) do |*%yield_args| %method({{args.splat}}) do |*%yield_args|
@ -80,13 +77,5 @@ module Spectator::Mocks
io << @spectator_double_name io << @spectator_double_name
io << ')' io << ')'
end end
protected def spectator_define_stub(stub : MethodStub) : Nil
@spectator_stubs << stub
end
protected def spectator_stub_calls(method : Symbol) : Array(MethodCall)
@spectator_stub_calls.select { |call| call.name == method }
end
end end
end end