From cede88bf0c59c0683ce08e37404d14019d5587f3 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 17 Aug 2019 08:24:06 -0600 Subject: [PATCH] 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")