2018-10-14 23:10:12 +00:00
|
|
|
require "./example_component"
|
2018-08-19 07:15:32 +00:00
|
|
|
|
|
|
|
module Spectator
|
2018-10-14 23:10:12 +00:00
|
|
|
abstract class ExampleGroup < ExampleComponent
|
|
|
|
include Enumerable(ExampleComponent)
|
|
|
|
include Iterable(ExampleComponent)
|
2018-08-19 07:15:32 +00:00
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def initialize(@hooks : ExampleHooks)
|
|
|
|
@before_all_hooks_run = false
|
|
|
|
@after_all_hooks_run = false
|
|
|
|
end
|
2018-09-23 20:34:42 +00:00
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
private getter! children : Array(ExampleComponent)
|
2018-09-23 20:34:42 +00:00
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def children=(children : Array(ExampleComponent))
|
|
|
|
raise "Attempted to reset example group children" if @children
|
|
|
|
@children = children
|
|
|
|
end
|
2018-09-23 20:34:42 +00:00
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def each
|
|
|
|
children.each do |child|
|
|
|
|
yield child
|
|
|
|
end
|
2018-09-15 16:45:47 +00:00
|
|
|
end
|
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def each : Iterator(ExampleComponent)
|
2018-10-15 00:29:01 +00:00
|
|
|
children.each
|
2018-10-14 23:10:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Remove this method.
|
2018-09-20 05:13:43 +00:00
|
|
|
def example_count
|
2018-09-23 20:34:42 +00:00
|
|
|
children.sum do |child|
|
2018-10-14 23:10:12 +00:00
|
|
|
child.is_a?(Example) ? 1 : child.as(ExampleGroup).example_count
|
2018-09-20 05:13:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
# TODO: Remove this method.
|
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-10-14 23:10:12 +00:00
|
|
|
array.concat(child.as(ExampleGroup).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
|
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def finished? : Bool
|
|
|
|
children.all?(&.finished?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_before_all_hooks : Nil
|
2018-09-15 16:45:47 +00:00
|
|
|
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
|
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def run_before_each_hooks : Nil
|
2018-10-14 07:02:52 +00:00
|
|
|
@hooks.run_before_each
|
2018-09-15 16:45:47 +00:00
|
|
|
end
|
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def run_after_all_hooks : Nil
|
2018-09-15 16:45:47 +00:00
|
|
|
unless @after_all_hooks_run
|
2018-10-14 23:10:12 +00:00
|
|
|
if 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
|
|
|
|
end
|
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def run_after_each_hooks : Nil
|
2018-10-14 07:02:52 +00:00
|
|
|
@hooks.run_after_each
|
2018-09-15 16:45:47 +00:00
|
|
|
end
|
|
|
|
|
2018-10-14 23:10:12 +00:00
|
|
|
def wrap_around_each_hooks(&block : ->) : ->
|
|
|
|
@hooks.wrap_around_each(&block)
|
2018-09-24 02:24:28 +00:00
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
end
|