Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adjustments to compensate for RSpec no longer taking the call stack a…

…s input when creating an Example or ExampleGroup
  • Loading branch information...
commit 851160bc2cc8b53fd19b7b1c3a69a854a8267e61 1 parent de0bce5
@glv authored
View
2  Gemfile
@@ -1,7 +1,7 @@
source :gemcutter
# gem 'bundler', '>= 0.9.16'
-gem 'rspec', '= 2.0.0'
+gem 'rspec', '= 2.0.1'
group :test do
gem 'rake', '>= 0.8.7'
View
20 Gemfile.lock
@@ -10,16 +10,16 @@ GEM
rubyforge (>= 2.0.0)
json_pure (1.4.6)
rake (0.8.7)
- rspec (2.0.0)
- rspec-core (= 2.0.0)
- rspec-expectations (= 2.0.0)
- rspec-mocks (= 2.0.0)
- rspec-core (2.0.0)
- rspec-expectations (2.0.0)
+ rspec (2.0.1)
+ rspec-core (~> 2.0.1)
+ rspec-expectations (~> 2.0.1)
+ rspec-mocks (~> 2.0.1)
+ rspec-core (2.0.1)
+ rspec-expectations (2.0.1)
diff-lcs (>= 1.1.2)
- rspec-mocks (2.0.0)
- rspec-core (= 2.0.0)
- rspec-expectations (= 2.0.0)
+ rspec-mocks (2.0.1)
+ rspec-core (~> 2.0.1)
+ rspec-expectations (~> 2.0.1)
rubyforge (2.0.4)
json_pure (>= 1.1.7)
@@ -30,4 +30,4 @@ PLATFORMS
DEPENDENCIES
jeweler (>= 1.4.0)
rake (>= 0.8.7)
- rspec (= 2.0.0)
+ rspec (= 2.0.1)
View
28 lib/rspec/unit/test_case.rb
@@ -23,8 +23,11 @@ def self.inherited(klass)
install_setup_and_teardown(klass)
- klass.set_it_up(test_case_name(klass), {:caller => caller})
- klass.metadata[:example_group][:test_unit] = true
+ klass.set_it_up(test_case_name(klass))
+ md = klass.metadata
+ md[:example_group][:test_unit] = true
+ md[:example_group][:file_path], md[:example_group][:line_number] = md.send(:file_and_line_number_from, caller)
+ md[:example_group][:location] = md.send(:location_from, md[:example_group])
children << klass
world.example_groups << klass
end
@@ -63,6 +66,16 @@ def self.caller_lines
@_caller_lines ||= {}
end
+ def self.find_caller_lines(name)
+ klass = self
+ while klass.respond_to?(:caller_lines)
+ lines = klass.caller_lines[name]
+ return lines unless lines.nil?
+ klass = klass.superclass
+ end
+ []
+ end
+
def self.test_method_metadata
@_test_method_metadata ||= {}
end
@@ -94,17 +107,20 @@ def self.number_of_tests
def self.tests
@tests ||= test_methods.sort.map do |m|
- meta = (test_method_metadata[m] || {}).merge({:caller => caller_lines[m],
- :full_description => "#{display_name}##{m}",
+ meta = (test_method_metadata[m] || {}).merge({:full_description => "#{display_name}##{m}",
:test_unit => true})
- Core::Example.new(self, m, meta, proc{execute(m)})
+ example = Core::Example.new(self, m, meta, proc{execute(m)})
+ example.metadata[:file_path], example.metadata[:line_number] = example.metadata.send(:file_and_line_number_from, find_caller_lines(m))
+ example.metadata[:location] = example.metadata.send(:location_from, example.metadata)
+ example
end
end
class <<self
- private :test_case_name, :caller_lines, :test_method_metadata,
+ private :test_case_name, :test_method_metadata,
:install_setup_and_teardown, :test_method?, :test_methods,
:number_of_tests, :tests
+ protected :caller_lines, :find_caller_lines
end
def initialize
View
73 spec/test_case_spec.rb
@@ -197,6 +197,31 @@ class SampleTestCase < RSpec::Unit::TestCase
end
end
+ describe "find_caller_lines" do
+ it "returns [] if the method name is not found" do
+ @foo.send(:find_caller_lines, 'wrong').should be_empty
+ bar = Class.new(@foo)
+ bar.send(:find_caller_lines, 'wrong').should be_empty
+ end
+
+ it "returns a stack trace array if the name is found in caller_lines" do
+ @foo.class_eval do
+ def test_bar; end
+ end
+
+ @foo.send(:find_caller_lines, 'test_bar').should_not be_empty
+ end
+
+ it "returns a stack trace array if the name is found in the parent's caller_lines" do
+ @foo.class_eval do
+ def test_bar; end
+ end
+ bar = Class.new(@foo)
+
+ bar.send(:find_caller_lines, 'test_bar').should_not be_empty
+ end
+ end
+
describe "test class metadata" do
before do
class SampleTestCaseForName < RSpec::Unit::TestCase
@@ -226,12 +251,7 @@ class SampleTestCaseForName < RSpec::Unit::TestCase
it "sets :location to file_path and line_number" do
@foo.metadata[:example_group][:location].should == "#{__FILE__}:#{@foo_definition_line}"
end
-
- it "sets :caller" do
- @foo.metadata[:example_group][:caller].first.should =~ Regexp.new(Regexp.escape(@foo.metadata[:example_group][:location]))
- @foo.metadata[:example_group][:caller].size.should be_>(@caller_at_foo_definition.size)
- end
-
+
it "has nil for :block and :describes" do
@foo.metadata[:example_group][:block].should be_nil
@foo.metadata[:example_group][:describes].should be_nil
@@ -304,14 +324,6 @@ def test_baz; end
test_baz_metadata[:example_group].should == @foo.metadata[:example_group]
end
- it "sets :caller" do
- @foo.class_eval do
- def test_baz; end
- end
- test_baz_metadata[:caller].first.should match(/^#{Regexp.escape(@foo.examples.first.metadata[:location])}/)
- test_baz_metadata[:caller].size.should be_>(caller.size)
- end
-
it "records test_info metadata for next test method" do
@foo.class_eval do
test_info :foo => :bar
@@ -327,6 +339,39 @@ def test_baz; end
def test_quux; end
end
find_example(@foo, 'test_quux').metadata[:foo].should be_nil
+ end
+
+ context "inherited methods" do
+ def test_baz_metadata
+ find_example(@bar, 'test_baz').metadata
+ end
+
+ it "sets :file_path to the file where the method is defined" do
+ @foo.class_eval do
+ def test_baz; end
+ end
+ @bar = Class.new(@foo)
+
+ test_baz_metadata[:file_path].should == __FILE__
+ end
+
+ it "sets :line_number to the line where the method definition begins" do
+ @foo.class_eval do
+ def test_baz; end
+ end
+ @bar = Class.new(@foo)
+
+ test_baz_metadata[:line_number].should == (__LINE__ - 4)
+ end
+
+ it "sets :location to file path and line number" do
+ @foo.class_eval do
+ def test_baz; end
+ end
+ @bar = Class.new(@foo)
+
+ test_baz_metadata[:location].should == "#{__FILE__}:#{__LINE__-4}"
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.