From aee3ead57812bbc8e105369b88906f2673d743ed Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 1 Jan 2019 17:48:07 -0700 Subject: [PATCH] Revert "Pass current example to before_each and after_each hooks" This reverts commit b5edb1e9f34167d18c16a51fa8a8f57f32ba8d1a. --- src/spectator/dsl/builder.cr | 4 ++-- src/spectator/dsl/example_group_builder.cr | 8 ++++---- src/spectator/example_group.cr | 20 ++++++++------------ src/spectator/example_hooks.cr | 18 ++++++++---------- src/spectator/runnable_example.cr | 4 ++-- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index 0c3d71c..096ca48 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -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 diff --git a/src/spectator/dsl/example_group_builder.cr b/src/spectator/dsl/example_group_builder.cr index 97734dd..93dc958 100644 --- a/src/spectator/dsl/example_group_builder.cr +++ b/src/spectator/dsl/example_group_builder.cr @@ -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 diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index a6a5e3a..0f88020 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -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 diff --git a/src/spectator/example_hooks.cr b/src/spectator/example_hooks.cr index 803feca..60e81ef 100644 --- a/src/spectator/example_hooks.cr +++ b/src/spectator/example_hooks.cr @@ -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 diff --git a/src/spectator/runnable_example.cr b/src/spectator/runnable_example.cr index 7ce98b1..0f27c77 100644 --- a/src/spectator/runnable_example.cr +++ b/src/spectator/runnable_example.cr @@ -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)