2018-08-19 07:15:32 +00:00
|
|
|
require "./example"
|
|
|
|
|
|
|
|
module Spectator
|
|
|
|
class ExampleGroup
|
2018-09-15 16:45:47 +00:00
|
|
|
ROOT = ExampleGroup.new("ROOT")
|
2018-08-19 07:15:32 +00:00
|
|
|
|
2018-09-15 16:45:47 +00:00
|
|
|
getter what : String
|
|
|
|
getter parent : ExampleGroup?
|
|
|
|
getter before_all_hooks = [] of ->
|
|
|
|
getter before_each_hooks = [] of ->
|
|
|
|
getter after_all_hooks = [] of ->
|
|
|
|
getter after_each_hooks = [] of ->
|
|
|
|
getter around_each_hooks = [] of Proc(Nil) ->
|
2018-09-20 03:58:32 +00:00
|
|
|
getter children = [] of ExampleFactory | ExampleGroup
|
2018-09-15 16:45:47 +00:00
|
|
|
|
|
|
|
@before_all_hooks_run = false
|
|
|
|
@after_all_hooks_run = false
|
|
|
|
|
|
|
|
def initialize(@what, @parent = nil)
|
|
|
|
if (parent = @parent)
|
2018-09-20 01:31:50 +00:00
|
|
|
parent.children << self
|
2018-09-15 16:45:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-20 03:58:32 +00:00
|
|
|
def examples : Enumerable(ExampleFactory)
|
|
|
|
@children.select { |child| child.is_a?(ExampleFactory) }.map { |child| child.unsafe_as(ExampleFactory) }
|
2018-09-20 01:31:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def groups : Enumerable(ExampleGroup)
|
|
|
|
@children.select { |child| child.is_a?(ExampleGroup) }.map { |child| child.unsafe_as(ExampleGroup) }
|
|
|
|
end
|
|
|
|
|
2018-09-20 05:13:43 +00:00
|
|
|
def example_count
|
|
|
|
@children.sum do |child|
|
|
|
|
child.is_a?(ExampleFactory) ? 1 : child.example_count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-21 20:03:09 +00:00
|
|
|
def all_examples(locals = {} of Symbol => ValueWrapper)
|
2018-09-20 05:13:43 +00:00
|
|
|
Array(Example).new(example_count).tap do |array|
|
|
|
|
@children.each do |child|
|
|
|
|
if child.is_a?(ExampleFactory)
|
2018-09-21 20:03:09 +00:00
|
|
|
array << child.build(locals)
|
2018-09-20 05:13:43 +00:00
|
|
|
else
|
2018-09-21 20:26:09 +00:00
|
|
|
array.concat(child.all_examples(locals))
|
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
|
|
|
|
@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
|
|
|
|
if all_examples.all?(&.finished?)
|
|
|
|
@after_all_hooks.each do |hook|
|
|
|
|
hook.call
|
|
|
|
end
|
|
|
|
@after_all_hooks_run = true
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
def wrap_around_each_hooks(&block : ->)
|
|
|
|
wrapper = block
|
|
|
|
@around_each_hooks.reverse_each do |hook|
|
|
|
|
wrapper = wrap_proc(hook, wrapper)
|
|
|
|
end
|
|
|
|
if (parent = @parent)
|
|
|
|
wrapper = parent.wrap_around_each_hooks(&wrapper)
|
|
|
|
end
|
|
|
|
wrapper
|
|
|
|
end
|
|
|
|
|
|
|
|
private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->)
|
|
|
|
-> { inner.call(wrapper) }
|
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
end
|