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

View file

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

View file

@ -37,7 +37,13 @@ module Spectator::SpecBuilder
end
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