Handle deferred stub with type

This commit is contained in:
Michael Miller 2019-10-06 10:24:51 -06:00
parent 49764e5873
commit 1bbe5067b6

View file

@ -1,18 +1,30 @@
module Spectator
abstract class Double
macro stub(definition, &block)
delegate {{definition.name.id}}, to: @internal
{%
name = nil
args = nil
body = nil
if definition.is_a?(Call) # stub foo { :bar }
name = definition.name.id
args = definition.args
body = definition.block.is_a?(Nop) ? block : definition.block
elsif definition.is_a?(TypeDeclaration) # stub foo : Symbol
name = definition.var
args = [] of MacroId
body = block
else
raise "Unrecognized stub format"
end
%}
delegate {{name}}, to: @internal
private class Internal
def {{definition.name.id}}({{definition.args.splat}})
{% if definition.block.is_a?(Nop) %}
{% if block.is_a?(Nop) %}
raise "Stubbed method called without being allowed"
{% else %}
{{block.body}}
{% end %}
def {{name}}({{args.splat}})
{% if body && !body.is_a?(Nop) %}
{{body.body}}
{% else %}
{{definition.block.body}}
raise "Stubbed method called without being allowed"
{% end %}
end
end