Class: Daddy::Formatter::Html

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

Defined Under Namespace

Classes: SnippetExtractor

Instance Method Summary collapse

Methods included from DaddyHtml

#feature_dir, #should_expand

Constructor Details

#initialize(runtime, path_or_io, options) ⇒ Html

Returns a new instance of Html.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/daddy/formatter/html.rb', line 17

def initialize(runtime, path_or_io, options)
  @path_or_io = path_or_io
  @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
end

Instance Method Details

#after_background(background) ⇒ Object



179
180
181
182
# File 'lib/daddy/formatter/html.rb', line 179

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

#after_comment(comment) ⇒ Object



135
136
137
138
# File 'lib/daddy/formatter/html.rb', line 135

def after_comment(comment)
  return if comment == '# language: ja'
  @builder << '</pre>'
end

#after_examples(examples) ⇒ Object



236
237
238
# File 'lib/daddy/formatter/html.rb', line 236

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

#after_feature(feature) ⇒ Object



126
127
128
# File 'lib/daddy/formatter/html.rb', line 126

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

#after_feature_element(feature_element) ⇒ Object



203
204
205
206
# File 'lib/daddy/formatter/html.rb', line 203

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

#after_features(features) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/daddy/formatter/html.rb', line 104

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

#after_menuObject



91
92
93
94
95
96
# File 'lib/daddy/formatter/html.rb', line 91

def after_menu
  if ENV['PUBLISH']
    @builder << '</div>'
    @builder << '</div>'
  end
end

#after_multiline_arg(multiline_arg) ⇒ Object



352
353
354
355
356
357
# File 'lib/daddy/formatter/html.rb', line 352

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

#after_outline_table(outline_table) ⇒ Object



227
228
229
230
# File 'lib/daddy/formatter/html.rb', line 227

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

#after_step(step) ⇒ Object



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

def after_step(step)
  move_progress
end

#after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/daddy/formatter/html.rb', line 292

def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
  return if @hide_this_step
  # print snippet for undefined steps
  if 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.snippet_text(keyword,step_match.instance_variable_get("@name") || '',step_multiline_class)
    end
  end
  @builder << '</li>'

  unless status == :undefined
    step_file = step_match.file_colon_line
    step_contents = "<div class=\"step_contents\"><pre>"
    step_file.gsub(/^([^:]*\.rb):(\d*)/) do
      line_index = $2.to_i - 1

      file = $1.force_encoding('UTF-8')
      if file.start_with?('daddy-') or file.start_with?('/daddy-')
        file = '/usr/local/lib/ruby/gems/1.9.1/gems/' + file
      end

      File.readlines(File.expand_path(file))[line_index..-1].each do |line|
        step_contents << line
        break if line.chop == 'end' or line.chop.start_with?('end ')
      end
    end
    step_contents << "</pre></div>"
    @builder << step_contents
  end

  print_messages
end

#after_steps(steps) ⇒ Object



253
254
255
# File 'lib/daddy/formatter/html.rb', line 253

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

#after_table_row(table_row) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/daddy/formatter/html.rb', line 374

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
    set_scenario_color_failed
  end
  if @outline_row
    @outline_row += 1
  end
  @step_number += 1
  move_progress
end

#after_tags(tags) ⇒ Object



146
147
148
# File 'lib/daddy/formatter/html.rb', line 146

def after_tags(tags)
  @tag_spacer = nil
end

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



184
185
186
187
188
189
190
191
# File 'lib/daddy/formatter/html.rb', line 184

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



174
175
176
177
# File 'lib/daddy/formatter/html.rb', line 174

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

#before_comment(comment) ⇒ Object



130
131
132
133
# File 'lib/daddy/formatter/html.rb', line 130

def before_comment(comment)
  return if comment == '# language: ja'
  @builder << '<pre class="comment">'
end

#before_examples(examples) ⇒ Object



232
233
234
# File 'lib/daddy/formatter/html.rb', line 232

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

#before_feature(feature) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/daddy/formatter/html.rb', line 112

def before_feature(feature)
  dir = feature_dir(feature)
  if @feature_dir != dir
    @builder << '<div class="feature_dir"><span class="val">'
    @builder << dir
    @builder << '</span></div>'
  end 

  @feature_dir = dir
  @feature = feature
  @exceptions = []
  @builder << '<div class="feature">'
end

#before_feature_element(feature_element) ⇒ Object



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

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

#before_features(features) ⇒ Object



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

def before_features(features)
  @step_count = get_step_count(features)

  @builder.declare!(:DOCTYPE, :html)
  @builder << '<html>'
  @builder << '<head>'
    @builder.meta('http-equiv' => 'Content-Type', :content => 'text/html;charset=utf-8')
    @builder.title 'Daddy'
    inline_css
    inline_js
  @builder << '</head>'
  @builder << '<body>'
  @builder << "<!-- Step count #{@step_count}-->"
  @builder << '<div class="cucumber">'
  @builder.div(:id => 'cucumber-header') do
    @builder.div(:id => 'label') do
      @builder.h1('Daddy')
    end
    @builder.div(:id => 'summary') do
      @builder.p('',:id => 'totals')
      @builder.p('',:id => 'duration')
      @builder.div(:id => 'expand-collapse') do
        @builder.p('すべて開く', :id => 'expander')
        @builder.p('すべて閉じる', :id => 'collapser')
      end
    end
  end
  
  before_menu
end

#before_menuObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/daddy/formatter/html.rb', line 79

def before_menu
  if ENV['PUBLISH']
    @builder << "<div>"
  
    @builder.div(:id => 'menu') do
        @builder << make_menu_for_publish
    end
  
    @builder << "<div class='contents'>"
  end
end

#before_multiline_arg(multiline_arg) ⇒ Object



345
346
347
348
349
350
# File 'lib/daddy/formatter/html.rb', line 345

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

#before_outline_table(outline_table) ⇒ Object



222
223
224
225
# File 'lib/daddy/formatter/html.rb', line 222

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

#before_step(step) ⇒ Object



257
258
259
260
261
# File 'lib/daddy/formatter/html.rb', line 257

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

#before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/daddy/formatter/html.rb', line 267

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
  @step_match = step_match
  @hide_this_step = false
  if exception
    if @exceptions.include?(exception)
      @hide_this_step = true
      return
    end
    @exceptions << exception
  end
  if status != :failed && @in_background ^ background
    @hide_this_step = true
    return
  end
  @status = status
  return if @hide_this_step
  set_scenario_color(status)
  
  if ! @delayed_messages.empty? and status == :passed
    @builder << "<li id='#{@step_id}' class='step #{status} expand'>"
  else
    @builder << "<li id='#{@step_id}' class='step #{status}'>"
  end
end

#before_steps(steps) ⇒ Object



248
249
250
251
# File 'lib/daddy/formatter/html.rb', line 248

def before_steps(steps)
  @step_count_in_scenario = steps.count
  @builder << '<ol>'
end

#before_table_row(table_row) ⇒ Object



367
368
369
370
371
372
# File 'lib/daddy/formatter/html.rb', line 367

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



140
141
142
143
144
# File 'lib/daddy/formatter/html.rb', line 140

def comment_line(comment_line)
  return if comment_line == '# language: ja'
  @builder.text!(comment_line)
  @builder.br
end

#doc_string(string) ⇒ Object



359
360
361
362
363
364
# File 'lib/daddy/formatter/html.rb', line 359

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



32
33
34
35
36
37
# File 'lib/daddy/formatter/html.rb', line 32

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



39
40
41
42
43
44
45
46
# File 'lib/daddy/formatter/html.rb', line 39

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



434
435
436
# File 'lib/daddy/formatter/html.rb', line 434

def empty_messages
  @delayed_messages = []
end

#examples_name(keyword, name) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/daddy/formatter/html.rb', line 240

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



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

def exception(exception, status)
  build_exception_detail(exception)
end

#extra_failure_content(file_colon_line) ⇒ Object



340
341
342
343
# File 'lib/daddy/formatter/html.rb', line 340

def extra_failure_content(file_colon_line)
  @snippet_extractor ||= SnippetExtractor.new
  "<pre class=\"ruby\"><code>#{@snippet_extractor.snippet(file_colon_line)}</code></pre>"
end

#feature_name(keyword, name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/daddy/formatter/html.rb', line 156

def feature_name(keyword, name)
  title = feature_dir(@feature, true) + @feature.file.split('/').last.gsub(/\.feature/, '')

  @builder.h2 do |h2|
    @builder.span(title, :class => 'val')
  end

  lines = name.split(/\r?\n/)
  unless lines.empty?
    @builder.p(:class => 'narrative') do
      lines.each do |line|
        @builder.text!(line.strip)
        @builder.br
      end
    end
  end
end

#make_menu_for_publishObject



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

def make_menu_for_publish
  menu = Rails.root + '/tmp/menu.html'
  system("erb -T - #{File.dirname(__FILE__)}/menu.html.erb > #{menu}")
  File.readlines(menu).join
end


411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/daddy/formatter/html.rb', line 411

def print_messages
  return if @delayed_messages.empty?

  #@builder.ol do
    @delayed_messages.each_with_index do |ann, i|
      @builder.li(:id => "#{@step_number}_#{i}", :class => 'step message') do
        @builder << ann
      end
    end
    @builder.script do |script|
      script << "$(function() {"
      script << "  $('##{@step_id}').css('cursor', 'pointer').click(function() {"
      script << "    $(this).nextAll('li[id^=\"#{@step_number}_\"]').toggle(250);"
      script << "  });"
      script << "});"
    end
  #end
  empty_messages
end


431
432
# File 'lib/daddy/formatter/html.rb', line 431

def print_table_row_messages
end

#puts(message) ⇒ Object



406
407
408
409
# File 'lib/daddy/formatter/html.rb', line 406

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

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



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/daddy/formatter/html.rb', line 208

def scenario_name(keyword, name, file_colon_line, source_indent)
  @step_number_in_scenario = 0
  
  @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



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

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



395
396
397
398
399
400
401
402
403
404
# File 'lib/daddy/formatter/html.rb', line 395

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)
  @col_index += 1
end

#tag_name(tag_name) ⇒ Object



150
151
152
153
154
# File 'lib/daddy/formatter/html.rb', line 150

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