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?)
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

View file

@ -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

View file

@ -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.