Add before_suite and after_suite

This commit is contained in:
Michael Miller 2021-07-17 14:19:16 -06:00
parent af13a89257
commit a810eef16c
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
4 changed files with 47 additions and 0 deletions

View file

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Examples can be skipped by using a `:pending` tag. A reason method can be specified: `pending: "Some excuse"`
- Examples can be skipped during execution by using `skip` or `pending` in the example block.
- Sample blocks can be temporarily skipped by using `xsample` or `xrandom_sample`.
- Add `before_suite` and `after_suite` hooks.
### Changed
- Simplify and reduce defined types and generics. Should speed up compilation times.

View file

@ -54,6 +54,12 @@ module Spectator::DSL
@@builder.add_pending_example(*args)
end
# Defines a block of code to execute before any and all examples in the test suite.
def before_suite(location = nil, label = "before_suite", &block)
hook = ExampleGroupHook.new(location: location, label: label, &block)
@@builder.before_suite(hook)
end
# Defines a block of code to execute before any and all examples in the current group.
def before_all(location = nil, label = "before_all", &block)
hook = ExampleGroupHook.new(location: location, label: label, &block)
@ -66,6 +72,12 @@ module Spectator::DSL
@@builder.before_each(hook)
end
# Defines a block of code to execute after any and all examples in the test suite.
def after_suite(location = nil, label = "after_suite", &block)
hook = ExampleGroupHook.new(location: location, label: label, &block)
@@builder.after_suite(hook)
end
# Defines a block of code to execute after any and all examples in the current group.
def after_all(location = nil, label = "after_all", &block)
hook = ExampleGroupHook.new(location: location, label: label, &block)

View file

@ -47,6 +47,16 @@ module Spectator::DSL
end
end
# Defines a block of code that will be invoked once before any examples in the suite.
# The block will not run in the context of the current running example.
# This means that values defined by `let` and `subject` are not available.
define_example_group_hook :before_suite
# Defines a block of code that will be invoked once after all examples in the suite.
# The block will not run in the context of the current running example.
# This means that values defined by `let` and `subject` are not available.
define_example_group_hook :after_suite
# Defines a block of code that will be invoked once before any examples in the group.
# The block will not run in the context of the current running example.
# This means that values defined by `let` and `subject` are not available.

View file

@ -135,6 +135,18 @@ module Spectator
current << PendingExampleBuilder.new(name, location, metadata, reason)
end
# Attaches a hook to be invoked before any and all examples in the test suite.
def before_suite(hook)
Log.trace { "Add before_suite hook #{hook}" }
root.add_before_all_hook(hook)
end
# Defines a block of code to execute before any and all examples in the test suite.
def before_suite(&block)
Log.trace { "Add before_suite hook" }
root.before_all(&block)
end
# Attaches a hook to be invoked before any and all examples in the current group.
def before_all(hook)
Log.trace { "Add before_all hook #{hook}" }
@ -161,6 +173,18 @@ module Spectator
current.before_each(&block)
end
# Attaches a hook to be invoked after any and all examples in the test suite.
def after_suite(hook)
Log.trace { "Add after_suite hook #{hook}" }
root.add_after_all_hook(hook)
end
# Defines a block of code to execute after any and all examples in the test suite.
def after_suite(&block)
Log.trace { "Add after_suite hook" }
root.after_all(&block)
end
# Attaches a hook to be invoked after any and all examples in the current group.
def after_all(hook)
Log.trace { "Add after_all hook #{hook}" }