Skip to content

Commit

Permalink
readability refactoring of #filter_applies?
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKVal committed Dec 6, 2011
1 parent 45eb0d3 commit 19d157c
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions lib/rspec/core/metadata.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -155,13 +155,7 @@ def filter_applies?(key, value, metadata=self)
case value case value
when Hash when Hash
if key == :locations if key == :locations
file_path = (self[:example_group] || {})[:file_path] filter_for_locations?(value, metadata)
expanded_path = file_path && File.expand_path( file_path )
if expanded_path && line_numbers = value[expanded_path]
filter_applies?(:line_numbers, line_numbers)
else
true
end
else else
value.all? { |k, v| filter_applies?(k, v, metadata[key]) } value.all? { |k, v| filter_applies?(k, v, metadata[key]) }
end end
Expand All @@ -180,14 +174,9 @@ def filter_applies?(key, value, metadata=self)
value.call(metadata[key]) rescue false value.call(metadata[key]) rescue false
end end
when String when String
metadata[key].to_s == value.to_s metadata[key].to_s == value
when Enumerable when Enumerable
if key == :line_numbers key == :line_numbers ? within_examples_lines?(value, metadata) : metadata[key] == value
preceding_declaration_lines = value.map{|v| world.preceding_declaration_line(v)}
!(relevant_line_numbers(metadata) & preceding_declaration_lines).empty?
else
metadata[key] == value
end
else else
metadata[key].to_s == value.to_s metadata[key].to_s == value.to_s
end end
Expand Down Expand Up @@ -234,13 +223,24 @@ def ensure_valid_keys(user_metadata)
end end
end end


def world def relevant_line_numbers(meta)
RSpec.world [meta[:line_number]] + (meta[:example_group] ? relevant_line_numbers(meta[:example_group]) : [])
end

def within_examples_lines?(line_numbers, metadata)
preceding_declaration_lines = line_numbers.map{|n| RSpec.world.preceding_declaration_line(n)}
!(relevant_line_numbers(metadata) & preceding_declaration_lines).empty?
end

def example_group_line_number(locations, metadata)
# An ExampleGroup always got one line number
locations[File.expand_path( metadata[:example_group][:file_path] )] if metadata[:example_group]
end end


def relevant_line_numbers(metadata) def filter_for_locations?(locations, metadata)
return [metadata[:line_number]] unless metadata[:example_group] # it ignores location filters for other files
[metadata[:line_number]] + relevant_line_numbers(metadata[:example_group]) line_number = example_group_line_number(locations, metadata)
line_number ? within_examples_lines?(line_number, metadata) : true
end end


end end
Expand Down

0 comments on commit 19d157c

Please sign in to comment.