From 01c4b7871603dd55876d4501a03b83f9591f28d7 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 15 Aug 2019 16:43:25 -0600 Subject: [PATCH 1/9] Move placeholder initializer up a level --- src/spectator.cr | 6 ++++++ src/spectator/dsl/structure_dsl.cr | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/spectator.cr b/src/spectator.cr index c00c080..671fadd 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -41,6 +41,12 @@ module Spectator # Include the DSL for creating groups, example, and more. include ::Spectator::DSL::StructureDSL + # Placeholder initializer. + # This is needed because examples and groups call super in their initializer. + # Those initializers pass the sample values upward through their hierarchy. + def initialize(_sample_values : ::Spectator::Internals::SampleValues) + end + # Pass off the "what" argument and block to `DSL::StructureDSL.describe`. # That method will handle creating a new group for this spec. describe({{what}}) {{block}} diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 8010d30..10e59e6 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -151,12 +151,6 @@ module Spectator::DSL # Sample values are a collection of test values that can be used in examples. # For more information, see `Internals::SampleValues`. module StructureDSL - # Placeholder initializer. - # This is needed because examples and groups call super in their initializer. - # Those initializers pass the sample values upward through their hierarchy. - def initialize(sample_values : Internals::SampleValues) - end - # Creates a new example group to describe a component. # The *what* argument describes "what" is being tested. # Additional example groups and DSL may be nested in the block. From 34b5487e6f88662aa4347f7374a765caf4e9d791 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 15 Aug 2019 17:38:54 -0600 Subject: [PATCH 2/9] Pass along class type instead of collection --- src/spectator/dsl/builder.cr | 4 ++-- src/spectator/dsl/sample_example_group_builder.cr | 5 +++++ src/spectator/dsl/structure_dsl.cr | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index a836559..82a0386 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -41,10 +41,10 @@ module Spectator::DSL # that is, a group defined by the `StructureDSL#sample` macro in the DSL. # A corresponding `#end_group` call must be made # when the group being started is finished. - # See `SampleExampleGroupBuilder#initialize` for the arguments + # See `SampleExampleGroupBuilder#create` for the arguments # as arguments to this method are passed directly to it. def start_sample_group(*args) : Nil - group = SampleExampleGroupBuilder.new(*args) + group = SampleExampleGroupBuilder.create(*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 5ea6468..dbf83ec 100644 --- a/src/spectator/dsl/sample_example_group_builder.cr +++ b/src/spectator/dsl/sample_example_group_builder.cr @@ -29,6 +29,11 @@ module Spectator::DSL 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. diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 10e59e6..176d358 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -558,7 +558,7 @@ module Spectator::DSL # Sample groups require additional configuration. ::Spectator::DSL::Builder.start_sample_group( {{collection.stringify}}, # String representation of the collection. - Sample%sample.new.%to_a, # All elements in the collection. + Sample%sample, # All elements in the collection. {{name.stringify}}, # Name for the current element. :%sample # Unique identifier for retrieving elements for the associated collection. ) @@ -678,7 +678,7 @@ module Spectator::DSL # Sample groups require additional configuration. ::Spectator::DSL::Builder.start_sample_group( {{collection.stringify}}, # String representation of the collection. - Sample%sample.new.%to_a, # All elements in the collection. + Sample%sample, # All elements in the collection. {{name.stringify}}, # Name for the current element. :%sample # Unique identifier for retrieving elements for the associated collection. ) From a59e38155ec5437bde507e9652a0a531e962568d Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 07:51:29 -0600 Subject: [PATCH 3/9] 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 From 43440d37b00fc91121ddf9b63394622e96b926bb Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 07:59:32 -0600 Subject: [PATCH 4/9] Explicit initializer with sample values Not sure why it isn't getting the one from the module. --- src/spectator/dsl/structure_dsl.cr | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 176d358..8433029 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -511,6 +511,13 @@ module Spectator::DSL # Include the parent module. include {{@type.id}} + # Placeholder initializer. + # This is needed because examples and groups call super in their initializer. + # Those initializers pass the sample values upward through their hierarchy. + def initialize(_sample_values : ::Spectator::Internals::SampleValues) + super + end + # Method that returns an array containing the collection. # This method should be called only once. # The framework stores the collection as an array for a couple of reasons. From 498635d2883f5e2177980fd2e6b11537578b2862 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 08:00:16 -0600 Subject: [PATCH 5/9] Pass along a proc to generate the array The "to_a" method is actually dynamically generated. --- src/spectator/dsl/sample_example_group_builder.cr | 6 +++--- src/spectator/dsl/structure_dsl.cr | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/spectator/dsl/sample_example_group_builder.cr b/src/spectator/dsl/sample_example_group_builder.cr index dbffdb4..8dc3cd6 100644 --- a/src/spectator/dsl/sample_example_group_builder.cr +++ b/src/spectator/dsl/sample_example_group_builder.cr @@ -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. diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 8433029..b6cd661 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -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. ) From 7f58c52e43f243d20aafe591068177767e22171e Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 08:05:05 -0600 Subject: [PATCH 6/9] Cleanup --- src/spectator/dsl/sample_example_group_builder.cr | 8 ++++++-- src/spectator/dsl/structure_dsl.cr | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/spectator/dsl/sample_example_group_builder.cr b/src/spectator/dsl/sample_example_group_builder.cr index 8dc3cd6..62c0746 100644 --- a/src/spectator/dsl/sample_example_group_builder.cr +++ b/src/spectator/dsl/sample_example_group_builder.cr @@ -2,6 +2,7 @@ require "./nested_example_group_builder" module Spectator::DSL # Specialized example group builder for "sample" groups. + # The type parameter `C` is the type to instantiate to create the collection. # The type parameter `T` should be the type of each element in the sample collection. # This builder creates a container group with groups inside for each item in the collection. # The hooks are only defined for the container group. @@ -9,7 +10,9 @@ module Spectator::DSL 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. + # The *collection_type* is the type to create that will produce the items. + # The *collection_builder* is a proc that takes an instance of *collection_type* + # and returns an actual array of items to create examples for. # The *name* is the variable name that the user accesses the current collection item with. # # In this code: @@ -25,7 +28,8 @@ 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 : C.class, @collection_builder : C -> Array(T), @name : String, @symbol : Symbol) + def initialize(what : String, @collection_type : C.class, @collection_builder : C -> Array(T), + @name : String, @symbol : Symbol) super(what) end diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index b6cd661..1957d03 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -564,11 +564,11 @@ module Spectator::DSL # Start a new example group. # Sample groups require additional configuration. ::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. + {{collection.stringify}}, # String representation of the collection. + Sample%sample, # Type that can construct the elements. + ->(s : Sample%sample) { s.%to_a }, # Proc to build the array of elements in the collection. + {{name.stringify}}, # Name for the current element. + :%sample # Unique identifier for retrieving elements for the associated collection. ) # Nest the block's content in the module. From c36859b42b40a07a37cb1cbcdc401e658bbb3f7e Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 08:10:26 -0600 Subject: [PATCH 7/9] Correct docs --- src/spectator/dsl/builder.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index e68e11c..a836559 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -41,7 +41,7 @@ module Spectator::DSL # that is, a group defined by the `StructureDSL#sample` macro in the DSL. # A corresponding `#end_group` call must be made # when the group being started is finished. - # See `SampleExampleGroupBuilder#create` for the arguments + # 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) From cede88bf0c59c0683ce08e37404d14019d5587f3 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 08:24:06 -0600 Subject: [PATCH 8/9] Update spec with new arguments --- spec/dsl/sample_example_group_builder_spec.cr | 99 ++++++++++++------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/spec/dsl/sample_example_group_builder_spec.cr b/spec/dsl/sample_example_group_builder_spec.cr index 6236007..6fedcc2 100644 --- a/spec/dsl/sample_example_group_builder_spec.cr +++ b/spec/dsl/sample_example_group_builder_spec.cr @@ -1,10 +1,21 @@ require "../spec_helper" +SAMPLE_VALUES_COLLECTION = %i[foo bar baz] + +struct SampleValueCollection + def initialize(sample_values : ::Spectator::Internals::SampleValues) + end + + def create + SAMPLE_VALUES_COLLECTION + end +end + describe Spectator::DSL::SampleExampleGroupBuilder do describe "#add_child" do it "creates the correct number of children" do - collection = %i[foo bar baz] - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) count = 4 count.times do factory = Spectator::DSL::ExampleFactory.new(PassingExample) @@ -15,13 +26,13 @@ describe Spectator::DSL::SampleExampleGroupBuilder do root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.size.should eq(2 * count * collection.size) + all_children.size.should eq(2 * count * SAMPLE_VALUES_COLLECTION.size) end context "with an ExampleFactory" do it "creates an example for each item in the collection" do - collection = %i[foo bar baz] - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) factory = Spectator::DSL::ExampleFactory.new(PassingExample) builder.add_child(factory) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) @@ -33,8 +44,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do context "with an ExampleGroupBuilder" do it "creates a group for each item in the collection" do - collection = %i[foo bar baz] - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar") builder.add_child(group_builder) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) @@ -48,7 +59,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do describe "#add_before_all_hook" do it "adds a hook" do hook_called = false - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_before_all_hook(->{ hook_called = true }) @@ -60,7 +72,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "attachs the hook to just the top-level group" do call_count = 0 - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_before_all_hook(->{ call_count += 1 }) @@ -72,7 +85,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "supports multiple hooks" do call_count = 0 - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) 5.times do |i| builder.add_before_all_hook(->{ call_count += i + 1 @@ -88,7 +102,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do describe "#add_before_each_hook" do it "adds a hook" do hook_called = false - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_before_each_hook(->{ hook_called = true }) @@ -100,20 +115,21 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "attachs the hook to just the top-level group" do call_count = 0 - collection = %i[foo bar] - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_before_each_hook(->{ call_count += 1 }) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_before_hooks) - call_count.should eq(collection.size) + call_count.should eq(SAMPLE_VALUES_COLLECTION.size) end it "supports multiple hooks" do call_count = 0 - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) 5.times do |i| builder.add_before_each_hook(->{ call_count += i + 1 @@ -129,7 +145,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do describe "#add_after_all_hook" do it "adds a hook" do hook_called = false - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_after_all_hook(->{ hook_called = true }) @@ -141,7 +158,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "attachs the hook to just the top-level group" do call_count = 0 - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_after_all_hook(->{ call_count += 1 }) @@ -153,7 +171,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "supports multiple hooks" do call_count = 0 - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) 5.times do |i| builder.add_after_all_hook(->{ call_count += i + 1 @@ -169,7 +188,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do describe "#add_after_each_hook" do it "adds a hook" do hook_called = false - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_after_each_hook(->{ hook_called = true }) @@ -181,20 +201,21 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "attachs the hook to just the top-level group" do call_count = 0 - collection = %i[foo bar] - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_after_each_hook(->{ call_count += 1 }) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_after_hooks) - call_count.should eq(collection.size) + call_count.should eq(SAMPLE_VALUES_COLLECTION.size) end it "supports multiple hooks" do call_count = 0 - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) 5.times do |i| builder.add_after_each_hook(->{ call_count += i + 1 @@ -210,7 +231,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do describe "#build" do it "passes along the what value" do what = "TEST" - builder = Spectator::DSL::SampleExampleGroupBuilder.new(what, %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new(what, SampleValueCollection, create_proc, "value", :foo) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) group.what.should eq(what) @@ -218,7 +240,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "passes along the parent" do factory = Spectator::DSL::ExampleFactory.new(SpyExample) - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_child(factory) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) @@ -227,7 +250,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do it "passes along the sample values" do factory = Spectator::DSL::ExampleFactory.new(SpyExample) - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) builder.add_child(factory) symbol = :test values = Spectator::Internals::SampleValues.empty.add(symbol, "foo", 12345) @@ -241,7 +265,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do symbol = :foo name = "value" factory = Spectator::DSL::ExampleFactory.new(SpyExample) - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], name, symbol) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, name, symbol) builder.add_child(factory) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) @@ -253,31 +278,32 @@ describe Spectator::DSL::SampleExampleGroupBuilder do end it "creates the correct number of sub-groups" do - collection = %i[foo bar baz] - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) factory = Spectator::DSL::ExampleFactory.new(PassingExample) builder.add_child(factory) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.size.should eq(collection.size) + group.children.size.should eq(SAMPLE_VALUES_COLLECTION.size) end it "passes the correct value to each sub-group" do factory = Spectator::DSL::ExampleFactory.new(SpyExample) symbol = :test count = 3 - collection = %i[foo bar baz] - expected = Array.new(collection.size * count) { |i| collection[i / count] } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", symbol) + expected = Array.new(SAMPLE_VALUES_COLLECTION.size * count) { |i| SAMPLE_VALUES_COLLECTION[i / count] } + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", symbol) count.times { builder.add_child(factory) } root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty) all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.map { |child| child.as(SpyExample).sample_values.get_value(symbol, typeof(collection.first)) }.should eq(expected) + all_children.map { |child| child.as(SpyExample).sample_values.get_value(symbol, typeof(SAMPLE_VALUES_COLLECTION.first)) }.should eq(expected) end it "specifies the parent of the children correctly" do - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) 3.times do factory = Spectator::DSL::ExampleFactory.new(PassingExample) group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz") @@ -299,7 +325,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do end it "specifies the container for the parent of the sub-groups" do - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", %i[foo bar], "value", :foo) + create_proc = ->(s : SampleValueCollection) { s.create } + builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) 3.times do factory = Spectator::DSL::ExampleFactory.new(PassingExample) group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz") From c21ff183f2a95d5844888ed1ea1f3c4d8cb57603 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 08:24:52 -0600 Subject: [PATCH 9/9] Update version to 0.8.1 --- shard.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shard.yml b/shard.yml index 363562c..2687138 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: spectator -version: 0.8.0 +version: 0.8.1 description: | A feature-rich spec testing framework for Crystal with similarities to RSpec.