mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Display better error when a double isn't found/defined
This commit is contained in:
parent
87a60cf92a
commit
cb466b4ff5
1 changed files with 34 additions and 18 deletions
|
@ -2,31 +2,39 @@ 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
|
||||||
|
type_name = "Double#{safe_name}".id
|
||||||
|
%}
|
||||||
|
|
||||||
{% if block.is_a?(Nop) %}
|
{% if block.is_a?(Nop) %}
|
||||||
create_double({{name}}, {{stubs.double_splat}})
|
create_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||||
{% else %}
|
{% else %}
|
||||||
define_double({{name}}, {{stubs.double_splat}}) {{block}}
|
define_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
macro create_double(name, **stubs)
|
macro create_double(type_name, name, **stubs)
|
||||||
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
{%
|
||||||
Double{{safe_name}}.new.tap do |%double|
|
type = type_name.resolve?
|
||||||
|
raise "Could not find a double labeled #{name}" unless type
|
||||||
|
%}
|
||||||
|
|
||||||
|
{{type_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
|
||||||
end
|
end
|
||||||
|
|
||||||
macro define_double(name, **stubs, &block)
|
macro define_double(type_name, name, **stubs, &block)
|
||||||
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
class {{type_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{{safe_name}}.new(true)
|
{{type_name}}.new(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Do something with **stubs?
|
# TODO: Do something with **stubs?
|
||||||
|
@ -36,31 +44,39 @@ module Spectator::DSL
|
||||||
end
|
end
|
||||||
|
|
||||||
macro null_double(name, **stubs, &block)
|
macro null_double(name, **stubs, &block)
|
||||||
|
{%
|
||||||
|
safe_name = name.id.symbolize.gsub(/\W/, "_").id
|
||||||
|
type_name = "Double#{safe_name}".id
|
||||||
|
%}
|
||||||
|
|
||||||
{% if block.is_a?(Nop) %}
|
{% if block.is_a?(Nop) %}
|
||||||
create_null_double({{name}}, {{stubs.double_splat}})
|
create_null_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||||
{% else %}
|
{% else %}
|
||||||
define_null_double({{name}}, {{stubs.double_splat}}) {{block}}
|
define_null_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
macro create_null_double(name, **stubs)
|
macro create_null_double(type_name, name, **stubs)
|
||||||
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
{%
|
||||||
Double{{safe_name}}.new(true).tap do |%double|
|
type = type_name.resolve?
|
||||||
|
raise "Could not find a double labeled #{name}" unless type
|
||||||
|
%}
|
||||||
|
|
||||||
|
{{type_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
|
||||||
end
|
end
|
||||||
|
|
||||||
macro define_null_double(name, **stubs, &block)
|
macro define_null_double(type_name, name, **stubs, &block)
|
||||||
{% safe_name = name.id.symbolize.gsub(/\W/, "_").id %}
|
class {{type_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{{safe_name}}.new(true)
|
{{type_name}}.new(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Do something with **stubs?
|
# TODO: Do something with **stubs?
|
||||||
|
|
Loading…
Reference in a new issue