Merge branch 'dev/mocks-and-doubles' of gitlab.com:arctic-fox/spectator into dev/mocks-and-doubles

This commit is contained in:
Michael Miller 2019-11-03 10:07:11 -07:00
commit cab9366fe8
2 changed files with 106 additions and 0 deletions

View file

@ -17,6 +17,30 @@ module Spectator::DSL
{% end %} {% end %}
end end
macro mock(name, &block)
{% if block.is_a?(Nop) %}
{{name}}.new.tap do |%inst|
%inst.spectator_test = self
end
{% else %}
{% resolved = name.resolve
type = if resolved < Reference
:class
elsif resolved < Value
:struct
else
:module
end
%}
{{type.id}} ::{{resolved.id}}
include ::Spectator::Mocks::Stubs
{{block.body}}
end
{% end %}
{% debug %}
end
def allow(double : ::Spectator::Mocks::Double) def allow(double : ::Spectator::Mocks::Double)
Mocks::OpenMock.new(double) Mocks::OpenMock.new(double)
end end

View file

@ -0,0 +1,82 @@
module Spectator
module Stubs
private macro stub(definition, &block)
{%
name = nil
params = nil
args = nil
body = nil
if definition.is_a?(Call) # stub foo { :bar }
name = definition.name.id
params = definition.args
args = params.map { |p| p.is_a?(TypeDeclaration) ? p.var : p.id }
body = definition.block.is_a?(Nop) ? block : definition.block
elsif definition.is_a?(TypeDeclaration) # stub foo : Symbol
name = definition.var
params = [] of MacroId
args = [] of MacroId
body = block
else
raise "Unrecognized stub format"
end
%}
def {{name}}{% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%method
end
{% if name.ends_with?('=') && name.id != "[]=" %}
def {{name}}(arg)
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, {arg}, NamedTuple.new)
@spectator_stub_calls << call
stub = @spectator_stubs.find(&.callable?(call))
if stub
stub.as(::Spectator::GenericMethodStub(typeof(%method(arg)))).call(call)
else
%method(arg)
end
end
{% else %}
def {{name}}(*args, **options){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, args, options)
@spectator_stub_calls << call
stub = @spectator_stubs.find(&.callable?(call))
if stub
stub.as(::Spectator::GenericMethodStub(typeof(%method(*args, **options)))).call(call)
else
%method(*args, **options)
end
end
{% if name != "[]=" %}
def {{name}}(*args, **options){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, args, options)
@spectator_stub_calls << call
stub = @spectator_stubs.find(&.callable?(call))
if stub
stub.as(::Spectator::GenericMethodStub(typeof(%method(*args, **options) { |*yield_args| yield *yield_args }))).call(call)
else
%method(*args, **options) do |*yield_args|
yield *yield_args
end
end
end
{% end %}
{% end %}
def %method({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
{% if body && !body.is_a?(Nop) %}
{{body.body}}
{% else %}
raise "Stubbed method called without being allowed"
# This code shouldn't be reached, but makes the compiler happy to have a matching type.
{% if definition.is_a?(TypeDeclaration) %}
%x = uninitialized {{definition.type}}
{% else %}
nil
{% end %}
{% end %}
end
{% debug %}
end
end
end