Pass original (and type constraint) to stub via block

This commit is contained in:
Michael Miller 2019-11-20 15:43:45 -07:00
parent 51a47fec98
commit 2dee50f19e
5 changed files with 9 additions and 7 deletions

View file

@ -12,7 +12,9 @@ module Spectator::Mocks
call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args)
::Spectator::Harness.current.mocks.record_call(self, call)
if (stub = ::Spectator::Harness.current.mocks.find_stub(self, call))
stub.call!(args, typeof(@values.fetch({{call.name.symbolize}}) { raise }))
stub.call!(args) do
@values.fetch({{call.name.symbolize}}) { raise "Consistency error - method stubbed with no implementation" }
end
else
@values.fetch({{call.name.symbolize}}) do
return nil if ::Spectator::Harness.current.mocks.expected?(self, {{call.name.symbolize}})

View file

@ -8,7 +8,7 @@ module Spectator::Mocks
call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args)
::Spectator::Harness.current.mocks.record_call(self, call)
if (stub = ::Spectator::Harness.current.mocks.find_stub(self, call))
stub.call!(args, typeof(@values.fetch({{call.name.symbolize}}) { self }))
stub.call!(args) { @values.fetch({{call.name.symbolize}}) { self } }
else
@values.fetch({{call.name.symbolize}}) { self }
end

View file

@ -39,7 +39,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Harness.current.mocks.record_call(self, %call)
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args, typeof(%method({{args.splat}})))
%stub.call!(%args) { %method({{args.splat}}) }
else
%method({{args.splat}})
end
@ -50,7 +50,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Harness.current.mocks.record_call(self, %call)
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args, typeof(%method({{args.splat}}) { |*%ya| yield *%ya }))
%stub.call!(%args) { %method({{args.splat}}) { |*%ya| yield *%ya } }
else
%method({{args.splat}}) do |*%yield_args|
yield *%yield_args

View file

@ -16,7 +16,7 @@ module Spectator::Mocks
abstract def call(args : GenericArguments(T, NT)) forall T, NT
def call!(args : GenericArguments(T, NT), _rt : RT.class) : RT forall T, NT, RT
def call!(args : GenericArguments(T, NT), &original : -> RT) : RT forall T, NT, RT
value = call(args)
if value.is_a?(RT)
value.as(RT)

View file

@ -56,7 +56,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
%harness.mocks.record_call(self, %call)
if (%stub = %harness.mocks.find_stub(self, %call))
return %stub.call!(%args, typeof({{original}}({{args.splat}})))
return %stub.call!(%args) { {{original}}({{args.splat}}) }
end
end
{{original}}({{args.splat}})
@ -68,7 +68,7 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
%harness.mocks.record_call(self, %call)
if (%stub = %harness.mocks.find_stub(self, %call))
return %stub.call!(%args, typeof({{original}}({{args.splat}}) { |*%ya| yield *%ya }))
return %stub.call!(%args) { {{original}}({{args.splat}}) { |*%ya| yield *%ya } }
end
end
{{original}}({{args.splat}}) do |*%yield_args|