Add TestContext

This commit is contained in:
Michael Miller 2019-09-17 20:57:59 -06:00
parent da8736f891
commit 6e1605f246
6 changed files with 36 additions and 3 deletions

View file

@ -33,6 +33,11 @@ module Spectator
@example_count = children.sum(&.example_count)
end
getter context
def initialize(@context : TestContext)
end
# Yields each direct descendant.
def each
children.each do |child|

View file

@ -18,7 +18,8 @@ module Spectator
# The parent's children must contain this group,
# otherwise there may be unexpected behavior.
# The *hooks* are stored to be triggered later.
def initialize(@what, @parent)
def initialize(@what, @parent, context)
super(context)
end
# Indicates wheter the group references a type.

View file

@ -1,3 +1,4 @@
require "../test_context"
require "./example_builder"
module Spectator::SpecBuilder
@ -31,6 +32,10 @@ module Spectator::SpecBuilder
@after_all_hooks << hook
end
private def context
TestContext.new(build_hooks)
end
private def build_hooks
ExampleHooks.empty
end

View file

@ -6,7 +6,7 @@ module Spectator::SpecBuilder
end
def build(group)
NestedExampleGroup.new(@what, group).tap do |group|
NestedExampleGroup.new(@what, group, context).tap do |group|
group.children = children.map do |child|
child.build(group).as(ExampleComponent)
end

View file

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

View file

@ -0,0 +1,22 @@
module Spectator
struct TestContext
def initialize(@hooks : ExampleHooks)
@before_all_hooks_run = false
@after_all_hooks_run = false
end
def run_before_hooks(wrapper : TestWrapper)
@hooks.run_before_all
@hooks.run_before_each(wrapper)
ensure
@before_all_hooks_run = true
end
def run_after_hooks(wrapper : TestWrapper)
@hooks.run_after_each(wrapper)
@hooks.run_after_all
ensure
@after_all_hooks_run = true
end
end
end