Class: RSpec::Core::Formatters::HtmlFormatter

Inherits:
BaseTextFormatter show all
Includes:
ERB::Util
Defined in:
lib/rspec/core/formatters/html_formatter.rb

Direct Known Subclasses

TextMateFormatter

Constant Summary

Constants included from Helpers

RSpec::Core::Formatters::Helpers::DEFAULT_PRECISION, RSpec::Core::Formatters::Helpers::SUB_SECOND_PRECISION

Instance Attribute Summary

Attributes inherited from BaseFormatter

#duration, #example_count, #example_group, #examples, #failed_examples, #failure_count, #output, #pending_count, #pending_examples

Instance Method Summary collapse

Methods inherited from BaseTextFormatter

#close, #colorise_summary, #dump_profile, #summary_line

Methods inherited from BaseFormatter

#add_example_group, #close, #example_group_finished, #format_backtrace, #stop

Methods included from Helpers

#format_seconds, #strip_trailing_zeroes

Constructor Details

#initialize(output) ⇒ HtmlFormatter

Returns a new instance of HtmlFormatter.



17
18
19
20
21
22
# File 'lib/rspec/core/formatters/html_formatter.rb', line 17

def initialize(output)
  super(output)
  @example_group_number = 0
  @example_number = 0
  @header_red = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object



10
11
12
# File 'lib/rspec/core/formatters/html_formatter.rb', line 10

def method_missing(m, *a, &b)
  # no-op
end

Instance Method Details

#dump_failuresObject



125
126
# File 'lib/rspec/core/formatters/html_formatter.rb', line 125

def dump_failures
end

#dump_pendingObject



128
129
# File 'lib/rspec/core/formatters/html_formatter.rb', line 128

def dump_pending
end

#dump_summary(duration, example_count, failure_count, pending_count) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rspec/core/formatters/html_formatter.rb', line 131

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 "<script type=\"text/javascript\">document.getElementById('duration').innerHTML = \"Finished in <strong>#{duration} seconds</strong>\";</script>"
  @output.puts "<script type=\"text/javascript\">document.getElementById('totals').innerHTML = \"#{totals}\";</script>"
  @output.puts "</div>"
  @output.puts "</div>"
  @output.puts "</body>"
  @output.puts "</html>"
  @output.flush
end

#example_failed(example) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rspec/core/formatters/html_formatter.rb', line 72

def example_failed(example)
  counter = 0
  exception = example.[:execution_result][:exception_encountered]
  extra = extra_failure_content(exception)
  failure_style = 'failed'
  failure_style = RSpec::Core::PendingExampleFixedError === exception ? 'pending_fixed' : 'failed'
  @output.puts "    <script type=\"text/javascript\">makeRed('rspec-header');</script>" unless @header_red
  @header_red = true
  @output.puts "    <script type=\"text/javascript\">makeRed('example_group_#{example_group_number}');</script>" unless @example_group_red
  @example_group_red = true
  move_progress
  @output.puts "    <dd class=\"spec #{failure_style}\">"
  @output.puts "      <span class=\"failed_spec_name\">#{h(example.description)}</span>"
  @output.puts "      <div class=\"failure\" id=\"failure_#{counter}\">"
  @output.puts "        <div class=\"message\"><pre>#{h(exception.message)}</pre></div>" unless exception.nil?
  @output.puts "        <div class=\"backtrace\"><pre>#{format_backtrace(exception.backtrace, example).join("\n")}</pre></div>" if exception
  @output.puts extra unless extra == ""
  @output.puts "      </div>"
  @output.puts "    </dd>"
  @output.flush
end

#example_group_numberObject

The number of the currently running example_group



25
26
27
# File 'lib/rspec/core/formatters/html_formatter.rb', line 25

def example_group_number
  @example_group_number
end

#example_group_started(example_group) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/core/formatters/html_formatter.rb', line 41

def example_group_started(example_group)
  super(example_group)
  @example_group_red = false
  @example_group_number += 1
  unless example_group_number == 1
    @output.puts "  </dl>"
    @output.puts "</div>"
  end
  @output.puts "<div class=\"example_group\">"
  @output.puts "  <dl>"
  @output.puts "  <dt id=\"example_group_#{example_group_number}\">#{h(example_group.description)}</dt>"
  @output.flush
end

#example_numberObject

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



30
31
32
# File 'lib/rspec/core/formatters/html_formatter.rb', line 30

def example_number
  @example_number
end

#example_passed(example) ⇒ Object



66
67
68
69
70
# File 'lib/rspec/core/formatters/html_formatter.rb', line 66

def example_passed(example)
  move_progress
  @output.puts "    <dd class=\"spec passed\"><span class=\"passed_spec_name\">#{h(example.description)}</span></dd>"
  @output.flush
end

#example_pending(example) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/rspec/core/formatters/html_formatter.rb', line 94

def example_pending(example)
  message = example.[:execution_result][:pending_message]
  @output.puts "    <script type=\"text/javascript\">makeYellow('rspec-header');</script>" unless @header_red
  @output.puts "    <script type=\"text/javascript\">makeYellow('example_group_#{example_group_number}');</script>" unless @example_group_red
  move_progress
  @output.puts "    <dd class=\"spec not_implemented\"><span class=\"not_implemented_spec_name\">#{h(example.description)} (PENDING: #{h(message)})</span></dd>"
  @output.flush
end

#example_started(example) ⇒ Object



61
62
63
64
# File 'lib/rspec/core/formatters/html_formatter.rb', line 61

def example_started(example)
  super(example)
  @example_number += 1
end

#extra_failure_content(exception) ⇒ Object

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.



106
107
108
109
110
# File 'lib/rspec/core/formatters/html_formatter.rb', line 106

def extra_failure_content(exception)
  require 'rspec/core/formatters/snippet_extractor'
  @snippet_extractor ||= SnippetExtractor.new
  "    <pre class=\"ruby\"><code>#{@snippet_extractor.snippet(exception)}</code></pre>"
end

#global_scriptsObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rspec/core/formatters/html_formatter.rb', line 200

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

#global_stylesObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rspec/core/formatters/html_formatter.rb', line 225

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

#html_headerObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rspec/core/formatters/html_formatter.rb', line 148

def html_header
  <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>RSpec results</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Expires" content="-1" />
  <meta http-equiv="Pragma" content="no-cache" />
  <style type="text/css">
  body {
    margin: 0;
    padding: 0;
    background: #fff;
    font-size: 80%;
  }
  </style>
  <script type="text/javascript">
    // <![CDATA[
#{global_scripts}
    // ]]>
  </script>
  <style type="text/css">
#{global_styles}
  </style>
</head>
<body>
EOF
end

#message(message) ⇒ Object



14
15
# File 'lib/rspec/core/formatters/html_formatter.rb', line 14

def message(message)
end

#move_progressObject



112
113
114
115
# File 'lib/rspec/core/formatters/html_formatter.rb', line 112

def move_progress
  @output.puts "    <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
  @output.flush
end

#percent_doneObject



117
118
119
120
121
122
123
# File 'lib/rspec/core/formatters/html_formatter.rb', line 117

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

#report_headerObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rspec/core/formatters/html_formatter.rb', line 181

def report_header
  <<-EOF
<div class="rspec-report">

<div id="rspec-header">
  <div id="label">
    <h1>RSpec Code Examples</h1>
  </div>

  <div id="summary">
    <p id="totals">&nbsp;</p>
    <p id="duration">&nbsp;</p>
  </div>
</div>

<div class="results">
EOF
end

#start(example_count) ⇒ Object



34
35
36
37
38
39
# File 'lib/rspec/core/formatters/html_formatter.rb', line 34

def start(example_count)
  super(example_count)
  @output.puts html_header
  @output.puts report_header
  @output.flush
end

#start_dumpObject



55
56
57
58
59
# File 'lib/rspec/core/formatters/html_formatter.rb', line 55

def start_dump
  @output.puts "  </dl>"
  @output.puts "</div>"
  @output.flush
end