Update spec with new arguments

This commit is contained in:
Michael Miller 2019-08-17 08:24:06 -06:00
parent c36859b42b
commit cede88bf0c

View file

@ -1,10 +1,21 @@
require "../spec_helper" 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 Spectator::DSL::SampleExampleGroupBuilder do
describe "#add_child" do describe "#add_child" do
it "creates the correct number of children" do it "creates the correct number of children" do
collection = %i[foo bar baz] create_proc = ->(s : SampleValueCollection) { s.create }
builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo)
count = 4 count = 4
count.times do count.times do
factory = Spectator::DSL::ExampleFactory.new(PassingExample) 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) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, 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 = 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 end
context "with an ExampleFactory" do context "with an ExampleFactory" do
it "creates an example for each item in the collection" do it "creates an example for each item in the collection" do
collection = %i[foo bar baz] create_proc = ->(s : SampleValueCollection) { s.create }
builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo)
factory = Spectator::DSL::ExampleFactory.new(PassingExample) factory = Spectator::DSL::ExampleFactory.new(PassingExample)
builder.add_child(factory) builder.add_child(factory)
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
@ -33,8 +44,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
context "with an ExampleGroupBuilder" do context "with an ExampleGroupBuilder" do
it "creates a group for each item in the collection" do it "creates a group for each item in the collection" do
collection = %i[foo bar baz] create_proc = ->(s : SampleValueCollection) { s.create }
builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo)
group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar") group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar")
builder.add_child(group_builder) builder.add_child(group_builder)
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) 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 describe "#add_before_all_hook" do
it "adds a hook" do it "adds a hook" do
hook_called = false 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(->{ builder.add_before_all_hook(->{
hook_called = true hook_called = true
}) })
@ -60,7 +72,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
it "attachs the hook to just the top-level group" do it "attachs the hook to just the top-level group" do
call_count = 0 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(->{ builder.add_before_all_hook(->{
call_count += 1 call_count += 1
}) })
@ -72,7 +85,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
it "supports multiple hooks" do it "supports multiple hooks" do
call_count = 0 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| 5.times do |i|
builder.add_before_all_hook(->{ builder.add_before_all_hook(->{
call_count += i + 1 call_count += i + 1
@ -88,7 +102,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
describe "#add_before_each_hook" do describe "#add_before_each_hook" do
it "adds a hook" do it "adds a hook" do
hook_called = false 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(->{ builder.add_before_each_hook(->{
hook_called = true hook_called = true
}) })
@ -100,20 +115,21 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
it "attachs the hook to just the top-level group" do it "attachs the hook to just the top-level group" do
call_count = 0 call_count = 0
collection = %i[foo bar] create_proc = ->(s : SampleValueCollection) { s.create }
builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo)
builder.add_before_each_hook(->{ builder.add_before_each_hook(->{
call_count += 1 call_count += 1
}) })
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_before_hooks) 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 end
it "supports multiple hooks" do it "supports multiple hooks" do
call_count = 0 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| 5.times do |i|
builder.add_before_each_hook(->{ builder.add_before_each_hook(->{
call_count += i + 1 call_count += i + 1
@ -129,7 +145,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
describe "#add_after_all_hook" do describe "#add_after_all_hook" do
it "adds a hook" do it "adds a hook" do
hook_called = false 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(->{ builder.add_after_all_hook(->{
hook_called = true hook_called = true
}) })
@ -141,7 +158,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
it "attachs the hook to just the top-level group" do it "attachs the hook to just the top-level group" do
call_count = 0 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(->{ builder.add_after_all_hook(->{
call_count += 1 call_count += 1
}) })
@ -153,7 +171,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
it "supports multiple hooks" do it "supports multiple hooks" do
call_count = 0 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| 5.times do |i|
builder.add_after_all_hook(->{ builder.add_after_all_hook(->{
call_count += i + 1 call_count += i + 1
@ -169,7 +188,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
describe "#add_after_each_hook" do describe "#add_after_each_hook" do
it "adds a hook" do it "adds a hook" do
hook_called = false 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(->{ builder.add_after_each_hook(->{
hook_called = true hook_called = true
}) })
@ -181,20 +201,21 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
it "attachs the hook to just the top-level group" do it "attachs the hook to just the top-level group" do
call_count = 0 call_count = 0
collection = %i[foo bar] create_proc = ->(s : SampleValueCollection) { s.create }
builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo)
builder.add_after_each_hook(->{ builder.add_after_each_hook(->{
call_count += 1 call_count += 1
}) })
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_after_hooks) 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 end
it "supports multiple hooks" do it "supports multiple hooks" do
call_count = 0 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| 5.times do |i|
builder.add_after_each_hook(->{ builder.add_after_each_hook(->{
call_count += i + 1 call_count += i + 1
@ -210,7 +231,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
describe "#build" do describe "#build" do
it "passes along the what value" do it "passes along the what value" do
what = "TEST" 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) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.what.should eq(what) group.what.should eq(what)
@ -218,7 +240,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
it "passes along the parent" do it "passes along the parent" do
factory = Spectator::DSL::ExampleFactory.new(SpyExample) 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) builder.add_child(factory)
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, 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 it "passes along the sample values" do
factory = Spectator::DSL::ExampleFactory.new(SpyExample) 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) builder.add_child(factory)
symbol = :test symbol = :test
values = Spectator::Internals::SampleValues.empty.add(symbol, "foo", 12345) values = Spectator::Internals::SampleValues.empty.add(symbol, "foo", 12345)
@ -241,7 +265,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
symbol = :foo symbol = :foo
name = "value" name = "value"
factory = Spectator::DSL::ExampleFactory.new(SpyExample) 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) builder.add_child(factory)
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty) group = builder.build(root, Spectator::Internals::SampleValues.empty)
@ -253,31 +278,32 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
end end
it "creates the correct number of sub-groups" do it "creates the correct number of sub-groups" do
collection = %i[foo bar baz] create_proc = ->(s : SampleValueCollection) { s.create }
builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", :foo) builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo)
factory = Spectator::DSL::ExampleFactory.new(PassingExample) factory = Spectator::DSL::ExampleFactory.new(PassingExample)
builder.add_child(factory) builder.add_child(factory)
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, 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 end
it "passes the correct value to each sub-group" do it "passes the correct value to each sub-group" do
factory = Spectator::DSL::ExampleFactory.new(SpyExample) factory = Spectator::DSL::ExampleFactory.new(SpyExample)
symbol = :test symbol = :test
count = 3 count = 3
collection = %i[foo bar baz] expected = Array.new(SAMPLE_VALUES_COLLECTION.size * count) { |i| SAMPLE_VALUES_COLLECTION[i / count] }
expected = Array.new(collection.size * count) { |i| collection[i / count] } create_proc = ->(s : SampleValueCollection) { s.create }
builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", collection, "value", symbol) builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", symbol)
count.times { builder.add_child(factory) } count.times { builder.add_child(factory) }
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, 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 = 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 end
it "specifies the parent of the children correctly" do 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 3.times do
factory = Spectator::DSL::ExampleFactory.new(PassingExample) factory = Spectator::DSL::ExampleFactory.new(PassingExample)
group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz") group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz")
@ -299,7 +325,8 @@ describe Spectator::DSL::SampleExampleGroupBuilder do
end end
it "specifies the container for the parent of the sub-groups" do 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 3.times do
factory = Spectator::DSL::ExampleFactory.new(PassingExample) factory = Spectator::DSL::ExampleFactory.new(PassingExample)
group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz") group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz")