mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Invoke before_each, before_all, after_each, and after_all hooks
This commit is contained in:
parent
a725c0d5be
commit
2c0238d178
2 changed files with 49 additions and 0 deletions
|
@ -11,6 +11,9 @@ module Spectator
|
|||
getter after_each_hooks = [] of ->
|
||||
getter around_each_hooks = [] of Example ->
|
||||
|
||||
@before_all_hooks_run = false
|
||||
@after_all_hooks_run = false
|
||||
|
||||
def initialize(@parent = nil)
|
||||
if (parent = @parent)
|
||||
parent.contexts << self
|
||||
|
@ -21,6 +24,48 @@ module Spectator
|
|||
add_examples
|
||||
end
|
||||
|
||||
def run_before_all_hooks
|
||||
if (parent = @parent)
|
||||
parent.run_before_all_hooks
|
||||
end
|
||||
unless @before_all_hooks_run
|
||||
@before_all_hooks.each do |hook|
|
||||
hook.call
|
||||
end
|
||||
@before_all_hooks_run = true
|
||||
end
|
||||
end
|
||||
|
||||
def run_before_each_hooks
|
||||
if (parent = @parent)
|
||||
parent.run_before_each_hooks
|
||||
end
|
||||
@before_each_hooks.each do |hook|
|
||||
hook.call
|
||||
end
|
||||
end
|
||||
|
||||
def run_after_all_hooks
|
||||
unless @after_all_hooks_run
|
||||
@after_all_hooks.each do |hook|
|
||||
hook.call
|
||||
end
|
||||
@after_all_hooks_run = true
|
||||
end
|
||||
if (parent = @parent)
|
||||
parent.run_after_all_hooks
|
||||
end
|
||||
end
|
||||
|
||||
def run_after_each_hooks
|
||||
@after_each_hooks.each do |hook|
|
||||
hook.call
|
||||
end
|
||||
if (parent = @parent)
|
||||
parent.run_after_each_hooks
|
||||
end
|
||||
end
|
||||
|
||||
protected def add_examples(array = [] of Example)
|
||||
array.concat(@examples)
|
||||
contexts.each do |context|
|
||||
|
|
|
@ -24,6 +24,8 @@ module Spectator
|
|||
|
||||
private def run_example(example)
|
||||
error = nil
|
||||
example.context.run_before_all_hooks
|
||||
example.context.run_before_each_hooks
|
||||
elapsed = Time.measure do
|
||||
begin
|
||||
example.run
|
||||
|
@ -31,6 +33,8 @@ module Spectator
|
|||
error = ex
|
||||
end
|
||||
end
|
||||
example.context.run_after_each_hooks
|
||||
example.context.run_after_all_hooks
|
||||
case error
|
||||
when Nil
|
||||
SuccessfulExampleResult.new(example, elapsed)
|
||||
|
|
Loading…
Reference in a new issue