Allow manual specification of return type

This commit is contained in:
Michael Miller 2020-11-07 10:56:33 -07:00
parent 2cdee0716f
commit 8966c0976b
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 11 additions and 9 deletions

View file

@ -7,7 +7,7 @@ module Spectator::Mocks
def initialize(@spectator_double_name : String, @null = false) def initialize(@spectator_double_name : String, @null = false)
end end
private macro stub(definition, *types, &block) private macro stub(definition, *types, return_type = :undefined, &block)
{% {%
name = nil name = nil
params = nil params = nil
@ -64,7 +64,7 @@ module Spectator::Mocks
end end
%} %}
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} def {{name}}({{params.splat}}){% if return_type != :undefined %} : {{return_type.id}}{% elsif definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args) %call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args)
::Spectator::Harness.current.mocks.record_call(self, %call) ::Spectator::Harness.current.mocks.record_call(self, %call)
@ -75,7 +75,7 @@ module Spectator::Mocks
end end
end end
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} def {{name}}({{params.splat}}){% if return_type != :undefined %} : {{return_type.id}}{% elsif definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args) %call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args)
::Spectator::Harness.current.mocks.record_call(self, %call) ::Spectator::Harness.current.mocks.record_call(self, %call)
@ -88,7 +88,7 @@ module Spectator::Mocks
end end
end end
def %method({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} def %method({{params.splat}}){% if return_type != :undefined %} : {{return_type.id}}{% elsif definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
{% if body && !body.is_a?(Nop) %} {% if body && !body.is_a?(Nop) %}
{{body.body}} {{body.body}}
{% else %} {% else %}
@ -99,7 +99,9 @@ module Spectator::Mocks
end end
# This code shouldn't be reached, but makes the compiler happy to have a matching return type. # This code shouldn't be reached, but makes the compiler happy to have a matching return type.
{% if definition.is_a?(TypeDeclaration) %} {% if return_type != :undefined %}
%x = uninitialized {{return_type}}
{% elsif definition.is_a?(TypeDeclaration) %}
%x = uninitialized {{definition.type}} %x = uninitialized {{definition.type}}
{% else %} {% else %}
nil nil

View file

@ -1,6 +1,6 @@
module Spectator::Mocks module Spectator::Mocks
module Stubs module Stubs
private macro stub(definition, *types, _file = __FILE__, _line = __LINE__, &block) private macro stub(definition, *types, _file = __FILE__, _line = __LINE__, return_type = :undefined, &block)
{% {%
receiver = nil receiver = nil
name = nil name = nil
@ -65,12 +65,12 @@ module Spectator::Mocks
%} %}
{% if body && !body.is_a?(Nop) %} {% if body && !body.is_a?(Nop) %}
def {{receiver}}%method({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} def {{receiver}}%method({{params.splat}}){% if return_type != :undefined %} : {{return_type.id}}{% elsif definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
{{body.body}} {{body.body}}
end end
{% end %} {% end %}
def {{receiver}}{{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} def {{receiver}}{{name}}({{params.splat}}){% if return_type != :undefined %} : {{return_type.id}}{% elsif definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
if (%harness = ::Spectator::Harness.current?) if (%harness = ::Spectator::Harness.current?)
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args) %call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args)
@ -89,7 +89,7 @@ module Spectator::Mocks
end end
end end
def {{receiver}}{{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} def {{receiver}}{{name}}({{params.splat}}){% if return_type != :undefined %} : {{return_type.id}}{% elsif definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
if (%harness = ::Spectator::Harness.current?) if (%harness = ::Spectator::Harness.current?)
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args) %call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args)