Call parent hooks

This commit is contained in:
Michael Miller 2019-09-18 09:51:45 -06:00
parent 957b8a54db
commit db3f89731c
4 changed files with 31 additions and 8 deletions

View file

@ -32,10 +32,6 @@ module Spectator::SpecBuilder
@after_all_hooks << hook
end
private def context
TestContext.new(build_hooks)
end
private def build_hooks
ExampleHooks.new(
@before_all_hooks.to_a,

View file

@ -1,3 +1,4 @@
require "../test_context"
require "./example_group_builder"
module Spectator::SpecBuilder
@ -6,6 +7,7 @@ module Spectator::SpecBuilder
end
def build(group)
context = TestContext.new(group.context, build_hooks)
NestedExampleGroup.new(@what, group, context).tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)

View file

@ -3,6 +3,7 @@ require "./example_group_builder"
module Spectator::SpecBuilder
class RootExampleGroupBuilder < ExampleGroupBuilder
def build
context = TestContext.new(nil, build_hooks)
RootExampleGroup.new(context).tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)

View file

@ -1,22 +1,46 @@
module Spectator
struct TestContext
def initialize(@hooks : ExampleHooks)
class TestContext
def initialize(@parent : TestContext?, @hooks : ExampleHooks)
@before_all_hooks_run = false
@after_all_hooks_run = false
end
def run_before_hooks(wrapper : TestWrapper)
run_before_all_hooks
run_before_each_hooks(wrapper)
end
protected def run_before_all_hooks
return if @before_all_hooks_run
@parent.try &.run_before_all_hooks
@hooks.run_before_all
@hooks.run_before_each(wrapper)
ensure
@before_all_hooks_run = true
end
protected def run_before_each_hooks(wrapper : TestWrapper)
@parent.try &.run_before_each_hooks(wrapper)
@hooks.run_before_each(wrapper)
end
def run_after_hooks(wrapper : TestWrapper)
@hooks.run_after_each(wrapper)
run_after_each_hooks(wrapper)
run_after_all_hooks
end
protected def run_after_all_hooks
return if @after_all_hooks_run
@hooks.run_after_all
@parent.try &.run_after_all_hooks
ensure
@after_all_hooks_run = true
end
protected def run_after_each_hooks(wrapper : TestWrapper)
@hooks.run_after_each(wrapper)
@parent.try &.run_after_each_hooks(wrapper)
end
end
end