Add ability to remove specific stubs

This commit is contained in:
Michael Miller 2022-10-09 13:38:29 -06:00
parent 422b0efa59
commit 25b9931002
No known key found for this signature in database
GPG Key ID: 32B47AE8F388A1FF
7 changed files with 36 additions and 0 deletions

View File

@ -103,6 +103,11 @@ module Spectator
@stubs.unshift(stub)
end
protected def _spectator_remove_stub(stub : Stub) : Nil
Log.debug { "Removing stub #{stub} from #{_spectator_stubbed_name}" }
@stubs.delete(stub)
end
protected def _spectator_clear_stubs : Nil
Log.debug { "Clearing stubs for #{_spectator_stubbed_name}" }
@stubs.clear

View File

@ -50,6 +50,10 @@ module Spectator
end
{% end %}
def _spectator_remove_stub(stub : ::Spectator::Stub) : Nil
@_spectator_stubs.try &.delete(stub)
end
def _spectator_clear_stubs : Nil
@_spectator_stubs = nil
end
@ -139,6 +143,10 @@ module Spectator
entry.stubs
end
def _spectator_remove_stub(stub : ::Spectator::Stub) : Nil
@@_spectator_mock_registry[self]?.try &.stubs.delete(stub)
end
def _spectator_clear_stubs : Nil
@@_spectator_mock_registry.delete(self)
end

View File

@ -26,6 +26,10 @@ module Spectator
_spectator_stubs.unshift(stub)
end
def _spectator_remove_stub(stub : Stub) : Nil
_spectator_stubs.delete(stub)
end
def _spectator_clear_stubs : Nil
_spectator_stubs.clear
end

View File

@ -25,6 +25,12 @@ module Spectator
@entries[key]
end
# Retrieves all stubs defined for a mocked object or nil if the object isn't mocked yet.
def []?(object : Reference)
key = Box.box(object)
@entries[key]?
end
# Retrieves all stubs defined for a mocked object.
#
# Yields to the block on the first retrieval.

View File

@ -28,6 +28,9 @@ module Spectator
# Defines a stub to change the behavior of a method.
abstract def _spectator_define_stub(stub : Stub) : Nil
# Removes a specific, previously defined stub.
abstract def _spectator_remove_stub(stub : Stub) : Nil
# Clears all previously defined stubs.
abstract def _spectator_clear_stubs : Nil

View File

@ -20,6 +20,10 @@ module Spectator
_spectator_stubs.unshift(stub)
end
def _spectator_remove_stub(stub : Stub) : Nil
_spectator_stubs.delete(stub)
end
def _spectator_clear_stubs : Nil
_spectator_stubs.clear
end

View File

@ -29,6 +29,12 @@ module Spectator
@entries[key]
end
# Retrieves all stubs defined for a mocked object or nil if the object isn't mocked yet.
def []?(object : T)
key = value_bytes(object)
@entries[key]?
end
# Retrieves all stubs defined for a mocked object.
#
# Yields to the block on the first retrieval.