mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Split out double macros
This commit is contained in:
parent
f50e71606e
commit
87a60cf92a
1 changed files with 56 additions and 34 deletions
|
@ -2,49 +2,71 @@ require "../mocks"
|
||||||
|
|
||||||
module Spectator::DSL
|
module Spectator::DSL
|
||||||
macro double(name, **stubs, &block)
|
macro double(name, **stubs, &block)
|
||||||
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
|
||||||
{% if block.is_a?(Nop) %}
|
{% if block.is_a?(Nop) %}
|
||||||
|
create_double({{name}}, {{stubs.double_splat}})
|
||||||
|
{% else %}
|
||||||
|
define_double({{name}}, {{stubs.double_splat}}) {{block}}
|
||||||
|
{% end %}
|
||||||
|
end
|
||||||
|
|
||||||
|
macro create_double(name, **stubs)
|
||||||
|
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
||||||
Double{{safe_name}}.new.tap do |%double|
|
Double{{safe_name}}.new.tap do |%double|
|
||||||
{% for name, value in stubs %}
|
{% for name, value in stubs %}
|
||||||
allow(%double).to receive({{name.id}}).and_return({{value}})
|
allow(%double).to receive({{name.id}}).and_return({{value}})
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
{% else %}
|
end
|
||||||
|
|
||||||
|
macro define_double(name, **stubs, &block)
|
||||||
|
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
||||||
class Double{{safe_name}} < ::Spectator::Mocks::Double
|
class Double{{safe_name}} < ::Spectator::Mocks::Double
|
||||||
def initialize(null = false)
|
def initialize(null = false)
|
||||||
super({{name.id.stringify}}, null)
|
super({{name.id.stringify}}, null)
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_null_object
|
def as_null_object
|
||||||
Double{{name.id}}.new(true)
|
Double{{safe_name}}.new(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Do something with **stubs?
|
||||||
|
|
||||||
{{block.body}}
|
{{block.body}}
|
||||||
end
|
end
|
||||||
{% end %}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
macro null_double(name, **stubs, &block)
|
macro null_double(name, **stubs, &block)
|
||||||
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
|
||||||
{% if block.is_a?(Nop) %}
|
{% if block.is_a?(Nop) %}
|
||||||
|
create_null_double({{name}}, {{stubs.double_splat}})
|
||||||
|
{% else %}
|
||||||
|
define_null_double({{name}}, {{stubs.double_splat}}) {{block}}
|
||||||
|
{% end %}
|
||||||
|
end
|
||||||
|
|
||||||
|
macro create_null_double(name, **stubs)
|
||||||
|
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
||||||
Double{{safe_name}}.new(true).tap do |%double|
|
Double{{safe_name}}.new(true).tap do |%double|
|
||||||
{% for name, value in stubs %}
|
{% for name, value in stubs %}
|
||||||
allow(%double).to receive({{name.id}}).and_return({{value}})
|
allow(%double).to receive({{name.id}}).and_return({{value}})
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
{% else %}
|
end
|
||||||
|
|
||||||
|
macro define_null_double(name, **stubs, &block)
|
||||||
|
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
||||||
class Double{{safe_name}} < ::Spectator::Mocks::Double
|
class Double{{safe_name}} < ::Spectator::Mocks::Double
|
||||||
def initialize(null = true)
|
def initialize(null = true)
|
||||||
super({{name.id.stringify}}, null)
|
super({{name.id.stringify}}, null)
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_null_object
|
def as_null_object
|
||||||
Double{{name.id}}.new(true)
|
Double{{safe_name}}.new(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Do something with **stubs?
|
||||||
|
|
||||||
{{block.body}}
|
{{block.body}}
|
||||||
end
|
end
|
||||||
{% end %}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
macro mock(name, &block)
|
macro mock(name, &block)
|
||||||
|
|
Loading…
Reference in a new issue