DRY up DSL

This commit is contained in:
Michael Miller 2018-09-23 16:07:03 -06:00
parent 0f323666a0
commit 70e37eeccf

View file

@ -16,7 +16,7 @@ module Spectator
include {{@type.id}} include {{@type.id}}
{% if what.is_a?(Path) || what.is_a?(Generic) %} {% if what.is_a?(Path) || what.is_a?(Generic) %}
_described_class {{what}} _spectator_described_class {{what}}
{% end %} {% end %}
::Spectator::DSL::Builder.start_group( ::Spectator::DSL::Builder.start_group(
@ -37,14 +37,10 @@ module Spectator
{{collection}} {{collection}}
end end
def %first
%collection.first
end
@%wrapper : ::Spectator::ValueWrapper @%wrapper : ::Spectator::ValueWrapper
def {{block.args.empty? ? "value".id : block.args.first}} def {{block.args.empty? ? "value".id : block.args.first}}
@%wrapper.as(::Spectator::TypedValueWrapper(typeof(%first))).value @%wrapper.as(::Spectator::TypedValueWrapper(typeof(%collection.first))).value
end end
def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper))
@ -52,9 +48,7 @@ module Spectator
@%wrapper = locals[:%group] @%wrapper = locals[:%group]
end end
_given_collection Collection%collection, %to_a do _spectator_given_collection Collection%collection, %to_a, %collection
{{collection}}
end
%to_a = Collection%collection.new.%to_a %to_a = Collection%collection.new.%to_a
::Spectator::DSL::Builder.start_given_group( ::Spectator::DSL::Builder.start_given_group(
@ -69,28 +63,6 @@ module Spectator
end end
end end
macro _given_collection(class_name, to_a_method_name, &block)
class {{class_name.id}}
include {{@type.id}}
def %collection
{{block.body}}
end
def %first
%collection.first
end
def {{to_a_method_name.id}}
Array(::Spectator::ValueWrapper).new.tap do |%array|
%collection.each do |%item|
%array << ::Spectator::TypedValueWrapper(typeof(%item)).new(%item)
end
end
end
end
end
macro subject(&block) macro subject(&block)
let(:subject) {{block}} let(:subject) {{block}}
end end
@ -142,61 +114,21 @@ module Spectator
end end
macro it(description, &block) macro it(description, &block)
class Wrapper%example _spectator_example_wrapper(Wrapper%example, %run) {{block}}
include ::Spectator::DSL::ExampleDSL
include {{@type.id}}
def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper))
super
end
def %run
{{block.body}}
end
end
class Example%example < ::Spectator::RunnableExample
def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::ValueWrapper))
super
@instance = Wrapper%example.new(locals)
end
_spectator_example(Example%example, Wrapper%example, ::Spectator::RunnableExample, {{description}}) do
protected def run_instance protected def run_instance
@instance.%run @instance.%run
end end
def description
{{description.is_a?(StringLiteral) ? description : description.stringify}}
end
end end
::Spectator::DSL::Builder.add_example(Example%example) ::Spectator::DSL::Builder.add_example(Example%example)
end end
macro pending(description, &block) macro pending(description, &block)
class Wrapper%example _spectator_example_wrapper(Wrapper%example, %run) {{block}}
include ::Spectator::DSL::ExampleDSL
include {{@type.id}}
def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) _spectator_example(Example%example, Wrapper%example, ::Spectator::PendingExample, {{description}})
super
end
def %run
{{block.body}}
end
end
class Example%example < ::Spectator::PendingExample
def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::ValueWrapper))
super
@instance = Wrapper%example.new(locals)
end
def description
{{description.is_a?(StringLiteral) ? description : description.stringify}}
end
end
::Spectator::DSL::Builder.add_example(Example%example) ::Spectator::DSL::Builder.add_example(Example%example)
end end
@ -205,7 +137,7 @@ module Spectator
raise NotImplementedError.new("Spectator::DSL#it_behaves_like") raise NotImplementedError.new("Spectator::DSL#it_behaves_like")
end end
macro _described_class(what) macro _spectator_described_class(what)
def described_class def described_class
{{what}}.tap do |thing| {{what}}.tap do |thing|
raise "#{thing} must be a type name to use #described_class or #subject,\ raise "#{thing} must be a type name to use #described_class or #subject,\
@ -213,14 +145,60 @@ module Spectator
end end
end end
_implicit_subject _spectator_implicit_subject
end end
macro _implicit_subject macro _spectator_implicit_subject
def subject def subject
described_class.new described_class.new
end end
end end
macro _spectator_given_collection(class_name, to_a_method_name, collection_method_name)
class {{class_name.id}}
include {{@type.id}}
def {{to_a_method_name.id}}
Array(::Spectator::ValueWrapper).new.tap do |array|
{{collection_method_name.id}}.each do |item|
array << ::Spectator::TypedValueWrapper(typeof(item)).new(item)
end
end
end
end
end
macro _spectator_example_wrapper(class_name, run_method_name, &block)
class {{class_name.id}}
include ::Spectator::DSL::ExampleDSL
include {{@type.id}}
def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper))
super
end
def {{run_method_name.id}}
{{block.body}}
end
end
end
macro _spectator_example(example_class_name, wrapper_class_name, base_class, description, &block)
class {{example_class_name.id}} < {{base_class.id}}
def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::ValueWrapper))
super
@instance = {{wrapper_class_name.id}}.new(locals)
end
{% if block.is_a?(Block) %}
{{block.body}}
{% end %}
def description
{{description.is_a?(StringLiteral) ? description : description.stringify}}
end
end
end
end end
end end
end end