Allow receive block syntax

This commit is contained in:
Michael Miller 2019-11-19 17:28:00 -07:00
parent b062472d98
commit 27acf7bced
2 changed files with 10 additions and 2 deletions

View file

@ -126,9 +126,13 @@ module Spectator::DSL
::Spectator::Mocks::ExpectAnyInstance({{type}}).new(%source)
end
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__)
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__, &block)
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %source)
{% if block.is_a?(Nop) %}
::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %source)
{% else %}
::Spectator::Mocks::ProcMethodStub.create({{method_name.id.symbolize}}, %source) { {{block.body}} }
{% end %}
end
macro receive_messages(_source_file = __FILE__, _source_line = __LINE__, **stubs)

View file

@ -7,6 +7,10 @@ module Spectator::Mocks
super(name, source, args)
end
def self.create(name, source, args = nil, &block : -> T) forall T
ProcMethodStub.new(name, source, block, args)
end
def call(_args : GenericArguments(T2, NT2), rt : RT.class) forall T2, NT2, RT
@proc.call
end