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 @after_all_hooks << hook
end end
private def context
TestContext.new(build_hooks)
end
private def build_hooks private def build_hooks
ExampleHooks.new( ExampleHooks.new(
@before_all_hooks.to_a, @before_all_hooks.to_a,

View file

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

View file

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

View file

@ -1,22 +1,46 @@
module Spectator module Spectator
struct TestContext class TestContext
def initialize(@hooks : ExampleHooks) def initialize(@parent : TestContext?, @hooks : ExampleHooks)
@before_all_hooks_run = false @before_all_hooks_run = false
@after_all_hooks_run = false @after_all_hooks_run = false
end end
def run_before_hooks(wrapper : TestWrapper) 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_all
@hooks.run_before_each(wrapper)
ensure ensure
@before_all_hooks_run = true @before_all_hooks_run = true
end 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) 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 @hooks.run_after_all
@parent.try &.run_after_all_hooks
ensure ensure
@after_all_hooks_run = true @after_all_hooks_run = true
end end
protected def run_after_each_hooks(wrapper : TestWrapper)
@hooks.run_after_each(wrapper)
@parent.try &.run_after_each_hooks(wrapper)
end
end end
end end