]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/runner/formatter/html_formatter.rb
ad153c8dc20714eb2d7b5fb6c917247c46a9a2e5
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / runner / formatter / html_formatter.rb
1 require 'erb'
2 require 'spec/runner/formatter/base_text_formatter'
3
4 module Spec
5   module Runner
6     module Formatter
7       class HtmlFormatter < BaseTextFormatter
8         include ERB::Util # for the #h method
9         
10         def initialize(options, output)
11           super
12           @current_example_group_number = 0
13           @current_example_number = 0
14         end
15
16         # The number of the currently running example_group
17         def current_example_group_number
18           @current_example_group_number
19         end
20         
21         # The number of the currently running example (a global counter)
22         def current_example_number
23           @current_example_number
24         end
25         
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
33
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
48
49         def start_dump
50           @output.puts "  </dl>"
51           @output.puts "</div>"
52           @output.flush
53         end
54
55         def example_started(example)
56           @current_example_number += 1
57         end
58
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
64
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
83
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
91
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.
94         #
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
100         
101         def move_progress
102           @output.puts "    <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
103           @output.flush
104         end
105
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
113
114         def dump_failure(counter, failure)
115         end
116
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
132
133         def html_header 
134           <<-EOF
135 <?xml version="1.0" encoding="UTF-8"?>
136 <!DOCTYPE html 
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">
140 <head>
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">
146   body {
147     margin: 0;
148     padding: 0;
149     background: #fff;
150     font-size: 80%;
151   }
152   </style>
153 </head>
154 <body>
155 EOF
156         end
157
158         def report_header
159           <<-EOF
160 <div class="rspec-report">
161   <script type="text/javascript">
162     // <![CDATA[
163 #{global_scripts}
164     // ]]>
165   </script>
166   <style type="text/css">
167 #{global_styles}
168   </style>
169
170 <div id="rspec-header">
171   <h1>RSpec Results</h1>
172
173   <div id="summary">
174     <p id="totals">&nbsp;</p>
175     <p id="duration">&nbsp;</p>
176   </div>
177 </div>
178
179 <div class="results">
180 EOF
181         end
182
183         def global_scripts
184           <<-EOF
185 function moveProgressBar(percentDone) {
186   document.getElementById("rspec-header").style.width = percentDone +"%";
187 }
188 function makeRed(element_id) {
189   document.getElementById(element_id).style.background = '#C40D0D';
190   document.getElementById(element_id).style.color = '#FFFFFF';
191 }
192
193 function makeYellow(element_id) {
194   if (element_id == "rspec-header" && document.getElementById(element_id).style.background != '#C40D0D')
195   {
196     document.getElementById(element_id).style.background = '#FAF834';
197     document.getElementById(element_id).style.color = '#000000';
198   }
199   else
200   {
201     document.getElementById(element_id).style.background = '#FAF834';
202     document.getElementById(element_id).style.color = '#000000';
203   }
204 }
205 EOF
206         end
207         
208         def global_styles
209           <<-EOF
210 #rspec-header {
211   background: #65C400; color: #fff;
212 }
213
214 .rspec-report h1 {
215   margin: 0px 10px 0px 10px;
216   padding: 10px;
217   font-family: "Lucida Grande", Helvetica, sans-serif;
218   font-size: 1.8em;
219 }
220
221 #summary {
222   margin: 0; padding: 5px 10px;
223   font-family: "Lucida Grande", Helvetica, sans-serif;
224   text-align: right;
225   position: absolute;
226   top: 0px;
227   right: 0px;
228 }
229
230 #summary p {
231   margin: 0 0 0 2px;
232 }
233
234 #summary #totals {
235   font-size: 1.2em;
236 }
237
238 .example_group {
239   margin: 0 10px 5px;
240   background: #fff;
241 }
242
243 dl {
244   margin: 0; padding: 0 0 5px;
245   font: normal 11px "Lucida Grande", Helvetica, sans-serif;
246 }
247
248 dt {
249   padding: 3px;
250   background: #65C400;
251   color: #fff;
252   font-weight: bold;
253 }
254
255 dd {
256   margin: 5px 0 5px 5px;
257   padding: 3px 3px 3px 18px;
258 }
259
260 dd.spec.passed {
261   border-left: 5px solid #65C400;
262   border-bottom: 1px solid #65C400;
263   background: #DBFFB4; color: #3D7700;
264 }
265
266 dd.spec.failed {
267   border-left: 5px solid #C20000;
268   border-bottom: 1px solid #C20000;
269   color: #C20000; background: #FFFBD3;
270 }
271
272 dd.spec.not_implemented {
273   border-left: 5px solid #FAF834;
274   border-bottom: 1px solid #FAF834;
275   background: #FCFB98; color: #131313;
276 }
277
278 dd.spec.pending_fixed {
279   border-left: 5px solid #0000C2;
280   border-bottom: 1px solid #0000C2;
281   color: #0000C2; background: #D3FBFF;
282 }
283
284 .backtrace {
285   color: #000;
286   font-size: 12px;
287 }
288
289 a {
290   color: #BE5C00;
291 }
292
293 /* Ruby code, style similar to vibrant ink */
294 .ruby {
295   font-size: 12px;
296   font-family: monospace;
297   color: white;
298   background-color: black;
299   padding: 0.1em 0 0.2em 0;
300 }
301
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; }
320
321 .ruby .offending { background-color: gray; }
322 .ruby .linenum {
323   width: 75px;
324   padding: 0.1em 1em 0.2em 0;
325   color: #000000;
326   background-color: #FFFBD3;
327 }
328 EOF
329         end
330       end
331     end
332   end
333 end