Introduce _spectator_stub_for_method? utility method

This commit is contained in:
Michael Miller 2022-04-15 17:16:49 -06:00
parent 3961662bf6
commit 4b21c9e6c1
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
5 changed files with 16 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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) %}

View file

@ -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

View file

@ -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