mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Initial mock registry code
This commit is contained in:
parent
cf8e028bd9
commit
875333cffe
1 changed files with 45 additions and 0 deletions
45
src/spectator/mocks/registry.cr
Normal file
45
src/spectator/mocks/registry.cr
Normal file
|
@ -0,0 +1,45 @@
|
|||
module Spectator::Mocks
|
||||
module Registry
|
||||
extend self
|
||||
|
||||
alias Key = Tuple(UInt64, UInt64)
|
||||
|
||||
private struct Entry
|
||||
getter stubs = Deque(MethodStub).new
|
||||
getter calls = Deque(MethodCall).new
|
||||
end
|
||||
|
||||
@entries = {} of Key => Entry
|
||||
|
||||
def reset
|
||||
@entries.clear
|
||||
end
|
||||
|
||||
def register(object)
|
||||
key = unique_key(object)
|
||||
@entries[key] = Entry.new
|
||||
end
|
||||
|
||||
def stub(object, stub : MethodStub)
|
||||
key = unique_key(object)
|
||||
@entries[key].stubs << stub
|
||||
rescue KeyError
|
||||
raise "Cannot stub unregistered mock"
|
||||
end
|
||||
|
||||
def record_call(object, call : MethodCall)
|
||||
key = unique_key(object)
|
||||
@entries[key].calls << call
|
||||
rescue KeyError
|
||||
raise "Cannot record call for unregistered mock"
|
||||
end
|
||||
|
||||
private def unique_key(reference : Reference)
|
||||
{reference.class.hash, reference.object_id}
|
||||
end
|
||||
|
||||
private def unique_key(value : Value)
|
||||
{value.class.hash, value.hash}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue