mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Switch to using Registry for mocks and fix various issues
This commit is contained in:
parent
8c2f8d973b
commit
0698ed655d
2 changed files with 19 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -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|
|
||||
|
|
Loading…
Reference in a new issue