Permalink
Browse files

support arity 2 procs for filters

- #556.
  • Loading branch information...
dchelimsky committed Feb 3, 2012
1 parent 14798e1 commit 49283ee504fc5ef12fc048ba23a27264f1138c9c
Showing with 45 additions and 5 deletions.
  1. +36 −4 lib/rspec/core/configuration.rb
  2. +4 −1 lib/rspec/core/metadata.rb
  3. +5 −0 spec/rspec/core/metadata_spec.rb
@@ -534,10 +534,26 @@ def alias_it_should_behave_like_to(new_name, report_label = '')
# or config files (e.g. `.rspec`).
#
# @example
- # filter_run_including :x => 'y'
+ # # given this declaration
+ # describe "something", :foo => 'bar' do
+ # # ...
+ # end
+ #
+ # # any of the following will include that group
+ # config.filter_run_including :foo => 'bar'
+ # config.filter_run_including :foo => /^ba/
+ # config.filter_run_including :foo => lambda {|v| v == 'bar'}
+ # config.filter_run_including :foo => lambda {|v,m| metadata[:foo] == 'bar'}
@exviva

exviva Feb 3, 2012

Contributor

Didn't you mean m[:foo] here and in several places below?

@dchelimsky

dchelimsky Feb 3, 2012

Owner

I did! Fixed in a9fd289

This is one area that executable docs like Cucumber do better than non-executable docs like RDoc :)

+ #
+ # # given a proc with an arity of 1, the lambda is passed the value related to the key, e.g.
+ # config.filter_run_including :foo => lambda {|v| v == 'bar'}
+ #
+ # # given a proc with an arity of 2, the lambda is passed the value related to the key,
+ # # and the metadata itself e.g.
+ # config.filter_run_including :foo => lambda {|v,m| metadata[:foo] == 'bar'}
#
# # with treat_symbols_as_metadata_keys_with_true_values = true
- # filter_run_including :foo # results in {:foo => true}
+ # filter_run_including :foo # same as filter_run_including :foo => true
def filter_run_including(*args)
filter_manager.include_with_low_priority build_metadata_hash_from(args)
end
@@ -576,10 +592,26 @@ def inclusion_filter
# or config files (e.g. `.rspec`).
#
# @example
- # filter_run_excluding :x => 'y'
+ # # given this declaration
+ # describe "something", :foo => 'bar' do
+ # # ...
+ # end
+ #
+ # # any of the following will exclude that group
+ # config.filter_run_excluding :foo => 'bar'
+ # config.filter_run_excluding :foo => /^ba/
+ # config.filter_run_excluding :foo => lambda {|v| v == 'bar'}
+ # config.filter_run_excluding :foo => lambda {|v,m| metadata[:foo] == 'bar'}
+ #
+ # # given a proc with an arity of 1, the lambda is passed the value related to the key, e.g.
+ # config.filter_run_excluding :foo => lambda {|v| v == 'bar'}
+ #
+ # # given a proc with an arity of 2, the lambda is passed the value related to the key,
+ # # and the metadata itself e.g.
+ # config.filter_run_excluding :foo => lambda {|v,m| metadata[:foo] == 'bar'}
#
# # with treat_symbols_as_metadata_keys_with_true_values = true
- # filter_run_excluding :foo # results in {:foo => true}
+ # filter_run_excluding :foo # same as filter_run_excluding :foo => true
def filter_run_excluding(*args)
filter_manager.exclude_with_low_priority build_metadata_hash_from(args)
end
@@ -180,7 +180,10 @@ def filter_applies?(key, value, metadata=self)
when Regexp
metadata[key] =~ value
when Proc
- value.call(metadata[key])
+ case value.arity
+ when 1 then value.call(metadata[key])
+ when 2 then value.call(metadata[key], metadata)
+ end
else
metadata[key].to_s == value.to_s
end
@@ -126,6 +126,11 @@ module Core
example_metadata.filter_applies?(:if, lambda { |v| !v }).should be_false
end
+ it "matches a proc with an arity of 2" do
+ example_metadata[:foo] = nil
+ example_metadata.filter_applies?(:foo, lambda { |v, m| m == example_metadata }).should be_true
+ end
+
context "with an Array" do
let(:metadata_with_array) {
group_metadata.for_example('example_with_array', :tag => [:one, 2, 'three', /four/])

0 comments on commit 49283ee

Please sign in to comment.