Pass along a proc to generate the array

The "to_a" method is actually dynamically generated.
This commit is contained in:
Michael Miller 2019-08-17 08:00:16 -06:00
parent 43440d37b0
commit 498635d288
2 changed files with 4 additions and 3 deletions

View file

@ -6,7 +6,7 @@ module Spectator::DSL
# This builder creates a container group with groups inside for each item in the collection.
# The hooks are only defined for the container group.
# By doing so, the hooks are defined once, are inherited, and use less memory.
class SampleExampleGroupBuilder(T) < NestedExampleGroupBuilder
class SampleExampleGroupBuilder(C, T) < NestedExampleGroupBuilder
# Creates a new group builder.
# The value for *what* should be the text the user specified for the collection.
# The *collection* is the actual array of items to create examples for.
@ -25,7 +25,7 @@ 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_type : T.class, @name : String, @symbol : Symbol)
def initialize(what : String, @collection_type : C.class, @collection_builder : C -> Array(T), @name : String, @symbol : Symbol)
super(what)
end
@ -35,7 +35,7 @@ module Spectator::DSL
# 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
collection = @collection_builder.call(@collection_type.new(sample_values))
# This creates the container for the sub-groups.
# The hooks are defined here, instead of repeating for each sub-group.

View file

@ -566,6 +566,7 @@ module Spectator::DSL
::Spectator::DSL::Builder.start_sample_group(
{{collection.stringify}}, # String representation of the collection.
Sample%sample, # All elements in the collection.
->(s : Sample%sample) { s.%to_a },
{{name.stringify}}, # Name for the current element.
:%sample # Unique identifier for retrieving elements for the associated collection.
)