Skip to content

Commit

Permalink
Improve indentation support in ExceptionPrinter.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed May 16, 2015
1 parent e84ba30 commit 96678ca
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/rspec/core/formatters/exception_presenter.rb
Expand Up @@ -12,6 +12,7 @@ def initialize(exception, example, options={})
@message_color = options.fetch(:message_color) { RSpec.configuration.failure_color }
@description = options.fetch(:description) { example.full_description }
@detail_formatter = options.fetch(:detail_formatter) { lambda { |*| } }
@indentation = options.fetch(:indentation, 2)
@failure_lines = options[:failure_lines]
end

Expand All @@ -36,8 +37,9 @@ def colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCo
end

def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::ConsoleCodes)
"\n #{failure_number}) #{description}#{detail_formatter.call(example, colorizer)}" \
"\n#{formatted_message_and_backtrace(colorizer)}"
alignment_basis = "#{' ' * @indentation}#{failure_number}) "
"\n#{alignment_basis}#{description}#{detail_formatter.call(example, colorizer)}" \
"\n#{formatted_message_and_backtrace(colorizer, alignment_basis.length)}"
end

private
Expand Down Expand Up @@ -117,15 +119,13 @@ def find_failed_line
end
end

def formatted_message_and_backtrace(colorizer)
formatted = ""
def formatted_message_and_backtrace(colorizer, indentation)
lines = colorized_message_lines(colorizer) + colorized_formatted_backtrace(colorizer)

colorized_message_lines(colorizer).each do |line|
formatted << RSpec::Support::EncodedString.new(" #{line}\n", encoding_of(formatted))
end
formatted = ""

colorized_formatted_backtrace(colorizer).each do |line|
formatted << RSpec::Support::EncodedString.new(" #{line}\n", encoding_of(formatted))
lines.each do |line|
formatted << RSpec::Support::EncodedString.new("#{' ' * indentation}#{line}\n", encoding_of(formatted))
end

formatted
Expand Down
42 changes: 42 additions & 0 deletions spec/rspec/core/formatters/exception_presenter_spec.rb
Expand Up @@ -12,6 +12,48 @@ module RSpec::Core
example.metadata[:absolute_file_path] = __FILE__
end

describe "#fully_formatted" do
line_num = __LINE__ + 2
let(:exception) { instance_double(Exception, :message => "Boom\nBam", :backtrace => [ "#{__FILE__}:#{line_num}"]) }
# The failure happened here!

it "formats the exception with all the normal details" do
expect(presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Example
| Failure/Error: # The failure happened here!
| Boom
| Bam
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
EOS
end

it "indents properly when given a multiple-digit failure index" do
expect(presenter.fully_formatted(100)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 100) Example
| Failure/Error: # The failure happened here!
| Boom
| Bam
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
EOS
end

it "allows the caller to specify additional indentation" do
presenter = Formatters::ExceptionPresenter.new(exception, example, :indentation => 4)

expect(presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Example
| Failure/Error: # The failure happened here!
| Boom
| Bam
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
EOS
end

end

describe "#read_failed_line" do
def read_failed_line
presenter.send(:read_failed_line)
Expand Down

0 comments on commit 96678ca

Please sign in to comment.