Defer collection evaluation until build

This commit is contained in:
Michael Miller 2019-08-17 07:51:29 -06:00
parent 34b5487e6f
commit a59e38155e
2 changed files with 5 additions and 8 deletions

View file

@ -44,7 +44,7 @@ module Spectator::DSL
# See `SampleExampleGroupBuilder#create` for the arguments # See `SampleExampleGroupBuilder#create` for the arguments
# as arguments to this method are passed directly to it. # as arguments to this method are passed directly to it.
def start_sample_group(*args) : Nil def start_sample_group(*args) : Nil
group = SampleExampleGroupBuilder.create(*args) group = SampleExampleGroupBuilder.new(*args)
push_group(group) push_group(group)
end end

View file

@ -25,26 +25,23 @@ module Spectator::DSL
# The *symbol* is passed along to the sample values # The *symbol* is passed along to the sample values
# so that the example code can retrieve the current item from the collection. # so that the example code can retrieve the current item from the collection.
# The symbol should be unique. # 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) super(what)
end 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. # Builds the example group.
# A new `NestedExampleGroup` will be returned # A new `NestedExampleGroup` will be returned
# which can have instances of `Example` and `ExampleGroup` nested in it. # which can have instances of `Example` and `ExampleGroup` nested in it.
# The *parent* should be the group that contains this group. # 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. # 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 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. # This creates the container for the sub-groups.
# The hooks are defined here, instead of repeating for each sub-group. # The hooks are defined here, instead of repeating for each sub-group.
NestedExampleGroup.new(@what, parent, hooks, conditions).tap do |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. # 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. # Create a sub-group for each item in the collection.
build_sub_group(group, sample_values, value).as(ExampleComponent) build_sub_group(group, sample_values, value).as(ExampleComponent)
end end