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 method_missing(m, *a, &b) # no-op end def message(message) end def initialize(output) super(output) @example_group_number = 0 @example_number = 0 @header_red = nil 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)}
" @output.flush end def example_failed(example) counter = 0 exception = example.metadata[:execution_result][:exception_encountered] extra = extra_failure_content(exception) failure_style = 'failed' failure_style = RSpec::Core::PendingExampleFixedError === exception ? 'pending_fixed' : 'failed' @output.puts " " unless @header_red @header_red = true @output.puts " " unless @example_group_red @example_group_red = true move_progress @output.puts "
" @output.puts " #{h(example.description)}" @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 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' @snippet_extractor ||= SnippetExtractor.new "
#{@snippet_extractor.snippet(exception)}
" 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}, #{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 html_header <<-EOF RSpec results EOF end def report_header <<-EOF

RSpec Code Examples

 

 

EOF end def global_scripts <<-EOF function moveProgressBar(percentDone) { document.getElementById("rspec-header").style.width = percentDone +"%"; } function makeRed(element_id) { document.getElementById(element_id).style.background = '#C40D0D'; document.getElementById(element_id).style.color = '#FFFFFF'; } function makeYellow(element_id) { if (element_id == "rspec-header" && document.getElementById(element_id).style.background != '#C40D0D') { document.getElementById(element_id).style.background = '#FAF834'; document.getElementById(element_id).style.color = '#000000'; } else { document.getElementById(element_id).style.background = '#FAF834'; document.getElementById(element_id).style.color = '#000000'; } } EOF end def global_styles <<-EOF #rspec-header { background: #65C400; color: #fff; height: 4em; } .rspec-report h1 { margin: 0px 10px 0px 10px; padding: 10px; font-family: "Lucida Grande", Helvetica, sans-serif; font-size: 1.8em; position: absolute; } #summary { margin: 0; padding: 5px 10px; font-family: "Lucida Grande", Helvetica, sans-serif; text-align: right; top: 0px; right: 0px; float:right; } #summary p { margin: 0 0 0 2px; } #summary #totals { font-size: 1.2em; } .example_group { margin: 0 10px 5px; background: #fff; } dl { margin: 0; padding: 0 0 5px; font: normal 11px "Lucida Grande", Helvetica, sans-serif; } dt { padding: 3px; background: #65C400; color: #fff; font-weight: bold; } dd { margin: 5px 0 5px 5px; padding: 3px 3px 3px 18px; } dd.spec.passed { border-left: 5px solid #65C400; border-bottom: 1px solid #65C400; background: #DBFFB4; color: #3D7700; } dd.spec.failed { border-left: 5px solid #C20000; border-bottom: 1px solid #C20000; color: #C20000; background: #FFFBD3; } dd.spec.not_implemented { border-left: 5px solid #FAF834; border-bottom: 1px solid #FAF834; background: #FCFB98; color: #131313; } dd.spec.pending_fixed { border-left: 5px solid #0000C2; border-bottom: 1px solid #0000C2; color: #0000C2; background: #D3FBFF; } .backtrace { color: #000; font-size: 12px; } a { color: #BE5C00; } /* Ruby code, style similar to vibrant ink */ .ruby { font-size: 12px; font-family: monospace; color: white; background-color: black; padding: 0.1em 0 0.2em 0; } .ruby .keyword { color: #FF6600; } .ruby .constant { color: #339999; } .ruby .attribute { color: white; } .ruby .global { color: white; } .ruby .module { color: white; } .ruby .class { color: white; } .ruby .string { color: #66FF00; } .ruby .ident { color: white; } .ruby .method { color: #FFCC00; } .ruby .number { color: white; } .ruby .char { color: white; } .ruby .comment { color: #9933CC; } .ruby .symbol { color: white; } .ruby .regex { color: #44B4CC; } .ruby .punct { color: white; } .ruby .escape { color: white; } .ruby .interp { color: white; } .ruby .expr { color: white; } .ruby .offending { background-color: gray; } .ruby .linenum { width: 75px; padding: 0.1em 1em 0.2em 0; color: #000000; background-color: #FFFBD3; } EOF end end end end end