Revert "Pass current example to before_each and after_each hooks"

This reverts commit b5edb1e9f3.
This commit is contained in:
Michael Miller 2019-01-01 17:48:07 -07:00
parent 191eb02cbe
commit aee3ead578
5 changed files with 24 additions and 30 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 : Example ->) : Nil
def add_before_each_hook(&block : ->) : 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 : Example ->) : Nil
def add_after_each_hook(&block : ->) : 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 Example ->
@before_each_hooks = [] of ->
@after_all_hooks = [] of ->
@after_each_hooks = [] of Example ->
@after_each_hooks = [] of ->
@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 : Example ->) : Nil
def add_before_each_hook(block : ->) : 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 : Example ->) : Nil
def add_after_each_hook(block : ->) : Nil
@after_each_hooks << block
end

View File

@ -124,10 +124,9 @@ module Spectator
# Runs all of the `before_all` and `before_each` hooks.
# This should run prior to every example in the group.
# The `example` argument should be the example about to be run.
def run_before_hooks(example : Example)
def run_before_hooks
run_before_all_hooks
run_before_each_hooks(example)
run_before_each_hooks
end
# Runs all of the `before_all` hooks.
@ -142,16 +141,14 @@ module Spectator
# Runs all of the `before_each` hooks.
# This method should run prior to every example in the group.
# The `example` argument should be the example about to be run.
protected def run_before_each_hooks(example) : Nil
@hooks.run_before_each(example)
protected def run_before_each_hooks : Nil
@hooks.run_before_each
end
# Runs all of the `after_all` and `after_each` hooks.
# This should run following every example in the group.
# The `example` argument should be the example that just ran.
def run_after_hooks(example : Example)
run_after_each_hooks(example)
def run_after_hooks
run_after_each_hooks
run_after_all_hooks
end
@ -169,9 +166,8 @@ module Spectator
# Runs all of the `after_each` hooks.
# This method should run following every example in the group.
# The `example` argument should be the example that just ran.
protected def run_after_each_hooks(example) : Nil
@hooks.run_after_each(example)
protected def run_after_each_hooks : Nil
@hooks.run_after_each
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 Example ->,
[] of ->,
[] of ->,
[] of Proc(Nil) ->
)
end
@ -17,9 +17,9 @@ module Spectator
# Creates a new set of hooks.
def initialize(
@before_all : Array(->),
@before_each : Array(Example ->),
@before_each : Array(->),
@after_all : Array(->),
@after_each : Array(Example ->),
@after_each : Array(->),
@around_each : Array(Proc(Nil) ->)
)
end
@ -32,9 +32,8 @@ module Spectator
# Runs all `before_each` hooks.
# These hooks should be run every time before each example in a group.
# The `example` argument should be the example about to be run.
def run_before_each(example : Example)
@before_each.each &.call(example)
def run_before_each
@before_each.each &.call
end
# Runs all `after_all` hooks.
@ -45,9 +44,8 @@ module Spectator
# Runs all `after_each` hooks.
# These hooks should be run every time after each example in a group.
# The `example` argument should be the example that just ran.
def run_after_each(example : Example)
@after_each.each &.call(example)
def run_after_each
@after_each.each &.call
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(self)
group.run_before_hooks
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(self)
group.run_after_hooks
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)