Permalink
Browse files

One more time: revert "Do-over: Refactor any/all_apply? in example_gr…

…oup, example, and metadata."

This reverts commit bad6adc.
  • Loading branch information...
dchelimsky committed Oct 1, 2011
1 parent e666dd1 commit 1680d3246312c6f555b547036b7da27a0ee7b7ee
@@ -438,7 +438,7 @@ def extend(mod, *args)
def configure_group(group)
include_or_extend_modules.each do |include_or_extend, mod, filters|
- next unless filters.empty? || group.any_apply?(filters)
+ next unless filters.empty? || group.apply?(:any?, filters)
group.send(include_or_extend, mod)
end
end
@@ -31,12 +31,9 @@ def around_hooks # :nodoc:
@around_hooks ||= example_group.around_hooks_for(self)
end
- def any_apply?(filters)
- @metadata.any_apply?(filters) || @example_group_class.any_apply?(filters)
- end
-
- def all_apply?(filters)
- @metadata.all_apply?(filters) || @example_group_class.all_apply?(filters)
+ def apply?(predicate, filters)
+ @metadata.apply?(predicate, filters) ||
+ @example_group_class.apply?(predicate, filters)
end
alias_method :pending?, :pending
@@ -309,12 +309,8 @@ def self.run_examples(reporter)
end.all?
end
- def self.any_apply?(filters)
- metadata.any_apply?(filters)
- end
-
- def self.all_apply?(filters)
- metadata.all_apply?(filters)
+ def self.apply?(predicate, filters)
+ metadata.apply?(predicate, filters)
end
def self.declaration_line_numbers
View
@@ -13,7 +13,7 @@ def initialize(options, &block)
end
def options_apply?(example_or_group)
- example_or_group.all_apply?(options)
+ !example_or_group || example_or_group.apply?(:all?, options)
end
def to_proc
@@ -77,7 +77,7 @@ def run_all!(example_group_instance)
class AfterHooks < HookCollection
def run_all(example_group_instance)
- reverse.each {|h| h.run_in(example_group_instance) }
+ reverse.each {|h| h.run_in(example_group_instance) } unless empty?
end
def run_all!(example_group_instance)
View
@@ -137,12 +137,10 @@ def configure_for_example(description, user_metadata)
update(user_metadata)
end
- def any_apply?(filters)
- filters.any? {|k,v| filter_applies?(k,v)}
- end
-
- def all_apply?(filters)
- filters.all? {|k,v| filter_applies?(k,v)}
+ def apply?(predicate, filters)
+ filters.send(predicate) do |key, value|
+ apply_filter(key, value)
+ end
end
def relevant_line_numbers(metadata)
@@ -154,19 +152,19 @@ def relevant_line_numbers(metadata)
end
end
- def filter_applies?(key, value, metadata=self)
+ def apply_filter(key, value, metadata=self)
case value
when Hash
if key == :locations
file_path = (self[:example_group] || {})[:file_path]
expanded_path = file_path && File.expand_path( file_path )
if expanded_path && line_numbers = value[expanded_path]
- self.filter_applies?(:line_numbers, line_numbers)
+ self.apply_filter(:line_numbers, line_numbers)
else
true
end
else
- value.all? { |k, v| filter_applies?(k, v, metadata[key]) }
+ value.all? { |k, v| apply_filter(k, v, metadata[key]) }
end
when Regexp
metadata[key] =~ value
View
@@ -70,14 +70,14 @@ def example_count
example_groups.collect {|g| g.descendants}.flatten.inject(0) { |sum, g| sum += g.filtered_examples.size }
end
- def apply_inclusion_filters(examples, filters)
- filters.empty? ? examples : examples.select {|e| e.any_apply?(filters)}
+ def apply_inclusion_filters(examples, conditions={})
+ examples.select(&apply?(:any?, conditions))
end
alias_method :find, :apply_inclusion_filters
- def apply_exclusion_filters(examples, filters)
- filters.empty? ? examples : examples.reject {|e| e.any_apply?(filters)}
+ def apply_exclusion_filters(examples, conditions={})
+ examples.reject(&apply?(:any?, conditions))
end
def preceding_declaration_line(filter_line)
@@ -139,6 +139,10 @@ def find_hook(hook, scope, group, example = nil)
private
+ def apply?(predicate, filter)
+ lambda {|example| filter.empty? || example.metadata.apply?(predicate, filter)}
+ end
+
def declaration_line_numbers
@line_numbers ||= example_groups.inject([]) do |lines, g|
lines + g.declaration_line_numbers
@@ -21,7 +21,7 @@ module Core
end
end
- describe "#filter_applies?" do
+ describe "#apply_filter" do
let(:parent_group_metadata) { Metadata.new.process('parent group', :caller => ["foo_spec.rb:#{__LINE__}"]) }
let(:group_metadata) { Metadata.new(parent_group_metadata).process('group', :caller => ["foo_spec.rb:#{__LINE__}"]) }
@@ -46,29 +46,29 @@ module Core
end
it "matches the group when the line_number is the example group line number" do
- # this call doesn't really make sense since filter_applies? is only called
+ # this call doesn't really make sense since apply_filter is only called
# for example metadata not group metadata
- group_metadata.filter_applies?(condition_key, group_condition).should be_true
+ group_metadata.apply_filter(condition_key, group_condition).should be_true
end
it "matches the example when the line_number is the grandparent example group line number" do
- example_metadata.filter_applies?(condition_key, parent_group_condition).should be_true
+ example_metadata.apply_filter(condition_key, parent_group_condition).should be_true
end
it "matches the example when the line_number is the parent example group line number" do
- example_metadata.filter_applies?(condition_key, group_condition).should be_true
+ example_metadata.apply_filter(condition_key, group_condition).should be_true
end
it "matches the example when the line_number is the example line number" do
- example_metadata.filter_applies?(condition_key, example_condition).should be_true
+ example_metadata.apply_filter(condition_key, example_condition).should be_true
end
it "matches when the line number is between this example and the next" do
- example_metadata.filter_applies?(condition_key, between_examples_condition).should be_true
+ example_metadata.apply_filter(condition_key, between_examples_condition).should be_true
end
it "does not match when the line number matches the next example" do
- example_metadata.filter_applies?(condition_key, next_example_condition).should be_false
+ example_metadata.apply_filter(condition_key, next_example_condition).should be_false
end
end
@@ -115,21 +115,21 @@ module Core
it_has_behavior "matching by line number"
it "ignores location filters for other files" do
- example_metadata.filter_applies?(:locations, {"/path/to/other_spec.rb" => [3,5,7]}).should be_true
+ example_metadata.apply_filter(:locations, {"/path/to/other_spec.rb" => [3,5,7]}).should be_true
end
end
it "matches a proc that evaluates to true" do
- example_metadata.filter_applies?(:if, lambda { |v| v }).should be_true
+ example_metadata.apply_filter(:if, lambda { |v| v }).should be_true
end
it "does not match a proc that evaluates to false" do
- example_metadata.filter_applies?(:if, lambda { |v| !v }).should be_false
+ example_metadata.apply_filter(:if, lambda { |v| !v }).should be_false
end
it "passes the metadata hash as the second argument if a given proc expects 2 args" do
passed_metadata = nil
- example_metadata.filter_applies?(:if, lambda { |v, m| passed_metadata = m })
+ example_metadata.apply_filter(:if, lambda { |v, m| passed_metadata = m })
passed_metadata.should eq(example_metadata)
end
end
@@ -49,7 +49,7 @@ module RSpec::Core
let(:example_groups) { [group1, group2, group3, group4] }
it "finds no groups when given no search parameters" do
- world.apply_inclusion_filters([],{}).should eq([])
+ world.apply_inclusion_filters([]).should eq([])
end
it "finds matching groups when filtering on :describes (described class or module)" do
@@ -132,15 +132,6 @@ module RSpec::Core
world.apply_exclusion_filters(group.examples, :name => /exclude/, "another_condition" => "foo").should eq([])
world.apply_exclusion_filters(group.examples, :name => /exclude/, "another_condition" => "foo1").should eq([])
end
-
- it "finds all if filters are empty" do
- group = ExampleGroup.describe(Bar) do
- example("foo") {}
- example("bar") {}
- end
- world.register(group)
- world.apply_exclusion_filters(group.examples, {}).should eq(group.examples)
- end
end
describe "#preceding_declaration_line (again)" do

0 comments on commit 1680d32

Please sign in to comment.