Skip to content
Browse files

finish pushing metadata into metadata object

- make sure all uses of [:caller] ref an array
- add [:location] to represent single line from caller
  • Loading branch information...
1 parent 934fe91 commit 33885079f93b44ffdc3677e7fdd46c0274cf0fef @dchelimsky dchelimsky committed
View
10 lib/rspec/core/example.rb
@@ -6,15 +6,7 @@ class Example
def initialize(behaviour, desc, options, example_block=nil)
@behaviour, @options, @example_block = behaviour, options, example_block
- @metadata = @behaviour.metadata.dup
- @metadata[:description] = desc.to_s
- @metadata[:execution_result] = {}
- @metadata[:caller] = options.delete(:caller)
- if @metadata[:caller]
- @metadata[:file_path] = @metadata[:caller].split(":")[0].strip
- @metadata[:line_number] = @metadata[:caller].split(":")[1].to_i
- end
- @metadata.update(options)
+ @metadata = @behaviour.metadata.for_example(desc, options)
end
def description
View
6 lib/rspec/core/example_group.rb
@@ -22,13 +22,13 @@ def self.extended_modules #:nodoc:
end
def self.example(desc=nil, options={}, &block)
- examples << Rspec::Core::Example.new(self, desc, options.update(:caller => caller[0]), block)
+ examples << Rspec::Core::Example.new(self, desc, options.update(:caller => caller), block)
end
def self.alias_example_to(new_alias, extra_options={})
new_alias = <<-END_RUBY
def self.#{new_alias}(desc=nil, options={}, &block)
- updated_options = options.update(:caller => caller[0])
+ updated_options = options.update(:caller => caller)
updated_options.update(#{extra_options.inspect})
examples << Rspec::Core::Example.new(self, desc, updated_options, block)
end
@@ -100,7 +100,7 @@ def self.describe(*args, &behaviour_block)
# describe 'foo', :shared => true will need this to be completed first
subclass('NestedLevel') do
args << {} unless args.last.is_a?(Hash)
- args.last.update(:behaviour_block => behaviour_block, :caller => caller(2))
+ args.last.update(:behaviour_block => behaviour_block, :caller => caller)
set_it_up(*args)
module_eval(&behaviour_block)
end
View
4 lib/rspec/core/formatters/base_text_formatter.rb
@@ -79,7 +79,7 @@ def dump_pending
output.puts "Pending:"
pending_examples.each do |pending_example, message|
output.puts " #{pending_example}"
- output.puts grey(" # #{format_caller(pending_example.metadata[:caller])}")
+ output.puts grey(" # #{format_caller(pending_example.metadata[:location])}")
end
end
output.flush
@@ -136,4 +136,4 @@ def grey(text)
end
-end
+end
View
41 lib/rspec/core/metadata.rb
@@ -28,41 +28,50 @@ def process(*args)
self[:behaviour][:name] = determine_name
self[:behaviour][:block] = extra_metadata.delete(:behaviour_block)
self[:behaviour][:caller] = extra_metadata.delete(:caller)
- self[:behaviour][:file_path] = determine_file_path(extra_metadata.delete(:file_path))
- self[:behaviour][:line_number] = determine_line_number(extra_metadata.delete(:line_number))
+ self[:behaviour][:file_path] = file_path_from(self[:behaviour], extra_metadata.delete(:file_path))
+ self[:behaviour][:line_number] = line_number_from(self[:behaviour], extra_metadata.delete(:line_number))
+ self[:behaviour][:location] = location_from(self[:behaviour])
update(extra_metadata)
end
- def for_example(desc, options)
- dup.configure_for_example(desc,options)
+ def for_example(description, options)
+ dup.configure_for_example(description,options)
end
- def configure_for_example(desc, options)
- store(:description, desc.to_s)
+ def configure_for_example(description, options)
+ store(:description, description.to_s)
store(:execution_result, {})
store(:caller, options.delete(:caller))
if self[:caller]
- store(:file_path, determine_file_path)
- store(:line_number, determine_line_number)
+ store(:file_path, file_path_from(self))
+ store(:line_number, line_number_from(self))
end
+ self[:location] = location_from(self)
update(options)
+ self
end
private
- def possible_files
- self[:behaviour][:caller].grep(/\_spec\.rb:/i)
+ def file_path_from(hash, given_file_path=nil)
+ given_file_path || file_and_line_number(hash)[0].strip
end
- def determine_file_path(given_file_path=nil)
- return given_file_path if given_file_path
- possible_files.first.split(':').first.strip
+ def line_number_from(hash, given_line_number=nil)
+ given_line_number || file_and_line_number(hash)[1].to_i
end
- def determine_line_number(given_line_number=nil)
- return given_line_number if given_line_number
- possible_files.first.split(':')[1].to_i
+ def location_from(metadata)
+ "#{metadata[:file_path]}:#{metadata[:line_number]}"
+ end
+
+ def file_and_line_number(hash)
+ candidate_entries_from_caller(hash).first.split(':')
+ end
+
+ def candidate_entries_from_caller(hash)
+ hash[:caller].grep(/\_spec\.rb:/i)
end
def determine_name
View
16 spec/rspec/core/metadata_spec.rb
@@ -66,10 +66,10 @@ module Core
end
describe "#metadata_for_example" do
- let(:caller_to_use) { caller(0) }
- let(:caller_line_number) { __LINE__ - 1 }
- let(:metadata) { Metadata.new.process(:caller => caller_to_use) }
- let(:mfe) { metadata.for_example("this description", {:caller => caller_to_use, :arbitrary => :options}) }
+ let(:caller_for_example) { caller(0) }
+ let(:line_number) { __LINE__ - 1 }
+ let(:metadata) { Metadata.new.process(:caller => caller(0)) }
+ let(:mfe) { metadata.for_example("this description", {:caller => caller_for_example, :arbitrary => :options}) }
it "stores the description" do
mfe[:description].should == "this description"
@@ -80,7 +80,7 @@ module Core
end
it "stores the caller" do
- mfe[:caller].should == caller_to_use
+ mfe[:caller].should == caller_for_example
end
it "extracts file path from caller" do
@@ -88,7 +88,11 @@ module Core
end
it "extracts line number from caller" do
- mfe[:line_number].should == caller_line_number
+ mfe[:line_number].should == line_number
+ end
+
+ it "extracts location from caller" do
+ mfe[:location].should == "#{__FILE__}:#{line_number}"
end
it "merges arbitrary options" do

0 comments on commit 3388507

Please sign in to comment.
Something went wrong with that request. Please try again.