diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index dbf6d60..309ba68 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -35,7 +35,10 @@ module Spectator end class Context%sample < {{@type.id}} - ::Spectator::SpecBuilder.start_sample_group({{what.stringify}}) + ::Spectator::SpecBuilder.start_sample_group({{what.stringify}}, :%sample) do |values| + sample = Sample%sample.new(values) + sample.%collection.to_a + end def {{block_arg}} 1 diff --git a/src/spectator/spec_builder.cr b/src/spectator/spec_builder.cr index a5efc17..7b0c93c 100644 --- a/src/spectator/spec_builder.cr +++ b/src/spectator/spec_builder.cr @@ -26,8 +26,8 @@ module Spectator # when the group being started is finished. # See `SampleExampleGroupBuilder#initialize` for the arguments # as arguments to this method are passed directly to it. - def start_sample_group(*args) : Nil - group = SampleExampleGroupBuilder.new(*args) + def start_sample_group(*args, &block : TestValues -> Array(T)) : Nil forall T + group = SampleExampleGroupBuilder(T).new(*args, block) @@stack.push(group) end diff --git a/src/spectator/spec_builder/sample_example_group_builder.cr b/src/spectator/spec_builder/sample_example_group_builder.cr index a7b3463..aab9a03 100644 --- a/src/spectator/spec_builder/sample_example_group_builder.cr +++ b/src/spectator/spec_builder/sample_example_group_builder.cr @@ -1,15 +1,17 @@ require "./nested_example_group_builder" module Spectator::SpecBuilder - class SampleExampleGroupBuilder < NestedExampleGroupBuilder - def initialize(@what : String | Symbol) - @id = :TODO + class SampleExampleGroupBuilder(T) < NestedExampleGroupBuilder + def initialize(what : String | Symbol, @id : Symbol, @collection_builder : TestValues -> Array(T)) + super(what) end def build(parent_group) - context = TestContext.new(parent_group.context, build_hooks, parent_group.context.values) + values = parent_group.context.values + collection = @collection_builder.call(values) + context = TestContext.new(parent_group.context, build_hooks, values) NestedExampleGroup.new(@what, parent_group, context).tap do |group| - group.children = [:TODO].map do |element| + group.children = collection.map do |element| build_sub_group(group, element).as(ExampleComponent) end end @@ -18,7 +20,7 @@ module Spectator::SpecBuilder private def build_sub_group(parent_group, element) values = parent_group.context.values.add(@id, @what.to_s, element) context = TestContext.new(parent_group.context, ExampleHooks.empty, values) - NestedExampleGroup.new("TODO", parent_group, context).tap do |group| + NestedExampleGroup.new(element.inspect, parent_group, context).tap do |group| group.children = children.map do |child| child.build(group).as(ExampleComponent) end