Adjust tests to use new hook methods

This commit is contained in:
Michael Miller 2018-12-05 20:21:19 -07:00
parent bd4d020d0f
commit 712aa99fed
5 changed files with 350 additions and 326 deletions

View file

@ -54,7 +54,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_all_hooks
group.run_before_hooks
hook_called.should eq(true)
end
@ -66,7 +66,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
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_all_hooks)
group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_before_hooks)
call_count.should eq(1)
end
@ -80,7 +80,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_all_hooks
group.run_before_hooks
call_count.should eq(15)
end
end
@ -94,7 +94,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_each_hooks
group.run_before_hooks
hook_called.should eq(true)
end
@ -107,7 +107,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
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_each_hooks)
group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_before_hooks)
call_count.should eq(collection.size)
end
@ -121,7 +121,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_each_hooks
group.run_before_hooks
call_count.should eq(15)
end
end
@ -135,7 +135,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_all_hooks
group.run_after_hooks
hook_called.should eq(true)
end
@ -147,7 +147,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
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_all_hooks)
group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_after_hooks)
call_count.should eq(1)
end
@ -161,7 +161,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_all_hooks
group.run_after_hooks
call_count.should eq(15)
end
end
@ -175,7 +175,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_each_hooks
group.run_after_hooks
hook_called.should eq(true)
end
@ -188,7 +188,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
})
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_each_hooks)
group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_after_hooks)
call_count.should eq(collection.size)
end
@ -202,7 +202,7 @@ describe Spectator::DSL::GivenExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_each_hooks
group.run_after_hooks
call_count.should eq(15)
end
end

View file

@ -47,7 +47,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_all_hooks
group.run_before_hooks
hook_called.should eq(true)
end
@ -61,7 +61,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_all_hooks
group.run_before_hooks
call_count.should eq(15)
end
end
@ -75,7 +75,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_each_hooks
group.run_before_hooks
hook_called.should eq(true)
end
@ -89,7 +89,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_before_each_hooks
group.run_before_hooks
call_count.should eq(15)
end
end
@ -103,7 +103,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_all_hooks
group.run_after_hooks
hook_called.should eq(true)
end
@ -117,7 +117,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_all_hooks
group.run_after_hooks
call_count.should eq(15)
end
end
@ -131,7 +131,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
})
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_each_hooks
group.run_after_hooks
hook_called.should eq(true)
end
@ -145,7 +145,7 @@ describe Spectator::DSL::NestedExampleGroupBuilder do
end
root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty)
group = builder.build(root, Spectator::Internals::SampleValues.empty)
group.run_after_each_hooks
group.run_after_hooks
call_count.should eq(15)
end
end

View file

@ -43,7 +43,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
hook_called = true
})
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_before_all_hooks
group.run_before_hooks
hook_called.should eq(true)
end
@ -56,7 +56,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
})
end
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_before_all_hooks
group.run_before_hooks
call_count.should eq(15)
end
end
@ -69,7 +69,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
hook_called = true
})
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_before_each_hooks
group.run_before_hooks
hook_called.should eq(true)
end
@ -82,7 +82,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
})
end
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_before_each_hooks
group.run_before_hooks
call_count.should eq(15)
end
end
@ -95,7 +95,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
hook_called = true
})
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_after_all_hooks
group.run_after_hooks
hook_called.should eq(true)
end
@ -108,7 +108,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
})
end
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_after_all_hooks
group.run_after_hooks
call_count.should eq(15)
end
end
@ -121,7 +121,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
hook_called = true
})
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_after_each_hooks
group.run_after_hooks
hook_called.should eq(true)
end
@ -134,7 +134,7 @@ describe Spectator::DSL::RootExampleGroupBuilder do
})
end
group = builder.build(Spectator::Internals::SampleValues.empty)
group.run_after_each_hooks
group.run_after_hooks
call_count.should eq(15)
end
end

View file

@ -89,16 +89,24 @@ describe Spectator::NestedExampleGroup do
end
end
describe "#run_before_all_hooks" do
it "runs a single hook" do
describe "#run_before_hooks" do
it "runs a single before_all hook" do
called = false
hooks = new_hooks(before_all: ->{ called = true; nil })
group = new_nested_group(hooks)
group.run_before_all_hooks
group.run_before_hooks
called.should be_true
end
it "runs multiple hooks" do
it "runs a single before_each hook" do
called = false
hooks = new_hooks(before_each: ->{ called = true; nil })
group = new_nested_group(hooks)
group.run_before_hooks
called.should be_true
end
it "runs multiple before_all hooks" do
call_count = 0
hooks = new_hooks(before_all: [
->{ call_count += 1; nil },
@ -106,7 +114,19 @@ describe Spectator::NestedExampleGroup do
->{ call_count += 3; nil },
])
group = new_nested_group(hooks)
group.run_before_all_hooks
group.run_before_hooks
call_count.should eq(6)
end
it "runs multiple before_each hooks" do
call_count = 0
hooks = new_hooks(before_each: [
->{ call_count += 1; nil },
->{ call_count += 2; nil },
->{ call_count += 3; nil },
])
group = new_nested_group(hooks)
group.run_before_hooks
call_count.should eq(6)
end
@ -116,114 +136,85 @@ describe Spectator::NestedExampleGroup do
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
],
before_each: [
->{ calls << :d; nil },
->{ calls << :e; nil },
->{ calls << :f; nil },
])
group = new_nested_group(hooks)
group.run_before_all_hooks
calls.should eq(%i[a b c])
group.run_before_hooks
calls.should eq(%i[a b c d e f])
end
it "runs the parent hooks" do
it "runs the parent before_all hooks" do
called = false
hooks = new_hooks(before_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = new_nested_group(parent: root)
group.run_before_all_hooks
group.run_before_hooks
called.should be_true
end
it "runs the parent hooks first" do
it "runs the parent before_each hooks" do
called = false
hooks = new_hooks(before_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = new_nested_group(parent: root)
group.run_before_hooks
called.should be_true
end
it "runs the parent before_all hooks first" do
calls = [] of Symbol
root_hooks = new_hooks(before_all: ->{ calls << :a; nil })
group_hooks = new_hooks(before_all: ->{ calls << :b; nil })
root = Spectator::RootExampleGroup.new(root_hooks)
group = new_nested_group(group_hooks, root)
group.run_before_all_hooks
group.run_before_hooks
calls.should eq(%i[a b])
end
it "runs the hooks once" do
call_count = 0
hooks = new_hooks(before_all: ->{ call_count += 1; nil })
group = new_nested_group(hooks)
2.times { group.run_before_all_hooks }
call_count.should eq(1)
end
end
describe "#run_before_each_hooks" do
it "runs a single hook" do
called = false
hooks = new_hooks(before_each: ->{ called = true; nil })
group = new_nested_group(hooks)
group.run_before_each_hooks
called.should be_true
end
it "runs multiple hooks" do
call_count = 0
hooks = new_hooks(before_each: [
->{ call_count += 1; nil },
->{ call_count += 2; nil },
->{ call_count += 3; nil },
])
group = new_nested_group(hooks)
group.run_before_each_hooks
call_count.should eq(6)
end
it "runs hooks in the correct order" do
calls = [] of Symbol
hooks = new_hooks(before_each: [
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
])
group = new_nested_group(hooks)
group.run_before_each_hooks
calls.should eq(%i[a b c])
end
it "runs the parent hooks" do
called = false
hooks = new_hooks(before_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = new_nested_group(parent: root)
group.run_before_each_hooks
called.should be_true
end
it "runs the parent hooks first" do
it "runs the parent before_each hooks first" do
calls = [] of Symbol
root_hooks = new_hooks(before_each: ->{ calls << :a; nil })
group_hooks = new_hooks(before_each: ->{ calls << :b; nil })
root = Spectator::RootExampleGroup.new(root_hooks)
group = new_nested_group(group_hooks, root)
group.run_before_each_hooks
group.run_before_hooks
calls.should eq(%i[a b])
end
it "runs the hooks multiple times" do
it "runs the before_all hooks once" do
call_count = 0
hooks = new_hooks(before_all: ->{ call_count += 1; nil })
group = new_nested_group(hooks)
2.times { group.run_before_hooks }
call_count.should eq(1)
end
it "runs the before_each hooks multiple times" do
call_count = 0
hooks = new_hooks(before_each: ->{ call_count += 1; nil })
group = new_nested_group(hooks)
2.times { group.run_before_each_hooks }
2.times { group.run_before_hooks }
call_count.should eq(2)
end
end
describe "#run_after_all_hooks" do
describe "#run_after_hooks" do
# No children are used for most of these examples.
# That's because `[].all?` is always true.
# Which means that all examples are considered finished, since there are none.
it "runs a single hook" do
it "runs a single after_all hook" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
group = new_nested_group(hooks)
group.run_after_all_hooks
group.run_after_hooks
called.should be_true
end
it "runs multiple hooks" do
it "runs multiple after_all hooks" do
call_count = 0
hooks = new_hooks(after_all: [
->{ call_count += 1; nil },
@ -231,169 +222,196 @@ describe Spectator::NestedExampleGroup do
->{ call_count += 3; nil },
])
group = new_nested_group(hooks)
group.run_after_all_hooks
group.run_after_hooks
call_count.should eq(6)
end
it "runs hooks in the correct order" do
calls = [] of Symbol
hooks = new_hooks(after_all: [
hooks = new_hooks(after_each: [
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
],
after_all: [
->{ calls << :d; nil },
->{ calls << :e; nil },
->{ calls << :f; nil },
])
group = new_nested_group(hooks)
group.run_after_all_hooks
calls.should eq(%i[a b c])
group.run_after_hooks
calls.should eq(%i[a b c d e f])
end
it "runs the parent hooks" do
it "runs the parent after_all hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = new_nested_group(parent: root)
group.run_after_all_hooks
group.run_after_hooks
called.should be_true
end
it "runs the parent hooks last" do
it "runs the parent after_each hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = new_nested_group(parent: root)
group.run_after_hooks
called.should be_true
end
it "runs the parent after_all hooks last" do
calls = [] of Symbol
root_hooks = new_hooks(after_all: ->{ calls << :a; nil })
group_hooks = new_hooks(after_all: ->{ calls << :b; nil })
root = Spectator::RootExampleGroup.new(root_hooks)
group = new_nested_group(group_hooks, root)
group.run_after_all_hooks
group.run_after_hooks
calls.should eq(%i[b a])
end
it "runs the hooks once" do
call_count = 0
hooks = new_hooks(after_all: ->{ call_count += 1; nil })
group = new_nested_group(hooks)
2.times { group.run_after_all_hooks }
call_count.should eq(1)
end
context "with no examples finished" do
it "doesn't run the hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty)
group = Spectator::NestedExampleGroup.new("what", root, hooks)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_all_hooks
called.should be_false
end
it "doesn't run the parent hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_all_hooks
called.should be_false
end
end
context "with some examples finished" do
it "doesn't run the hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty)
group = Spectator::NestedExampleGroup.new("what", root, hooks)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do |i|
PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example|
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_all_hooks
called.should be_false
end
it "doesn't run the parent hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do |i|
PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example|
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_all_hooks
called.should be_false
end
end
end
describe "#run_after_each_hooks" do
it "runs a single hook" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
group = new_nested_group(hooks)
group.run_after_each_hooks
called.should be_true
end
it "runs multiple hooks" do
call_count = 0
hooks = new_hooks(after_each: [
->{ call_count += 1; nil },
->{ call_count += 2; nil },
->{ call_count += 3; nil },
])
group = new_nested_group(hooks)
group.run_after_each_hooks
call_count.should eq(6)
end
it "runs hooks in the correct order" do
calls = [] of Symbol
hooks = new_hooks(after_each: [
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
])
group = new_nested_group(hooks)
group.run_after_each_hooks
calls.should eq(%i[a b c])
end
it "runs the parent hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = new_nested_group(parent: root)
group.run_after_each_hooks
called.should be_true
end
it "runs the parent hooks last" do
it "runs the parent after_each hooks last" do
calls = [] of Symbol
root_hooks = new_hooks(after_each: ->{ calls << :a; nil })
group_hooks = new_hooks(after_each: ->{ calls << :b; nil })
root = Spectator::RootExampleGroup.new(root_hooks)
group = new_nested_group(group_hooks, root)
group.run_after_each_hooks
group.run_after_hooks
calls.should eq(%i[b a])
end
it "runs the hooks multiple times" do
it "runs the after_all hooks once" do
call_count = 0
hooks = new_hooks(after_all: ->{ call_count += 1; nil })
group = new_nested_group(hooks)
2.times { group.run_after_hooks }
call_count.should eq(1)
end
it "runs the after_each hooks multiple times" do
call_count = 0
hooks = new_hooks(after_each: ->{ call_count += 1; nil })
group = new_nested_group(hooks)
2.times { group.run_after_each_hooks }
2.times { group.run_after_hooks }
call_count.should eq(2)
end
context "with no examples finished" do
it "doesn't run the after_all hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty)
group = Spectator::NestedExampleGroup.new("what", root, hooks)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_hooks
called.should be_false
end
it "runs the after_each hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty)
group = Spectator::NestedExampleGroup.new("what", root, hooks)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_hooks
called.should be_true
end
it "doesn't run the parent after_all hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_hooks
called.should be_false
end
it "runs the parent after_each hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_hooks
called.should be_true
end
end
context "with some examples finished" do
it "doesn't run the after_all hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty)
group = Spectator::NestedExampleGroup.new("what", root, hooks)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do |i|
PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example|
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_hooks
called.should be_false
end
it "runs the after_each hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty)
group = Spectator::NestedExampleGroup.new("what", root, hooks)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do |i|
PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example|
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_hooks
called.should be_true
end
it "doesn't run the parent after_all hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do |i|
PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example|
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_hooks
called.should be_false
end
it "runs the parent after_each hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
root = Spectator::RootExampleGroup.new(hooks)
group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty)
root.children = [group.as(Spectator::ExampleComponent)]
group.children = Array(Spectator::ExampleComponent).new(5) do |i|
PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example|
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_hooks
called.should be_true
end
end
end
describe "#wrap_around_each_hooks" do

View file

@ -64,58 +64,36 @@ def complex_root_group
end
describe Spectator::RootExampleGroup do
describe "#run_before_all_hooks" do
it "runs a single hook" do
describe "#run_before_hooks" do
it "runs a before_all hook" do
called = false
hooks = new_hooks(before_all: ->{ called = true; nil })
group = new_root_group(hooks)
group.run_before_all_hooks
group.run_before_hooks
called.should be_true
end
it "runs multiple hooks" do
call_count = 0
hooks = new_hooks(before_all: [
->{ call_count += 1; nil },
->{ call_count += 2; nil },
->{ call_count += 3; nil },
])
group = new_root_group(hooks)
group.run_before_all_hooks
call_count.should eq(6)
end
it "runs hooks in the correct order" do
calls = [] of Symbol
hooks = new_hooks(before_all: [
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
])
group = new_root_group(hooks)
group.run_before_all_hooks
calls.should eq(%i[a b c])
end
it "runs the hooks once" do
call_count = 0
hooks = new_hooks(before_all: ->{ call_count += 1; nil })
group = new_root_group(hooks)
2.times { group.run_before_all_hooks }
call_count.should eq(1)
end
end
describe "#run_before_each_hooks" do
it "runs a single hook" do
it "runs a before_each hook" do
called = false
hooks = new_hooks(before_each: ->{ called = true; nil })
group = new_root_group(hooks)
group.run_before_each_hooks
group.run_before_hooks
called.should be_true
end
it "runs multiple hooks" do
it "runs multiple before_all hooks" do
call_count = 0
hooks = new_hooks(before_all: [
->{ call_count += 1; nil },
->{ call_count += 2; nil },
->{ call_count += 3; nil },
])
group = new_root_group(hooks)
group.run_before_hooks
call_count.should eq(6)
end
it "runs multiple before_each hooks" do
call_count = 0
hooks = new_hooks(before_each: [
->{ call_count += 1; nil },
@ -123,44 +101,65 @@ describe Spectator::RootExampleGroup do
->{ call_count += 3; nil },
])
group = new_root_group(hooks)
group.run_before_each_hooks
group.run_before_hooks
call_count.should eq(6)
end
it "runs hooks in the correct order" do
calls = [] of Symbol
hooks = new_hooks(before_each: [
hooks = new_hooks(before_all: [
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
],
before_each: [
->{ calls << :d; nil },
->{ calls << :e; nil },
->{ calls << :f; nil },
])
group = new_root_group(hooks)
group.run_before_each_hooks
calls.should eq(%i[a b c])
group.run_before_hooks
calls.should eq(%i[a b c d e f])
end
it "runs the hooks multiple times" do
it "runs the before_all hooks once" do
call_count = 0
hooks = new_hooks(before_all: ->{ call_count += 1; nil })
group = new_root_group(hooks)
2.times { group.run_before_hooks }
call_count.should eq(1)
end
it "runs the before_each hooks multiple times" do
call_count = 0
hooks = new_hooks(before_each: ->{ call_count += 1; nil })
group = new_root_group(hooks)
2.times { group.run_before_each_hooks }
2.times { group.run_before_hooks }
call_count.should eq(2)
end
end
describe "#run_after_all_hooks" do
describe "#run_after_hooks" do
# No children are used for most of these examples.
# That's because `[].all?` is always true.
# Which means that all examples are considered finished, since there are none.
it "runs a single hook" do
it "runs a single after_all hook" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
group = new_root_group(hooks)
group.run_after_all_hooks
group.run_after_hooks
called.should be_true
end
it "runs multiple hooks" do
it "runs a single after_each hook" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
group = new_root_group(hooks)
group.run_after_hooks
called.should be_true
end
it "runs multiple after_all hooks" do
call_count = 0
hooks = new_hooks(after_all: [
->{ call_count += 1; nil },
@ -168,45 +167,81 @@ describe Spectator::RootExampleGroup do
->{ call_count += 3; nil },
])
group = new_root_group(hooks)
group.run_after_all_hooks
group.run_after_hooks
call_count.should eq(6)
end
it "runs multiple after_each hooks" do
call_count = 0
hooks = new_hooks(after_all: [
->{ call_count += 1; nil },
->{ call_count += 2; nil },
->{ call_count += 3; nil },
])
group = new_root_group(hooks)
group.run_after_hooks
call_count.should eq(6)
end
it "runs hooks in the correct order" do
calls = [] of Symbol
hooks = new_hooks(after_all: [
hooks = new_hooks(after_each: [
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
],
after_all: [
->{ calls << :d; nil },
->{ calls << :e; nil },
->{ calls << :f; nil },
])
group = new_root_group(hooks)
group.run_after_all_hooks
calls.should eq(%i[a b c])
group.run_after_hooks
calls.should eq(%i[a b c d e f])
end
it "runs the hooks once" do
it "runs the after_all hooks once" do
call_count = 0
hooks = new_hooks(after_all: ->{ call_count += 1; nil })
group = new_root_group(hooks)
2.times { group.run_after_all_hooks }
2.times { group.run_after_hooks }
call_count.should eq(1)
end
it "runs the after_each hooks multiple times" do
call_count = 0
hooks = new_hooks(after_each: ->{ call_count += 1; nil })
group = new_root_group(hooks)
2.times { group.run_after_hooks }
call_count.should eq(2)
end
context "with no examples finished" do
it "doesn't run the hooks" do
it "doesn't run the after_all hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
group = Spectator::RootExampleGroup.new(hooks)
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_all_hooks
group.run_after_hooks
called.should be_false
end
it "runs the after_each hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
group = Spectator::RootExampleGroup.new(hooks)
group.children = Array(Spectator::ExampleComponent).new(5) do
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
end
group.run_after_hooks
called.should be_true
end
end
context "with some examples finished" do
it "doesn't run the hooks" do
it "doesn't run the after_all hooks" do
called = false
hooks = new_hooks(after_all: ->{ called = true; nil })
group = Spectator::RootExampleGroup.new(hooks)
@ -215,51 +250,22 @@ describe Spectator::RootExampleGroup do
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_all_hooks
group.run_after_hooks
called.should be_false
end
end
end
describe "#run_after_each_hooks" do
it "runs a single hook" do
it "runs the after_each hooks" do
called = false
hooks = new_hooks(after_each: ->{ called = true; nil })
group = new_root_group(hooks)
group.run_after_each_hooks
group = Spectator::RootExampleGroup.new(hooks)
group.children = Array(Spectator::ExampleComponent).new(5) do |i|
PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example|
Spectator::Internals::Harness.run(example) if i % 2 == 0
end
end
group.run_after_hooks
called.should be_true
end
it "runs multiple hooks" do
call_count = 0
hooks = new_hooks(after_each: [
->{ call_count += 1; nil },
->{ call_count += 2; nil },
->{ call_count += 3; nil },
])
group = new_root_group(hooks)
group.run_after_each_hooks
call_count.should eq(6)
end
it "runs hooks in the correct order" do
calls = [] of Symbol
hooks = new_hooks(after_each: [
->{ calls << :a; nil },
->{ calls << :b; nil },
->{ calls << :c; nil },
])
group = new_root_group(hooks)
group.run_after_each_hooks
calls.should eq(%i[a b c])
end
it "runs the hooks multiple times" do
call_count = 0
hooks = new_hooks(after_each: ->{ call_count += 1; nil })
group = new_root_group(hooks)
2.times { group.run_after_each_hooks }
call_count.should eq(2)
end
end