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 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 : Example ->) : Nil def add_before_each_hook(&block : ->) : 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 : Example ->) : Nil def add_after_each_hook(&block : ->) : 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 Example -> @before_each_hooks = [] of ->
@after_all_hooks = [] of -> @after_all_hooks = [] of ->
@after_each_hooks = [] of Example -> @after_each_hooks = [] of ->
@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 : Example ->) : Nil def add_before_each_hook(block : ->) : 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 : Example ->) : Nil def add_after_each_hook(block : ->) : Nil
@after_each_hooks << block @after_each_hooks << block
end end

View file

@ -124,10 +124,9 @@ 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.
# The `example` argument should be the example about to be run. def run_before_hooks
def run_before_hooks(example : Example)
run_before_all_hooks run_before_all_hooks
run_before_each_hooks(example) run_before_each_hooks
end end
# Runs all of the `before_all` hooks. # Runs all of the `before_all` hooks.
@ -142,16 +141,14 @@ 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.
# The `example` argument should be the example about to be run. protected def run_before_each_hooks : Nil
protected def run_before_each_hooks(example) : Nil @hooks.run_before_each
@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.
# The `example` argument should be the example that just ran. def run_after_hooks
def run_after_hooks(example : Example) run_after_each_hooks
run_after_each_hooks(example)
run_after_all_hooks run_after_all_hooks
end end
@ -169,9 +166,8 @@ 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.
# The `example` argument should be the example that just ran. protected def run_after_each_hooks : Nil
protected def run_after_each_hooks(example) : Nil @hooks.run_after_each
@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 Example ->, [] of ->,
[] 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(Example ->), @before_each : Array(->),
@after_all : Array(->), @after_all : Array(->),
@after_each : Array(Example ->), @after_each : Array(->),
@around_each : Array(Proc(Nil) ->) @around_each : Array(Proc(Nil) ->)
) )
end end
@ -32,9 +32,8 @@ 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.
# The `example` argument should be the example about to be run. def run_before_each
def run_before_each(example : Example) @before_each.each &.call
@before_each.each &.call(example)
end end
# Runs all `after_all` hooks. # Runs all `after_all` hooks.
@ -45,9 +44,8 @@ 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.
# The `example` argument should be the example that just ran. def run_after_each
def run_after_each(example : Example) @after_each.each &.call
@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(self) group.run_before_hooks
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(self) group.run_after_hooks
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)