Class: Webr::Jasmine::Reporter::Html

Inherits:
Base
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/webr/jasmine/reporter/html.rb

Instance Method Summary collapse

Methods inherited from Base

#filter_backtrace, #initialize, #log, #reportRunnerStarting, #reportSpecResults, #reportSpecStarting, #reportSuiteResults

Constructor Details

This class inherits a constructor from Webr::Jasmine::Reporter::Base

Instance Method Details

#render_cssObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/webr/jasmine/reporter/html.rb', line 65

def render_css
  <<-EOCSS
  <style>
    html, body {
      padding: 0;
      margin: 0;
    }
    body {
      font-size: 80%;
      font-family: "Lucida Grande", Helvetica, sans-serif;
    }
    #header {
      background: #000;
      color: #fff;
      height: 4em;
    }
    #header.passed {
      background-color: #65C400;
    }
    #header.failed {
      background-color: #c40d0d;
    }

    #header h1 {
      font-size: 1.8em;
      margin: 0 10px;
      padding: 10px;
      position: absolute;
    }

    .summary {
      float: right;
      margin: 0;
      padding: 5px 10px;
      right: 0;
      top: 0;
    }
    .summary p {
      margin: 0 0 0 2px;
    }
    .summary .totals {
      font-size: 1.2em;
    }

    #results {
      margin-bottom: 1em;
      padding-right: 10px;
    }

    .group {
      margin: 0 0 5px 10px;
      padding: 0 0 5px;
      font-size: 11px;
    }
    .group-name {
      background-color: #000;
      color: #fff;
      font-weight: bold;
      padding: 3px;
    }
    .group.failed .group-name {
      background-color: #c40d0d;
    }
    .group.passed .group-name {
      background-color: #65C400;
    }

    .example {
      margin: 5px 0 5px 5px;
      padding: 3px 3px 3px 18px;
    }
    .example.failed {
      background-color: #fffbd3;
      border-bottom: 1px solid #c40d0d;
      border-left: 5px solid #c40d0d;
      color: #C20000;
    }
    .example.passed {
      background-color: #DBFFB4;
      border-bottom: 1px solid #65C400;
      border-left: 5px solid #65C400;
      color: #3D7700;
    }
    .example .backtrace {
      color: #000;
    }

  </style>
  EOCSS
end

#render_results(suites_or_specs) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/webr/jasmine/reporter/html.rb', line 18

def render_results(suites_or_specs)
  html = []
  suites_or_specs.each do |suite_or_spec|
    html << render_suite(suite_or_spec) if @jasmine.isSuite(suite_or_spec)
    html << render_spec(suite_or_spec)  if @jasmine.isSpec(suite_or_spec)
  end
  html.join
end

#render_spec(spec) ⇒ Object



40
41
42
43
44
# File 'lib/webr/jasmine/reporter/html.rb', line 40

def render_spec(spec)
  result = spec.results.passed ? 'passed' : 'failed'
  content = spec.results.passed ? '' : render_spec_failed(spec)
  "<div class='example #{result}'><div class='example-name'>#{h(spec.description)}</div>#{content}</div>"
end

#render_spec_failed(spec) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/webr/jasmine/reporter/html.rb', line 46

def render_spec_failed(spec)
  html = []
  name = spec.description
  results = spec.results
  results.getItems.each do |item|
    unless item.passed
      unless item.passed
        backtrace = if error = item['error']
          error.respond_to?(:stack) ? textmate_backtrace(h(filter_backtrace(error.stack))) : h(error)
        else
          textmate_backtrace h(filter_backtrace(item.trace.stack))
        end
        html << "<div class='example-failure'><div class='message'><pre>#{h(item.to_s)}</pre></div><div class='backtrace'><pre>#{backtrace}</pre></div></div>"
      end
    end
  end
  html.join
end

#render_suite(suite) ⇒ Object



27
28
29
30
31
# File 'lib/webr/jasmine/reporter/html.rb', line 27

def render_suite(suite)
  result = suite_passed(suite) ? 'passed' : 'failed'
  content = render_results(suite.children)
  "<div class='group #{result}'><div class='group-name'>#{h(suite.description)}</div>#{content}</div>"
end

#render_summary(runner) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/webr/jasmine/reporter/html.rb', line 156

def render_summary(runner)
  suites  = runner.suites
  specs   = runner.specs
  results = runner.results

  #TODO: Change this. This method is private in Ruby 1.9 - which is a little weird.
  hours, minutes, seconds, fraction = Date.send(:day_fraction_to_time, @finished_at - @started_at)
  time_taken = "%0.8f" % (hours*60*60 + minutes*60 + seconds + fraction.to_f)

  totals = "Examples: #{specs.length}, Failure#{'s' unless results.failedCount == 1}: #{results.failedCount}"
  duration = "Finished in #{time_taken}s"
  "<div class='summary'><p class='totals'>#{totals}</p><p class='duration'>#{duration}</p></div></div>"
end

#reportRunnerResults(runner) ⇒ Object



5
6
7
8
# File 'lib/webr/jasmine/reporter/html.rb', line 5

def reportRunnerResults(runner)
  super(runner)
  puts summarize(runner)
end

#suite_passed(suite) ⇒ Object



33
34
35
36
37
38
# File 'lib/webr/jasmine/reporter/html.rb', line 33

def suite_passed(suite)
  suite.results.getItems.each do |item|
    return false unless item.passed
  end
  true
end

#summarize(runner) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/webr/jasmine/reporter/html.rb', line 10

def summarize(runner)
  css = render_css
  summary = render_summary(runner)
  results = render_results(runner.topLevelSuites)
  status = runner.results.failedCount > 0 ? 'failed' : 'passed'
  "<!DOCTYPE html><html><head><title>Jasmine results</title>#{css}</head><body></body><div class='report'><div id='header' class='#{status}'><h1>Jasmine Code Examples</h1>#{summary}<div id='results' class='results'>#{results}</div></div></html>"
end

#textmate_backtrace(s) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/webr/jasmine/reporter/html.rb', line 170

def textmate_backtrace(s)
  lines = []
  s.each_line do |line|
   lines << line.gsub(/(\/.*\.js):(\d*)/) { "<a href=\"txmt://open?url=file://#{File.expand_path($1)}&line=#{$2}\">#{$1}:#{$2}</a>" }
  end
  lines.join
end