Add logging

This commit is contained in:
Michael Miller 2022-03-12 12:59:44 -07:00
parent 59f966d0e0
commit c1a14d1ad2
No known key found for this signature in database
GPG Key ID: 32B47AE8F388A1FF
1 changed files with 9 additions and 3 deletions

View File

@ -20,6 +20,8 @@ module Spectator
abstract class Double abstract class Double
include Stubable include Stubable
Log = Spectator::Log.for(self)
macro define(type_name, name = nil, **value_methods, &block) macro define(type_name, name = nil, **value_methods, &block)
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %} {% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
class {{type_name.id}} < {{@type.name}} class {{type_name.id}} < {{@type.name}}
@ -32,11 +34,14 @@ module Spectator
end end
end end
# Stores responses to messages (method calls). def initialize(@stubs : Array(Stub) = [] of Stub)
@stubs = [] of Stub end
private def _spectator_find_stub(call) : Stub? private def _spectator_find_stub(call) : Stub?
@stubs.find &.===(call) Log.debug { "Finding stub for #{call}" }
stub = @stubs.find &.===(call)
Log.debug { stub ? "Found stub #{stub} for #{call}" : "Did not find stub for #{call}" }
stub
end end
# Utility returning the double's name as a string. # Utility returning the double's name as a string.
@ -56,6 +61,7 @@ module Spectator
# Handle all methods but only respond to configured messages. # Handle all methods but only respond to configured messages.
# Raises an `UnexpectedMessage` error for non-configures messages. # Raises an `UnexpectedMessage` error for non-configures messages.
macro method_missing(call) macro method_missing(call)
Log.debug { "Got undefined method " + {{call.stringify}} }
args = ::Spectator::Arguments.capture({{call.args.splat(", ")}}{% if call.named_args %}{{call.named_args.splat}}{% end %}) args = ::Spectator::Arguments.capture({{call.args.splat(", ")}}{% if call.named_args %}{{call.named_args.splat}}{% end %})
call = ::Spectator::MethodCall.new({{call.name.symbolize}}, args) call = ::Spectator::MethodCall.new({{call.name.symbolize}}, args)
raise ::Spectator::UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message :{{call.name}} with #{args}") raise ::Spectator::UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message :{{call.name}} with #{args}")