diff --git a/spec/dsl/given_example_group_builder_spec.cr b/spec/dsl/given_example_group_builder_spec.cr index a1fb033..3f24de1 100644 --- a/spec/dsl/given_example_group_builder_spec.cr +++ b/spec/dsl/given_example_group_builder_spec.cr @@ -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 diff --git a/spec/dsl/nested_example_group_builder_spec.cr b/spec/dsl/nested_example_group_builder_spec.cr index 772b4cc..9d15dcf 100644 --- a/spec/dsl/nested_example_group_builder_spec.cr +++ b/spec/dsl/nested_example_group_builder_spec.cr @@ -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 diff --git a/spec/dsl/root_example_group_builder_spec.cr b/spec/dsl/root_example_group_builder_spec.cr index 14a4e3c..96a9a7a 100644 --- a/spec/dsl/root_example_group_builder_spec.cr +++ b/spec/dsl/root_example_group_builder_spec.cr @@ -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 diff --git a/spec/nested_example_group_spec.cr b/spec/nested_example_group_spec.cr index 9404b4c..e8bdf66 100644 --- a/spec/nested_example_group_spec.cr +++ b/spec/nested_example_group_spec.cr @@ -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 diff --git a/spec/root_example_group_spec.cr b/spec/root_example_group_spec.cr index 02e9953..eebec64 100644 --- a/spec/root_example_group_spec.cr +++ b/spec/root_example_group_spec.cr @@ -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 - called = false - hooks = new_hooks(after_each: ->{ called = true; nil }) - group = new_root_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_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) + 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 |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