Rework how children are handled

Uses a loose version of the composite pattern.
This is to keep examples in the order they were declared.
This commit is contained in:
Michael Miller 2018-09-19 19:31:50 -06:00
parent d679c356fd
commit 06630e87d3

View file

@ -6,25 +6,34 @@ module Spectator
getter what : String getter what : String
getter parent : ExampleGroup? getter parent : ExampleGroup?
getter examples = [] of Example
getter groups = [] of ExampleGroup
getter before_all_hooks = [] of -> getter before_all_hooks = [] of ->
getter before_each_hooks = [] of -> getter before_each_hooks = [] of ->
getter after_all_hooks = [] of -> getter after_all_hooks = [] of ->
getter after_each_hooks = [] of -> getter after_each_hooks = [] of ->
getter around_each_hooks = [] of Proc(Nil) -> getter around_each_hooks = [] of Proc(Nil) ->
getter children = [] of Example | ExampleGroup
@before_all_hooks_run = false @before_all_hooks_run = false
@after_all_hooks_run = false @after_all_hooks_run = false
def initialize(@what, @parent = nil) def initialize(@what, @parent = nil)
if (parent = @parent) if (parent = @parent)
parent.groups << self parent.children << self
end end
end end
def examples : Enumerable(Example)
@children.select { |child| child.is_a?(Example) }.map { |child| child.unsafe_as(Example) }
end
def groups : Enumerable(ExampleGroup)
@children.select { |child| child.is_a?(ExampleGroup) }.map { |child| child.unsafe_as(ExampleGroup) }
end
def all_examples def all_examples
add_examples Array(Example).new.tap do |array|
add_examples(array)
end
end end
def run_before_all_hooks def run_before_all_hooks
@ -86,12 +95,14 @@ module Spectator
-> { inner.call(wrapper) } -> { inner.call(wrapper) }
end end
protected def add_examples(array = [] of Example) protected def add_examples(array : Array(Example))
array.concat(@examples) @children.each do |child|
groups.each do |group| if child.is_a?(Example)
group.add_examples(array) array << child
else
child.add_examples(array)
end
end end
array
end end
end end
end end