From 4b21c9e6c1e218a66559439cfaffbf9ad96b50f2 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 15 Apr 2022 17:16:49 -0600 Subject: [PATCH] Introduce _spectator_stub_for_method? utility method --- src/spectator/mocks/double.cr | 6 +++++- src/spectator/mocks/lazy_double.cr | 2 +- src/spectator/mocks/mock.cr | 6 +++++- src/spectator/mocks/null_double.cr | 4 ++-- src/spectator/mocks/stubbable.cr | 3 +++ 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index 03d974f..8effacc 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -110,6 +110,10 @@ module Spectator stub 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. private def _spectator_stubbed_name : String {% if anno = @type.annotation(StubbedName) %} @@ -130,7 +134,7 @@ module Spectator private def _spectator_abstract_stub_fallback(call : MethodCall) 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)." end diff --git a/src/spectator/mocks/lazy_double.cr b/src/spectator/mocks/lazy_double.cr index 6ad7a27..405c1de 100644 --- a/src/spectator/mocks/lazy_double.cr +++ b/src/spectator/mocks/lazy_double.cr @@ -32,7 +32,7 @@ module Spectator end 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)." } raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") else diff --git a/src/spectator/mocks/mock.cr b/src/spectator/mocks/mock.cr index f4c2f16..97802e6 100644 --- a/src/spectator/mocks/mock.cr +++ b/src/spectator/mocks/mock.cr @@ -23,10 +23,14 @@ module Spectator @_spectator_stubs.unshift(stub) end - def _spectator_find_stub(call : ::Spectator::MethodCall) : ::Spectator::Stub? + private def _spectator_find_stub(call : ::Spectator::MethodCall) : ::Spectator::Stub? @_spectator_stubs.find &.===(call) 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. private def _spectator_stubbed_name : String \{% if anno = @type.annotation(::Spectator::StubbedName) %} diff --git a/src/spectator/mocks/null_double.cr b/src/spectator/mocks/null_double.cr index 8717e5e..452353c 100644 --- a/src/spectator/mocks/null_double.cr +++ b/src/spectator/mocks/null_double.cr @@ -24,7 +24,7 @@ module Spectator end 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)." } raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") else @@ -40,7 +40,7 @@ module Spectator # Default implementation that raises a `TypeCastError` since the return type isn't self. 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)." } raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") else diff --git a/src/spectator/mocks/stubbable.cr b/src/spectator/mocks/stubbable.cr index 6c2e103..44e371e 100644 --- a/src/spectator/mocks/stubbable.cr +++ b/src/spectator/mocks/stubbable.cr @@ -22,6 +22,9 @@ module Spectator # or nil if no stubs satisfy it. 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. abstract def _spectator_define_stub(stub : Stub) : Nil