Permalink
Browse files

example.description returns the location of the example if there is no

explicit description or matcher-generated description.

- Fixes #585.
  • Loading branch information...
1 parent 5436bc2 commit 34cb8f9d85b2d7f9f6d78c016d0f708f438bc5a3 @dchelimsky dchelimsky committed Mar 23, 2012
Showing with 56 additions and 26 deletions.
  1. +2 −0 Changelog.md
  2. +12 −8 lib/rspec/core/example.rb
  3. +39 −16 spec/rspec/core/example_spec.rb
  4. +3 −2 spec/rspec/core/pending_example_spec.rb
View
@@ -10,6 +10,8 @@ Enhancements
Bug fixes
* Properly parse `SPEC_OPTS` options.
+* example.description returns the location of the example if there is no
+ explicit description or matcher-generated description.
### 2.9.0 / 2012-03-17
[full changelog](http://github.com/rspec/rspec-core/compare/v2.8.0...v2.9.0)
@@ -17,7 +17,16 @@ def self.delegate_to_metadata(*keys)
end
end
- delegate_to_metadata :description, :full_description, :execution_result, :file_path, :pending, :location
+ delegate_to_metadata :full_description, :execution_result, :file_path, :pending, :location
+
+ # Returns the string submitted to `example` or its aliases (e.g.
+ # `specify`, `it`, etc). If no string is submitted (e.g. `it { should
+ # do_something }`) it returns the message generated by the matcher if
+ # there is one, otherwise returns a message including the location of the
+ # example.
+ def description
+ metadata[:description].to_s.empty? ? "example at #{location}" : metadata[:description]
+ end
# @attr_reader
#
@@ -225,17 +234,12 @@ def run_after_each
end
def assign_auto_description
- if description.empty? and !pending?
+ if metadata[:description].empty? and !pending?
if RSpec.configuration.expecting_with_rspec?
metadata[:description] = RSpec::Matchers.generated_description
- RSpec::Matchers.clear_generated_description
- else
- raise NotImplementedError.new(
- "Generated descriptions are only supported when you use rspec-expectations. " +
- "You must give every example an explicit description."
- )
end
end
+ RSpec::Matchers.clear_generated_description
end
def record(results={})
@@ -45,11 +45,7 @@ def metadata_hash(*args)
end
end
- describe "auto-generated example descriptions" do
- let(:generated_description) { "the generated description" }
- let(:rspec_example) { example_group.specify { 5.should eq(5) } }
- before(:each) { RSpec::Matchers.stub(:generated_description => generated_description) }
-
+ describe "when there is no explicit description" do
def expect_with(*frameworks)
RSpec.configuration.stub(:expecting_with_rspec?).and_return(frameworks.include?(:rspec))
@@ -65,35 +61,62 @@ def assert(val)
context "when `expect_with :rspec` is configured" do
before(:each) { expect_with :rspec }
- it "generates a description for an example with no description" do
- expect {
- example_group.run
- }.to change { rspec_example.metadata[:description] }.from("").to(generated_description)
+ it "uses the matcher-generated description" do
+ example_group.example { 5.should eq(5) }
+ example_group.run
+ example_group.examples.first.description.should eq("should eq 5")
+ end
+
+ it "uses the file and line number if there is no matcher-generated description" do
+ example = example_group.example {}
+ example_group.run
+ example.description.should match(/example at #{__FILE__}:#{__LINE__ -2}/)
+ end
+
+ it "uses the file and line number if there is an error before the matcher" do
+ example = example_group.example { 5.should eq(5) }
+ example_group.before { raise }
+ example_group.run
+ example.description.should match(/example at #{__FILE__}:#{__LINE__ -3}/)
end
end
context "when `expect_with :rspec, :stdlib` is configured" do
before(:each) { expect_with :rspec, :stdlib }
- it "generates a description for an example with no description" do
- expect {
- example_group.run
- }.to change { rspec_example.metadata[:description] }.from("").to(generated_description)
+ it "uses the matcher-generated description" do
+ example_group.example { 5.should eq(5) }
+ example_group.run
+ example_group.examples.first.description.should eq("should eq 5")
+ end
+
+ it "uses the file and line number if there is no matcher-generated description" do
+ example = example_group.example {}
+ example_group.run
+ example.description.should match(/example at #{__FILE__}:#{__LINE__ -2}/)
+ end
+
+ it "uses the file and line number if there is an error before the matcher" do
+ example = example_group.example { 5.should eq(5) }
+ example_group.before { raise }
+ example_group.run
+ example.description.should match(/example at #{__FILE__}:#{__LINE__ -3}/)
end
end
context "when `expect_with :stdlib` is configured" do
- let!(:stdlib_example) { example_group.specify { assert 5 == 5 } }
before(:each) { expect_with :stdlib }
it "does not attempt to get the generated description from RSpec::Matchers" do
RSpec::Matchers.should_not_receive(:generated_description)
+ example_group.example { assert 5 == 5 }
example_group.run
end
- it "fails an example with no description" do
+ it "uses the file and line number" do
+ example = example_group.example { assert 5 == 5 }
example_group.run
- stdlib_example.should have_failed_with(NotImplementedError)
+ example.description.should match(/example at #{__FILE__}:#{__LINE__ -2}/)
end
end
end
@@ -70,9 +70,10 @@
end
example = group.examples.last
example.run(group.new, stub.as_null_object)
- example.description.should be_empty
+ example.description.should match(/example at/)
end
end
+
context "after another example with some assertion" do
it "does not show any message" do
group = RSpec::Core::ExampleGroup.describe('group') do
@@ -85,7 +86,7 @@
end
example = group.examples.last
example.run(group.new, stub.as_null_object)
- example.description.should be_empty
+ example.description.should match(/example at/)
end
end
end

0 comments on commit 34cb8f9

Please sign in to comment.