Re-add pre- and post-condition hooks

Accidentally removed in hook refactoring.
Addresses https://gitlab.com/arctic-fox/spectator/-/issues/62
This commit is contained in:
Michael Miller 2021-09-16 09:01:51 -06:00
parent e5a3a92085
commit a51640105c
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
7 changed files with 50 additions and 3 deletions

View file

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Fix `Spectator.configure` block calls to `filter_run_excluding` and `filter_run_including`. [#61](https://gitlab.com/arctic-fox/spectator/-/issues/61)
- Re-add pre- and post-condition hooks. [#62](https://gitlab.com/arctic-fox/spectator/-/issues/62)
## [0.10.0] - 2021-09-19
### Fixed

View file

@ -96,6 +96,18 @@ module Spectator::DSL
builder.around_each(hook)
end
# Defines a block of code to execute before every example in the current group
def pre_condition(location = nil, label = "pre_condition", &block : Example -> _)
hook = ExampleHook.new(location: location, label: label, &block)
builder.pre_condition(hook)
end
# Defines a block of code to execute after every example in the current group.
def post_condition(location = nil, label = "post_condition", &block : Example ->)
hook = ExampleHook.new(location: location, label: label, &block)
builder.post_condition(hook)
end
# Constructs the test spec.
# Returns the spec instance.
#

View file

@ -138,5 +138,15 @@ module Spectator::DSL
# 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
# Defines a block of code that will be invoked before 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.
define_example_hook :pre_condition
# Defines a block of code that will be invoked after 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.
define_example_hook :post_condition
end
end

View file

@ -115,10 +115,16 @@ module Spectator
end
private def run_internal
@group.try(&.call_before_each(self))
if group = @group
group.call_before_each(self)
group.call_pre_condition(self)
end
@entrypoint.call(self)
@finished = true
@group.try(&.call_after_each(self))
if group = @group
group.call_post_condition(self)
group.call_after_each(self)
end
end
# Executes code within the example's test context.

View file

@ -58,6 +58,20 @@ module Spectator
procsy
end
define_hook pre_condition : ExampleHook do |example|
Log.trace { "Processing pre_condition hooks for #{self}" }
@group.try &.call_pre_condition(example)
pre_condition_hooks.each &.call(example)
end
define_hook post_condition : ExampleHook, :prepend do |example|
Log.trace { "Processing post_condition hooks for #{self}" }
post_condition_hooks.each &.call(example)
@group.try &.call_post_condition(example)
end
# Creates the example group.
# The *name* describes the purpose of the group.
# It can be a `Symbol` to describe a type.

View file

@ -19,6 +19,8 @@ module Spectator
define_hook after_all : ExampleGroupHook, :prepend
define_hook before_each : ExampleHook
define_hook after_each : ExampleHook, :prepend
define_hook pre_condition : ExampleHook
define_hook post_condition : ExampleHook, :prepend
define_hook around_each : ExampleProcsyHook
@children = [] of NodeBuilder
@ -53,6 +55,8 @@ module Spectator
after_all_hooks.reverse_each { |hook| group.after_all(hook) }
after_each_hooks.reverse_each { |hook| group.after_each(hook) }
around_each_hooks.each { |hook| group.around_each(hook) }
pre_condition_hooks.each { |hook| group.pre_condition(hook) }
post_condition_hooks.reverse_each { |hook| group.post_condition(hook) }
end
end
end

View file

@ -18,7 +18,7 @@ module Spectator
class SpecBuilder
Log = ::Spectator::Log.for(self)
delegate before_all, after_all, before_each, after_each, around_each, to: current
delegate before_all, after_all, before_each, after_each, around_each, pre_condition, post_condition, to: current
# Stack tracking the current group.
# The bottom of the stack (first element) is the root group.