Class: RSpec::Core::Formatters::ExceptionPresenter Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | rspec-core/lib/rspec/core/formatters/exception_presenter.rb |
Constant Summary
-
PENDING_DETAIL_FORMATTER =
# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 453Proc.new do |example, colorizer| colorizer.wrap("# #{example.execution_result.}", :detail) end
Class Method Summary
- .new(exception, example, options = {}) ⇒ ExceptionPresenter constructor Internal use only
Instance Attribute Summary
- #description readonly Internal use only
- #example readonly Internal use only
- #exception readonly Internal use only
- #backtrace_formatter readonly private Internal use only
- #detail_formatter readonly private Internal use only
- #extra_detail_formatter readonly private Internal use only
- #message_color readonly private Internal use only
Instance Method Summary
- #colorized_formatted_backtrace(colorizer = ::RSpec::Core::Formatters::ConsoleCodes) Internal use only
- #colorized_message_lines(colorizer = ::RSpec::Core::Formatters::ConsoleCodes) Internal use only
- #formatted_backtrace(exception = @exception) Internal use only
-
#formatted_cause(_)
Internal use only
:nocov:
- #fully_formatted(failure_number, colorizer = ::RSpec::Core::Formatters::ConsoleCodes) Internal use only
- #fully_formatted_lines(failure_number, colorizer) Internal use only
- #message_lines Internal use only
- #add_shared_group_lines(lines, colorizer) private Internal use only
-
#encoded_description(description)
private
Internal use only
:nocov:
-
#encoded_string(string)
private
See additional method definition at line 125.
-
#encoding_of(_string)
private
Internal use only
:nocov:
- #exception_backtrace private Internal use only
- #exception_class_name(exception = @exception) private Internal use only
-
#exception_lines
private
Internal use only
:nocov:
-
#exception_message_string(exception)
private
See additional method definition at line 190.
- #extra_failure_lines private Internal use only
- #failure_lines private Internal use only
- #failure_slash_error_lines private Internal use only
- #final_exception(exception, previous = []) private Internal use only
- #find_failed_line private Internal use only
- #formatted_message_and_backtrace(colorizer) private Internal use only
- #indent_lines(lines, failure_number) private Internal use only
- #read_failed_lines private Internal use only
Instance Attribute Details
#backtrace_formatter (readonly, private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 12
attr_reader :exception, :example, :description, :, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
#description (readonly)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 12
attr_reader :exception, :example, :description, :, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
#detail_formatter (readonly, private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 12
attr_reader :exception, :example, :description, :, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
#example (readonly)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 12
attr_reader :exception, :example, :description, :, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
#exception (readonly)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 12
attr_reader :exception, :example, :description, :, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
#extra_detail_formatter (readonly, private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 12
attr_reader :exception, :example, :description, :, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
#message_color (readonly, private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 12
attr_reader :exception, :example, :description, :, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
Instance Method Details
#colorized_formatted_backtrace(colorizer = ::RSpec::Core::Formatters::ConsoleCodes)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 83
def colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCodes) formatted_backtrace.map do |backtrace_info| colorizer.wrap "# #{backtrace_info}", RSpec.configuration.detail_color end end
#colorized_message_lines(colorizer = ::RSpec::Core::Formatters::ConsoleCodes)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 38
def (colorizer=::RSpec::Core::Formatters::ConsoleCodes) add_shared_group_lines(failure_lines, colorizer).map do |line| colorizer.wrap line, end end
#encoded_description(description) (private)
:nocov:
# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 297
def encoded_description(description) return if description.nil? encoded_string(description) end
#encoded_string(string) (private)
See additional method definition at line 125.
# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 133
def encoded_string(string) RSpec::Support::EncodedString.new(string, Encoding.default_external) end
#encoding_of(_string) (private)
:nocov:
# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 130
def encoding_of(string) string.encoding end
#exception_backtrace (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 303
def exception_backtrace exception.backtrace || [] end
#exception_class_name(exception = @exception) (private)
[ GitHub ]#exception_lines (private)
:nocov:
# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 209
def exception_lines @exception_lines ||= begin lines = [] lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/ encoded_string( (exception)).split("\n").each do |line| lines << (line.empty? ? line : " #{line}") end lines end end
#exception_message_string(exception) (private)
See additional method definition at line 190.
#extra_failure_lines (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 220
def extra_failure_lines @extra_failure_lines ||= begin lines = Array(example. [:extra_failure_lines]) unless lines.empty? lines.unshift('') unless lines.first == '' lines.push('') unless lines.last == '' end lines end end
#failure_lines (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 161
def failure_lines @failure_lines ||= [].tap do |lines| lines.concat(failure_slash_error_lines) sections = [failure_slash_error_lines, exception_lines] if sections.any? { |section| section.size > 1 } && !exception_lines.first.empty? lines << '' end lines.concat(exception_lines) lines.concat(extra_failure_lines) end end
#failure_slash_error_lines (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 175
def failure_slash_error_lines lines = read_failed_lines if lines.count == 1 lines[0] = "Failure/Error: #{lines[0].strip}" else least_indentation = SnippetExtractor.least_indentation_from(lines) lines = lines.map { |line| line.sub(/^#{least_indentation}/, ' ') } lines.unshift('Failure/Error:') end lines end
#final_exception(exception, previous = []) (private)
[ GitHub ]#find_failed_line (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 267
def find_failed_line line_regex = RSpec.configuration.in_project_source_dir_regex loaded_spec_files = RSpec.configuration.loaded_spec_files exception_backtrace.reject! do |line| line.start_with?("<internal:") end exception_backtrace.find do |line| next unless (line_path = line[/(.+?):(\d+)(|:\d+)/, 1]) path = File. (line_path) loaded_spec_files.include?(path) || path =~ line_regex end || exception_backtrace.first end
#formatted_backtrace(exception = @exception)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 44
def formatted_backtrace(exception=@exception) backtrace_formatter.format_backtrace(exception.backtrace, example. ) + formatted_cause(exception) end
#formatted_cause(_)
:nocov:
# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 77
def formatted_cause(exception) last_cause = final_exception(exception, [exception]) cause = [] if exception.cause cause << '------------------' cause << '--- Caused by: ---' cause << "#{exception_class_name(last_cause)}:" unless exception_class_name(last_cause) =~ /RSpec/ encoded_string( (last_cause)).split("\n").each do |line| cause << " #{line}" end unless last_cause.backtrace.nil? || last_cause.backtrace.empty? lines = backtrace_formatter.format_backtrace(last_cause.backtrace, example. ) lines = [lines[0]] unless RSpec.configuration.full_cause_backtrace # rubocop:disable Metrics/BlockNesting lines.each do |line| cause << (" #{line}") end end end cause end
#formatted_message_and_backtrace(colorizer) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 282
def (colorizer) lines = (colorizer) + colorized_formatted_backtrace(colorizer) encoding = encoding_of("") lines.map do |line| RSpec::Support::EncodedString.new(line, encoding) end end
#fully_formatted(failure_number, colorizer = ::RSpec::Core::Formatters::ConsoleCodes)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 89
def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::ConsoleCodes) lines = fully_formatted_lines(failure_number, colorizer) lines.join("\n") << "\n" end
#fully_formatted_lines(failure_number, colorizer)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 94
def fully_formatted_lines(failure_number, colorizer) lines = [ encoded_description(description), detail_formatter.call(example, colorizer), (colorizer), extra_detail_formatter.call(failure_number, colorizer), ].compact.flatten lines = indent_lines(lines, failure_number) lines.unshift("") lines end
#indent_lines(lines, failure_number) (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 139
def indent_lines(lines, failure_number) alignment_basis = ' ' * @indentation alignment_basis << "#{failure_number}) " if failure_number indentation = ' ' * alignment_basis.length lines.each_with_index.map do |line, index| if index == 0 "#{alignment_basis}#{line}" elsif line.empty? line else "#{indentation}#{line}" end end end
#message_lines
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 34
def add_shared_group_lines(failure_lines, Notifications::NullColorizer) end
#read_failed_lines (private)
[ GitHub ]# File 'rspec-core/lib/rspec/core/formatters/exception_presenter.rb', line 241
def read_failed_lines matching_line = find_failed_line unless matching_line return ["Unable to find matching line from backtrace"] end file_and_line_number = matching_line.match(/(.+?):(\d+)(|:\d+)/) unless file_and_line_number return ["Unable to infer file and line number from backtrace"] end file_path, line_number = file_and_line_number[1..2] max_line_count = RSpec.configuration.max_displayed_failure_line_count lines = SnippetExtractor.extract_expression_lines_at(file_path, line_number.to_i, max_line_count) RSpec.world.syntax_highlighter.highlight(lines) rescue SnippetExtractor::NoSuchFileError ["Unable to find #{file_path} to read failed line"] rescue SnippetExtractor::NoSuchLineError ["Unable to find matching line in #{file_path}"] rescue SecurityError # :nocov: - SecurityError is no longer produced starting in ruby 2.7 ["Unable to read failed line"] # :nocov: end