Permalink
Browse files

And, once again: Refactor any/all_apply? in example_group, example, a…

…nd metadata.

This reverts commit 1680d32.

The problem was world was checking example.any_apply? which also checks
its example group. By going directly to the example's
metadata.any_apply?, we avoid checking the example group and all is
right with the world.
  • Loading branch information...
1 parent 1680d32 commit 3d90f00810371afc22cdb8850b1674866daf8524 @dchelimsky dchelimsky committed Oct 1, 2011
@@ -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.apply?(:any?, filters)
+ next unless filters.empty? || group.any_apply?(filters)
group.send(include_or_extend, mod)
end
end
@@ -31,9 +31,12 @@ def around_hooks # :nodoc:
@around_hooks ||= example_group.around_hooks_for(self)
end
- def apply?(predicate, filters)
- @metadata.apply?(predicate, filters) ||
- @example_group_class.apply?(predicate, filters)
+ 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)
end
alias_method :pending?, :pending
@@ -309,8 +309,12 @@ def self.run_examples(reporter)
end.all?
end
- def self.apply?(predicate, filters)
- metadata.apply?(predicate, filters)
+ def self.any_apply?(filters)
+ metadata.any_apply?(filters)
+ end
+
+ def self.all_apply?(filters)
+ metadata.all_apply?(filters)
end
def self.declaration_line_numbers
@@ -13,7 +13,7 @@ def initialize(options, &block)
end
def options_apply?(example_or_group)
- !example_or_group || example_or_group.apply?(:all?, options)
+ example_or_group.all_apply?(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) } unless empty?
+ reverse.each {|h| h.run_in(example_group_instance) }
end
def run_all!(example_group_instance)
@@ -137,10 +137,12 @@ def configure_for_example(description, user_metadata)
update(user_metadata)
end
- def apply?(predicate, filters)
- filters.send(predicate) do |key, value|
- apply_filter(key, value)
- 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)}
end
def relevant_line_numbers(metadata)
@@ -152,19 +154,19 @@ def relevant_line_numbers(metadata)
end
end
- def apply_filter(key, value, metadata=self)
+ def filter_applies?(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.apply_filter(:line_numbers, line_numbers)
+ self.filter_applies?(:line_numbers, line_numbers)
else
true
end
else
- value.all? { |k, v| apply_filter(k, v, metadata[key]) }
+ value.all? { |k, v| filter_applies?(k, v, metadata[key]) }
end
when Regexp
metadata[key] =~ value
@@ -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, conditions={})
- examples.select(&apply?(:any?, conditions))
+ def apply_inclusion_filters(examples, filters)
+ filters.empty? ? examples : examples.select {|e| e.metadata.any_apply?(filters)}
end
alias_method :find, :apply_inclusion_filters
- def apply_exclusion_filters(examples, conditions={})
- examples.reject(&apply?(:any?, conditions))
+ def apply_exclusion_filters(examples, filters)
+ filters.empty? ? examples : examples.reject {|e| e.metadata.any_apply?(filters)}
end
def preceding_declaration_line(filter_line)
@@ -139,10 +139,6 @@ 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 "#apply_filter" do
+ describe "#filter_applies?" 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 apply_filter is only called
+ # this call doesn't really make sense since filter_applies? is only called
# for example metadata not group metadata
- group_metadata.apply_filter(condition_key, group_condition).should be_true
+ group_metadata.filter_applies?(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.apply_filter(condition_key, parent_group_condition).should be_true
+ example_metadata.filter_applies?(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.apply_filter(condition_key, group_condition).should be_true
+ example_metadata.filter_applies?(condition_key, group_condition).should be_true
end
it "matches the example when the line_number is the example line number" do
- example_metadata.apply_filter(condition_key, example_condition).should be_true
+ example_metadata.filter_applies?(condition_key, example_condition).should be_true
end
it "matches when the line number is between this example and the next" do
- example_metadata.apply_filter(condition_key, between_examples_condition).should be_true
+ example_metadata.filter_applies?(condition_key, between_examples_condition).should be_true
end
it "does not match when the line number matches the next example" do
- example_metadata.apply_filter(condition_key, next_example_condition).should be_false
+ example_metadata.filter_applies?(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.apply_filter(:locations, {"/path/to/other_spec.rb" => [3,5,7]}).should be_true
+ example_metadata.filter_applies?(: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.apply_filter(:if, lambda { |v| v }).should be_true
+ example_metadata.filter_applies?(:if, lambda { |v| v }).should be_true
end
it "does not match a proc that evaluates to false" do
- example_metadata.apply_filter(:if, lambda { |v| !v }).should be_false
+ example_metadata.filter_applies?(: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.apply_filter(:if, lambda { |v, m| passed_metadata = m })
+ example_metadata.filter_applies?(: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,6 +132,15 @@ 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 3d90f00

Please sign in to comment.