New stub syntax to support operator methods

Works like:
  stub :[], index : Int32 { 42 }

Addresses https://github.com/icy-arctic-fox/spectator/issues/14
This commit is contained in:
Michael Miller 2020-10-01 18:23:28 -06:00
parent 93fa6f6e72
commit 9ac6121201
No known key found for this signature in database
GPG Key ID: F9A0C5C65B162436
2 changed files with 32 additions and 2 deletions

View File

@ -7,7 +7,7 @@ module Spectator::Mocks
def initialize(@spectator_double_name : String, @null = false)
end
private macro stub(definition, &block)
private macro stub(definition, *types, &block)
{%
name = nil
params = nil
@ -44,6 +44,21 @@ module Spectator::Mocks
params = [] of MacroId
args = [] of MacroId
body = block
elsif definition.is_a?(SymbolLiteral) # stub :foo, arg : Int32
name = definition.id
named = false
params = types
if params.last.is_a?(Call)
body = params.last.block
params[-1] = params.last.name
end
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 = block unless body
else
raise "Unrecognized stub format"
end

View File

@ -1,6 +1,6 @@
module Spectator::Mocks
module Stubs
private macro stub(definition, _file = __FILE__, _line = __LINE__, &block)
private macro stub(definition, *types, _file = __FILE__, _line = __LINE__, &block)
{%
receiver = nil
name = nil
@ -30,6 +30,21 @@ module Spectator::Mocks
params = [] of MacroId
args = [] of MacroId
body = block
elsif definition.is_a?(SymbolLiteral) # stub :foo, arg : Int32
name = definition.id
named = false
params = types
if params.last.is_a?(Call)
body = params.last.block
params[-1] = params.last.name
end
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 = block unless body
else
raise "Unrecognized stub format"
end