Permalink
Browse files

reorg and add some docs to Metadata

  • Loading branch information...
1 parent 810895a commit 25b1ff5ca4052da3f0dc7c5bfc91ae3fd485d6c1 @dchelimsky dchelimsky committed Oct 2, 2011
Showing with 55 additions and 49 deletions.
  1. +55 −49 lib/rspec/core/metadata.rb
@@ -2,6 +2,8 @@ module RSpec
module Core
class Metadata < Hash
+ # Used to extend metadata Hashes to support lazy evaluation of locations
+ # and descriptions.
module MetadataHash
def [](key)
return super if has_key?(key)
@@ -12,7 +14,7 @@ def [](key)
file_path, line_number = file_and_line_number
store(:file_path, file_path)
store(:line_number, line_number)
- self[key]
+ super
when :execution_result
store(:execution_result, {})
when :describes
@@ -26,6 +28,8 @@ def [](key)
end
end
+ private
+
def location
"#{self[:file_path]}:#{self[:line_number]}"
end
@@ -75,16 +79,6 @@ def initialize(parent_group_metadata=nil)
yield self if block_given?
end
- RESERVED_KEYS = [
- :description,
- :example_group,
- :execution_result,
- :file_path,
- :full_description,
- :line_number,
- :location
- ]
-
def process(*args)
user_metadata = args.last.is_a?(Hash) ? args.pop : {}
ensure_valid_keys(user_metadata)
@@ -95,38 +89,10 @@ def process(*args)
update(user_metadata)
end
- def ensure_valid_keys(user_metadata)
- RESERVED_KEYS.each do |key|
- if user_metadata.keys.include?(key)
- raise <<-EOM
-#{"*"*50}
-:#{key} is not allowed
-
-RSpec reserves some hash keys for its own internal use,
-including :#{key}, which is used on:
-
- #{caller(0)[4]}.
-
-Here are all of RSpec's reserved hash keys:
-
- #{RESERVED_KEYS.join("\n ")}
-#{"*"*50}
-EOM
- raise ":#{key} is not allowed"
- end
- end
- end
-
def for_example(description, user_metadata)
dup.extend(MetadataHash).configure_for_example(description, user_metadata)
end
- def configure_for_example(description, user_metadata)
- store(:description_args, [description])
- store(:caller, user_metadata.delete(:caller) || caller)
- update(user_metadata)
- end
-
def any_apply?(filters)
filters.any? {|k,v| filter_applies?(k,v)}
end
@@ -135,23 +101,14 @@ def all_apply?(filters)
filters.all? {|k,v| filter_applies?(k,v)}
end
- def relevant_line_numbers(metadata)
- line_numbers = [metadata[:line_number]]
- if metadata[:example_group]
- line_numbers + relevant_line_numbers(metadata[:example_group])
- else
- line_numbers
- end
- end
-
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.filter_applies?(:line_numbers, line_numbers)
+ filter_applies?(:line_numbers, line_numbers)
else
true
end
@@ -186,12 +143,61 @@ def filter_applies?(key, value, metadata=self)
end
end
+ protected
+
+ def configure_for_example(description, user_metadata)
+ store(:description_args, [description])
+ store(:caller, user_metadata.delete(:caller) || caller)
+ update(user_metadata)
+ end
+
private
+ RESERVED_KEYS = [
+ :description,
+ :example_group,
+ :execution_result,
+ :file_path,
+ :full_description,
+ :line_number,
+ :location
+ ]
+
+ def ensure_valid_keys(user_metadata)
+ RESERVED_KEYS.each do |key|
+ if user_metadata.keys.include?(key)
+ raise <<-EOM
+#{"*"*50}
+:#{key} is not allowed
+
+RSpec reserves some hash keys for its own internal use,
+including :#{key}, which is used on:
+
+ #{caller(0)[4]}.
+
+Here are all of RSpec's reserved hash keys:
+
+ #{RESERVED_KEYS.join("\n ")}
+#{"*"*50}
+EOM
+ raise ":#{key} is not allowed"
+ end
+ end
+ end
+
def world
RSpec.world
end
+ def relevant_line_numbers(metadata)
+ line_numbers = [metadata[:line_number]]
+ if metadata[:example_group]
+ line_numbers + relevant_line_numbers(metadata[:example_group])
+ else
+ line_numbers
+ end
+ end
+
end
end
end

0 comments on commit 25b1ff5

Please sign in to comment.