From 855a5f223980625d5c3443991c7d4a38af8aa7cd Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 23 Sep 2018 15:26:19 -0600 Subject: [PATCH] Move hook storage to their own class --- src/spectator/dsl/example_group_builder.cr | 12 +++++- .../dsl/given_example_group_builder.cr | 2 +- src/spectator/example_group.cr | 36 ++++------------- src/spectator/example_hooks.cr | 39 +++++++++++++++++++ 4 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 src/spectator/example_hooks.cr diff --git a/src/spectator/dsl/example_group_builder.cr b/src/spectator/dsl/example_group_builder.cr index f3814ce..42be910 100644 --- a/src/spectator/dsl/example_group_builder.cr +++ b/src/spectator/dsl/example_group_builder.cr @@ -38,13 +38,23 @@ module Spectator end def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup - ExampleGroup.new(@what, parent).tap do |group| + ExampleGroup.new(@what, parent, build_hooks).tap do |group| children = @children.map do |child| child.build(group, locals).as(ExampleGroup::Child) end group.children = children end end + + private def build_hooks + ExampleHooks.new( + @before_all_hooks, + @before_each_hooks, + @after_all_hooks, + @after_each_hooks, + @around_each_hooks + ) + end end end end diff --git a/src/spectator/dsl/given_example_group_builder.cr b/src/spectator/dsl/given_example_group_builder.cr index e1e50d9..3de65b6 100644 --- a/src/spectator/dsl/given_example_group_builder.cr +++ b/src/spectator/dsl/given_example_group_builder.cr @@ -9,7 +9,7 @@ module Spectator end def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup - ExampleGroup.new(@what, parent).tap do |group| + ExampleGroup.new(@what, parent, build_hooks).tap do |group| children = [] of ExampleGroup::Child @collection.each do |value| iter_locals = locals.merge({@symbol => value}) diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index 154213d..6797508 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -11,16 +11,9 @@ module Spectator private getter! children : Array(Child) setter children - getter before_all_hooks = [] of -> - getter before_each_hooks = [] of -> - getter after_all_hooks = [] of -> - getter after_each_hooks = [] of -> - getter around_each_hooks = [] of Proc(Nil) -> + private getter hooks : ExampleHooks - @before_all_hooks_run = false - @after_all_hooks_run = false - - def initialize(@what, @parent) + def initialize(@what, @parent, @hooks) end def examples : Enumerable(Example) @@ -54,9 +47,7 @@ module Spectator parent.run_before_all_hooks end unless @before_all_hooks_run - @before_all_hooks.each do |hook| - hook.call - end + hooks.run_before_all @before_all_hooks_run = true end end @@ -65,17 +56,13 @@ module Spectator if (parent = @parent) parent.run_before_each_hooks end - @before_each_hooks.each do |hook| - hook.call - end + hooks.run_before_each end def run_after_all_hooks unless @after_all_hooks_run if all_examples.all?(&.finished?) - @after_all_hooks.each do |hook| - hook.call - end + hooks.run_after_all @after_all_hooks_run = true end end @@ -85,27 +72,18 @@ module Spectator end def run_after_each_hooks - @after_each_hooks.each do |hook| - hook.call - end + hooks.run_after_each if (parent = @parent) parent.run_after_each_hooks end end def wrap_around_each_hooks(&block : ->) - wrapper = block - @around_each_hooks.reverse_each do |hook| - wrapper = wrap_proc(hook, wrapper) - end + wrapper = hooks.wrap_around_each(&block) if (parent = @parent) wrapper = parent.wrap_around_each_hooks(&wrapper) end wrapper end - - private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->) - -> { inner.call(wrapper) } - end end end diff --git a/src/spectator/example_hooks.cr b/src/spectator/example_hooks.cr new file mode 100644 index 0000000..9886315 --- /dev/null +++ b/src/spectator/example_hooks.cr @@ -0,0 +1,39 @@ +module Spectator + class ExampleHooks + def initialize( + @before_all : Array(->), + @before_each : Array(->), + @after_all : Array(->), + @after_each : Array(->), + @around_each : Array(Proc(Nil) ->)) + end + + def run_before_all + @before_all.each(&.call) + end + + def run_before_each + @before_each.each(&.call) + end + + def run_after_all + @after_all.each(&.call) + end + + def run_after_each + @after_each.each(&.call) + end + + def wrap_around_each(&block : ->) + wrapper = block + @around_each.reverse_each do |hook| + wrapper = wrap_proc(hook, wrapper) + end + wrapper + end + + private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->) + -> { inner.call(wrapper) } + end + end +end