Skip to content

Commit

Permalink
Revert "Refactor any/all_apply? in example_group, example, and metada…
Browse files Browse the repository at this point in the history
…ta."

This reverts commit da80d2e, which
introduced failures in some rubies, but not all (so I didn't notice
them).
  • Loading branch information
dchelimsky committed Oct 1, 2011
1 parent 68d51bb commit 13f6889
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions lib/rspec/core/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions lib/rspec/core/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -182,6 +180,8 @@ def filter_applies?(key, value, metadata=self)
else
value.call(metadata[key]) rescue false
end
when String
metadata[key].to_s == value.to_s
when Enumerable
if key == :line_numbers
preceding_declaration_lines = value.map{|v| world.preceding_declaration_line(v)}
Expand Down
12 changes: 8 additions & 4 deletions lib/rspec/core/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions spec/rspec/core/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__}"]) }
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions spec/rspec/core/world_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 13f6889

Please sign in to comment.