Obscure and disallow double name method

This commit is contained in:
Michael Miller 2022-03-03 22:34:57 -07:00
parent 5644d54470
commit 9594604770
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
2 changed files with 14 additions and 13 deletions

View file

@ -9,6 +9,6 @@ module Spectator
module DSL
# Keywords that cannot be used in specs using the DSL.
# These are either problematic or reserved for internal use.
RESERVED_KEYWORDS = %i[initialize]
RESERVED_KEYWORDS = %i[initialize _spectator_double_name]
end
end

View file

@ -5,6 +5,15 @@ module Spectator
def initialize(@name : String? = nil, **@messages : **Messages)
end
# Utility returning the double's name as a string.
private def _spectator_double_name : String
if name = @name
"\"#{name}\""
else
"Anonymous"
end
end
# TODO: Define macro to redefine a type's method.
# TODO: Better error for type mismatch
macro finished
@ -27,7 +36,7 @@ module Spectator
@messages[{{meth.name.symbolize}}]
{% end %}
\{% else %}
raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
raise UnexpectedMessage.new("Double<#{_spectator_double_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
\{% end %}
end
{% end %}
@ -52,7 +61,7 @@ module Spectator
@messages[{{meth.name.symbolize}}]
{% end %}
\{% else %}
raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
raise UnexpectedMessage.new("Double<#{_spectator_double_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
\{% end %}
end
{% end %}
@ -77,26 +86,18 @@ module Spectator
@messages[{{meth.name.symbolize}}]
{% end %}
\{% else %}
raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
raise UnexpectedMessage.new("Double<#{_spectator_double_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
\{% end %}
end
{% end %}
{% end %}
private def _name
if name = @name
"\"#{name}\""
else
"Anonymous"
end
end
end
macro method_missing(call)
\{% if Messages.keys.includes?({{call.name.symbolize}}.id) %}
@messages[{{call.name.symbolize}}]
\{% else %}
raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{call.name}} with (<TODO: ARGS>).")
raise UnexpectedMessage.new("Double<#{_spectator_double_name}> received unexpected message :{{call.name}} with (<TODO: ARGS>).")
\{% end %}
end
end