Skip to content
Browse files

Handle filter_applies? with an array in a more generic way.

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.

- #504.
  • Loading branch information...
1 parent 571f189 commit 864e192f5ffc65bb6e10b7141f2cc266717fe531 @dchelimsky dchelimsky committed
Showing with 27 additions and 16 deletions.
  1. +4 −7 lib/rspec/core/metadata.rb
  2. +23 −9 spec/rspec/core/metadata_spec.rb
View
11 lib/rspec/core/metadata.rb
@@ -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)
@@ -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
@@ -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
View
32 spec/rspec/core/metadata_spec.rb
@@ -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

0 comments on commit 864e192

Please sign in to comment.
Something went wrong with that request. Please try again.