Add DSL and code to create around_each hooks

This commit is contained in:
Michael Miller 2021-01-16 16:47:40 -07:00
parent 36c2a5d368
commit 153933b044
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 30 additions and 0 deletions

View file

@ -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.

View file

@ -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

View file

@ -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.