Class: Opal::RSpec::BrowserFormatter

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Includes:
ERB::Util
Defined in:
opal/opal/rspec/browser_formatter.rb

Defined Under Namespace

Classes: Element

Constant Summary collapse

CSS_STYLES =
::RSpec::Core::Formatters::HtmlPrinter::GLOBAL_STYLES
REPORT_TEMPLATE =
<<-EOF
<div class="rspec-report">

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

    <div id="display-filters">
      <input id="passed_checkbox"  name="passed_checkbox"  type="checkbox" checked="checked" onchange="apply_filters()" value="1" /> <label for="passed_checkbox">Passed</label>
      <input id="failed_checkbox"  name="failed_checkbox"  type="checkbox" checked="checked" onchange="apply_filters()" value="2" /> <label for="failed_checkbox">Failed</label>
      <input id="pending_checkbox" name="pending_checkbox" type="checkbox" checked="checked" onchange="apply_filters()" value="3" /> <label for="pending_checkbox">Pending</label>
    </div>

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

  <div id="rspec-results" class="results">
  </div>
</div>
EOF

Instance Method Summary collapse

Instance Method Details

#add_scriptsObject



90
91
92
93
# File 'opal/opal/rspec/browser_formatter.rb', line 90

def add_scripts
  content = ::RSpec::Core::Formatters::HtmlPrinter::GLOBAL_SCRIPTS
  `window.eval(#{content})`
end

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



78
79
80
81
82
83
84
85
86
87
88
# File 'opal/opal/rspec/browser_formatter.rb', line 78

def dump_summary(duration, example_count, failure_count, pending_count)
  super

  totals = "#{example_count} examples, #{failure_count} failures"
  Element.id('totals').html = totals

  duration = "Finished in <strong>#{sprintf("%.5f", duration)} seconds</strong>"
  Element.id('duration').html = duration

  add_scripts
end

#example_failed(example) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'opal/opal/rspec/browser_formatter.rb', line 48

def example_failed(example)
  super
  duration = sprintf("%0.5f", example.execution_result[:run_time])

  error = example.execution_result[:exception]
  error_name = error.class.name.to_s
  output = "#{short_padding}#{error_name}:\n"
  error.message.to_s.split("\n").each { |line| output += "#{long_padding}  #{line}\n" }

  @example_group_failed = true

  @rspec_dl << Element.new(:dd, class_name: "example failed", html: <<-HTML)
    <span class="failed_spec_name">#{h example.description}</span>
    <span class="duration">#{duration}s</span>
    <div class="failure">
      <div class="message"><pre>#{h output}</pre></div>
    </div>
  HTML
end

#example_group_finished(example_group) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'opal/opal/rspec/browser_formatter.rb', line 38

def example_group_finished(example_group)
  super

  if @example_group_failed
    @rspec_group.class_name = "example_group failed"
    @rspec_dt.class_name = "failed"
    Element.id('rspec-header').class_name = 'failed'
  end
end

#example_group_started(example_group) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'opal/opal/rspec/browser_formatter.rb', line 21

def example_group_started(example_group)
  super

  @example_group_failed = false
  parents = example_group.parent_groups.size

  @rspec_group  = Element.new(:div, class_name: "example_group passed")
  @rspec_dl     = Element.new(:dl)
  @rspec_dt     = Element.new(:dt, class_name: "passed", text: example_group.description)
  @rspec_group << @rspec_dl
  @rspec_dl << @rspec_dt

  @rspec_dl.style 'margin-left', "#{(parents - 2) * 15}px"

  @rspec_results << @rspec_group
end

#example_passed(example) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'opal/opal/rspec/browser_formatter.rb', line 68

def example_passed(example)
  super
  duration = sprintf("%0.5f", example.execution_result[:run_time])

  @rspec_dl << Element.new(:dd, class_name: "example passed", html: <<-HTML)
    <span class="passed_spec_name">#{h example.description}</span>
    <span class="duration">#{duration}s</span>
  HTML
end

#long_paddingObject



99
100
101
# File 'opal/opal/rspec/browser_formatter.rb', line 99

def long_padding
  '     '
end

#short_paddingObject



95
96
97
# File 'opal/opal/rspec/browser_formatter.rb', line 95

def short_padding
  '  '
end

#start(example_count) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'opal/opal/rspec/browser_formatter.rb', line 10

def start(example_count)
  super
  target = Element.new(`document.body`)
  target << Element.new(:div, html: REPORT_TEMPLATE)
  @rspec_results = Element.id('rspec-results')

  css_text = CSS_STYLES + "\n body { padding: 0; margin: 0 }"
  styles = Element.new(:style, type: 'text/css', css_text: css_text)
  styles.append_to_head
end