Skip to content

Commit

Permalink
lazy eval description, full_description, and describes
Browse files Browse the repository at this point in the history
- doesn't have much impact yet since the autogen descriptions still
  check for the presence of description, therefore it gets eval'd, but
  that is fixable!
  • Loading branch information
dchelimsky committed Oct 1, 2011
1 parent 111b619 commit 0704cbb
Showing 1 changed file with 46 additions and 43 deletions.
89 changes: 46 additions & 43 deletions lib/rspec/core/metadata.rb
Expand Up @@ -2,7 +2,7 @@ module RSpec
module Core module Core
class Metadata < Hash class Metadata < Hash


module LocationKeys module MetadataHash
def [](key) def [](key)
return super if has_key?(key) return super if has_key?(key)
case key case key
Expand All @@ -13,6 +13,14 @@ def [](key)
store(:file_path, file_path) store(:file_path, file_path)
store(:line_number, line_number) store(:line_number, line_number)
self[key] self[key]
when :execution_result
store(:execution_result, {})
when :describes
store(:describes, described_class_from(*self[:description_args]))
when :description
store(:description, description_from(*self[:description_args]))
when :full_description
store(:full_description, full_description)
else else
super super
end end
Expand All @@ -30,17 +38,48 @@ def file_and_line_number
def first_caller_from_outside_rspec def first_caller_from_outside_rspec
self[:caller].detect {|l| l !~ /\/lib\/rspec\/core/} self[:caller].detect {|l| l !~ /\/lib\/rspec\/core/}
end end

def description_from(*args)
args.inject("") do |result, a|
a = a.to_s.strip
if result == ""
a
elsif a =~ /^(#|::|\.)/
"#{result}#{a}"
else
"#{result} #{a}"
end
end
end

def full_description
x = self
all_args = [self[:description_args]].compact
while x.has_key?(:example_group)
x = x[:example_group]
all_args.unshift x[:description_args]
end
description_from(*all_args.flatten)
end

def described_class_from(*args)
x = self
while x.has_key?(:example_group)
x = x[:example_group]
end
y = x[:description_args].first
String === y || Symbol === y ? nil :y
end
end end


attr_reader :parent_group_metadata attr_reader :parent_group_metadata


def initialize(parent_group_metadata=nil) def initialize(parent_group_metadata=nil)
if parent_group_metadata if parent_group_metadata
@parent_group_metadata = parent_group_metadata update(parent_group_metadata)
update(@parent_group_metadata) store(:example_group, {:example_group => parent_group_metadata[:example_group]}.extend(MetadataHash))
store(:example_group, {:example_group => @parent_group_metadata[:example_group]}.extend(LocationKeys))
else else
store(:example_group, {}.extend(LocationKeys)) store(:example_group, {}.extend(MetadataHash))
end end


yield self if block_given? yield self if block_given?
Expand All @@ -62,9 +101,6 @@ def process(*args)


self[:example_group].store(:description_args, args) self[:example_group].store(:description_args, args)
self[:example_group].store(:caller, user_metadata.delete(:caller) || caller) self[:example_group].store(:caller, user_metadata.delete(:caller) || caller)
self[:example_group].store(:describes, described_class_from(*args))
self[:example_group].store(:description, description_from(*args))
self[:example_group].store(:full_description, full_description_from(*args))
self[:example_group].store(:block, user_metadata.delete(:example_group_block)) self[:example_group].store(:block, user_metadata.delete(:example_group_block))


update(user_metadata) update(user_metadata)
Expand Down Expand Up @@ -93,21 +129,11 @@ def ensure_valid_keys(user_metadata)
end end


def for_example(description, user_metadata) def for_example(description, user_metadata)
dup.extend(LocationKeys).configure_for_example(description, user_metadata) dup.extend(MetadataHash).configure_for_example(description, user_metadata)
end

def [](key)
return super if has_key?(key)
case key
when :execution_result
store(:execution_result, {})
when :full_description
store(:full_description, full_description_from(self[:example_group][:full_description], self[:description]))
end
end end


def configure_for_example(description, user_metadata) def configure_for_example(description, user_metadata)
store(:description, description.to_s) store(:description_args, [description])
store(:caller, user_metadata.delete(:caller) || caller) store(:caller, user_metadata.delete(:caller) || caller)
update(user_metadata) update(user_metadata)
end end
Expand Down Expand Up @@ -175,29 +201,6 @@ def world
RSpec.world RSpec.world
end end


def description_from(*args)
args.inject("") do |result, a|
a = a.to_s.strip
if result == ""
a
elsif a =~ /^(#|::|\.)/
"#{result}#{a}"
else
"#{result} #{a}"
end
end
end

def full_description_from(*args)
parent_group_metadata && description_from(parent_group_metadata[:example_group][:full_description], *args) ||
description_from(*args)
end

def described_class_from(*args)
parent_group_metadata && parent_group_metadata[:example_group][:describes] || begin
args.first unless String === args.first || Symbol === args.first
end
end
end end
end end
end end

0 comments on commit 0704cbb

Please sign in to comment.