Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit d8d0a34

Browse files
author
Tim Mertens
committed
Add ability to filter hooks via simple keys.
1 parent 561cce6 commit d8d0a34

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

features/hooks/filtering.feature

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,42 @@ Feature: filters
244244
.after context
245245
"""
246246

247+
Scenario: Filtering hooks using symbols
248+
Given a file named "filter_example_hooks_with_symbol_spec.rb" with:
249+
"""ruby
250+
RSpec.configure do |config|
251+
config.before(:example, :foo) do
252+
invoked_hooks << :before_example_foo_bar
253+
end
254+
end
255+
256+
RSpec.describe "a filtered before :example hook" do
257+
let(:invoked_hooks) { [] }
258+
259+
describe "group without a matching metadata key" do
260+
it "does not run the hook" do
261+
expect(invoked_hooks).to be_empty
262+
end
263+
264+
it "does not run the hook for an example with metadata hash containing the key with a falsey value", :foo => nil do
265+
expect(invoked_hooks).to be_empty
266+
end
267+
268+
it "runs the hook for an example with metadata hash containing the key with a truthy value", :foo => :bar do
269+
expect(invoked_hooks).to eq([:before_example_foo_bar])
270+
end
271+
272+
it "runs the hook for an example with only the key defined", :foo do
273+
expect(invoked_hooks).to eq([:before_example_foo_bar])
274+
end
275+
end
276+
277+
describe "group with matching metadata key", :foo do
278+
it "runs the hook" do
279+
expect(invoked_hooks).to eq([:before_example_foo_bar])
280+
end
281+
end
282+
end
283+
"""
284+
When I run `rspec filter_example_hooks_with_symbol_spec.rb`
285+
Then the examples should all pass

lib/rspec/core/metadata_filter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ def apply?(predicate, filters, metadata)
1515
# @private
1616
def filter_applies?(key, value, metadata)
1717
silence_metadata_example_group_deprecations do
18-
return filter_applies_to_any_value?(key, value, metadata) if Array === metadata[key] && !(Proc === value)
1918
return location_filter_applies?(value, metadata) if key == :locations
2019
return id_filter_applies?(value, metadata) if key == :ids
2120
return filters_apply?(key, value, metadata) if Hash === value
2221

2322
return false unless metadata.key?(key)
23+
return true if TrueClass === value && !!metadata[key]
24+
return filter_applies_to_any_value?(key, value, metadata) if Array === metadata[key] && !(Proc === value)
2425

2526
case value
2627
when Regexp

spec/rspec/core/metadata_filter_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ def filter_applies?(key, value, metadata)
157157
metadata = { :foo => "words" }
158158
expect(filter_applies?(:foo, { :bar => /word/ }, metadata)).to be_falsey
159159
end
160+
161+
it 'matches when a metadata key is specified without a value and exists in the metadata hash' do
162+
metadata = { :foo => "words" }
163+
expect(filter_applies?(:foo, true, metadata)).to be_truthy
164+
end
160165
end
161166

162167
context "with an Array" do
@@ -199,6 +204,10 @@ def filter_applies?(key, value, metadata)
199204
it "does not match a proc that evaluates to false" do
200205
expect(filter_applies?(:tag, lambda { |values| values.include? 'nothing' }, metadata_with_array)).to be_falsey
201206
end
207+
208+
it 'matches when a metadata key is specified without a value and exists in the metadata hash' do
209+
expect(filter_applies?(:tag, true, metadata_with_array)).to be_truthy
210+
end
202211
end
203212
end
204213
end

0 commit comments

Comments
 (0)