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 end
# Adds a block of code to run before each example in the current group. # 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) current_group.add_before_each_hook(block)
end end
@ -80,7 +80,7 @@ module Spectator::DSL
end end
# Adds a block of code to run after each example in the current group. # 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) current_group.add_after_each_hook(block)
end end

View file

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

View file

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

View file

@ -7,9 +7,9 @@ module Spectator
def self.empty def self.empty
new( new(
[] of ->, [] of ->,
[] of Example ->,
[] of ->, [] of ->,
[] of ->, [] of Example ->,
[] 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(Example ->),
@after_all : Array(->), @after_all : Array(->),
@after_each : Array(->), @after_each : Array(Example ->),
@around_each : Array(Proc(Nil) ->) @around_each : Array(Proc(Nil) ->)
) )
end end
@ -32,8 +32,9 @@ 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 # The `example` argument should be the example about to be run.
@before_each.each &.call def run_before_each(example : Example)
@before_each.each &.call(example)
end end
# Runs all `after_all` hooks. # Runs all `after_all` hooks.
@ -44,8 +45,9 @@ module Spectator
# Runs all `after_each` hooks. # Runs all `after_each` 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 # The `example` argument should be the example that just ran.
@after_each.each &.call def run_after_each(example : Example)
@after_each.each &.call(example)
end end
# Creates a proc that runs the `around_each` hooks # 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. # Runs the hooks that should be performed before starting the test code.
private def run_before_hooks private def run_before_hooks
group.run_before_hooks group.run_before_hooks(self)
rescue ex rescue ex
# If an error occurs in the before hooks, skip running the example. # If an error occurs in the before hooks, skip running the example.
raise Exception.new("Error encountered while running before hooks", ex) 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. # Runs the hooks that should be performed after the test code finishes.
private def run_after_hooks private def run_after_hooks
group.run_after_hooks group.run_after_hooks(self)
rescue ex rescue ex
# If an error occurs in the after hooks, elevate it to abort testing. # If an error occurs in the after hooks, elevate it to abort testing.
raise Exception.new("Error encountered while running after hooks", ex) raise Exception.new("Error encountered while running after hooks", ex)