Class: RSpec::Core::Formatters::WebKit

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

Constant Summary collapse

VERSION =

Version constant

'2.0.0'
DEFAULT_DATADIR =

Look up the datadir falling back to a relative path (mostly for prerelease testing)

Pathname( Config.datadir('webkit-rspec-formatter') )
BASE_PATH =
Pathname( __FILE__ ).dirname.parent.parent.parent.parent +
'data/webkit-rspec-formatter'
BASE_HREF =

The base HREF used in the header to map stuff to the datadir

"file://#{BASE_PATH}/"
TEMPLATE_DIR =

The directory to grab ERb templates out of

BASE_PATH + 'templates'
HEADER_TEMPLATE =

The page part templates

TEMPLATE_DIR + 'header.rhtml'
PASSED_EXAMPLE_TEMPLATE =
TEMPLATE_DIR + 'passed.rhtml'
FAILED_EXAMPLE_TEMPLATE =
TEMPLATE_DIR + 'failed.rhtml'
PENDING_EXAMPLE_TEMPLATE =
TEMPLATE_DIR + 'pending.rhtml'
PENDFIX_EXAMPLE_TEMPLATE =
TEMPLATE_DIR + 'pending-fixed.rhtml'
TEMPLATE_DIR + 'footer.rhtml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ WebKit

Create a new formatter



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/core/formatters/webkit.rb', line 43

def initialize( output ) # :notnew:
  super
  @previous_nesting_depth = 0
  @example_number = 0
  @failcounter = 0
  @snippet_extractor = RSpec::Core::Formatters::SnippetExtractor.new
  @example_templates = {
    :passed        => self.load_template(PASSED_EXAMPLE_TEMPLATE),
    :failed        => self.load_template(FAILED_EXAMPLE_TEMPLATE),
    :pending       => self.load_template(PENDING_EXAMPLE_TEMPLATE),
    :pending_fixed => self.load_template(PENDFIX_EXAMPLE_TEMPLATE),
  }

  Thread.current['logger-output'] = []
end

Instance Attribute Details

#example_countObject (readonly)

Attributes made readable for ERb



65
66
67
# File 'lib/rspec/core/formatters/webkit.rb', line 65

def example_count
  @example_count
end

#example_group_numberObject (readonly)

Attributes made readable for ERb



65
66
67
# File 'lib/rspec/core/formatters/webkit.rb', line 65

def example_group_number
  @example_group_number
end

#example_numberObject (readonly)

Attributes made readable for ERb



65
66
67
# File 'lib/rspec/core/formatters/webkit.rb', line 65

def example_number
  @example_number
end

#failcounterObject

The counter for failed example IDs



68
69
70
# File 'lib/rspec/core/formatters/webkit.rb', line 68

def failcounter
  @failcounter
end

Instance Method Details

#backtrace_line(line) ⇒ Object

Format backtrace lines to include a textmate link to the file/line in question.



171
172
173
174
175
176
177
# File 'lib/rspec/core/formatters/webkit.rb', line 171

def backtrace_line( line )
  return nil unless line = super
  return nil if line =~ %r{r?spec/mate|textmate-command}
  return h( line.strip ).gsub( /([^:]*\.rb):(\d*)/ ) do
    "<a href=\"txmt://open?url=file://#{File.expand_path($1)}&amp;line=#{$2}\">#{$1}:#{$2}</a> "
  end
end

#dump_failures(*unused) ⇒ Object

Returns content to be output when a failure occurs during the run; overridden to do nothing, as failures are handled by #example_failed.



191
192
# File 'lib/rspec/core/formatters/webkit.rb', line 191

def dump_failures( *unused )
end

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

Output the content generated at the end of the run.



196
197
198
199
# File 'lib/rspec/core/formatters/webkit.rb', line 196

def dump_summary( duration, example_count, failure_count, pending_count )
  @output.puts self.render_footer( duration, example_count, failure_count, pending_count )
  @output.flush
end

#example_failed(example) ⇒ Object

Callback – called when an example is exited with a failure.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rspec/core/formatters/webkit.rb', line 146

def example_failed( example )
  super

  counter   = self.failcounter += 1
  exception = example.[:execution_result][:exception_encountered]
  extra     = self.extra_failure_content( exception )
  template  = if exception.is_a?( RSpec::Core::PendingExampleFixedError )
    then @example_templates[:pending_fixed]
    else @example_templates[:failed]
    end

  @output.puts( template.result(binding()) )
  @output.flush
end

#example_group_started(example_group) ⇒ Object Also known as: add_example_group

Callback called by each example group when it’s entered –



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
# File 'lib/rspec/core/formatters/webkit.rb', line 79

def example_group_started( example_group )
  super
  nesting_depth = example_group.ancestors.length

  # Close the previous example groups if this one isn't a 
  # descendent of the previous one
  if @previous_nesting_depth.nonzero? && @previous_nesting_depth >= nesting_depth
    ( @previous_nesting_depth - nesting_depth + 1 ).times do
      @output.puts "  </dl>", "</section>", "  </dd>"
    end
  end

  @output.puts "<!-- nesting: %d, previous: %d -->" %
    [ nesting_depth, @previous_nesting_depth ]
  @previous_nesting_depth = nesting_depth

  if @previous_nesting_depth == 1
    @output.puts %{<section class="example-group">}
  else
    @output.puts %{<dd class="nested-group"><section class="example-group">}
  end

  @output.puts %{  <dl>},
    %{  <dt id="%s">%s</dt>} % [
      example_group.name.gsub(/[\W_]+/, '-').downcase,
      h(example_group.description)
    ]
  @output.flush
end

#example_passed(example) ⇒ Object

Callback – called when an example is exited with no failures.



138
139
140
141
142
# File 'lib/rspec/core/formatters/webkit.rb', line 138

def example_passed( example )
  status = 'passed'
  @output.puts( @example_templates[:passed].result(binding()) )
  @output.flush
end

#example_pending(example) ⇒ Object

Callback – called when an example is exited via a ‘pending’.



163
164
165
166
167
# File 'lib/rspec/core/formatters/webkit.rb', line 163

def example_pending( example )
  status = 'pending'
  @output.puts( @example_templates[:pending].result(binding()) )
  @output.flush
end

#example_started(example) ⇒ Object

Callback – called when an example is entered



130
131
132
133
134
# File 'lib/rspec/core/formatters/webkit.rb', line 130

def example_started( example )
  @example_number += 1
  Thread.current[ 'logger-output' ] ||= []
  Thread.current[ 'logger-output' ].clear
end

#extra_failure_content(exception) ⇒ Object

Return any stuff that should be appended to the current example because it’s failed. Returns a snippet of the source around the failure.



183
184
185
186
# File 'lib/rspec/core/formatters/webkit.rb', line 183

def extra_failure_content( exception )
  snippet = @snippet_extractor.snippet( exception )
  return "    <pre class=\"ruby\"><code>#{snippet}</code></pre>"
end

#load_template(templatepath) ⇒ Object

Load the ERB template at templatepath and return it.

Parameters:

  • templatepath (Pathname)

    the fully-qualified path to the template file



218
219
220
# File 'lib/rspec/core/formatters/webkit.rb', line 218

def load_template( templatepath )
  return ERB.new( templatepath.read, nil, '%<>' ).freeze
end

#log_messagesObject

Fetch any log messages added to the thread-local Array



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

def log_messages
  return Thread.current[ 'logger-output' ] || []
end

Render the footer template in the context of the receiver.



210
211
212
213
# File 'lib/rspec/core/formatters/webkit.rb', line 210

def render_footer( duration, example_count, failure_count, pending_count )
  template = self.load_template( FOOTER_TEMPLATE )
  return template.result( binding() )
end

#render_header(example_count) ⇒ Object

Render the header template in the context of the receiver.



203
204
205
206
# File 'lib/rspec/core/formatters/webkit.rb', line 203

def render_header( example_count )
  template = self.load_template( HEADER_TEMPLATE )
  return template.result( binding() )
end

#start(example_count) ⇒ Object

Start the page by rendering the header.



72
73
74
75
# File 'lib/rspec/core/formatters/webkit.rb', line 72

def start( example_count )
  @output.puts self.render_header( example_count )
  @output.flush
end

#start_dumpObject

Callback – called when the examples are finished.



118
119
120
121
122
123
124
125
126
# File 'lib/rspec/core/formatters/webkit.rb', line 118

def start_dump
  @previous_nesting_depth.downto( 1 ) do |i|
    @output.puts "  </dl>",
                 "</section>"
    @output.puts "  </dd>" unless i == 1
  end

  @output.flush
end