Match RSpec behavior

This commit is contained in:
Michael Miller 2022-03-30 20:28:36 -06:00
parent a8e55e32d2
commit 613c969fbb
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
5 changed files with 59 additions and 56 deletions

View file

@ -110,13 +110,8 @@ module Spectator
end
private def _spectator_stub_fallback(call : MethodCall, &)
if @stubs.any? { |stub| stub.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
Log.trace { "Fallback for #{call} - call original" }
yield
end
Log.trace { "Fallback for #{call} - call original" }
yield
end
private def _spectator_stub_fallback(call : MethodCall, _type, &)
@ -124,6 +119,12 @@ module Spectator
end
private def _spectator_abstract_stub_fallback(call : MethodCall)
Log.info do
break unless @stubs.any? { |stub| stub.method == call.method }
"Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)."
end
raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}")
end

View file

@ -31,6 +31,16 @@ module Spectator
"#<LazyDouble #{@name || "Anonymous"}>"
end
private def _spectator_stub_fallback(call : MethodCall, &)
if @stubs.any? { |stub| stub.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
Log.trace { "Fallback for #{call} - call original" }
yield
end
end
# Handles all messages.
macro method_missing(call)
Log.trace { "Got undefined method `{{call.name}}({{*call.args}}{% if call.named_args %}{% unless call.args.empty? %}, {% end %}{{*call.named_args}}{% end %}){% if call.block %} { ... }{% end %}`" }

View file

@ -23,16 +23,6 @@ module Spectator
{% end %}
end
private def _spectator_stub_fallback(call : MethodCall, &)
if @stubs.any? { |stub| stub.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
Log.trace { "Fallback for #{call} - return original" }
yield
end
end
private def _spectator_abstract_stub_fallback(call : MethodCall)
if @stubs.any? { |stub| stub.method == call.method }
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }
@ -43,10 +33,12 @@ module Spectator
end
end
# Specialization that matches when the return type matches self.
private def _spectator_abstract_stub_fallback(call : MethodCall, _type : self)
_spectator_abstract_stub_fallback(call)
end
# 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 }
Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." }