Some initial work to cleanup the DSL builder

This commit is contained in:
Michael Miller 2018-09-22 15:15:29 -06:00
parent ff382ba372
commit a1b5533504
4 changed files with 119 additions and 22 deletions

View file

@ -5,10 +5,8 @@ module Spectator
VERSION = "0.1.0" VERSION = "0.1.0"
macro describe(what, source_file = __FILE__, source_line = __LINE__, &block) macro describe(what, source_file = __FILE__, source_line = __LINE__, &block)
module Spectator module SpectatorExamples
module Examples ::Spectator::DSL::StructureDSL.describe({{what}}) {{block}}
DSL::StructureDSL.describe({{what}}) {{block}}
end
end end
end end

View file

@ -0,0 +1,59 @@
module Spectator
module DSL
module Builder
extend self
@group_stack = [ExampleGroupBuilder.new]
private def current_group
@group_stack.last
end
private def push_group(group : ExampleGroupBuilder)
current_group.add_child(group)
@group_stack.push(group)
end
def start_group(what : String) : Nil
push_group(ExampleGroupBuilder.new(what))
end
def start_given_group(what : String, values : Array(ValueWrapper)) : Nil
push_group(GivenExampleGroupBuilder.new(what, values))
end
def end_group : Nil
@group_stack.pop
end
def add_example(factory : ExampleFactory) : Nil
current_group.add_child(factory)
end
def add_before_all_hook(&block : ->) : Nil
current_group.add_before_all_hook(block)
end
def add_before_each_hook(&block : ->) : Nil
current_group.add_before_each_hook(block)
end
def add_after_all_hook(&block : ->) : Nil
current_group.add_after_all_hook(block)
end
def add_after_each_hook(&block : ->) : Nil
current_group.add_after_each_hook(block)
end
def add_around_each_hook(&block : Proc(Nil) ->) : Nil
current_group.add_around_each_hook(block)
end
protected def build : Array(Example)
# TODO
ExampleGroup.new
end
end
end
end

View file

@ -0,0 +1,41 @@
module Spectator
module DSL
class ExampleGroupBuilder
@children = [] of ExampleFactory | ExampleGroupBuilder
@before_all_hooks = [] of ->
@before_each_hooks = [] of ->
@after_all_hooks = [] of ->
@after_each_hooks = [] of ->
@around_each_hooks = [] of Proc(Nil) ->
def add_child(child : ExampleFactory | ExampleGroupBuilder) : Nil
@children << child
end
def add_before_all_hook(&block : ->) : Nil
@before_all_hooks << block
end
def add_before_each_hook(&block : ->) : Nil
@before_each_hooks << block
end
def add_after_all_hook(&block : ->) : Nil
@after_all_hooks << block
end
def add_after_each_hook(&block : ->) : Nil
@after_each_hooks << block
end
def add_around_each_hook(&block : Proc(Nil) ->) : Nil
@around_each_hooks << block
end
def build : Array(Example)
# TODO
Array(Example).new
end
end
end
end

View file

@ -12,17 +12,17 @@ module Spectator
module Group%group module Group%group
include {{@type.id}} include {{@type.id}}
::Spectator::Definitions::GROUPS[\{{@type.symbolize}}] =
ExampleGroup.new(
{{what.is_a?(StringLiteral) ? what : what.stringify}},
::Spectator::Definitions::GROUPS[{{@type.symbolize}}]
)
{% if what.is_a?(Path) || what.is_a?(Generic) %} {% if what.is_a?(Path) || what.is_a?(Generic) %}
_described_class {{what}} _described_class {{what}}
{% end %} {% end %}
::Spectator::DSL::Builder.start_group(
{{what.is_a?(StringLiteral) ? what : what.stringify}}
)
{{block.body}} {{block.body}}
::Spectator::DSL::Builder.end_group
end end
end end
@ -54,15 +54,14 @@ module Spectator
end end
%to_a = Collection%collection.new.%to_a %to_a = Collection%collection.new.%to_a
::Spectator::Definitions::GROUPS[\{{@type.symbolize}}] = ::Spectator::DSL::Builder.start_given_group(
GivenExampleGroup(typeof(%to_a.first)).new( {{collection.stringify}},
{{collection.stringify}}, %to_a
%to_a, )
:%group,
::Spectator::Definitions::GROUPS[{{@type.symbolize}}]
)
{{block.body}} {{block.body}}
::Spectator::DSL::Builder.end_group
end end
end end
@ -115,23 +114,23 @@ module Spectator
end end
macro before_all(&block) macro before_all(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].before_all_hooks << -> {{block}} ::Spectator::DSL::Builder.add_before_all_hook {{block}}
end end
macro before_each(&block) macro before_each(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].before_each_hooks << -> {{block}} ::Spectator::DSL::Builder.add_before_each_hook {{block}}
end end
macro after_all(&block) macro after_all(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].after_all_hooks << -> {{block}} ::Spectator::DSL::Builder.add_after_all_hook {{block}}
end end
macro after_each(&block) macro after_each(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].after_each_hooks << -> {{block}} ::Spectator::DSL::Builder.add_after_each_hook {{block}}
end end
macro around_each(&block) macro around_each(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].around_each_hooks << Proc(Proc(Nil), Nil).new {{block}} ::Spectator::DSL::Builder.add_around_each_hook {{block}}
end end
def include_examples def include_examples