From ef7e0462cbffa9e70e42a3fd4870c39b0f6264e5 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 24 Sep 2018 00:25:15 -0600 Subject: [PATCH] GivenExampleGroupBuilder produces group of groups Now a group is created for each element in the collection. A group to contain the sub-groups is returned. --- .../dsl/given_example_group_builder.cr | 19 +++++++++++-------- src/spectator/example_hooks.cr | 10 ++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/spectator/dsl/given_example_group_builder.cr b/src/spectator/dsl/given_example_group_builder.cr index 27041b9..78e1141 100644 --- a/src/spectator/dsl/given_example_group_builder.cr +++ b/src/spectator/dsl/given_example_group_builder.cr @@ -10,15 +10,18 @@ module Spectator def build(parent : ExampleGroup?, sample_values : Internals::SampleValues) : ExampleGroup ExampleGroup.new(@what, parent, build_hooks).tap do |group| - children = [] of ExampleGroup::Child - @collection.each do |value| - iter_values = sample_values.add(@symbol, @symbol.to_s, value) - iter_children = @children.map do |child| - child.build(group, iter_values) - end - children.concat(iter_children) + group.children = @collection.map do |value| + build_sub_group(group, sample_values, value).as(ExampleGroup::Child) + end + end + end + + private def build_sub_group(parent : ExampleGroup, sample_values : Internals::SampleValues, value : T) : ExampleGroup + sub_values = sample_values.add(@symbol, @symbol.to_s, value) + ExampleGroup.new(value.to_s, parent, ExampleHooks.empty).tap do |group| + group.children = @children.map do |child| + child.build(group, sub_values).as(ExampleGroup::Child) end - group.children = children end end end diff --git a/src/spectator/example_hooks.cr b/src/spectator/example_hooks.cr index 9886315..2745c8e 100644 --- a/src/spectator/example_hooks.cr +++ b/src/spectator/example_hooks.cr @@ -1,5 +1,15 @@ module Spectator class ExampleHooks + def self.empty + new( + [] of ->, + [] of ->, + [] of ->, + [] of ->, + [] of Proc(Nil) -> + ) + end + def initialize( @before_all : Array(->), @before_each : Array(->),