mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Introduce _spectator_stub_for_method? utility method
This commit is contained in:
parent
3961662bf6
commit
4b21c9e6c1
5 changed files with 16 additions and 5 deletions
|
@ -110,6 +110,10 @@ module Spectator
|
||||||
stub
|
stub
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||||
|
@stubs.any? { |stub| stub.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) %}
|
||||||
|
@ -130,7 +134,7 @@ module Spectator
|
||||||
|
|
||||||
private def _spectator_abstract_stub_fallback(call : MethodCall)
|
private def _spectator_abstract_stub_fallback(call : MethodCall)
|
||||||
Log.info do
|
Log.info do
|
||||||
break unless @stubs.any? { |stub| stub.method == call.method }
|
break unless _spectator_stub_for_method?(call.method)
|
||||||
|
|
||||||
"Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)."
|
"Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)."
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,7 @@ module Spectator
|
||||||
end
|
end
|
||||||
|
|
||||||
private def _spectator_stub_fallback(call : MethodCall, &)
|
private def _spectator_stub_fallback(call : MethodCall, &)
|
||||||
if @stubs.any? { |stub| stub.method == call.method }
|
if _spectator_stub_for_method?(call.method)
|
||||||
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }
|
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }
|
||||||
raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}")
|
raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}")
|
||||||
else
|
else
|
||||||
|
|
|
@ -23,10 +23,14 @@ module Spectator
|
||||||
@_spectator_stubs.unshift(stub)
|
@_spectator_stubs.unshift(stub)
|
||||||
end
|
end
|
||||||
|
|
||||||
def _spectator_find_stub(call : ::Spectator::MethodCall) : ::Spectator::Stub?
|
private def _spectator_find_stub(call : ::Spectator::MethodCall) : ::Spectator::Stub?
|
||||||
@_spectator_stubs.find &.===(call)
|
@_spectator_stubs.find &.===(call)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||||
|
@_spectator_stubs.any? { |stub| stub.method == method }
|
||||||
|
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) %}
|
||||||
|
|
|
@ -24,7 +24,7 @@ module Spectator
|
||||||
end
|
end
|
||||||
|
|
||||||
private def _spectator_abstract_stub_fallback(call : MethodCall)
|
private def _spectator_abstract_stub_fallback(call : MethodCall)
|
||||||
if @stubs.any? { |stub| stub.method == call.method }
|
if _spectator_stub_for_method?(call.method)
|
||||||
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }
|
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }
|
||||||
raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}")
|
raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}")
|
||||||
else
|
else
|
||||||
|
@ -40,7 +40,7 @@ module Spectator
|
||||||
|
|
||||||
# Default implementation that raises a `TypeCastError` since the return type isn't self.
|
# Default implementation that raises a `TypeCastError` since the return type isn't self.
|
||||||
private def _spectator_abstract_stub_fallback(call : MethodCall, type)
|
private def _spectator_abstract_stub_fallback(call : MethodCall, type)
|
||||||
if @stubs.any? { |stub| stub.method == call.method }
|
if _spectator_stub_for_method?(call.method)
|
||||||
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }
|
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }
|
||||||
raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}")
|
raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}")
|
||||||
else
|
else
|
||||||
|
|
|
@ -22,6 +22,9 @@ module Spectator
|
||||||
# or nil if no stubs satisfy it.
|
# or nil if no stubs satisfy it.
|
||||||
abstract def _spectator_find_stub(call : MethodCall) : Stub?
|
abstract def _spectator_find_stub(call : MethodCall) : Stub?
|
||||||
|
|
||||||
|
# Utility method that looks for stubs for methods with the name specified.
|
||||||
|
abstract def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||||
|
|
||||||
# Defines a stub to change the behavior of a method.
|
# Defines a stub to change the behavior of a method.
|
||||||
abstract def _spectator_define_stub(stub : Stub) : Nil
|
abstract def _spectator_define_stub(stub : Stub) : Nil
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue