diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index feb2db2..b0e1cbf 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -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. diff --git a/src/spectator/mocks/stubable.cr b/src/spectator/mocks/stubable.cr index 0a28ddd..5a3705e 100644 --- a/src/spectator/mocks/stubable.cr +++ b/src/spectator/mocks/stubable.cr @@ -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