Initial implementation of invoking #around_each hooks

My brain hurts. 😖
This commit is contained in:
Michael Miller 2018-09-13 10:44:21 -06:00
parent 9f778c5cb9
commit e4d46ca603
2 changed files with 21 additions and 3 deletions

View file

@ -12,7 +12,7 @@ module Spectator
getter before_each_hooks = [] of -> getter before_each_hooks = [] of ->
getter after_all_hooks = [] of -> getter after_all_hooks = [] of ->
getter after_each_hooks = [] of -> getter after_each_hooks = [] of ->
getter around_each_hooks = [] of Example -> getter around_each_hooks = [] of Proc(Nil) ->
@before_all_hooks_run = false @before_all_hooks_run = false
@after_all_hooks_run = false @after_all_hooks_run = false
@ -71,6 +71,21 @@ module Spectator
end end
end end
def wrap_around_each_hooks(&block : ->)
wrapper = block
@around_each_hooks.reverse_each do |hook|
wrapper = wrap_proc(hook, wrapper)
end
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
protected def add_examples(array = [] of Example) protected def add_examples(array = [] of Example)
array.concat(@examples) array.concat(@examples)
contexts.each do |context| contexts.each do |context|

View file

@ -114,7 +114,7 @@ module Spectator
end end
macro around_each(&block) macro around_each(&block)
::Spectator::ContextDefinitions::MAPPING[{{@type.stringify}}].around_each_hooks << -> {{block}} ::Spectator::ContextDefinitions::MAPPING[{{@type.stringify}}].around_each_hooks << Proc(Proc(Nil), Nil).new {{block}}
end end
def include_examples def include_examples
@ -159,7 +159,10 @@ module Spectator
context.run_before_all_hooks context.run_before_all_hooks
context.run_before_each_hooks context.run_before_each_hooks
begin begin
wrapper = context.wrap_around_each_hooks do
Example%example.new.%run({% for v, i in var_names %}%var{i}{% if i < var_names.size - 1 %}, {% end %}{% end %}) Example%example.new.%run({% for v, i in var_names %}%var{i}{% if i < var_names.size - 1 %}, {% end %}{% end %})
end
wrapper.call
ensure ensure
@finished = true @finished = true
context.run_after_each_hooks context.run_after_each_hooks