Implement before and after hooks

This commit is contained in:
Michael Miller 2019-09-17 20:58:26 -06:00
parent 6e1605f246
commit 957b8a54db
3 changed files with 22 additions and 9 deletions

View file

@ -7,9 +7,9 @@ module Spectator
def self.empty def self.empty
new( new(
[] of ->, [] of ->,
[] of TestMethod,
[] of ->, [] of ->,
[] of ->, [] of TestMethod,
[] of ->,
[] of Proc(Nil) -> [] of Proc(Nil) ->
) )
end end
@ -17,9 +17,9 @@ module Spectator
# Creates a new set of hooks. # Creates a new set of hooks.
def initialize( def initialize(
@before_all : Array(->), @before_all : Array(->),
@before_each : Array(->), @before_each : Array(TestMethod),
@after_all : Array(->), @after_all : Array(->),
@after_each : Array(->), @after_each : Array(TestMethod),
@around_each : Array(Proc(Nil) ->) @around_each : Array(Proc(Nil) ->)
) )
end end
@ -32,8 +32,10 @@ module Spectator
# Runs all "before-each" hooks. # Runs all "before-each" hooks.
# These hooks should be run every time before each example in a group. # These hooks should be run every time before each example in a group.
def run_before_each def run_before_each(wrapper : TestWrapper)
@before_each.each &.call @before_each.each do |hook|
wrapper.call(hook)
end
end end
# Runs all "after-all" hooks. # Runs all "after-all" hooks.
@ -44,8 +46,10 @@ module Spectator
# Runs all "after-all" hooks. # Runs all "after-all" hooks.
# These hooks should be run every time after each example in a group. # These hooks should be run every time after each example in a group.
def run_after_each def run_after_each(wrapper : TestWrapper)
@after_each.each &.call @after_each.each do |hook|
wrapper.call(hook)
end
end end
# Creates a proc that runs the "around-each" hooks # Creates a proc that runs the "around-each" hooks

View file

@ -15,8 +15,11 @@ module Spectator
# Runs all hooks and the example code. # Runs all hooks and the example code.
# A captured result is returned. # A captured result is returned.
private def capture_result private def capture_result
context = group.context
ResultCapture.new.tap do |result| ResultCapture.new.tap do |result|
context.run_before_hooks(test_wrapper)
run_example(result) run_example(result)
context.run_after_hooks(test_wrapper)
end end
end end

View file

@ -37,7 +37,13 @@ module Spectator::SpecBuilder
end end
private def build_hooks private def build_hooks
ExampleHooks.empty ExampleHooks.new(
@before_all_hooks.to_a,
@before_each_hooks.to_a,
@after_all_hooks.to_a,
@after_each_hooks.to_a,
[] of Proc(Nil) ->
)
end end
end end
end end