Get around_each hooks working again

This commit is contained in:
Michael Miller 2019-09-18 21:51:35 -06:00
parent b17d19689e
commit 24eb5e419a
7 changed files with 33 additions and 14 deletions

View file

@ -29,7 +29,7 @@ module Spectator
{{block.body}} {{block.body}}
end end
# TODO: Handle wrapping proc and test together. ::Spectator::SpecBuilder.add_around_each_hook { |test, proc| test.as({{@type.id}}).%hook(proc) }
end end
end end
end end

View file

@ -10,7 +10,7 @@ module Spectator
[] of TestMethod, [] of TestMethod,
[] of ->, [] of ->,
[] of TestMethod, [] of TestMethod,
[] of Proc(Nil) -> [] of ::SpectatorTest, Proc(Nil) ->
) )
end end
@ -20,7 +20,7 @@ module Spectator
@before_each : Array(TestMethod), @before_each : Array(TestMethod),
@after_all : Array(->), @after_all : Array(->),
@after_each : Array(TestMethod), @after_each : Array(TestMethod),
@around_each : Array(Proc(Nil) ->) @around_each : Array(::SpectatorTest, Proc(Nil) ->)
) )
end end
@ -56,19 +56,18 @@ module Spectator
# in addition to a block passed to this method. # in addition to a block passed to this method.
# To call the block and all "around-each" hooks, # To call the block and all "around-each" hooks,
# just invoke `Proc#call` on the returned proc. # just invoke `Proc#call` on the returned proc.
def wrap_around_each(&block : ->) : -> def wrap_around_each(test, block : ->)
wrapper = block wrapper = block
# Must wrap in reverse order, # Must wrap in reverse order,
# otherwise hooks will run in the wrong order. # otherwise hooks will run in the wrong order.
@around_each.reverse_each do |hook| @around_each.reverse_each do |hook|
wrapper = wrap_proc(hook, wrapper) wrapper = wrap_foo(test, hook, wrapper)
end end
wrapper wrapper
end end
# Utility method for wrapping one proc with another. private def wrap_foo(test, hook, wrapper)
private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->) ->{ hook.call(test, wrapper) }
->{ inner.call(wrapper) }
end end
end end
end end

View file

@ -25,11 +25,13 @@ module Spectator
# Runs the test code and captures the result. # Runs the test code and captures the result.
private def run_example(result) private def run_example(result)
wrapper = test_wrapper.around_hook(group.context)
# Capture how long it takes to run the test code. # Capture how long it takes to run the test code.
result.elapsed = Time.measure do result.elapsed = Time.measure do
begin begin
test_wrapper.run # Actually run the example code. wrapper.call
rescue ex # Catch all errors and handle them later. rescue ex # Catch all errors and handle them later.
result.error = ex result.error = ex
end end
end end

View file

@ -70,9 +70,9 @@ module Spectator
end end
# Adds a block of code to run before and after each example in the current group. # Adds a block of code to run before and after each example in the current group.
# The block of code will be given another proc as an argument. # The block of code will be given another hook as an argument.
# It is expected that the block will call the proc. # It is expected that the block will call the hook.
def add_around_each_hook(&block : Proc(Nil) ->) : Nil def add_around_each_hook(&block : ::SpectatorTest, Proc(Nil) ->) : Nil
@@stack.current.add_around_each_hook(block) @@stack.current.add_around_each_hook(block)
end end

View file

@ -11,6 +11,7 @@ module Spectator::SpecBuilder
@after_each_hooks = Deque(TestMethod).new @after_each_hooks = Deque(TestMethod).new
@before_all_hooks = Deque(->).new @before_all_hooks = Deque(->).new
@after_all_hooks = Deque(->).new @after_all_hooks = Deque(->).new
@around_each_hooks = Deque(::SpectatorTest, Proc(Nil) ->).new
def add_child(child : Child) def add_child(child : Child)
@children << child @children << child
@ -32,13 +33,17 @@ module Spectator::SpecBuilder
@after_all_hooks << hook @after_all_hooks << hook
end end
def add_around_each_hook(hook : ::SpectatorTest, Proc(Nil) ->)
@around_each_hooks << hook
end
private def build_hooks private def build_hooks
ExampleHooks.new( ExampleHooks.new(
@before_all_hooks.to_a, @before_all_hooks.to_a,
@before_each_hooks.to_a, @before_each_hooks.to_a,
@after_all_hooks.to_a, @after_all_hooks.to_a,
@after_each_hooks.to_a, @after_each_hooks.to_a,
[] of Proc(Nil) -> @around_each_hooks.to_a
) )
end end
end end

View file

@ -42,5 +42,14 @@ module Spectator
@hooks.run_after_each(wrapper) @hooks.run_after_each(wrapper)
@parent.try &.run_after_each_hooks(wrapper) @parent.try &.run_after_each_hooks(wrapper)
end end
def wrap_around_each_hooks(test, &block : ->)
wrapper = @hooks.wrap_around_each(test, block)
if (parent = @parent)
parent.wrap_around_each_hooks(test, &wrapper)
else
wrapper
end
end
end end
end end

View file

@ -24,5 +24,9 @@ module Spectator
def call(method : TestMethod) : Nil def call(method : TestMethod) : Nil
method.call(@test) method.call(@test)
end end
def around_hook(context : TestContext)
context.wrap_around_each_hooks(@test) { run }
end
end end
end end