require 'erb' require 'rspec/core/formatters/base_text_formatter' module RSpec module Core module Formatters class HtmlFormatter < BaseTextFormatter include ERB::Util # for the #h method def initialize(output) super(output) @example_group_number = 0 @example_number = 0 @header_red = nil end private def method_missing(m, *a, &b) # no-op end public def message(message) end # The number of the currently running example_group def example_group_number @example_group_number end # The number of the currently running example (a global counter) def example_number @example_number end def start(example_count) super(example_count) @output.puts html_header @output.puts report_header @output.flush end def example_group_started(example_group) super(example_group) @example_group_red = false @example_group_number += 1 unless example_group_number == 1 @output.puts " " @output.puts "" end @output.puts "
" @output.puts "
" @output.puts "
#{h(example_group.description)}
" @output.flush end def start_dump @output.puts "
" @output.puts "
" @output.flush end def example_started(example) super(example) @example_number += 1 end def example_passed(example) move_progress @output.puts "
#{h(example.description)}#{sprintf("%.5f", example.execution_result[:run_time])}s
" @output.flush end def example_failed(example) super(example) exception = example.metadata[:execution_result][:exception] extra = extra_failure_content(exception) @output.puts " " unless @header_red @header_red = true @output.puts " " unless @example_group_red @output.puts " " unless @example_group_red @example_group_red = true move_progress @output.puts "
" @output.puts " #{h(example.description)}" @output.puts " #{sprintf('%.5f', example.execution_result[:run_time])}s" @output.puts "
" @output.puts "
#{h(exception.message)}
" unless exception.nil? @output.puts "
#{format_backtrace(exception.backtrace, example).join("\n")}
" if exception @output.puts extra unless extra == "" @output.puts "
" @output.puts "
" @output.flush end def example_pending(example) message = example.metadata[:execution_result][:pending_message] @output.puts " " unless @header_red @output.puts " " unless @example_group_red @output.puts " " unless @example_group_red move_progress @output.puts "
#{h(example.description)} (PENDING: #{h(message)})
" @output.flush end # Override this method if you wish to output extra HTML for a failed spec. For example, you # could output links to images or other files produced during the specs. # def extra_failure_content(exception) require 'rspec/core/formatters/snippet_extractor' backtrace = exception.backtrace.map {|line| backtrace_line(line)} backtrace.compact! @snippet_extractor ||= SnippetExtractor.new "
#{@snippet_extractor.snippet(backtrace)}
" end def move_progress @output.puts " " @output.flush end def percent_done result = 100.0 if @example_count > 0 result = ((example_number).to_f / @example_count.to_f * 1000).to_i / 10.0 end result end def dump_failures end def dump_pending end def dump_summary(duration, example_count, failure_count, pending_count) # TODO - kill dry_run? if dry_run? totals = "This was a dry-run" else totals = "#{example_count} example#{'s' unless example_count == 1}, " totals << "#{failure_count} failure#{'s' unless failure_count == 1}" totals << ", #{pending_count} pending" if pending_count > 0 end @output.puts "" @output.puts "" @output.puts "" @output.puts "" @output.puts "" @output.puts "" @output.flush end def current_indentation "style=\"margin-left: #{(example_group.ancestors.size - 1) * 15}px;\"" end def html_header <<-EOF RSpec results EOF end def report_header <<-EOF

RSpec Code Examples

 

 

EOF end def global_scripts <<-EOF function addClass(element_id, classname) { document.getElementById(element_id).className += (" " + classname); } function removeClass(element_id, classname) { var elem = document.getElementById(element_id); var classlist = elem.className.replace(classname,''); elem.className = classlist; } function moveProgressBar(percentDone) { document.getElementById("rspec-header").style.width = percentDone +"%"; } function makeRed(element_id) { removeClass(element_id, 'passed'); removeClass(element_id, 'not_implemented'); addClass(element_id,'failed'); } function makeYellow(element_id) { var elem = document.getElementById(element_id); if (elem.className.indexOf("failed") == -1) { // class doesn't includes failed if (elem.className.indexOf("not_implemented") == -1) { // class doesn't include not_implemented removeClass(element_id, 'passed'); addClass(element_id,'not_implemented'); } } } function apply_filters() { var passed_filter = document.getElementById('passed_checkbox').checked; var failed_filter = document.getElementById('failed_checkbox').checked; var pending_filter = document.getElementById('pending_checkbox').checked; assign_display_style("example passed", passed_filter); assign_display_style("example failed", failed_filter); assign_display_style("example not_implemented", pending_filter); assign_display_style_for_group("example_group passed", passed_filter); assign_display_style_for_group("example_group not_implemented", pending_filter, pending_filter || passed_filter); assign_display_style_for_group("example_group failed", failed_filter, failed_filter || pending_filter || passed_filter); } function get_display_style(display_flag) { var style_mode = 'none'; if (display_flag == true) { style_mode = 'block'; } return style_mode; } function assign_display_style(classname, display_flag) { var style_mode = get_display_style(display_flag); var elems = document.getElementsByClassName(classname) for (var i=0; i