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-02 19:58:56 -06:00
commit 0c7de3d0d1
2 changed files with 30 additions and 37 deletions

View file

@ -16,9 +16,15 @@ module Spectator
args = nil args = nil
body = nil body = nil
if definition.is_a?(Call) # stub foo { :bar } if definition.is_a?(Call) # stub foo { :bar }
named = false
name = definition.name.id name = definition.name.id
params = definition.args params = definition.args
args = params.map { |p| p.is_a?(TypeDeclaration) ? p.var : p.id } args = params.map do |p|
n = p.is_a?(TypeDeclaration) ? p.var : p.id
r = named ? "#{n}: #{n}".id : n
named = true if n.starts_with?('*')
r
end
body = definition.block.is_a?(Nop) ? block : definition.block body = definition.block.is_a?(Nop) ? block : definition.block
elsif definition.is_a?(TypeDeclaration) # stub foo : Symbol elsif definition.is_a?(TypeDeclaration) # stub foo : Symbol
name = definition.var name = definition.var
@ -30,51 +36,34 @@ module Spectator
end end
%} %}
{% if name.ends_with?('=') && name.id != "[]=" %} def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
def {{name}}(arg) %call = ::Spectator::GenericMethodCall.create({{name.symbolize}}{% unless args.empty? %}, {{args.splat}}{% end %})
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, {arg}, NamedTuple.new) @spectator_stub_calls << %call
@spectator_stub_calls << call if (%stub = @spectator_stubs.find(&.callable?(%call)))
stub = @spectator_stubs.find(&.callable?(call)) %stub.as(::Spectator::GenericMethodStub(typeof(%method({{args.splat}})))).call(%call)
if stub else
stub.as(::Spectator::GenericMethodStub(typeof(%method(arg)))).call(call) %method({{args.splat}})
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 end
end
{% if name != "[]=" %} def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
def {{name}}(*args, **options){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} %call = ::Spectator::GenericMethodCall.create({{name.symbolize}}{% unless args.empty? %}, {{args.splat}}{% end %})
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, args, options) @spectator_stub_calls << %call
@spectator_stub_calls << call if (%stub = @spectator_stubs.find(&.callable?(%call)))
stub = @spectator_stubs.find(&.callable?(call)) %stub.as(::Spectator::GenericMethodStub(typeof(%method({{args.splat}}) { |*%yield_args| yield *%yield_args }))).call(%call)
if stub else
stub.as(::Spectator::GenericMethodStub(typeof(%method(*args, **options) { |*yield_args| yield *yield_args }))).call(call) %method({{args.splat}}) do |*%yield_args|
else yield *%yield_args
%method(*args, **options) do |*yield_args|
yield *yield_args
end
end
end end
{% end %} end
{% end %} end
def %method({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} def %method({{params.splat}}){% if 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 %}
raise "Stubbed method called without being allowed" raise "Stubbed method called without being allowed"
# This code shouldn't be reached, but makes the compiler happy to have a matching type. # This code shouldn't be reached, but makes the compiler happy to have a matching return type.
{% if definition.is_a?(TypeDeclaration) %} {% if definition.is_a?(TypeDeclaration) %}
%x = uninitialized {{definition.type}} %x = uninitialized {{definition.type}}
{% else %} {% else %}

View file

@ -10,6 +10,10 @@ module Spectator
super(name) super(name)
end end
def self.create(name : Symbol, *args, **options)
GenericMethodCall.new(name, args, options)
end
def to_s(io) def to_s(io)
io << name io << name
return if @args.empty? && @options.empty? return if @args.empty? && @options.empty?