| Class | Spec::Runner::Formatter::HtmlFormatter |
| In: |
lib/spec/runner/formatter/html_formatter.rb
|
| Parent: | BaseTextFormatter |
# File lib/spec/runner/formatter/html_formatter.rb, line 6
6: def initialize(output)
7: super
8: @current_behaviour_number = 0
9: @current_example_number = 0
10: end
# File lib/spec/runner/formatter/html_formatter.rb, line 30
30: def add_behaviour(name)
31: @behaviour_red = false
32: @behaviour_red = false
33: @current_behaviour_number += 1
34: unless current_behaviour_number == 1
35: @output.puts " </dl>"
36: @output.puts "</div>"
37: end
38: @output.puts "<div class=\"behaviour\">"
39: @output.puts " <dl>"
40: @output.puts " <dt id=\"behaviour_#{current_behaviour_number}\">#{escape(name)}</dt>"
41: @output.flush
42: end
The number of the currently running behaviour
# File lib/spec/runner/formatter/html_formatter.rb, line 13
13: def current_behaviour_number
14: @current_behaviour_number
15: end
The number of the currently running example
# File lib/spec/runner/formatter/html_formatter.rb, line 18
18: def current_example_number
19: @current_example_number
20: end
# File lib/spec/runner/formatter/html_formatter.rb, line 102
102: def dump_failure(counter, failure)
103: end
# File lib/spec/runner/formatter/html_formatter.rb, line 105
105: def dump_summary(duration, example_count, failure_count, not_implemented_count)
106: if @dry_run
107: totals = "This was a dry-run"
108: else
109: totals = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
110: totals << ", #{not_implemented_count} not implemented" if not_implemented_count > 0
111: end
112: @output.puts "<script type=\"text/javascript\">document.getElementById('duration').innerHTML = \"Finished in <strong>#{duration} seconds</strong>\";</script>"
113: @output.puts "<script type=\"text/javascript\">document.getElementById('totals').innerHTML = \"#{totals}\";</script>"
114: @output.puts "</div>"
115: @output.puts "</div>"
116: @output.puts "</body>"
117: @output.puts "</html>"
118: @output.flush
119: end
# File lib/spec/runner/formatter/html_formatter.rb, line 98
98: def escape(string)
99: string.gsub(/&/n, '&').gsub(/\"/n, '"').gsub(/>/n, '>').gsub(/</n, '<')
100: end
# File lib/spec/runner/formatter/html_formatter.rb, line 57
57: def example_failed(name, counter, failure)
58: extra = extra_failure_content(failure)
59:
60: @current_example_number += 1
61: @output.puts " <script type=\"text/javascript\">makeRed('rspec-header');</script>" unless @header_red
62: @header_red = true
63: @output.puts " <script type=\"text/javascript\">makeRed('behaviour_#{current_behaviour_number}');</script>" unless @behaviour_red
64: @behaviour_red = true
65: move_progress
66: @output.puts " <dd class=\"spec failed\">"
67: @output.puts " <span class=\"failed_spec_name\">#{escape(name)}</span>"
68: @output.puts " <div class=\"failure\" id=\"failure_#{counter}\">"
69: @output.puts " <div class=\"message\"><pre>#{escape(failure.exception.message)}</pre></div>" unless failure.exception.nil?
70: @output.puts " <div class=\"backtrace\"><pre>#{format_backtrace(failure.exception.backtrace)}</pre></div>" unless failure.exception.nil?
71: @output.puts extra unless extra == ""
72: @output.puts " </div>"
73: @output.puts " </dd>"
74: @output.flush
75: end
# File lib/spec/runner/formatter/html_formatter.rb, line 77
77: def example_not_implemented(name)
78: @current_example_number += 1
79: @output.puts " <script type=\"text/javascript\">makeYellow('rspec-header');</script>" unless @header_red
80: @output.puts " <script type=\"text/javascript\">makeYellow('behaviour_#{current_behaviour_number}');</script>" unless @behaviour_red
81: move_progress
82: @output.puts " <dd class=\"spec not_implemented\"><span class=\"not_implemented_spec_name\">#{escape(name)}</span></dd>"
83: @output.flush
84: end
# File lib/spec/runner/formatter/html_formatter.rb, line 50
50: def example_passed(name)
51: @current_example_number += 1
52: move_progress
53: @output.puts " <dd class=\"spec passed\"><span class=\"passed_spec_name\">#{escape(name)}</span></dd>"
54: @output.flush
55: 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.
# File lib/spec/runner/formatter/html_formatter.rb, line 88
88: def extra_failure_content(failure)
89: " <pre class=\"ruby\"><code>#{@snippet_extractor.snippet(failure.exception)}</code></pre>"
90: end
# File lib/spec/runner/formatter/html_formatter.rb, line 174
174: def global_scripts
175: "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"
176: end
# File lib/spec/runner/formatter/html_formatter.rb, line 200
200: def global_styles
201: "#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.behaviour {\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\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"
202: end
# File lib/spec/runner/formatter/html_formatter.rb, line 121
121: def html_header
122: "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<!DOCTYPE html\nPUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\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=iso-8859-1\" />\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"
123: end
# File lib/spec/runner/formatter/html_formatter.rb, line 92
92: def move_progress
93: percent_done = @example_count == 0 ? 100.0 : (current_example_number.to_f / @example_count.to_f * 1000).to_i / 10.0
94: @output.puts " <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
95: @output.flush
96: end
# File lib/spec/runner/formatter/html_formatter.rb, line 148
148: def report_header
149: "<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\"> </p>\n<p id=\"duration\"> </p>\n</div>\n</div>\n\n<div class=\"results\">\n"
150: end
# File lib/spec/runner/formatter/html_formatter.rb, line 22
22: def start(example_count)
23: @example_count = example_count
24:
25: @output.puts html_header
26: @output.puts report_header
27: @output.flush
28: end