From bd4d020d0f170ff1c59f460ca78b0f86f70a2076 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 5 Dec 2018 19:57:14 -0700 Subject: [PATCH] Condense visible hook methods --- src/spectator/example_group.cr | 22 ++++++++++++++++++---- src/spectator/nested_example_group.cr | 8 ++++---- src/spectator/runnable_example.cr | 6 ++---- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index 1091ffa..0f88020 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -122,11 +122,18 @@ module Spectator children.all?(&.finished?) end + # Runs all of the `before_all` and `before_each` hooks. + # This should run prior to every example in the group. + def run_before_hooks + run_before_all_hooks + run_before_each_hooks + end + # Runs all of the `before_all` hooks. # This should run prior to any examples in the group. # The hooks will be run only once. # Subsequent calls to this method will do nothing. - def run_before_all_hooks : Nil + protected def run_before_all_hooks : Nil return if @before_all_hooks_run @hooks.run_before_all @before_all_hooks_run = true @@ -134,16 +141,23 @@ module Spectator # Runs all of the `before_each` hooks. # This method should run prior to every example in the group. - def run_before_each_hooks : Nil + 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. + def run_after_hooks + run_after_each_hooks + run_after_all_hooks + end + # Runs all of the `after_all` hooks. # This should run following all examples in the group. # The hooks will be run only once, # and only after all examples in the group have finished. # Subsequent calls after the hooks have been run will do nothing. - def run_after_all_hooks : Nil + protected def run_after_all_hooks : Nil return if @after_all_hooks_run return unless finished? @hooks.run_after_all @@ -152,7 +166,7 @@ module Spectator # Runs all of the `after_each` hooks. # This method should run following every example in the group. - def run_after_each_hooks : Nil + protected def run_after_each_hooks : Nil @hooks.run_after_each end diff --git a/src/spectator/nested_example_group.cr b/src/spectator/nested_example_group.cr index 9fcb70f..ab2aae0 100644 --- a/src/spectator/nested_example_group.cr +++ b/src/spectator/nested_example_group.cr @@ -26,7 +26,7 @@ module Spectator # The hooks will be run only once. # Subsequent calls to this method will do nothing. # Parent `before_all` hooks will be run first. - def run_before_all_hooks : Nil + protected def run_before_all_hooks : Nil parent.run_before_all_hooks super end @@ -34,7 +34,7 @@ module Spectator # Runs all of the `before_each` hooks. # This method should run prior to every example in the group. # Parent `before_each` hooks will be run first. - def run_before_each_hooks : Nil + protected def run_before_each_hooks : Nil parent.run_before_each_hooks super end @@ -45,7 +45,7 @@ module Spectator # and only after all examples in the group have finished. # Subsequent calls after the hooks have been run will do nothing. # Parent `after_all` hooks will be run last. - def run_after_all_hooks : Nil + protected def run_after_all_hooks : Nil super parent.run_after_all_hooks end @@ -53,7 +53,7 @@ module Spectator # Runs all of the `after_each` hooks. # This method should run following every example in the group. # Parent `after_each` hooks will be run last. - def run_after_each_hooks : Nil + protected def run_after_each_hooks : Nil super parent.run_after_each_hooks end diff --git a/src/spectator/runnable_example.cr b/src/spectator/runnable_example.cr index 5e2953c..e8e13d6 100644 --- a/src/spectator/runnable_example.cr +++ b/src/spectator/runnable_example.cr @@ -19,8 +19,7 @@ module Spectator # Runs the hooks that should be performed before starting the test code. private def run_before_hooks - group.run_before_all_hooks - group.run_before_each_hooks + 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) @@ -28,8 +27,7 @@ module Spectator # Runs the hooks that should be performed after the test code finishes. private def run_after_hooks(result) - group.run_after_each_hooks - group.run_after_all_hooks + group.run_after_hooks rescue ex # Store the error from the hooks # if the example didn't encounter an error.