DSL fully added back in

This commit is contained in:
Michael Miller 2021-01-30 16:43:13 -07:00
parent d43d309a47
commit 73985772b7
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 150 additions and 156 deletions

View file

@ -1,13 +1,4 @@
# require "./dsl/*" require "./dsl/*"
require "./dsl/builder"
require "./dsl/examples"
require "./dsl/expectations"
require "./dsl/groups"
require "./dsl/hooks"
require "./dsl/matchers"
require "./dsl/tags"
require "./dsl/top"
require "./dsl/values"
module Spectator module Spectator
# Namespace containing methods representing the spec domain specific language. # Namespace containing methods representing the spec domain specific language.

View file

@ -1,173 +1,175 @@
require "../mocks" require "../mocks"
module Spectator::DSL module Spectator::DSL
macro double(name = "Anonymous", **stubs, &block) module Mocks
{% if name.is_a?(StringLiteral) || name.is_a?(StringInterpolation) %} macro double(name = "Anonymous", **stubs, &block)
anonymous_double({{name}}, {{stubs.double_splat}}) {% if name.is_a?(StringLiteral) || name.is_a?(StringInterpolation) %}
{% else %} anonymous_double({{name}}, {{stubs.double_splat}})
{%
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 %} {% else %}
define_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}} {%
{% end %} safe_name = name.id.symbolize.gsub(/\W/, "_").id
{% end %} type_name = "Double#{safe_name}".id
end %}
macro create_double(type_name, name, **stubs) {% if block.is_a?(Nop) %}
{% type_name.resolve? || raise("Could not find a double labeled #{name}") %} create_double({{type_name}}, {{name}}, {{stubs.double_splat}})
{% else %}
{{type_name}}.new.tap do |%double| define_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
{% for name, value in stubs %} {% end %}
allow(%double).to receive({{name.id}}).and_return({{value}})
{% end %} {% end %}
end end
end
macro define_double(type_name, name, **stubs, &block) macro create_double(type_name, name, **stubs)
{% begin %} {% type_name.resolve? || raise("Could not find a double labeled #{name}") %}
{% if (name.is_a?(Path) || name.is_a?(Generic)) && (resolved = name.resolve?) %}
verify_double({{name}})
class {{type_name}} < ::Spectator::Mocks::VerifyingDouble(::{{resolved.id}})
{% else %}
class {{type_name}} < ::Spectator::Mocks::Double
def initialize(null = false)
super({{name.id.stringify}}, null)
end
{% end %}
def as_null_object {{type_name}}.new.tap do |%double|
{{type_name}}.new(true) {% for name, value in stubs %}
allow(%double).to receive({{name.id}}).and_return({{value}})
{% end %}
end end
# TODO: Do something with **stubs?
{{block.body}}
end end
{% end %}
end
def anonymous_double(name = "Anonymous", **stubs) macro define_double(type_name, name, **stubs, &block)
Mocks::AnonymousDouble.new(name, stubs) {% begin %}
end {% if (name.is_a?(Path) || name.is_a?(Generic)) && (resolved = name.resolve?) %}
verify_double({{name}})
class {{type_name}} < ::Spectator::Mocks::VerifyingDouble(::{{resolved.id}})
{% else %}
class {{type_name}} < ::Spectator::Mocks::Double
def initialize(null = false)
super({{name.id.stringify}}, null)
end
{% end %}
macro null_double(name, **stubs, &block) def as_null_object
{% if name.is_a?(StringLiteral) || name.is_a?(StringInterpolation) %} {{type_name}}.new(true)
anonymous_null_double({{name}}, {{stubs.double_splat}}) end
{% else %}
{%
safe_name = name.id.symbolize.gsub(/\W/, "_").id
type_name = "Double#{safe_name}".id
%}
{% if block.is_a?(Nop) %} # TODO: Do something with **stubs?
create_null_double({{type_name}}, {{name}}, {{stubs.double_splat}})
{% else %}
define_null_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
{% end %}
{% end %}
end
macro create_null_double(type_name, name, **stubs)
{% type_name.resolve? || raise("Could not find a double labeled #{name}") %}
{{type_name}}.new(true).tap do |%double|
{% for name, value in stubs %}
allow(%double).to receive({{name.id}}).and_return({{value}})
{% end %}
end
end
macro define_null_double(type_name, name, **stubs, &block)
class {{type_name}} < ::Spectator::Mocks::Double
def initialize(null = true)
super({{name.id.stringify}}, null)
end
def as_null_object
{{type_name}}.new(true)
end
# TODO: Do something with **stubs?
{{block.body}}
end
end
def anonymous_null_double(name = "Anonymous", **stubs)
AnonymousNullDouble.new(name, stubs)
end
macro mock(name, &block)
{% resolved = name.resolve
type = if resolved < Reference
:class
elsif resolved < Value
:struct
else
:module
end %}
{% begin %}
{{type.id}} ::{{resolved.id}}
include ::Spectator::Mocks::Stubs
{{block.body}} {{block.body}}
end end
{% end %} {% end %}
end end
macro verify_double(name, &block) def anonymous_double(name = "Anonymous", **stubs)
{% resolved = name.resolve Mocks::AnonymousDouble.new(name, stubs)
type = if resolved < Reference end
:class
elsif resolved < Value
:struct
else
:module
end %}
{% begin %}
{{type.id}} ::{{resolved.id}}
include ::Spectator::Mocks::Reflection
macro finished macro null_double(name, **stubs, &block)
_spectator_reflect {% if name.is_a?(StringLiteral) || name.is_a?(StringInterpolation) %}
end anonymous_null_double({{name}}, {{stubs.double_splat}})
{% else %}
{%
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
macro create_null_double(type_name, name, **stubs)
{% type_name.resolve? || raise("Could not find a double labeled #{name}") %}
{{type_name}}.new(true).tap do |%double|
{% for name, value in stubs %}
allow(%double).to receive({{name.id}}).and_return({{value}})
{% end %}
end end
{% end %} end
end
def allow(thing) macro define_null_double(type_name, name, **stubs, &block)
Mocks::Allow.new(thing) class {{type_name}} < ::Spectator::Mocks::Double
end def initialize(null = true)
super({{name.id.stringify}}, null)
end
def allow_any_instance_of(type : T.class) forall T def as_null_object
Mocks::AllowAnyInstance(T).new {{type_name}}.new(true)
end end
macro expect_any_instance_of(type, _source_file = __FILE__, _source_line = __LINE__) # TODO: Do something with **stubs?
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
::Spectator::Mocks::ExpectAnyInstance({{type}}).new(%source)
end
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__, &block) {{block.body}}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) end
{% if block.is_a?(Nop) %} end
::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %source)
{% else %}
::Spectator::Mocks::ProcMethodStub.create({{method_name.id.symbolize}}, %source) { {{block.body}} }
{% end %}
end
macro receive_messages(_source_file = __FILE__, _source_line = __LINE__, **stubs) def anonymous_null_double(name = "Anonymous", **stubs)
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) AnonymousNullDouble.new(name, stubs)
%stubs = [] of ::Spectator::Mocks::MethodStub end
{% for name, value in stubs %}
%stubs << ::Spectator::Mocks::ValueMethodStub.new({{name.id.symbolize}}, %source, {{value}}) macro mock(name, &block)
{% end %} {% resolved = name.resolve
%stubs type = if resolved < Reference
:class
elsif resolved < Value
:struct
else
:module
end %}
{% begin %}
{{type.id}} ::{{resolved.id}}
include ::Spectator::Mocks::Stubs
{{block.body}}
end
{% end %}
end
macro verify_double(name, &block)
{% resolved = name.resolve
type = if resolved < Reference
:class
elsif resolved < Value
:struct
else
:module
end %}
{% begin %}
{{type.id}} ::{{resolved.id}}
include ::Spectator::Mocks::Reflection
macro finished
_spectator_reflect
end
end
{% end %}
end
def allow(thing)
Mocks::Allow.new(thing)
end
def allow_any_instance_of(type : T.class) forall T
Mocks::AllowAnyInstance(T).new
end
macro expect_any_instance_of(type, _source_file = __FILE__, _source_line = __LINE__)
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
::Spectator::Mocks::ExpectAnyInstance({{type}}).new(%source)
end
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__, &block)
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
{% if block.is_a?(Nop) %}
::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %source)
{% else %}
::Spectator::Mocks::ProcMethodStub.create({{method_name.id.symbolize}}, %source) { {{block.body}} }
{% end %}
end
macro receive_messages(_source_file = __FILE__, _source_line = __LINE__, **stubs)
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
%stubs = [] of ::Spectator::Mocks::MethodStub
{% for name, value in stubs %}
%stubs << ::Spectator::Mocks::ValueMethodStub.new({{name.id.symbolize}}, %source, {{value}})
{% end %}
%stubs
end
end end
end end

View file

@ -13,6 +13,7 @@ class SpectatorTestContext < SpectatorContext
include ::Spectator::DSL::Groups include ::Spectator::DSL::Groups
include ::Spectator::DSL::Hooks include ::Spectator::DSL::Hooks
include ::Spectator::DSL::Matchers include ::Spectator::DSL::Matchers
include ::Spectator::DSL::Mocks
include ::Spectator::DSL::Values include ::Spectator::DSL::Values
@subject = ::Spectator::LazyWrapper.new @subject = ::Spectator::LazyWrapper.new