Class | Spec::Runner::Formatter::HtmlFormatter
In: | lib/spec/runner/formatter/html_formatter.rb
Parent: | BaseTextFormatter

### Methods

add_example_group dump_failure dump_summary example_failed example_group_number example_number example_passed example_pending example_started extra_failure_content global_scripts global_styles html_header method_missing move_progress new percent_done report_header start start_dump

### Included Modules

ERB::Util

### Public Class methods

new(options, output)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 10
    10:         def initialize(options, output)
    11:           super
    12:           @example_group_number = 0
    13:           @example_number = 0
    14:         end

### Public Instance methods

add_example_group(example_group)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 38
    38:         def add_example_group(example_group)
    39:           super
    40:           @example_group_red = false
    41:           @example_group_red = false
    42:           @example_group_number += 1
    43:           unless example_group_number == 1
    44:             @output.puts "  </dl>"
    45:             @output.puts "</div>"
    46:           end
    47:           @output.puts "<div class=\"example_group\">"
    48:           @output.puts "  <dl>"
    49:           @output.puts "  <dt id=\"example_group_#{example_group_number}\">#{h(example_group.description)}</dt>"
    50:           @output.flush
    51:         end

dump_failure(counter, failure)

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 118
    118:         def dump_failure(counter, failure)
    119:         end

dump_summary(duration, example_count, failure_count, pending_count)

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 121
    121:         def dump_summary(duration, example_count, failure_count, pending_count)
    122:           if dry_run?
    123:             totals = "This was a dry-run"
    124:           else
    125:             totals = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
    126:             totals << ", #{pending_count} pending" if pending_count > 0
    127:           end
    128:           @output.puts "<script type=\"text/javascript\">document.getElementById('duration').innerHTML = \"Finished in <strong>#{duration} seconds</strong>\";</script>"
    129:           @output.puts "<script type=\"text/javascript\">document.getElementById('totals').innerHTML = \"#{totals}\";</script>"
    130:           @output.puts "</div>"
    131:           @output.puts "</div>"
    132:           @output.puts "</body>"
    133:           @output.puts "</html>"
    134:           @output.flush
    135:         end

example_failed(example, counter, failure)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 69
    69:         def example_failed(example, counter, failure)
    70:           extra = extra_failure_content(failure)
    71:           failure_style = failure.pending_fixed? ? 'pending_fixed' : 'failed'
    72:           @output.puts "    <script type=\"text/javascript\">makeRed('rspec-header');</script>" unless @header_red
    73:           @header_red = true
    74:           @output.puts "    <script type=\"text/javascript\">makeRed('example_group_#{example_group_number}');</script>" unless @example_group_red
    75:           @example_group_red = true
    76:           move_progress
    77:           @output.puts "    <dd class=\"spec #{failure_style}\">"
    78:           @output.puts "      <span class=\"failed_spec_name\">#{h(example.description)}</span>"
    79:           @output.puts "      <div class=\"failure\" id=\"failure_#{counter}\">"
    80:           @output.puts "        <div class=\"message\"><pre>#{h(failure.exception.message)}</pre></div>" unless failure.exception.nil?
    81:           @output.puts "        <div class=\"backtrace\"><pre>#{format_backtrace(failure.exception.backtrace)}</pre></div>" unless failure.exception.nil?
    82:           @output.puts extra unless extra == ""
    83:           @output.puts "      </div>"
    84:           @output.puts "    </dd>"
    85:           @output.flush
    86:         end

example_group_number()

The number of the currently running example_group

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 21
    21:         def example_group_number
    22:           @example_group_number
    23:         end

example_number()

The number of the currently running example (a global counter)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 26
    26:         def example_number
    27:           @example_number
    28:         end

example_passed(example)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 63
    63:         def example_passed(example)
    64:           move_progress
    65:           @output.puts "    <dd class=\"spec passed\"><span class=\"passed_spec_name\">#{h(example.description)}</span></dd>"
    66:           @output.flush
    67:         end

example_pending(example, message)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 88
    88:         def example_pending(example, message)
    89:           @output.puts "    <script type=\"text/javascript\">makeYellow('rspec-header');</script>" unless @header_red
    90:           @output.puts "    <script type=\"text/javascript\">makeYellow('example_group_#{example_group_number}');</script>" unless @example_group_red
    91:           move_progress
    92:           @output.puts "    <dd class=\"spec not_implemented\"><span class=\"not_implemented_spec_name\">#{h(example.description)} (PENDING: #{h(message)})</span></dd>"
    93:           @output.flush
    94:         end

example_started(example)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 59
    59:         def example_started(example)
    60:           @example_number += 1
    61:         end

extra_failure_content(failure)

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.

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 99
     99:         def extra_failure_content(failure)
    100:           require 'spec/runner/formatter/snippet_extractor'
    101:           @snippet_extractor ||= SnippetExtractor.new
    102:           "    <pre class=\"ruby\"><code>#{@snippet_extractor.snippet(failure.exception)}</code></pre>"
    103:         end

global_scripts()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 189
    189:         def global_scripts
    190:           "function moveProgressBar(percentDone) {\ndocument.getElementById(\"rspec-header\").style.width = percentDone +\"%\";\n}\nfunction makeRed(element_id) {\ndocument.getElementById(element_id).style.background = '#C40D0D';\ndocument.getElementById(element_id).style.color = '#FFFFFF';\n}\n\nfunction makeYellow(element_id) {\nif (element_id == \"rspec-header\" && document.getElementById(element_id).style.background != '#C40D0D')\n{\ndocument.getElementById(element_id).style.background = '#FAF834';\ndocument.getElementById(element_id).style.color = '#000000';\n}\nelse\n{\ndocument.getElementById(element_id).style.background = '#FAF834';\ndocument.getElementById(element_id).style.color = '#000000';\n}\n}\n"
    191:         end

global_styles()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 215
    215:         def global_styles
    216:           "#rspec-header {\nbackground: #65C400; color: #fff;\n}\n\n.rspec-report h1 {\nmargin: 0px 10px 0px 10px;\npadding: 10px;\nfont-family: \"Lucida Grande\", Helvetica, sans-serif;\nfont-size: 1.8em;\n}\n\n#summary {\nmargin: 0; padding: 5px 10px;\nfont-family: \"Lucida Grande\", Helvetica, sans-serif;\ntext-align: right;\nposition: absolute;\ntop: 0px;\nright: 0px;\n}\n\n#summary p {\nmargin: 0 0 0 2px;\n}\n\n#summary #totals {\nfont-size: 1.2em;\n}\n\n.example_group {\nmargin: 0 10px 5px;\nbackground: #fff;\n}\n\ndl {\nmargin: 0; padding: 0 0 5px;\nfont: normal 11px \"Lucida Grande\", Helvetica, sans-serif;\n}\n\ndt {\npadding: 3px;\nbackground: #65C400;\ncolor: #fff;\nfont-weight: bold;\n}\n\ndd {\nmargin: 5px 0 5px 5px;\npadding: 3px 3px 3px 18px;\n}\n\ndd.spec.passed {\nborder-left: 5px solid #65C400;\nborder-bottom: 1px solid #65C400;\nbackground: #DBFFB4; color: #3D7700;\n}\n\ndd.spec.failed {\nborder-left: 5px solid #C20000;\nborder-bottom: 1px solid #C20000;\ncolor: #C20000; background: #FFFBD3;\n}\n\ndd.spec.not_implemented {\nborder-left: 5px solid #FAF834;\nborder-bottom: 1px solid #FAF834;\nbackground: #FCFB98; color: #131313;\n}\n\ndd.spec.pending_fixed {\nborder-left: 5px solid #0000C2;\nborder-bottom: 1px solid #0000C2;\ncolor: #0000C2; background: #D3FBFF;\n}\n\n.backtrace {\ncolor: #000;\nfont-size: 12px;\n}\n\na {\ncolor: #BE5C00;\n}\n\n/* Ruby code, style similar to vibrant ink */\n.ruby {\nfont-size: 12px;\nfont-family: monospace;\ncolor: white;\nbackground-color: black;\npadding: 0.1em 0 0.2em 0;\n}\n\n.ruby .keyword { color: #FF6600; }\n.ruby .constant { color: #339999; }\n.ruby .attribute { color: white; }\n.ruby .global { color: white; }\n.ruby .module { color: white; }\n.ruby .class { color: white; }\n.ruby .string { color: #66FF00; }\n.ruby .ident { color: white; }\n.ruby .method { color: #FFCC00; }\n.ruby .number { color: white; }\n.ruby .char { color: white; }\n.ruby .comment { color: #9933CC; }\n.ruby .symbol { color: white; }\n.ruby .regex { color: #44B4CC; }\n.ruby .punct { color: white; }\n.ruby .escape { color: white; }\n.ruby .interp { color: white; }\n.ruby .expr { color: white; }\n\n.ruby .offending { background-color: gray; }\n.ruby .linenum {\nwidth: 75px;\npadding: 0.1em 1em 0.2em 0;\ncolor: #000000;\nbackground-color: #FFFBD3;\n}\n"
    217:         end

html_header()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 137
    137:         def html_header
    138:           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html\nPUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n<head>\n<title>RSpec results</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<meta http-equiv=\"Expires\" content=\"-1\" />\n<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n<style type=\"text/css\">\nbody {\nmargin: 0;\npadding: 0;\nbackground: #fff;\nfont-size: 80%;\n}\n</style>\n</head>\n<body>\n"
    139:         end

method_missing(sym, *args)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 16
    16:         def method_missing(sym, *args)
    17:           # no-op
    18:         end

move_progress()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 105
    105:         def move_progress
    106:           @output.puts "    <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
    107:           @output.flush
    108:         end

percent_done()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 110
    110:         def percent_done
    111:           result = 100.0
    112:           if @example_count != 0
    113:             result = ((example_number).to_f / @example_count.to_f * 1000).to_i / 10.0
    114:           end
    115:           result
    116:         end

report_header()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 163
    163:         def report_header
    164:           "<div class=\"rspec-report\">\n<script type=\"text/javascript\">\n// <![CDATA[\n\#{global_scripts}\n// ]]>\n</script>\n<style type=\"text/css\">\n\#{global_styles}\n</style>\n\n<div id=\"rspec-header\">\n<h1>RSpec Results</h1>\n\n<div id=\"summary\">\n<p id=\"totals\">&nbsp;</p>\n<p id=\"duration\">&nbsp;</p>\n</div>\n</div>\n\n<div class=\"results\">\n"
    165:         end

start(example_count)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 30
    30:         def start(example_count)
    31:           @example_count = example_count
    32:
    33:           @output.puts html_header
    34:           @output.puts report_header
    35:           @output.flush
    36:         end

start_dump()

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 53
    53:         def start_dump
    54:           @output.puts "  </dl>"
    55:           @output.puts "</div>"
    56:           @output.flush
    57:         end

[Validate]