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"
macro describe(what, source_file = __FILE__, source_line = __LINE__, &block)
module Spectator
module Examples
DSL::StructureDSL.describe({{what}}) {{block}}
end
module SpectatorExamples
::Spectator::DSL::StructureDSL.describe({{what}}) {{block}}
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
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) %}
_described_class {{what}}
{% end %}
::Spectator::DSL::Builder.start_group(
{{what.is_a?(StringLiteral) ? what : what.stringify}}
)
{{block.body}}
::Spectator::DSL::Builder.end_group
end
end
@ -54,15 +54,14 @@ module Spectator
end
%to_a = Collection%collection.new.%to_a
::Spectator::Definitions::GROUPS[\{{@type.symbolize}}] =
GivenExampleGroup(typeof(%to_a.first)).new(
{{collection.stringify}},
%to_a,
:%group,
::Spectator::Definitions::GROUPS[{{@type.symbolize}}]
)
::Spectator::DSL::Builder.start_given_group(
{{collection.stringify}},
%to_a
)
{{block.body}}
::Spectator::DSL::Builder.end_group
end
end
@ -115,23 +114,23 @@ module Spectator
end
macro before_all(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].before_all_hooks << -> {{block}}
::Spectator::DSL::Builder.add_before_all_hook {{block}}
end
macro before_each(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].before_each_hooks << -> {{block}}
::Spectator::DSL::Builder.add_before_each_hook {{block}}
end
macro after_all(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].after_all_hooks << -> {{block}}
::Spectator::DSL::Builder.add_after_all_hook {{block}}
end
macro after_each(&block)
::Spectator::Definitions::GROUPS[{{@type.symbolize}}].after_each_hooks << -> {{block}}
::Spectator::DSL::Builder.add_after_each_hook {{block}}
end
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
def include_examples