2
require 'spec/runner/formatter/base_text_formatter'
7
class HtmlFormatter < BaseTextFormatter
8
include ERB::Util # for the #h method
10
def initialize(options, output)
12
@current_example_group_number = 0
13
@current_example_number = 0
16
# The number of the currently running example_group
17
def current_example_group_number
18
@current_example_group_number
21
# The number of the currently running example (a global counter)
22
def current_example_number
23
@current_example_number
26
def start(example_count)
27
@example_count = example_count
29
@output.puts html_header
30
@output.puts report_header
34
def add_example_group(example_group)
36
@example_group_red = false
37
@example_group_red = false
38
@current_example_group_number += 1
39
unless current_example_group_number == 1
43
@output.puts "<div class=\"example_group\">"
45
@output.puts " <dt id=\"example_group_#{current_example_group_number}\">#{h(example_group.description)}</dt>"
55
def example_started(example)
56
@current_example_number += 1
59
def example_passed(example)
61
@output.puts " <dd class=\"spec passed\"><span class=\"passed_spec_name\">#{h(example.description)}</span></dd>"
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
70
@output.puts " <script type=\"text/javascript\">makeRed('example_group_#{current_example_group_number}');</script>" unless @example_group_red
71
@example_group_red = true
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>"
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
88
@output.puts " <dd class=\"spec not_implemented\"><span class=\"not_implemented_spec_name\">#{h(example.description)} (PENDING: #{h(message)})</span></dd>"
92
# Override this method if you wish to output extra HTML for a failed spec. For example, you
93
# could output links to images or other files produced during the specs.
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>"
102
@output.puts " <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
108
if @example_count != 0
109
result = ((current_example_number).to_f / @example_count.to_f * 1000).to_i / 10.0
114
def dump_failure(counter, failure)
117
def dump_summary(duration, example_count, failure_count, pending_count)
119
totals = "This was a dry-run"
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
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>"
135
<?xml version="1.0" encoding="UTF-8"?>
137
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
138
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
139
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
141
<title>RSpec results</title>
142
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
143
<meta http-equiv="Expires" content="-1" />
144
<meta http-equiv="Pragma" content="no-cache" />
145
<style type="text/css">
160
<div class="rspec-report">
161
<script type="text/javascript">
166
<style type="text/css">
170
<div id="rspec-header">
171
<h1>RSpec Results</h1>
174
<p id="totals"> </p>
175
<p id="duration"> </p>
179
<div class="results">
185
function moveProgressBar(percentDone) {
186
document.getElementById("rspec-header").style.width = percentDone +"%";
188
function makeRed(element_id) {
189
document.getElementById(element_id).style.background = '#C40D0D';
190
document.getElementById(element_id).style.color = '#FFFFFF';
193
function makeYellow(element_id) {
194
if (element_id == "rspec-header" && document.getElementById(element_id).style.background != '#C40D0D')
196
document.getElementById(element_id).style.background = '#FAF834';
197
document.getElementById(element_id).style.color = '#000000';
201
document.getElementById(element_id).style.background = '#FAF834';
202
document.getElementById(element_id).style.color = '#000000';
211
background: #65C400; color: #fff;
215
margin: 0px 10px 0px 10px;
217
font-family: "Lucida Grande", Helvetica, sans-serif;
222
margin: 0; padding: 5px 10px;
223
font-family: "Lucida Grande", Helvetica, sans-serif;
244
margin: 0; padding: 0 0 5px;
245
font: normal 11px "Lucida Grande", Helvetica, sans-serif;
256
margin: 5px 0 5px 5px;
257
padding: 3px 3px 3px 18px;
261
border-left: 5px solid #65C400;
262
border-bottom: 1px solid #65C400;
263
background: #DBFFB4; color: #3D7700;
267
border-left: 5px solid #C20000;
268
border-bottom: 1px solid #C20000;
269
color: #C20000; background: #FFFBD3;
272
dd.spec.not_implemented {
273
border-left: 5px solid #FAF834;
274
border-bottom: 1px solid #FAF834;
275
background: #FCFB98; color: #131313;
278
dd.spec.pending_fixed {
279
border-left: 5px solid #0000C2;
280
border-bottom: 1px solid #0000C2;
281
color: #0000C2; background: #D3FBFF;
293
/* Ruby code, style similar to vibrant ink */
296
font-family: monospace;
298
background-color: black;
299
padding: 0.1em 0 0.2em 0;
302
.ruby .keyword { color: #FF6600; }
303
.ruby .constant { color: #339999; }
304
.ruby .attribute { color: white; }
305
.ruby .global { color: white; }
306
.ruby .module { color: white; }
307
.ruby .class { color: white; }
308
.ruby .string { color: #66FF00; }
309
.ruby .ident { color: white; }
310
.ruby .method { color: #FFCC00; }
311
.ruby .number { color: white; }
312
.ruby .char { color: white; }
313
.ruby .comment { color: #9933CC; }
314
.ruby .symbol { color: white; }
315
.ruby .regex { color: #44B4CC; }
316
.ruby .punct { color: white; }
317
.ruby .escape { color: white; }
318
.ruby .interp { color: white; }
319
.ruby .expr { color: white; }
321
.ruby .offending { background-color: gray; }
324
padding: 0.1em 1em 0.2em 0;
326
background-color: #FFFBD3;