Move hook storage to their own class

This commit is contained in:
Michael Miller 2018-09-23 15:26:19 -06:00
parent 57c9f2c58e
commit 855a5f2239
4 changed files with 58 additions and 31 deletions

View file

@ -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

View file

@ -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})

View file

@ -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

View file

@ -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