Add ability to define default stub for mocks

This commit is contained in:
Michael Miller 2019-11-10 12:49:36 -07:00
parent eb1a99b3ab
commit 4fea5ea435

View file

@ -1,6 +1,6 @@
module Spectator::Mocks
module Stubs
private macro stub(definition, &block)
private macro stub(definition, _file = __FILE__, _line = __LINE__, &block)
{%
name = nil
params = nil
@ -33,6 +33,15 @@ module Spectator::Mocks
end.id
%}
{% if body && !body.is_a?(Nop) %}
%source = ::Spectator::Source.new({{_file}}, {{_line}})
%proc = ->{
{{body.body}}
}
%ds = ::Spectator::Mocks::ProcMethodStub.new({{name.symbolize}}, %source, %proc)
::Spectator::SpecBuilder.add_default_stub({{@type.name}}, %ds)
{% end %}
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
if (%harness = ::Spectator::Harness.current?)
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})