diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index 954bfc7..d1d4886 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -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 diff --git a/src/spectator/dsl/example_group_builder.cr b/src/spectator/dsl/example_group_builder.cr index fda8402..f3814ce 100644 --- a/src/spectator/dsl/example_group_builder.cr +++ b/src/spectator/dsl/example_group_builder.cr @@ -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 diff --git a/src/spectator/dsl/given_example_group_builder.cr b/src/spectator/dsl/given_example_group_builder.cr index 5c6c4f9..7b53532 100644 --- a/src/spectator/dsl/given_example_group_builder.cr +++ b/src/spectator/dsl/given_example_group_builder.cr @@ -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 diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index 533f47c..154213d 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -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