From a1b55335049dada3ed140b8ff7e6ba33c7015d47 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 22 Sep 2018 15:15:29 -0600 Subject: [PATCH] Some initial work to cleanup the DSL builder --- src/spectator.cr | 6 +-- src/spectator/dsl/builder.cr | 59 ++++++++++++++++++++++ src/spectator/dsl/example_group_builder.cr | 41 +++++++++++++++ src/spectator/dsl/structure_dsl.cr | 35 +++++++------ 4 files changed, 119 insertions(+), 22 deletions(-) create mode 100644 src/spectator/dsl/builder.cr create mode 100644 src/spectator/dsl/example_group_builder.cr diff --git a/src/spectator.cr b/src/spectator.cr index c9ee602..b1e2b2f 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -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 diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr new file mode 100644 index 0000000..dd69fbb --- /dev/null +++ b/src/spectator/dsl/builder.cr @@ -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 diff --git a/src/spectator/dsl/example_group_builder.cr b/src/spectator/dsl/example_group_builder.cr new file mode 100644 index 0000000..407cfa2 --- /dev/null +++ b/src/spectator/dsl/example_group_builder.cr @@ -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 diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 5159a7e..d193bdf 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -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