mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Move hook storage to their own class
This commit is contained in:
parent
57c9f2c58e
commit
855a5f2239
4 changed files with 58 additions and 31 deletions
|
@ -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
|
||||
|
|
|
@ -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})
|
||||
|
|
|
@ -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
|
||||
|
|
39
src/spectator/example_hooks.cr
Normal file
39
src/spectator/example_hooks.cr
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue