Class: Spec::Runner::Formatter::HtmlFormatter

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

### Methods

add_example_group current_example_group_number current_example_number dump_failure dump_summary example_failed example_passed example_pending example_started extra_failure_content global_scripts global_styles html_header 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:           @current_example_group_number = 0
    13:           @current_example_number = 0
    14:         end

### Public Instance methods

add_example_group(example_group)

[Source]

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

current_example_group_number()

The number of the currently running example_group

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 17
    17:         def current_example_group_number
    18:           @current_example_group_number
    19:         end

current_example_number()

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

[Source]

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

dump_failure(counter, failure)

[Source]

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

dump_summary(duration, example_count, failure_count, pending_count)

[Source]

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

example_failed(example, counter, failure)

[Source]

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

example_passed(example)

[Source]

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

example_pending(example_group_description, example, message)

[Source]

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

example_started(example)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 55
    55:         def example_started(example)
    56:           @current_example_number += 1
    57:         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 95
    95:         def extra_failure_content(failure)
    96:           require 'spec/runner/formatter/snippet_extractor'
    97:           @snippet_extractor ||= SnippetExtractor.new
    98:           "    <pre class=\"ruby\"><code>#{@snippet_extractor.snippet(failure.exception)}</code></pre>"
    99:         end

global_scripts()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 185
    185:         def global_scripts
    186:           "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"
    187:         end

global_styles()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 211
    211:         def global_styles
    212:           "#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"
    213:         end

html_header()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 133
    133:         def html_header
    134:           "<?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"
    135:         end

move_progress()

[Source]

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

percent_done()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 106
    106:         def percent_done
    107:           result = 100.0
    108:           if @example_count != 0
    109:             result = ((current_example_number).to_f / @example_count.to_f * 1000).to_i / 10.0
    110:           end
    111:           result
    112:         end

report_header()

[Source]

         # File lib/spec/runner/formatter/html_formatter.rb, line 159
    159:         def report_header
    160:           "<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"
    161:         end

start(example_count)

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 26
    26:         def start(example_count)
    27:           @example_count = example_count
    28:
    29:           @output.puts html_header
    30:           @output.puts report_header
    31:           @output.flush
    32:         end

start_dump()

[Source]

        # File lib/spec/runner/formatter/html_formatter.rb, line 49
    49:         def start_dump
    50:           @output.puts "  </dl>"
    51:           @output.puts "</div>"
    52:           @output.flush
    53:         end

[Validate]