Pass current example to before_each and after_each hooks

This commit is contained in:
Michael Miller 2019-01-01 16:56:23 -07:00
parent abf7c8831c
commit b5edb1e9f3
5 changed files with 30 additions and 24 deletions

View file

@ -70,7 +70,7 @@ module Spectator::DSL
end
# Adds a block of code to run before each example in the current group.
def add_before_each_hook(&block : ->) : Nil
def add_before_each_hook(&block : Example ->) : Nil
current_group.add_before_each_hook(block)
end
@ -80,7 +80,7 @@ module Spectator::DSL
end
# Adds a block of code to run after each example in the current group.
def add_after_each_hook(&block : ->) : Nil
def add_after_each_hook(&block : Example ->) : Nil
current_group.add_after_each_hook(block)
end

View file

@ -12,9 +12,9 @@ module Spectator::DSL
# Hooks added to the group so far.
@before_all_hooks = [] of ->
@before_each_hooks = [] of ->
@before_each_hooks = [] of Example ->
@after_all_hooks = [] of ->
@after_each_hooks = [] of ->
@after_each_hooks = [] of Example ->
@around_each_hooks = [] of Proc(Nil) ->
# Adds a new example factory or group builder to this group.
@ -28,7 +28,7 @@ module Spectator::DSL
end
# Adds a hook to run before each example (and nested example) in this group.
def add_before_each_hook(block : ->) : Nil
def add_before_each_hook(block : Example ->) : Nil
@before_each_hooks << block
end
@ -38,7 +38,7 @@ module Spectator::DSL
end
# Adds a hook to run after each example (and nested example) in this group.
def add_after_each_hook(block : ->) : Nil
def add_after_each_hook(block : Example ->) : Nil
@after_each_hooks << block
end

View file

@ -124,9 +124,10 @@ module Spectator
# Runs all of the `before_all` and `before_each` hooks.
# This should run prior to every example in the group.
def run_before_hooks
# The `example` argument should be the example about to be run.
def run_before_hooks(example : Example)
run_before_all_hooks
run_before_each_hooks
run_before_each_hooks(example)
end
# Runs all of the `before_all` hooks.
@ -141,14 +142,16 @@ module Spectator
# Runs all of the `before_each` hooks.
# This method should run prior to every example in the group.
protected def run_before_each_hooks : Nil
@hooks.run_before_each
# The `example` argument should be the example about to be run.
protected def run_before_each_hooks(example) : Nil
@hooks.run_before_each(example)
end
# Runs all of the `after_all` and `after_each` hooks.
# This should run following every example in the group.
def run_after_hooks
run_after_each_hooks
# The `example` argument should be the example that just ran.
def run_after_hooks(example : Example)
run_after_each_hooks(example)
run_after_all_hooks
end
@ -166,8 +169,9 @@ module Spectator
# Runs all of the `after_each` hooks.
# This method should run following every example in the group.
protected def run_after_each_hooks : Nil
@hooks.run_after_each
# The `example` argument should be the example that just ran.
protected def run_after_each_hooks(example) : Nil
@hooks.run_after_each(example)
end
# Creates a proc that runs the `around_each` hooks

View file

@ -7,9 +7,9 @@ module Spectator
def self.empty
new(
[] of ->,
[] of Example ->,
[] of ->,
[] of ->,
[] of ->,
[] of Example ->,
[] 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(Example ->),
@after_all : Array(->),
@after_each : Array(->),
@after_each : Array(Example ->),
@around_each : Array(Proc(Nil) ->)
)
end
@ -32,8 +32,9 @@ 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
# The `example` argument should be the example about to be run.
def run_before_each(example : Example)
@before_each.each &.call(example)
end
# Runs all `after_all` hooks.
@ -44,8 +45,9 @@ module Spectator
# Runs all `after_each` hooks.
# These hooks should be run every time after each example in a group.
def run_after_each
@after_each.each &.call
# The `example` argument should be the example that just ran.
def run_after_each(example : Example)
@after_each.each &.call(example)
end
# Creates a proc that runs the `around_each` hooks

View file

@ -19,7 +19,7 @@ module Spectator
# Runs the hooks that should be performed before starting the test code.
private def run_before_hooks
group.run_before_hooks
group.run_before_hooks(self)
rescue ex
# If an error occurs in the before hooks, skip running the example.
raise Exception.new("Error encountered while running before hooks", ex)
@ -27,7 +27,7 @@ module Spectator
# Runs the hooks that should be performed after the test code finishes.
private def run_after_hooks
group.run_after_hooks
group.run_after_hooks(self)
rescue ex
# If an error occurs in the after hooks, elevate it to abort testing.
raise Exception.new("Error encountered while running after hooks", ex)