mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add methods for recording calls to stubs
This commit is contained in:
parent
c70e4792af
commit
c98edcec5d
4 changed files with 39 additions and 1 deletions
|
@ -63,6 +63,8 @@ module Spectator
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@calls = [] of MethodCall
|
||||||
|
|
||||||
# Creates the double.
|
# Creates the double.
|
||||||
#
|
#
|
||||||
# An initial set of *stubs* can be provided.
|
# An initial set of *stubs* can be provided.
|
||||||
|
@ -111,6 +113,14 @@ module Spectator
|
||||||
@stubs.any? { |stub| stub.method == method }
|
@stubs.any? { |stub| stub.method == method }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _spectator_record_call(call : MethodCall) : Nil
|
||||||
|
@calls << call
|
||||||
|
end
|
||||||
|
|
||||||
|
def _spectator_calls(method : Symbol) : Enumerable(MethodCall)
|
||||||
|
@calls.select { |call| call.method == method }
|
||||||
|
end
|
||||||
|
|
||||||
# Returns the double's name formatted for user output.
|
# Returns the double's name formatted for user output.
|
||||||
private def _spectator_stubbed_name : String
|
private def _spectator_stubbed_name : String
|
||||||
{% if anno = @type.annotation(StubbedName) %}
|
{% if anno = @type.annotation(StubbedName) %}
|
||||||
|
|
|
@ -52,6 +52,8 @@ module Spectator
|
||||||
@_spectator_stubs = nil
|
@_spectator_stubs = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private getter _spectator_calls = [] of ::Spectator::MethodCall
|
||||||
|
|
||||||
# Returns the mock's name formatted for user output.
|
# Returns the mock's name formatted for user output.
|
||||||
private def _spectator_stubbed_name : String
|
private def _spectator_stubbed_name : String
|
||||||
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
|
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
|
||||||
|
@ -126,6 +128,10 @@ module Spectator
|
||||||
@@_spectator_mock_registry.delete(self)
|
@@_spectator_mock_registry.delete(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _spectator_calls
|
||||||
|
[] of ::Spectator::MethodCall
|
||||||
|
end
|
||||||
|
|
||||||
# Returns the mock's name formatted for user output.
|
# Returns the mock's name formatted for user output.
|
||||||
private def _spectator_stubbed_name : String
|
private def _spectator_stubbed_name : String
|
||||||
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
|
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
|
||||||
|
|
|
@ -8,15 +8,23 @@ module Spectator
|
||||||
#
|
#
|
||||||
# Bridges functionality between mocks and stubs
|
# Bridges functionality between mocks and stubs
|
||||||
# Implements the abstracts methods from `Stubbable`.
|
# Implements the abstracts methods from `Stubbable`.
|
||||||
|
#
|
||||||
# Types including this module will need to implement `#_spectator_stubs`.
|
# Types including this module will need to implement `#_spectator_stubs`.
|
||||||
# It should return a mutable list of stubs.
|
# It should return a mutable list of stubs.
|
||||||
# This is used to store the stubs for the mocked type.
|
# This is used to store the stubs for the mocked type.
|
||||||
|
#
|
||||||
|
# Additionally, the `#_spectator_calls` (getter with no arguments) must be implemented.
|
||||||
|
# It should return a mutable list of method calls.
|
||||||
|
# This is used to store the calls to stubs for the mocked type.
|
||||||
module Mocked
|
module Mocked
|
||||||
include Stubbable
|
include Stubbable
|
||||||
|
|
||||||
# Retrieves an enumerable collection of stubs.
|
# Retrieves an mutable collection of stubs.
|
||||||
abstract def _spectator_stubs
|
abstract def _spectator_stubs
|
||||||
|
|
||||||
|
# Retrieves an mutable collection of calls to stubs.
|
||||||
|
abstract def _spectator_calls
|
||||||
|
|
||||||
def _spectator_define_stub(stub : ::Spectator::Stub) : Nil
|
def _spectator_define_stub(stub : ::Spectator::Stub) : Nil
|
||||||
_spectator_stubs.unshift(stub)
|
_spectator_stubs.unshift(stub)
|
||||||
end
|
end
|
||||||
|
@ -33,6 +41,14 @@ module Spectator
|
||||||
_spectator_stubs.any? { |stub| stub.method == method }
|
_spectator_stubs.any? { |stub| stub.method == method }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _spectator_record_call(call : MethodCall) : Nil
|
||||||
|
_spectator_calls << call
|
||||||
|
end
|
||||||
|
|
||||||
|
def _spectator_calls(method : Symbol) : Enumerable(MethodCall)
|
||||||
|
_spectator_calls.select { |call| call.method == method }
|
||||||
|
end
|
||||||
|
|
||||||
# Method called when a stub isn't found.
|
# Method called when a stub isn't found.
|
||||||
#
|
#
|
||||||
# The received message is captured in *call*.
|
# The received message is captured in *call*.
|
||||||
|
|
|
@ -31,6 +31,12 @@ module Spectator
|
||||||
# Clears all previously defined stubs.
|
# Clears all previously defined stubs.
|
||||||
abstract def _spectator_clear_stubs : Nil
|
abstract def _spectator_clear_stubs : Nil
|
||||||
|
|
||||||
|
# Saves a call that was made to a stubbed method.
|
||||||
|
abstract def _spectator_record_call(call : MethodCall) : Nil
|
||||||
|
|
||||||
|
# Retrieves all previously saved calls for the specified method.
|
||||||
|
abstract def _spectator_calls(method : Symbol) : Enumerable(MethodCall)
|
||||||
|
|
||||||
# Method called when a stub isn't found.
|
# Method called when a stub isn't found.
|
||||||
#
|
#
|
||||||
# The received message is captured in *call*.
|
# The received message is captured in *call*.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue