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

Improve and simplify metadata filtering. #2294

Merged
merged 1 commit into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Enhancements:

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

Bug Fixes:

Expand Down
29 changes: 12 additions & 17 deletions lib/rspec/core/metadata_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@ def apply?(predicate, filters, metadata)
end

# @private
def filter_applies?(key, value, metadata)
def filter_applies?(key, filter_value, metadata)
silence_metadata_example_group_deprecations do
return location_filter_applies?(value, metadata) if key == :locations
return id_filter_applies?(value, metadata) if key == :ids
return filters_apply?(key, value, metadata) if Hash === value

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

case value
when Regexp
metadata[key] =~ value
when Proc
proc_filter_applies?(key, value, metadata)
else
metadata[key].to_s == value.to_s
end
return location_filter_applies?(filter_value, metadata) if key == :locations
return id_filter_applies?(filter_value, metadata) if key == :ids
return filters_apply?(key, filter_value, metadata) if Hash === filter_value

meta_value = metadata.fetch(key) { return false }

return true if TrueClass === filter_value && !!meta_value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we !! the meta_value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because TrueClass === '' returns false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually ignore that, why do we !! when it's not being evaluated on it's own and it's not being returned?

Copy link
Contributor

@mrageh mrageh Jul 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TrueClass === filter_value && !!meta_value here we use !! and to me it looks like a no-op unless the return value of meta_value can be a non-boolean value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me it looks like a no-op unless the return value of meta_value can be a non-boolean value.

It can be a non-boolean value, but thats not an issue if we don't leak it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so !! converts it to a boolean value, which is either true or false, thanks @JonRowe

return proc_filter_applies?(key, filter_value, metadata) if Proc === filter_value
return filter_applies_to_any_value?(key, filter_value, metadata) if Array === meta_value

filter_value === meta_value || filter_value.to_s == meta_value.to_s
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/rspec/core/metadata_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ def filter_applies?(key, value, metadata)
}.to raise_error(ArgumentError)
end

it "matches an arbitrary object that has implemented `===` for matching" do
matcher = Object.new
def matcher.===(str)
str.include?("T")
end

expect(filter_applies?(:foo, matcher, {:foo => "a sing"})).to be false
expect(filter_applies?(:foo, matcher, {:foo => "a sTring"})).to be true
end

context "with an :ids filter" do
it 'matches examples with a matching id and rerun_file_path' do
metadata = { :scoped_id => "1:2", :rerun_file_path => "some/file" }
Expand Down