require'rspec/core/formatters/base_formatter'require'set'moduleRSpecmoduleCoremoduleFormatters# Base for all of RSpec's built-in formatters. See RSpec::Core::Formatters::BaseFormatter# to learn more about all of the methods called by the reporter.## @see RSpec::Core::Formatters::BaseFormatter# @see RSpec::Core::ReporterclassBaseTextFormatter<BaseFormatterFormatters.registerself,:message,:dump_summary,:dump_failures,:dump_profile,:dump_pending,:seeddefmessage(notification)output.putsnotification.messageenddefdump_failures(notification)returniffailed_examples.empty?output.putsoutput.puts"Failures:"failed_examples.each_with_indexdo|example,index|output.putspending_fixed?(example)?dump_pending_fixed(example,index):dump_failure(example,index)dump_backtrace(example)endend# @api public## Colorizes the output red for failure, yellow for# pending, and green otherwise.## @param [String] stringdefcolorise_summary(summary)ifsummary.failure_count>0color(summary.summary_line,RSpec.configuration.failure_color)elsifsummary.pending_count>0color(summary.summary_line,RSpec.configuration.pending_color)elsecolor(summary.summary_line,RSpec.configuration.success_color)endenddefdump_summary(summary)dump_profileunlessmute_profile_output?(summary.failure_count)output.puts"\nFinished in #{format_duration(summary.duration)}\n"output.putscolorise_summary(summary)dump_commands_to_rerun_failed_examplesend# @api public## Outputs commands which can be used to re-run failed examples.#defdump_commands_to_rerun_failed_examplesreturniffailed_examples.empty?output.putsoutput.puts("Failed examples:")output.putsfailed_examples.eachdo|example|output.puts(failure_color("rspec #{RSpec::Core::Metadata::relative_path(example.location)}")+" "+detail_color("# #{example.full_description}"))endend# @api public## Outputs the slowest examples and example groups in a report when using `--profile COUNT` (default 10).#defdump_profiledump_profile_slowest_examplesdump_profile_slowest_example_groupsend# @api privatedefdump_profile_slowest_examplessorted_examples=slowest_examplestime_taken=sorted_examples[:slows]/sorted_examples[:total]percentage='%.1f'%((time_taken.nan??0.0:time_taken)*100)output.puts"\nTop #{sorted_examples[:examples].size} slowest examples (#{format_seconds(sorted_examples[:slows])} seconds, #{percentage}% of total time):\n"sorted_examples[:examples].eachdo|example|output.puts" #{example.full_description}"output.puts" #{bold(format_seconds(example.execution_result[:run_time]))}#{bold("seconds")}#{format_caller(example.location)}"endend# @api privatedefdump_profile_slowest_example_groupssorted_groups=slowest_groupsreturnifsorted_groups.empty?output.puts"\nTop #{sorted_groups.size} slowest example groups:"slowest_groups.eachdo|loc,hash|average="#{bold(format_seconds(hash[:average]))}#{bold("seconds")} average"total="#{format_seconds(hash[:total_time])} seconds"count=pluralize(hash[:count],"example")output.puts" #{hash[:description]}"output.puts" #{average} (#{total} / #{count}) #{loc}"endenddefdump_pending(notification)unlesspending_examples.empty?output.putsoutput.puts"Pending:"pending_examples.eachdo|pending_example|output.putspending_color(" #{pending_example.full_description}")output.putsdetail_color(" # #{pending_example.execution_result[:pending_message]}")output.putsdetail_color(" # #{format_caller(pending_example.location)}")endendenddefseed(notification)returnunlessnotification.seed_used?output.putsoutput.puts"Randomized with seed #{notification.seed}"output.putsenddefclose(notification)output.closeifIO===output&&output!=$stdoutendVT100_COLORS={:black=>30,:red=>31,:green=>32,:yellow=>33,:blue=>34,:magenta=>35,:cyan=>36,:white=>37}VT100_COLOR_CODES=VT100_COLORS.values.to_setdefcolor_code_for(code_or_symbol)ifVT100_COLOR_CODES.include?(code_or_symbol)code_or_symbolelseVT100_COLORS.fetch(code_or_symbol)docolor_code_for(:white)endendenddefcolorize(text,code_or_symbol)"\e[#{color_code_for(code_or_symbol)}m#{text}\e[0m"endprotecteddefbold(text)color_enabled??"\e[1m#{text}\e[0m":textenddefcolor(text,color_code)color_enabled??colorize(text,color_code):textenddeffailure_color(text)color(text,RSpec.configuration.failure_color)enddefsuccess_color(text)color(text,RSpec.configuration.success_color)enddefpending_color(text)color(text,RSpec.configuration.pending_color)enddeffixed_color(text)color(text,RSpec.configuration.fixed_color)enddefdetail_color(text)color(text,RSpec.configuration.detail_color)enddefdefault_color(text)color(text,RSpec.configuration.default_color)enddefred(text)RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#red",:replacement=>"#failure_color")color(text,:red)enddefgreen(text)RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#green",:replacement=>"#success_color")color(text,:green)enddefyellow(text)RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#yellow",:replacement=>"#pending_color")color(text,:yellow)enddefblue(text)RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#blue",:replacement=>"#fixed_color")color(text,:blue)enddefmagenta(text)RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#magenta")color(text,:magenta)enddefcyan(text)RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#cyan",:replacement=>"#detail_color")color(text,:cyan)enddefwhite(text)RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#white",:replacement=>"#default_color")color(text,:white)enddefshort_padding' 'enddeflong_padding' 'endprivatedefformat_caller(caller_info)configuration.backtrace_formatter.backtrace_line(caller_info.to_s.split(':in `block').first)enddefdump_backtrace(example,key=:exception)format_backtrace(example.execution_result[key].backtrace,example).eachdo|backtrace_info|output.putsdetail_color("#{long_padding}# #{backtrace_info}")endenddefdump_pending_fixed(example,index)output.puts"#{short_padding}#{index.next}) #{example.full_description} FIXED"output.putsfixed_color("#{long_padding}Expected pending '#{example.metadata[:execution_result][:pending_message]}' to fail. No Error was raised.")enddefpending_fixed?(example)example.execution_result[:pending_fixed]enddefdump_failure(example,index)output.puts"#{short_padding}#{index.next}) #{example.full_description}"dump_failure_info(example)enddefdump_failure_info(example,key=:exception)exception=example.execution_result[key]exception_class_name=exception_class_name_for(exception)output.puts"#{long_padding}#{failure_color("Failure/Error:")}#{failure_color(read_failed_line(exception,example).strip)}"output.puts"#{long_padding}#{failure_color(exception_class_name)}:"unlessexception_class_name=~/RSpec/exception.message.to_s.split("\n").each{|line|output.puts"#{long_padding}#{failure_color(line)}"}ifexception.messageifshared_group=find_shared_group(example)dump_shared_failure_info(shared_group)endenddefexception_class_name_for(exception)name=exception.class.name.to_sname="(anonymous error class)"ifname==''nameenddefdump_shared_failure_info(group)output.puts"#{long_padding}Shared Example Group: \"#{group.metadata[:shared_group_name]}\" called from "+"#{configuration.backtrace_formatter.backtrace_line(group.metadata[:example_group][:location])}"enddeffind_shared_group(example)group_and_parent_groups(example).find{|group|group.metadata[:shared_group_name]}enddefgroup_and_parent_groups(example)example.example_group.parent_groups+[example.example_group]endendendendend