shard-spectator/src/spectator/example_group.cr

90 lines
1.8 KiB
Crystal
Raw Normal View History

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
def example_count
2018-09-23 20:34:42 +00:00
children.sum do |child|
child.is_a?(Example) ? 1 : child.example_count
end
end
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
2018-09-23 20:34:42 +00:00
array.concat(child.all_examples)
end
end
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