2018-08-19 07:15:32 +00:00
|
|
|
require "./example"
|
|
|
|
|
|
|
|
module Spectator
|
|
|
|
class ExampleGroup
|
2018-09-23 20:34:42 +00:00
|
|
|
alias Child = Example | ExampleGroup
|
2018-08-19 07:15:32 +00:00
|
|
|
|
2018-09-15 16:45:47 +00:00
|
|
|
getter what : String
|
2018-09-23 20:34:42 +00:00
|
|
|
|
|
|
|
getter! parent : ExampleGroup
|
|
|
|
|
|
|
|
private getter! children : Array(Child)
|
|
|
|
setter children
|
|
|
|
|
2018-10-14 07:02:52 +00:00
|
|
|
def initialize(@what, @parent, @hooks : ExampleHooks)
|
2018-10-10 19:03:47 +00:00
|
|
|
@before_all_hooks_run = false
|
|
|
|
@after_all_hooks_run = false
|
2018-09-15 16:45:47 +00:00
|
|
|
end
|
|
|
|
|
2018-09-20 05:13:43 +00:00
|
|
|
def example_count
|
2018-09-23 20:34:42 +00:00
|
|
|
children.sum do |child|
|
|
|
|
child.is_a?(Example) ? 1 : child.example_count
|
2018-09-20 05:13:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-23 20:34:42 +00:00
|
|
|
def all_examples
|
2018-09-20 05:13:43 +00:00
|
|
|
Array(Example).new(example_count).tap do |array|
|
2018-09-23 20:34:42 +00:00
|
|
|
children.each do |child|
|
|
|
|
if child.is_a?(Example)
|
|
|
|
array << child
|
2018-09-20 05:13:43 +00:00
|
|
|
else
|
2018-09-23 20:34:42 +00:00
|
|
|
array.concat(child.all_examples)
|
2018-09-20 05:13:43 +00:00
|
|
|
end
|
|
|
|
end
|
2018-09-20 01:31:50 +00:00
|
|
|
end
|
2018-09-15 16:45:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_before_all_hooks
|
|
|
|
if (parent = @parent)
|
|
|
|
parent.run_before_all_hooks
|
|
|
|
end
|
|
|
|
unless @before_all_hooks_run
|
2018-10-14 07:02:52 +00:00
|
|
|
@hooks.run_before_all
|
2018-09-15 16:45:47 +00:00
|
|
|
@before_all_hooks_run = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_before_each_hooks
|
|
|
|
if (parent = @parent)
|
|
|
|
parent.run_before_each_hooks
|
|
|
|
end
|
2018-10-14 07:02:52 +00:00
|
|
|
@hooks.run_before_each
|
2018-09-15 16:45:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_after_all_hooks
|
|
|
|
unless @after_all_hooks_run
|
|
|
|
if all_examples.all?(&.finished?)
|
2018-10-14 07:02:52 +00:00
|
|
|
@hooks.run_after_all
|
2018-09-15 16:45:47 +00:00
|
|
|
@after_all_hooks_run = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if (parent = @parent)
|
|
|
|
parent.run_after_all_hooks
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_after_each_hooks
|
2018-10-14 07:02:52 +00:00
|
|
|
@hooks.run_after_each
|
2018-09-15 16:45:47 +00:00
|
|
|
if (parent = @parent)
|
|
|
|
parent.run_after_each_hooks
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wrap_around_each_hooks(&block : ->)
|
2018-10-14 07:02:52 +00:00
|
|
|
wrapper = @hooks.wrap_around_each(&block)
|
2018-09-15 16:45:47 +00:00
|
|
|
if (parent = @parent)
|
|
|
|
wrapper = parent.wrap_around_each_hooks(&wrapper)
|
|
|
|
end
|
|
|
|
wrapper
|
|
|
|
end
|
2018-09-24 02:24:28 +00:00
|
|
|
|
|
|
|
def to_s(io)
|
|
|
|
if (parent = @parent)
|
|
|
|
parent.to_s(io)
|
|
|
|
io << ' '
|
|
|
|
end
|
|
|
|
io << what
|
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
end
|