Skip to content

Commit

Permalink
Handle filter_applies? with an array in a more generic way.
Browse files Browse the repository at this point in the history
Previous implementation handles only strings, numbers, symbols. This
handles anything in any array the same way it handles the same value
when its not in an array.

- rspec#504.
  • Loading branch information
dchelimsky committed Dec 17, 2011
1 parent 571f189 commit 864e192
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
11 changes: 4 additions & 7 deletions lib/rspec/core/metadata.rb
Expand Up @@ -169,6 +169,8 @@ def all_apply?(filters)

# @private
def filter_applies?(key, value, metadata=self)
return metadata[key].any? {|v| filter_applies?(key, v, {key => value})} if Array === metadata[key]

case key
when :line_numbers
metadata.line_number_filter_applies?(value)
Expand All @@ -177,7 +179,7 @@ def filter_applies?(key, value, metadata=self)
else
case value
when Hash
value.all? { |k, v| filter_applies?(k, v, metadata[key]) }
value.all? {|k, v| filter_applies?(k, v, metadata[key])}
when Regexp
metadata[key] =~ value
when Proc
Expand All @@ -193,12 +195,7 @@ def filter_applies?(key, value, metadata=self)
value.call(metadata[key]) rescue false
end
else
case metadata[key]
when Array
metadata[key].collect{|v| v.to_s}.include? value.to_s
else
metadata[key].to_s == value.to_s
end
metadata[key].to_s == value.to_s
end
end
end
Expand Down
32 changes: 23 additions & 9 deletions spec/rspec/core/metadata_spec.rb
Expand Up @@ -131,20 +131,34 @@ module Core
example_metadata.filter_applies?(:if, lambda { |v, m| passed_metadata = m })
passed_metadata.should eq(example_metadata)
end

context "with tag is Array" do
let(:metadata_with_array) { group_metadata.for_example('example_with_array', :tag => [:one, 2, 'three']) }
it "should match tag:one" do

context "with an Array" do
let(:metadata_with_array) {
group_metadata.for_example('example_with_array', :tag => [:one, 2, 'three', /four/])
}

it "matches a symbol" do
metadata_with_array.filter_applies?(:tag, 'one').should be_true
metadata_with_array.filter_applies?(:tag, :one).should be_true
metadata_with_array.filter_applies?(:tag, 'two').should be_false
end
it "should not match tag:4" do
metadata_with_array.filter_applies?(:tag, '4').should_not be_true

it "matches a string" do
metadata_with_array.filter_applies?(:tag, 'three').should be_true
metadata_with_array.filter_applies?(:tag, :three).should be_true
metadata_with_array.filter_applies?(:tag, 'tree').should be_false
end
it "should match tag:'2'" do

it "matches an integer" do
metadata_with_array.filter_applies?(:tag, '2').should be_true
metadata_with_array.filter_applies?(:tag, 2).should be_true
metadata_with_array.filter_applies?(:tag, 3).should be_false
end
it "should match tag:one" do
metadata_with_array.filter_applies?(:tag, 'three').should be_true

it "matches a regexp" do
metadata_with_array.filter_applies?(:tag, 'four').should be_true
metadata_with_array.filter_applies?(:tag, 'fourtune').should be_true
metadata_with_array.filter_applies?(:tag, 'fortune').should be_false
end
end
end
Expand Down

0 comments on commit 864e192

Please sign in to comment.