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. 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") 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/sample_example_group_builder.cr b/src/spectator/dsl/sample_example_group_builder.cr index 5ea6468..62c0746 100644 --- a/src/spectator/dsl/sample_example_group_builder.cr +++ b/src/spectator/dsl/sample_example_group_builder.cr @@ -2,14 +2,17 @@ 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. # 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. + # 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 : 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 @@ -35,11 +39,13 @@ 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_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. 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 diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 8010d30..1957d03 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. @@ -517,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. @@ -563,10 +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.new.%to_a, # All elements in the collection. - {{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. @@ -684,7 +686,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. )