class Lucid::Formatter::Html

Public Class Methods

new(runtime, path_or_io, options) click to toggle source
# 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

Public Instance Methods

after_background(background) click to toggle source
# File lib/lucid/formatter/html.rb, line 133
def after_background(background)
  @in_background = nil
  @builder << '</div>'
end
after_comment(comment) click to toggle source
# File lib/lucid/formatter/html.rb, line 95
def after_comment(comment)
  @builder << '</pre>'
end
after_examples(examples) click to toggle source
# File lib/lucid/formatter/html.rb, line 190
def after_examples(examples)
  @builder << '</div>'
end
after_feature(feature) click to toggle source
# File lib/lucid/formatter/html.rb, line 87
def after_feature(feature)
  @builder << '</div>'
end
after_feature_element(feature_element) click to toggle source
# File lib/lucid/formatter/html.rb, line 157
def after_feature_element(feature_element)
  @builder << '</div>'
  @open_step_list = true
end
after_features(features) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/lucid/formatter/html.rb, line 181
def after_outline_table(outline_table)
  @builder << '</table>'
  @outline_row = nil
end
after_step(step) click to toggle source
# File lib/lucid/formatter/html.rb, line 216
def after_step(step)
  move_progress
end
after_step_result(step_result) click to toggle source
# 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) click to toggle source
# File lib/lucid/formatter/html.rb, line 206
def after_steps(steps)
  @builder << '</ol>'
end
after_table_row(table_row) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/lucid/formatter/html.rb, line 128
def before_background(background)
  @in_background = true
  @builder << '<div class="background">'
end
before_comment(comment) click to toggle source
# File lib/lucid/formatter/html.rb, line 91
def before_comment(comment)
  @builder << '<pre class="comment">'
end
before_examples(examples) click to toggle source
# File lib/lucid/formatter/html.rb, line 186
def before_examples(examples)
   @builder << '<div class="examples">'
end
before_feature(feature) click to toggle source
# File lib/lucid/formatter/html.rb, line 82
def before_feature(feature)
  @exceptions = []
  @builder << '<div class="feature">'
end
before_feature_element(feature_element) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/lucid/formatter/html.rb, line 202
def before_steps(steps)
  @builder << '<ol>'
end
before_table_row(table_row) click to toggle source
# 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) click to toggle source
# File lib/lucid/formatter/html.rb, line 99
def comment_line(comment_line)
  @builder.text!(comment_line)
  @builder.br
end
doc_string(string) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_messages() click to toggle source
# File lib/lucid/formatter/html.rb, line 363
def empty_messages
  @delayed_messages = []
end
examples_name(keyword, name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
print_messages() click to toggle source
print_table_row_messages() click to toggle source
puts(message) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

backtrace_line(line) click to toggle source
# File lib/lucid/formatter/html.rb, line 527
def backtrace_line(line)
  line.gsub(/\A([^:]*\.(?:rb|feature|haml)):(\d*).*\z/) do
    if ENV['TM_PROJECT_DIRECTORY']
      "<a href=\"txmt://open?url=file://#{File.expand_path($1)}&line=#{$2}\">#{$1}:#{$2}</a> "
    else
      line
    end
  end
end
build_cell(cell_type, value, attributes) click to toggle source
# File lib/lucid/formatter/html.rb, line 449
def build_cell(cell_type, value, attributes)
  @builder.__send__(cell_type, attributes) do
    @builder.div do
      @builder.span(value,:class => 'step param')
    end
  end
end
build_exception_detail(exception) click to toggle source
# File lib/lucid/formatter/html.rb, line 369
def build_exception_detail(exception)
  backtrace = Array.new
  @builder.div(:class => 'message') do
    message = exception.message
    if defined?(RAILS_ROOT) && message.include?('Exception caught')
      matches = message.match(/Showing <i>(.+)<\/i>(?:.+) #(\d+)/)
      backtrace += ["#{RAILS_ROOT}/#{matches[1]}:#{matches[2]}"] if matches
      matches = message.match(/<code>([^(\/)]+)<\//m)
      message = matches ? matches[1] : ""
    end

    unless exception.instance_of?(RuntimeError)
      message = "#{message} (#{exception.class})"
    end

    @builder.pre do
      @builder.text!(message)
    end
  end
  @builder.div(:class => 'backtrace') do
    @builder.pre do
      backtrace = exception.backtrace
      backtrace.delete_if { |x| x =~ /\/gems\/(lucid|rspec)/ }
      @builder << backtrace_line(backtrace.join("\n"))
    end
  end
  extra = extra_failure_content(backtrace)
  @builder << extra unless extra == ""
end
build_step(keyword, step_match, status) click to toggle source
# File lib/lucid/formatter/html.rb, line 426
def build_step(keyword, step_match, status)
  step_name = step_match.format_args(lambda{|param| %{<span class="param">#{param}</span>}})
  @builder.div(:class => 'step_name') do |div|
    @builder.span(keyword, :class => 'keyword')
    @builder.span(:class => 'step val') do |name|
      name << h(step_name).gsub(/&lt;span class=&quot;(.*?)&quot;&gt;/, '<span class="\1">').gsub(/&lt;\/span&gt;/, '</span>')
    end
  end

  step_file = step_match.file_colon_line
  step_file.gsub(/^([^:]*\.rb):(\d*)/) do
    if ENV['TM_PROJECT_DIRECTORY']
      step_file = "<a href=\"txmt://open?url=file://#{File.expand_path($1)}&line=#{$2}\">#{$1}:#{$2}</a> "
    end
  end

  @builder.div(:class => 'step_file') do |div|
    @builder.span do
      @builder << step_file
    end
  end
end
create_builder(io) click to toggle source
# File lib/lucid/formatter/html.rb, line 565
def create_builder(io)
  Builder::XmlMarkup.new(:target => io, :indent => 0)
end
dump_count(count, what, state=nil) click to toggle source
# File lib/lucid/formatter/html.rb, line 561
def dump_count(count, what, state=nil)
  [count, state, "#{what}#{count == 1 ? '' : 's'}"].compact.join(" ")
end
format_exception(exception) click to toggle source
# File lib/lucid/formatter/html.rb, line 523
def format_exception(exception)
  (["#{exception.message}"] + exception.backtrace).join("\n")
end
inline_css() click to toggle source
# File lib/lucid/formatter/html.rb, line 457
def inline_css
  @builder.style(:type => 'text/css') do
    @builder << File.read(File.dirname(__FILE__) + '/lucid.css')
  end
end
inline_jquery() click to toggle source
# File lib/lucid/formatter/html.rb, line 470
def inline_jquery
  File.read(File.dirname(__FILE__) + '/jquery-min.js')
end
inline_js() click to toggle source
# File lib/lucid/formatter/html.rb, line 463
def inline_js
  @builder.script(:type => 'text/javascript') do
    @builder << inline_jquery
    @builder << inline_js_content
  end
end
inline_js_content() click to toggle source
# File lib/lucid/formatter/html.rb, line 474
      def inline_js_content
        <<-EOF

  SCENARIOS = "h3[id^='scenario_'],h3[id^=background_]";

  $(document).ready(function() {
    $(SCENARIOS).css('cursor', 'pointer');
    $(SCENARIOS).click(function() {
      $(this).siblings().toggle(250);
    });

    $("#collapser").css('cursor', 'pointer');
    $("#collapser").click(function() {
      $(SCENARIOS).siblings().hide();
    });

    $("#expander").css('cursor', 'pointer');
    $("#expander").click(function() {
      $(SCENARIOS).siblings().show();
    });
  })

  function moveProgressBar(percentDone) {
    $("lucid-header").css('width', percentDone +"%");
  }
  function makeRed(element_id) {
    $('#'+element_id).css('background', '#C40D0D');
    $('#'+element_id).css('color', '#FFFFFF');
  }
  function makeYellow(element_id) {
    $('#'+element_id).css('background', '#FAF834');
    $('#'+element_id).css('color', '#000000');
  }

        EOF
      end
move_progress() click to toggle source
# File lib/lucid/formatter/html.rb, line 511
def move_progress
  @builder << " <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
end
percent_done() click to toggle source
# File lib/lucid/formatter/html.rb, line 515
def percent_done
  result = 100.0
  if @step_count != 0
    result = ((@step_number).to_f / @step_count.to_f * 1000).to_i / 10.0
  end
  result
end
print_stat_string(features) click to toggle source
print_stats(features) click to toggle source
print_status_counts() { |status| ... } click to toggle source
set_scenario_color(status) click to toggle source
# File lib/lucid/formatter/html.rb, line 399
def set_scenario_color(status)
  if status.nil? or status == :undefined or status == :pending
    set_scenario_color_pending
  end
  if status == :failed
    set_scenario_color_failed
  end
end
set_scenario_color_failed() click to toggle source
# File lib/lucid/formatter/html.rb, line 408
def set_scenario_color_failed
  @builder.script do
    @builder.text!("makeRed('lucid-header');") unless @header_red
    @header_red = true
    scenario_or_background = @in_background ? "background" : "scenario"
    @builder.text!("makeRed('#{scenario_or_background}_#{@scenario_number}');") unless @scenario_red
    @scenario_red = true
  end
end
set_scenario_color_pending() click to toggle source
# File lib/lucid/formatter/html.rb, line 418
def set_scenario_color_pending
  @builder.script do
    @builder.text!("makeYellow('lucid-header');") unless @header_red
    scenario_or_background = @in_background ? "background" : "scenario"
    @builder.text!("makeYellow('#{scenario_or_background}_#{@scenario_number}');") unless @scenario_red
  end
end