shard-spectator/src/spectator/example_group.cr

80 lines
1.7 KiB
Crystal
Raw Normal View History

require "./example_component"
2018-08-19 07:15:32 +00:00
module Spectator
abstract class ExampleGroup < ExampleComponent
include Enumerable(ExampleComponent)
include Iterable(ExampleComponent)
2018-08-19 07:15:32 +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
private getter! children : Array(ExampleComponent)
2018-09-23 20:34:42 +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
def each
children.each do |child|
yield child
end
2018-09-15 16:45:47 +00:00
end
def each : Iterator(ExampleComponent)
children.each
end
def example_count : Int
children.sum(&.example_count)
end
# TODO: Remove this method.
2018-09-23 20:34:42 +00:00
def all_examples
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
else
array.concat(child.as(ExampleGroup).all_examples)
end
end
end
2018-09-15 16:45:47 +00:00
end
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
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
def run_after_all_hooks : Nil
2018-09-15 16:45:47 +00:00
unless @after_all_hooks_run
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
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
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