Condense visible hook methods

This commit is contained in:
Michael Miller 2018-12-05 19:57:14 -07:00
parent e0f5862960
commit bd4d020d0f
3 changed files with 24 additions and 12 deletions

View file

@ -122,11 +122,18 @@ module Spectator
children.all?(&.finished?) children.all?(&.finished?)
end 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. # Runs all of the `before_all` hooks.
# This should run prior to any examples in the group. # This should run prior to any examples in the group.
# The hooks will be run only once. # The hooks will be run only once.
# Subsequent calls to this method will do nothing. # 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 return if @before_all_hooks_run
@hooks.run_before_all @hooks.run_before_all
@before_all_hooks_run = true @before_all_hooks_run = true
@ -134,16 +141,23 @@ 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.
def run_before_each_hooks : Nil protected def run_before_each_hooks : Nil
@hooks.run_before_each @hooks.run_before_each
end 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. # Runs all of the `after_all` hooks.
# This should run following all examples in the group. # This should run following all examples in the group.
# The hooks will be run only once, # The hooks will be run only once,
# and only after all examples in the group have finished. # and only after all examples in the group have finished.
# Subsequent calls after the hooks have been run will do nothing. # 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 if @after_all_hooks_run
return unless finished? return unless finished?
@hooks.run_after_all @hooks.run_after_all
@ -152,7 +166,7 @@ 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.
def run_after_each_hooks : Nil protected def run_after_each_hooks : Nil
@hooks.run_after_each @hooks.run_after_each
end end

View file

@ -26,7 +26,7 @@ module Spectator
# The hooks will be run only once. # The hooks will be run only once.
# Subsequent calls to this method will do nothing. # Subsequent calls to this method will do nothing.
# Parent `before_all` hooks will be run first. # 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 parent.run_before_all_hooks
super super
end end
@ -34,7 +34,7 @@ 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.
# Parent `before_each` hooks will be run first. # 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 parent.run_before_each_hooks
super super
end end
@ -45,7 +45,7 @@ module Spectator
# and only after all examples in the group have finished. # and only after all examples in the group have finished.
# Subsequent calls after the hooks have been run will do nothing. # Subsequent calls after the hooks have been run will do nothing.
# Parent `after_all` hooks will be run last. # Parent `after_all` hooks will be run last.
def run_after_all_hooks : Nil protected def run_after_all_hooks : Nil
super super
parent.run_after_all_hooks parent.run_after_all_hooks
end end
@ -53,7 +53,7 @@ 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.
# Parent `after_each` hooks will be run last. # Parent `after_each` hooks will be run last.
def run_after_each_hooks : Nil protected def run_after_each_hooks : Nil
super super
parent.run_after_each_hooks parent.run_after_each_hooks
end end

View file

@ -19,8 +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_all_hooks group.run_before_hooks
group.run_before_each_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)
@ -28,8 +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(result) private def run_after_hooks(result)
group.run_after_each_hooks group.run_after_hooks
group.run_after_all_hooks
rescue ex rescue ex
# Store the error from the hooks # Store the error from the hooks
# if the example didn't encounter an error. # if the example didn't encounter an error.