diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index 8a2fdd4..ebeeca5 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -59,6 +59,12 @@ module Spectator::DSL @@builder.after_each(hook) end + # Defines a block of code to execute around every example in the current group. + def around_each(source = nil, label = "around_each", &block : Example::Procsy ->) + hook = ExampleProcsyHook.new(source: source, label: label, &block) + @@builder.around_each(hook) + end + # Sets the configuration of the spec. # # See `Spec::Builder#config=` for usage details. diff --git a/src/spectator/dsl/hooks.cr b/src/spectator/dsl/hooks.cr index a14ee9e..33b3428 100644 --- a/src/spectator/dsl/hooks.cr +++ b/src/spectator/dsl/hooks.cr @@ -66,5 +66,15 @@ module Spectator::DSL # The block will be run in the context of the current running example. # This means that values defined by `let` and `subject` are available. define_example_hook :after_each + + # Defines a block of code that will be invoked around every example in the group. + # The block will be run in the context of the current running example. + # This means that values defined by `let` and `subject` are available. + # + # The block will execute before the example. + # An `Example::Procsy` is passed to the block. + # The `Example::Procsy#run` method should be called to ensure the example runs. + # More code can run afterwards (in the block). + define_example_hook :around_each end end diff --git a/src/spectator/spec_builder.cr b/src/spectator/spec_builder.cr index 770a7cb..8afbb6e 100644 --- a/src/spectator/spec_builder.cr +++ b/src/spectator/spec_builder.cr @@ -149,6 +149,20 @@ module Spectator current_group.after_each(&block) end + # Attaches a hook to be invoked around every example in the current group. + # The current example in procsy form is provided as a block argument. + def around_each(hook) + Log.trace { "Add around_each hook #{hook}" } + current_group.add_around_each_hook(hook) + end + + # Defines a block of code to execute around every example in the current group. + # The current example in procsy form is provided as a block argument. + def around_each(&block : Example -> _) + Log.trace { "Add around_each hook" } + current_group.around_each(&block) + end + # Builds the configuration to use for the spec. # A `ConfigBuilder` is yielded to the block provided to this method. # That builder will be used to create the configuration.