Skip to content

Commit

Permalink
Implementation for issue rspec#82
Browse files Browse the repository at this point in the history
  • Loading branch information
justinko committed Aug 2, 2010
1 parent d1d0d82 commit a62a575
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 17 deletions.
4 changes: 3 additions & 1 deletion lib/rspec/core/example_group.rb
Expand Up @@ -64,10 +64,12 @@ def self.#{new_name}(name, &customization_block)
shared_block = world.shared_example_groups[name]
raise "Could not find shared example group named \#{name.inspect}" unless shared_block
describe("#{report_label || "it should behave like"} \#{name}") do
group = describe("#{report_label || "it should behave like"} \#{name}") do
module_eval &shared_block
module_eval &customization_block if customization_block
end
group.metadata[:shared_group_name] = name
group
end
END_RUBY
end
Expand Down
8 changes: 8 additions & 0 deletions lib/rspec/core/formatters/base_text_formatter.rb
Expand Up @@ -26,6 +26,14 @@ def dump_failures
exception.message.split("\n").each do |line|
output.puts "#{padding}#{red(line)}"
end

example.example_group.ancestors.push(example.example_group).each do |group|
if group.metadata[:shared_group_name]
output.puts "#{padding}Shared Example Group: \"#{group.metadata[:shared_group_name]}\" called from " +
"#{backtrace_line(group.metadata[:example_group][:location])}"
break
end
end
end

format_backtrace(exception.backtrace, example).each do |backtrace_info|
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec/core/shared_example_group.rb
Expand Up @@ -22,7 +22,8 @@ def self.created_from_caller(other_caller)
end

def self.included(kls)
kls.module_eval(&@shared_block)
kls.describe(&@shared_block)
kls.children.first.metadata[:shared_group_name] = name
end
end

Expand Down
112 changes: 99 additions & 13 deletions spec/rspec/core/formatters/base_text_formatter_spec.rb
Expand Up @@ -3,22 +3,22 @@
module RSpec::Core::Formatters

describe BaseTextFormatter do
let(:output) { StringIO.new }
let(:formatter) { RSpec::Core::Formatters::BaseTextFormatter.new(output) }

describe "#summary_line" do
let(:output) { StringIO.new }
let(:formatter) { RSpec::Core::Formatters::BaseTextFormatter.new(output) }

context "with 0s" do
it "outputs pluralized (excluding pending)" do
formatter.summary_line(0,0,0).should eq("0 examples, 0 failures")
end
end

context "with 1s" do
it "outputs singular (including pending)" do
formatter.summary_line(1,1,1).should eq("1 example, 1 failure, 1 pending")
end
end

context "with 2s" do
it "outputs pluralized (including pending)" do
formatter.summary_line(2,2,2).should eq("2 examples, 2 failures, 2 pending")
Expand All @@ -27,18 +27,104 @@ module RSpec::Core::Formatters
end

describe "#dump_failures" do
it "preserves formatting" do
output = StringIO.new
group = RSpec::Core::ExampleGroup.describe("group name")
example = group.example("example name") { "this".should eq("that") }
formatter = RSpec::Core::Formatters::BaseTextFormatter.new(output)
let(:group) { RSpec::Core::ExampleGroup.describe("group name") }

before { RSpec.configuration.stub(:color_enabled?) { false } }

def run_all_and_dump_failures
group.run_all(formatter)

RSpec.configuration.stub(:color_enabled?) { false }
formatter.dump_failures
end

it "preserves formatting" do
group.example("example name") { "this".should eq("that") }

run_all_and_dump_failures

output.string.should =~ /group name example name/m
output.string.should =~ /(\s+)expected \"that\"\n\1 got \"this\"/m
end

context 'for #share_examples_for' do
it 'outputs the name and location' do

share_examples_for 'foo bar' do
it("example name") { "this".should eq("that") }
end

line = __LINE__.next
group.it_should_behave_like('foo bar')

run_all_and_dump_failures

output.string.should include(
'Shared Example Group: "foo bar" called from ' +
"./spec/rspec/core/formatters/base_text_formatter_spec.rb:#{line}"
)
end

context 'that contains nested example groups' do
it 'outputs the name and location' do
share_examples_for 'foo bar' do
describe 'nested group' do
it("example name") { "this".should eq("that") }
end
end

line = __LINE__.next
group.it_should_behave_like('foo bar')

run_all_and_dump_failures

output.string.should include(
'Shared Example Group: "foo bar" called from ' +
"./spec/rspec/core/formatters/base_text_formatter_spec.rb:#{line}"
)
end
end
end

context 'for #share_as' do
it 'outputs the name and location' do

share_as :FooBar do
it("example name") { "this".should eq("that") }
end

line = __LINE__.next
group.send(:include, FooBar)

run_all_and_dump_failures

output.string.should include(
'Shared Example Group: "FooBar" called from ' +
"./spec/rspec/core/formatters/base_text_formatter_spec.rb:#{line}"
)
end

context 'that contains nested example groups' do
it 'outputs the name and location' do

share_as :NestedFoo do
describe 'nested group' do
describe 'hell' do
it("example name") { "this".should eq("that") }
end
end
end

line = __LINE__.next
group.send(:include, NestedFoo)

run_all_and_dump_failures

output.string.should include(
'Shared Example Group: "NestedFoo" called from ' +
"./spec/rspec/core/formatters/base_text_formatter_spec.rb:#{line}"
)
end
end
end
end
end
end
end
7 changes: 5 additions & 2 deletions spec/rspec/core/shared_example_group_spec.rb
Expand Up @@ -40,8 +40,11 @@ module RSpec::Core
end
end
group = ExampleGroup.describe('group') { include Cornucopia }
group.examples.length.should == 1
group.examples.first.metadata[:description].should == "is plentiful"
phantom_group = group.children.first
phantom_group.description.should eql("")
phantom_group.metadata[:shared_group_name].should eql('Cornucopia')
phantom_group.examples.length.should == 1
phantom_group.examples.first.metadata[:description].should == "is plentiful"
end
end

Expand Down

0 comments on commit a62a575

Please sign in to comment.