From a59e38155ec5437bde507e9652a0a531e962568d Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 07:51:29 -0600 Subject: [PATCH] Defer collection evaluation until build --- src/spectator/dsl/builder.cr | 2 +- src/spectator/dsl/sample_example_group_builder.cr | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index 82a0386..e68e11c 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -44,7 +44,7 @@ module Spectator::DSL # See `SampleExampleGroupBuilder#create` for the arguments # as arguments to this method are passed directly to it. def start_sample_group(*args) : Nil - group = SampleExampleGroupBuilder.create(*args) + group = SampleExampleGroupBuilder.new(*args) push_group(group) end diff --git a/src/spectator/dsl/sample_example_group_builder.cr b/src/spectator/dsl/sample_example_group_builder.cr index dbf83ec..dbffdb4 100644 --- a/src/spectator/dsl/sample_example_group_builder.cr +++ b/src/spectator/dsl/sample_example_group_builder.cr @@ -25,26 +25,23 @@ module Spectator::DSL # The *symbol* is passed along to the sample values # so that the example code can retrieve the current item from the collection. # The symbol should be unique. - def initialize(what : String, @collection : Array(T), @name : String, @symbol : Symbol) + def initialize(what : String, @collection_type : T.class, @name : String, @symbol : Symbol) super(what) end - def self.create(what, collection_type : C.class, name, symbol) forall C - collection = collection_type.new(sample_values).to_a - SampleExampleGroupBuilder.new(what, collection, name, symbol) - end - # Builds the example group. # A new `NestedExampleGroup` will be returned # which can have instances of `Example` and `ExampleGroup` nested in it. # The *parent* should be the group that contains this group. # The *sample_values* will be given to all of the examples (and groups) nested in this group. def build(parent : ExampleGroup, sample_values : Internals::SampleValues) : NestedExampleGroup + collection = @collection_type.new(sample_values).to_a + # This creates the container for the sub-groups. # The hooks are defined here, instead of repeating for each sub-group. NestedExampleGroup.new(@what, parent, hooks, conditions).tap do |group| # Set the container group's children to be sub-groups for each item in the collection. - group.children = @collection.map do |value| + group.children = collection.map do |value| # Create a sub-group for each item in the collection. build_sub_group(group, sample_values, value).as(ExampleComponent) end