Add ability to filter hooks via simple keys.
Created by: tmertens
Also, add feature spec documentation for various filter types.
This resolves a pain point for me, where we want to create conditional before/after/around blocks that run if a key is set on an example with any value. Previously, in order to handle any possible value, we had to provide a Proc which evaluates to true for this to work. This PR adds the ability to simply specify the key as a filter, and if it exists in the metadata hash, it will run the block (provided no other filters filter it out).
For example, suppose we want to timecop an entire example to a specified date/time. Previously, this would require a Proc to handle evaluation of the metadata value in order to make the block run:
config.around(:example, travel: ->(date_time) { date_time.present? }) do |example|
Timecop.travel example.metadata[:travel] do
example.run
end
end
This PR makes this scenario simpler and more efficient due to not having to evaluate a Proc for every example containing the specified key:
config.around(:example, :travel) do |example|
Timecop.travel example.metadata[:travel] do
example.run
end
end
I also added feature specs for all of the filter types, except :location
and :ids
, which seem to be more specific to the CLI. These various options were previously undocumented and only apparent if you actually read through the Core::MetadataFilter
class or tests.
Necessary Changes:
- Add a filter for when only a key is provided.
- Minor refactor of
Core::MetadataFilter#filter_applies?
to satisfy rubocop, and satisfy this feature. - Add tests!
All previously existing tests pass without modification, so this should be a backwards compatible feature.