public
Description: RSpec-syntax compatible framework for RubySpecs
Homepage: http://rubyspec.org
Clone URL: git://github.com/brixen/mspec.git
brixen (author)
Fri May 23 22:52:37 -0700 2008
commit  9e4dd3abfbdf8f87c455414647f1070a5a6d39f5
tree    4262285222515713ec6132de5561fb23ea04dea4
parent  4218fed7333151c4fd737056aee4175a5cd1638a
mspec / lib / mspec / runner / formatters / html.rb
100644 88 lines (80 sloc) 2.04 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'mspec/expectations/expectations'
require 'mspec/runner/formatters/dotted'
 
class HtmlFormatter < DottedFormatter
  def register
    super
    MSpec.register :start, self
    MSpec.register :enter, self
    MSpec.register :leave, self
  end
 
  def start
    print <<-EOH
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Spec Output For #{RUBY_NAME} (#{RUBY_VERSION})</title>
<style type="text/css">
ul {
list-style: none;
}
.fail {
color: red;
}
.pass {
color: green;
}
#details :target {
background-color: #ffffe0;
}
</style>
</head>
<body>
EOH
  end
 
  def enter(describe)
    print "<div><p>#{describe}</p>\n<ul>\n"
  end
 
  def leave
    print "</ul>\n</div>\n"
  end
 
  def after(state)
    desc = "- #{state.it}"
    if state.exception?
      @states << state
      count = @counter.failures + @counter.errors - state.exceptions.size
      state.exceptions.each do |msg, exc|
        outcome = state.failure?(exc) ? "FAILED" : "ERROR"
        count += 1
        print %[<li class="fail">#{desc} (<a href="#details-#{count}">#{outcome} - #{count}</a>)</li>\n]
      end
    else
      print %[<li class="pass">#{desc}</li>\n]
    end
  end
 
  def finish
    success = @states.empty?
    unless success
      print "<hr>\n"
      print %[<ol id="details">]
      count = 0
      @states.each do |state|
        state.exceptions.each do |msg, exc|
          outcome = failure?(state) ? "FAILED" : "ERROR"
          print %[\n<li id="details-#{count += 1}"><p>#{escape(state.description)} #{outcome}</p>\n<p>]
          print escape(message(exc))
          print "</p>\n<pre>\n"
          print escape(backtrace(exc))
          print "</pre>\n</li>\n"
        end
      end
      print "</ol>\n"
    end
    print %[<p>#{@timer.format}</p>\n]
    print %[<p class="#{success ? "pass" : "fail"}">#{@tally.format}</p>\n]
    print "</body>\n</html>\n"
  end
 
  def escape(string)
    string.gsub("&", "&nbsp;").gsub("<", "&lt;").gsub(">", "&gt;")
  end
end