Implement building groups and examples

This commit is contained in:
Michael Miller 2018-09-23 14:34:42 -06:00
parent 5b79e76b44
commit 94e2f54713
4 changed files with 42 additions and 24 deletions

View file

@ -58,7 +58,7 @@ module Spectator
end
protected def build : ExampleGroup
root_group.build({} of Symbol => ValueWrapper)
root_group.build(nil, {} of Symbol => ValueWrapper)
end
end
end

View file

@ -1,7 +1,9 @@
module Spectator
module DSL
class ExampleGroupBuilder
@children = [] of ExampleFactory | ExampleGroupBuilder
alias Child = ExampleFactory | ExampleGroupBuilder
@children = [] of Child
@before_all_hooks = [] of ->
@before_each_hooks = [] of ->
@after_all_hooks = [] of ->
@ -11,7 +13,7 @@ module Spectator
def initialize(@what : String)
end
def add_child(child : ExampleFactory | ExampleGroupBuilder) : Nil
def add_child(child : Child) : Nil
@children << child
end
@ -35,8 +37,13 @@ module Spectator
@around_each_hooks << block
end
def build(locals : Hash(Symbol, ValueWrapper)) : ExampleGroup
raise NotImplementedError
def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup
ExampleGroup.new(@what, parent).tap do |group|
children = @children.map do |child|
child.build(group, locals).as(ExampleGroup::Child)
end
group.children = children
end
end
end
end

View file

@ -8,8 +8,18 @@ module Spectator
super(what)
end
def build(locals : Hash(Symbol, ValueWrapper)) : ExampleGroup
raise NotImplementedError
def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup
ExampleGroup.new(@what, parent).tap do |group|
children = [] of ExampleGroup::Child
@collection.each do |value|
iter_locals = locals.merge({:TODO => value})
iter_children = @children.map do |child|
child.build(group, iter_locals)
end
children.concat(iter_children)
end
group.children = children
end
end
end
end

View file

@ -2,47 +2,48 @@ require "./example"
module Spectator
class ExampleGroup
ROOT = ExampleGroup.new("ROOT")
alias Child = Example | ExampleGroup
getter what : String
getter parent : ExampleGroup?
getter! parent : ExampleGroup
private getter! children : Array(Child)
setter children
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) ->
getter children = [] of ExampleFactory | ExampleGroup
@before_all_hooks_run = false
@after_all_hooks_run = false
def initialize(@what, @parent = nil)
if (parent = @parent)
parent.children << self
end
def initialize(@what, @parent)
end
def examples : Enumerable(ExampleFactory)
@children.select { |child| child.is_a?(ExampleFactory) }.map { |child| child.unsafe_as(ExampleFactory) }
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) }
children.select { |child| child.is_a?(ExampleGroup) }.map { |child| child.unsafe_as(ExampleGroup) }
end
def example_count
@children.sum do |child|
child.is_a?(ExampleFactory) ? 1 : child.example_count
children.sum do |child|
child.is_a?(Example) ? 1 : child.example_count
end
end
def all_examples(locals = {} of Symbol => ValueWrapper)
def all_examples
Array(Example).new(example_count).tap do |array|
@children.each do |child|
if child.is_a?(ExampleFactory)
array << child.build(locals)
children.each do |child|
if child.is_a?(Example)
array << child
else
array.concat(child.all_examples(locals))
array.concat(child.all_examples)
end
end
end