Move method masking macro to Stubable

Rename to stub_all.
This commit is contained in:
Michael Miller 2022-03-12 11:56:54 -07:00
parent 85cc28c499
commit 56f1c77a6c
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
2 changed files with 15 additions and 15 deletions

View file

@ -43,23 +43,9 @@ module Spectator
{% end %}
end
# Redefines all methods on a type to conditionally respond to messages.
# Methods will raise `UnexpectedMessage` if they're called when they shouldn't be.
# Otherwise, they'll return the configured response.
private macro _spectator_mask_methods(type_name)
{% type = type_name.resolve %}
{% if type.superclass %}
_spectator_mask_methods({{type.superclass}})
{% end %}
{% for meth in type.methods.reject { |m| DSL::RESERVED_KEYWORDS.includes?(m.name.symbolize) } %}
abstract_stub {{meth}}
{% end %}
end
# "Hide" existing methods and methods from ancestors by overriding them.
macro finished
_spectator_mask_methods({{@type.name(generic_args: false)}})
stub_all {{@type.name(generic_args: false)}}
end
# Handle all methods but only respond to configured messages.

View file

@ -76,5 +76,19 @@ module Spectator
{{method}}
stub {{method}}
end
# Redefines all methods on a type to conditionally respond to messages.
# Methods will raise `UnexpectedMessage` if they're called when they shouldn't be.
# Otherwise, they'll return the configured response.
private macro stub_all(type_name, *, with style = :abstract_stub)
{% type = type_name.resolve %}
{% if type.superclass %}
stub_all({{type.superclass}}, with: {{style}})
{% end %}
{% for meth in type.methods.reject { |m| DSL::RESERVED_KEYWORDS.includes?(m.name.symbolize) } %}
{{style.id}} {{meth}}
{% end %}
end
end
end