Class: Lucid::Formatter::Html

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, Duration, Io
Defined in:
lib/lucid/formatter/html.rb

Defined Under Namespace

Classes: MatcherExtractor

Instance Method Summary collapse

Methods included from Io

#ensure_dir, #ensure_file, #ensure_io

Methods included from Duration

#format_duration

Constructor Details

#initialize(runtime, path_or_io, options) ⇒ Html

Returns a new instance of Html.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lucid/formatter/html.rb', line 13

def initialize(runtime, path_or_io, options)
  @io = ensure_io(path_or_io, "html")
  @runtime = runtime
  @options = options
  @buffer = {}
  @builder = create_builder(@io)
  @feature_number = 0
  @scenario_number = 0
  @step_number = 0
  @header_red = nil
  @delayed_messages = []
  @img_id = 0
  @inside_outline = false
end

Instance Method Details

#after_background(background) ⇒ Object



133
134
135
136
# File 'lib/lucid/formatter/html.rb', line 133

def after_background(background)
  @in_background = nil
  @builder << '</div>'
end

#after_comment(comment) ⇒ Object



95
96
97
# File 'lib/lucid/formatter/html.rb', line 95

def after_comment(comment)
  @builder << '</pre>'
end

#after_examples(examples) ⇒ Object



190
191
192
# File 'lib/lucid/formatter/html.rb', line 190

def after_examples(examples)
  @builder << '</div>'
end

#after_feature(feature) ⇒ Object



87
88
89
# File 'lib/lucid/formatter/html.rb', line 87

def after_feature(feature)
  @builder << '</div>'
end

#after_feature_element(feature_element) ⇒ Object



157
158
159
160
# File 'lib/lucid/formatter/html.rb', line 157

def after_feature_element(feature_element)
  @builder << '</div>'
  @open_step_list = true
end

#after_features(features) ⇒ Object



75
76
77
78
79
80
# File 'lib/lucid/formatter/html.rb', line 75

def after_features(features)
  print_stats(features)
  @builder << '</div>'
  @builder << '</body>'
  @builder << '</html>'
end

#after_multiline_arg(multiline_arg) ⇒ Object



279
280
281
282
283
284
# File 'lib/lucid/formatter/html.rb', line 279

def after_multiline_arg(multiline_arg)
  return if @hide_this_step || @skip_step
  if AST::Table === multiline_arg
    @builder << '</table>'
  end
end

#after_outline_table(outline_table) ⇒ Object



181
182
183
184
# File 'lib/lucid/formatter/html.rb', line 181

def after_outline_table(outline_table)
  @builder << '</table>'
  @outline_row = nil
end

#after_step(step) ⇒ Object



216
217
218
# File 'lib/lucid/formatter/html.rb', line 216

def after_step(step)
  move_progress
end

#after_step_result(step_result) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/lucid/formatter/html.rb', line 240

def after_step_result(step_result)
  return if @hide_this_step
  if step_result.status == :undefined
    keyword = @step.actual_keyword if @step.respond_to?(:actual_keyword)
    step_multiline_class = @step.multiline_arg ? @step.multiline_arg.class : nil
    @builder.pre do |pre|
      pre << @runtime.matcher_text(keyword,step_result.step_name || '',step_result.step_multiline_class)
    end
  end
  @builder << '</li>'
  print_messages
end

#after_steps(steps) ⇒ Object



206
207
208
# File 'lib/lucid/formatter/html.rb', line 206

def after_steps(steps)
  @builder << '</ol>'
end

#after_table_row(table_row) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/lucid/formatter/html.rb', line 300

def after_table_row(table_row)
  return if @hide_this_step
  print_table_row_messages
  @builder << '</tr>'
  if table_row.exception
    @builder.tr do
      @builder.td(:colspan => @col_index.to_s, :class => 'failed') do
        @builder.pre do |pre|
          pre << h(format_exception(table_row.exception))
        end
      end
    end
    if table_row.exception.is_a? ::Lucid::Pending
      set_scenario_color_pending
    else
      set_scenario_color_failed
    end
  end
  if @outline_row
    @outline_row += 1
  end
  @step_number += 1
  move_progress
end

#after_tags(tags) ⇒ Object



104
105
106
# File 'lib/lucid/formatter/html.rb', line 104

def after_tags(tags)
  @tag_spacer = nil
end

#background_name(keyword, name, file_colon_line, source_indent) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/lucid/formatter/html.rb', line 138

def background_name(keyword, name, file_colon_line, source_indent)
  @listing_background = true
  @builder.h3(:id => "background_#{@scenario_number}") do |h3|
    @builder.span(keyword, :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

#before_background(background) ⇒ Object



128
129
130
131
# File 'lib/lucid/formatter/html.rb', line 128

def before_background(background)
  @in_background = true
  @builder << '<div class="background">'
end

#before_comment(comment) ⇒ Object



91
92
93
# File 'lib/lucid/formatter/html.rb', line 91

def before_comment(comment)
  @builder << '<pre class="comment">'
end

#before_examples(examples) ⇒ Object



186
187
188
# File 'lib/lucid/formatter/html.rb', line 186

def before_examples(examples)
   @builder << '<div class="examples">'
end

#before_feature(feature) ⇒ Object



82
83
84
85
# File 'lib/lucid/formatter/html.rb', line 82

def before_feature(feature)
  @exceptions = []
  @builder << '<div class="feature">'
end

#before_feature_element(feature_element) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/lucid/formatter/html.rb', line 147

def before_feature_element(feature_element)
  @scenario_number+=1
  @scenario_red = false
  css_class = {
    AST::Scenario        => 'scenario',
    AST::ScenarioOutline => 'scenario outline'
  }[feature_element.class]
  @builder << "<div class='#{css_class}'>"
end

#before_features(features) ⇒ Object



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
# File 'lib/lucid/formatter/html.rb', line 44

def before_features(features)
  @step_count = features.step_count

  @builder.declare!(:DOCTYPE, :html)

  @builder << '<html>'
  @builder.head do
    @builder.meta(:charset => 'utf-8')
    @builder.title 'Lucid'
    inline_css
    inline_js
  end
  @builder << '<body>'
  @builder << "<!-- Step count #{@step_count}-->"
  @builder << '<div class="lucid">'

  @builder.div(:id => 'lucid-header') do
    @builder.div(:id => 'label') do
      @builder.h1('Lucid Features')
    end
    @builder.div(:id => 'summary') do
      @builder.p('',:id => 'totals')
      @builder.p('',:id => 'duration')
      @builder.div(:id => 'expand-collapse') do
        @builder.p('Expand All', :id => 'expander')
        @builder.p('Collapse All', :id => 'collapser')
      end
    end
  end
end

#before_multiline_arg(multiline_arg) ⇒ Object



272
273
274
275
276
277
# File 'lib/lucid/formatter/html.rb', line 272

def before_multiline_arg(multiline_arg)
  return if @hide_this_step || @skip_step
  if AST::Table === multiline_arg
    @builder << '<table>'
  end
end

#before_outline_table(outline_table) ⇒ Object



174
175
176
177
178
179
# File 'lib/lucid/formatter/html.rb', line 174

def before_outline_table(outline_table)
  @inside_outline = true
  @outline_row = 0
  @builder << '<table>'
  @inside_outline = false
end

#before_step(step) ⇒ Object



210
211
212
213
214
# File 'lib/lucid/formatter/html.rb', line 210

def before_step(step)
  @step_id = step.dom_id
  @step_number += 1
  @step = step
end

#before_step_result(step_result) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/lucid/formatter/html.rb', line 220

def before_step_result(step_result)
  @step_match = step_result.step_match
  @hide_this_step = false
  if step_result.exception
    if @exceptions.include?(step_result.exception)
      @hide_this_step = true
      return
    end
    @exceptions << step_result.exception
  end
  if step_result.status != :failed && @in_background ^ step_result.background
    @hide_this_step = true
    return
  end
  @status = step_result.status
  return if @hide_this_step
  set_scenario_color(step_result.status)
  @builder << "<li id='#{@step_id}' class='step #{step_result.status}'>"
end

#before_steps(steps) ⇒ Object



202
203
204
# File 'lib/lucid/formatter/html.rb', line 202

def before_steps(steps)
  @builder << '<ol>'
end

#before_table_row(table_row) ⇒ Object



293
294
295
296
297
298
# File 'lib/lucid/formatter/html.rb', line 293

def before_table_row(table_row)
  @row_id = table_row.dom_id
  @col_index = 0
  return if @hide_this_step
  @builder << "<tr class='step' id='#{@row_id}'>"
end

#comment_line(comment_line) ⇒ Object



99
100
101
102
# File 'lib/lucid/formatter/html.rb', line 99

def comment_line(comment_line)
  @builder.text!(comment_line)
  @builder.br
end

#doc_string(string) ⇒ Object



286
287
288
289
290
291
# File 'lib/lucid/formatter/html.rb', line 286

def doc_string(string)
  return if @hide_this_step
  @builder.pre(:class => 'val') do |pre|
    @builder << h(string).gsub("\n", '&#x000A;')
  end
end

#embed(src, mime_type, label) ⇒ Object



28
29
30
31
32
33
# File 'lib/lucid/formatter/html.rb', line 28

def embed(src, mime_type, label)
  case(mime_type)
  when /^image\/(png|gif|jpg|jpeg)/
    embed_image(src, label)
  end
end

#embed_image(src, label) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/lucid/formatter/html.rb', line 35

def embed_image(src, label)
  id = "img_#{@img_id}"
  @img_id += 1
  @builder.span(:class => 'embed') do |pre|
    pre << %{<a href="" onclick="img=document.getElementById('#{id}'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');return false">#{label}</a><br>&nbsp;
    <img id="#{id}" style="display: none" src="#{src}"/>}
  end
end

#empty_messagesObject



363
364
365
# File 'lib/lucid/formatter/html.rb', line 363

def empty_messages
  @delayed_messages = []
end

#examples_name(keyword, name) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/lucid/formatter/html.rb', line 194

def examples_name(keyword, name)
  @builder.h4 do
    @builder.span(keyword, :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

#exception(exception, status) ⇒ Object



262
263
264
265
# File 'lib/lucid/formatter/html.rb', line 262

def exception(exception, status)
  return if @hide_this_step
  build_exception_detail(exception)
end

#extra_failure_content(file_colon_line) ⇒ Object



267
268
269
270
# File 'lib/lucid/formatter/html.rb', line 267

def extra_failure_content(file_colon_line)
  @matcher_extractor ||= MatcherExtractor.new
  "<pre class=\"ruby\"><code>#{@matcher_extractor.matcher(file_colon_line)}</code></pre>"
end

#feature_name(keyword, name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/lucid/formatter/html.rb', line 114

def feature_name(keyword, name)
  lines = name.split(/\r?\n/)
  return if lines.empty?
  @builder.h2 do |h2|
    @builder.span(keyword + ': ' + lines[0], :class => 'val')
  end
  @builder.p(:class => 'narrative') do
    lines[1..-1].each do |line|
      @builder.text!(line.strip)
      @builder.br
    end
  end
end


341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/lucid/formatter/html.rb', line 341

def print_messages
  return if @delayed_messages.empty?

  #@builder.ol do
    @delayed_messages.each do |ann|
      @builder.li(:class => 'step message') do
        @builder << ann
      end
    end
  #end
  empty_messages
end


354
355
356
357
358
359
360
361
# File 'lib/lucid/formatter/html.rb', line 354

def print_table_row_messages
  return if @delayed_messages.empty?

  @builder.td(:class => 'message') do
    @builder << @delayed_messages.join(", ")
  end
  empty_messages
end

#puts(message) ⇒ Object



336
337
338
339
# File 'lib/lucid/formatter/html.rb', line 336

def puts(message)
  @delayed_messages << message
  #@builder.pre(message, :class => 'message')
end

#scenario_name(keyword, name, file_colon_line, source_indent) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lucid/formatter/html.rb', line 162

def scenario_name(keyword, name, file_colon_line, source_indent)
  @builder.span(:class => 'scenario_file') do
    @builder << file_colon_line
  end
  @listing_background = false
  @builder.h3(:id => "scenario_#{@scenario_number}") do
    @builder.span(keyword + ':', :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

#step_name(keyword, step_match, status, source_indent, background, file_colon_line) ⇒ Object



253
254
255
256
257
258
259
260
# File 'lib/lucid/formatter/html.rb', line 253

def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
  background_in_scenario = background && !@listing_background
  @skip_step = background_in_scenario

  unless @skip_step
    build_step(keyword, step_match, status)
  end
end

#table_cell_value(value, status) ⇒ Object



325
326
327
328
329
330
331
332
333
334
# File 'lib/lucid/formatter/html.rb', line 325

def table_cell_value(value, status)
  return if @hide_this_step

  @cell_type = @outline_row == 0 ? :th : :td
  attributes = {:id => "#{@row_id}_#{@col_index}", :class => 'step'}
  attributes[:class] += " #{status}" if status
  build_cell(@cell_type, value, attributes)
  set_scenario_color(status) if @inside_outline
  @col_index += 1
end

#tag_name(tag_name) ⇒ Object



108
109
110
111
112
# File 'lib/lucid/formatter/html.rb', line 108

def tag_name(tag_name)
  @builder.text!(@tag_spacer) if @tag_spacer
  @tag_spacer = ' '
  @builder.span(tag_name, :class => 'tag')
end