mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Initial code to support anonymous doubles
This commit is contained in:
parent
b0562981f7
commit
b9fe1b6a60
3 changed files with 58 additions and 16 deletions
|
@ -2,15 +2,19 @@ require "../mocks"
|
|||
|
||||
module Spectator::DSL
|
||||
macro double(name, **stubs, &block)
|
||||
{%
|
||||
safe_name = name.id.symbolize.gsub(/\W/, "_").id
|
||||
type_name = "Double#{safe_name}".id
|
||||
%}
|
||||
|
||||
{% if block.is_a?(Nop) %}
|
||||
create_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||
{% if name.is_a?(StringLiteral) %}
|
||||
anonymous_double({{name}}, {{stubs.double_splat}})
|
||||
{% else %}
|
||||
define_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
|
||||
{%
|
||||
safe_name = name.id.symbolize.gsub(/\W/, "_").id
|
||||
type_name = "Double#{safe_name}".id
|
||||
%}
|
||||
|
||||
{% if block.is_a?(Nop) %}
|
||||
create_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||
{% else %}
|
||||
define_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
|
||||
{% end %}
|
||||
{% end %}
|
||||
end
|
||||
|
||||
|
@ -40,16 +44,24 @@ module Spectator::DSL
|
|||
end
|
||||
end
|
||||
|
||||
macro null_double(name, **stubs, &block)
|
||||
{%
|
||||
safe_name = name.id.symbolize.gsub(/\W/, "_").id
|
||||
type_name = "Double#{safe_name}".id
|
||||
%}
|
||||
def anonymous_double(name : String, **stubs)
|
||||
Mocks::AnonymousDouble.new(name, stubs)
|
||||
end
|
||||
|
||||
{% if block.is_a?(Nop) %}
|
||||
create_null_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||
macro null_double(name, **stubs, &block)
|
||||
{% if name.is_a?(StringLiteral) %}
|
||||
anonymous_null_double({{name}}, {{stubs.double_splat}})
|
||||
{% else %}
|
||||
define_null_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
|
||||
{%
|
||||
safe_name = name.id.symbolize.gsub(/\W/, "_").id
|
||||
type_name = "Double#{safe_name}".id
|
||||
%}
|
||||
|
||||
{% if block.is_a?(Nop) %}
|
||||
create_null_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||
{% else %}
|
||||
define_null_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
|
||||
{% end %}
|
||||
{% end %}
|
||||
end
|
||||
|
||||
|
@ -79,6 +91,10 @@ module Spectator::DSL
|
|||
end
|
||||
end
|
||||
|
||||
def anonymous_null_double(name : Strings, **stubs)
|
||||
AnonymousNullDouble.new(name, stubs)
|
||||
end
|
||||
|
||||
macro mock(name, &block)
|
||||
{% resolved = name.resolve
|
||||
type = if resolved < Reference
|
||||
|
|
16
src/spectator/mocks/anonymous_double.cr
Normal file
16
src/spectator/mocks/anonymous_double.cr
Normal file
|
@ -0,0 +1,16 @@
|
|||
module Spectator::Mocks
|
||||
class AnonymousDouble(T)
|
||||
def initialize(@name : String, @values : T)
|
||||
end
|
||||
|
||||
def as_null_object
|
||||
AnonymousNullDouble.new(@values)
|
||||
end
|
||||
|
||||
macro method_missing(call)
|
||||
@values.fetch({{call.name.symbolize}}) do
|
||||
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
10
src/spectator/mocks/anonymous_null_double.cr
Normal file
10
src/spectator/mocks/anonymous_null_double.cr
Normal file
|
@ -0,0 +1,10 @@
|
|||
module Spectator::Mocks
|
||||
class AnonymousNullDouble(T)
|
||||
def initialize(@name : String, @values : T)
|
||||
end
|
||||
|
||||
macro method_missing(call)
|
||||
@values.fetch({{call.name.symbolize}}) { self }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue