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

Commit 82146c8

Browse files
authored
Merge pull request #2294 from rspec/myron/simplify-metadata-filtering
Improve and simplify metadata filtering.
2 parents 1d86a8b + 975db6c commit 82146c8

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Enhancements:
55

66
* Warn when duplicate shared exmaples definitions are loaded due to being
77
defined in files matching the spec pattern (e.g. `_spec.rb`) (#2278, Devon Estes)
8+
* Improve metadata filtering so that it can match against any object
9+
that implements `===` instead of treating regular expressions as
10+
special. (Myron Marston, #2294)
811

912
Bug Fixes:
1013

lib/rspec/core/metadata_filter.rb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,19 @@ def apply?(predicate, filters, metadata)
1313
end
1414

1515
# @private
16-
def filter_applies?(key, value, metadata)
16+
def filter_applies?(key, filter_value, metadata)
1717
silence_metadata_example_group_deprecations do
18-
return location_filter_applies?(value, metadata) if key == :locations
19-
return id_filter_applies?(value, metadata) if key == :ids
20-
return filters_apply?(key, value, metadata) if Hash === value
21-
22-
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)
25-
26-
case value
27-
when Regexp
28-
metadata[key] =~ value
29-
when Proc
30-
proc_filter_applies?(key, value, metadata)
31-
else
32-
metadata[key].to_s == value.to_s
33-
end
18+
return location_filter_applies?(filter_value, metadata) if key == :locations
19+
return id_filter_applies?(filter_value, metadata) if key == :ids
20+
return filters_apply?(key, filter_value, metadata) if Hash === filter_value
21+
22+
meta_value = metadata.fetch(key) { return false }
23+
24+
return true if TrueClass === filter_value && !!meta_value
25+
return proc_filter_applies?(key, filter_value, metadata) if Proc === filter_value
26+
return filter_applies_to_any_value?(key, filter_value, metadata) if Array === meta_value
27+
28+
filter_value === meta_value || filter_value.to_s == meta_value.to_s
3429
end
3530
end
3631

spec/rspec/core/metadata_filter_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def filter_applies?(key, value, metadata)
107107
}.to raise_error(ArgumentError)
108108
end
109109

110+
it "matches an arbitrary object that has implemented `===` for matching" do
111+
matcher = Object.new
112+
def matcher.===(str)
113+
str.include?("T")
114+
end
115+
116+
expect(filter_applies?(:foo, matcher, {:foo => "a sing"})).to be false
117+
expect(filter_applies?(:foo, matcher, {:foo => "a sTring"})).to be true
118+
end
119+
110120
context "with an :ids filter" do
111121
it 'matches examples with a matching id and rerun_file_path' do
112122
metadata = { :scoped_id => "1:2", :rerun_file_path => "some/file" }

0 commit comments

Comments
 (0)