require 'erb'
require 'spec/runner/formatter/base_text_formatter'
module Spec
module Runner
module Formatter
module Story
class HtmlFormatter < BaseTextFormatter
include ERB::Util
def initialize(options, where)
super
@previous_type = nil
@scenario_text = ""
@story_text = ""
@scenario_failed = false
@story_failed = false
end
def run_started(count)
@output.puts <<-EOF
Stories
EOF
end
def collected_steps(steps)
unless steps.empty?
@output.puts "
"
steps.each do |step|
@output.puts " - #{step}
"
end
@output.puts "
"
end
end
def run_ended
@output.puts <<-EOF
EOF
end
def story_started(title, narrative)
@story_failed = false
@story_text = <<-EOF
Story: #{h title}
#{h(narrative).split("\n").join("
")}
EOF
end
def story_ended(title, narrative)
if @story_failed
@output.puts <<-EOF
EOF
else
@output.puts <<-EOF
EOF
end
@output.puts <<-EOF
#{@story_text}
EOF
end
def scenario_started(story_title, scenario_name)
@previous_type = nil
@scenario_failed = false
@scenario_text = <<-EOF
Scenario: #{h scenario_name}
EOF
end
def scenario_ended
if @scenario_failed
@story_text += <<-EOF
EOF
else
@story_text += <<-EOF
EOF
end
@story_text += <<-EOF
#{@scenario_text}
EOF
end
def found_scenario(type, description)
end
def scenario_succeeded(story_title, scenario_name)
scenario_ended
end
def scenario_pending(story_title, scenario_name, reason)
scenario_ended
end
def scenario_failed(story_title, scenario_name, err)
@scenario_failed = true
@story_failed = true
scenario_ended
end
def step_upcoming(type, description, *args)
end
def step_succeeded(type, description, *args)
print_step('passed', type, description, *args) # TODO: uses succeeded CSS class
end
def step_pending(type, description, *args)
print_step('pending', type, description, *args)
end
def step_failed(type, description, *args)
print_step('failed', type, description, *args)
end
def print_step(klass, type, description, *args)
spans = args.map { |arg| "#{arg}" }
desc_string = description.step_name
arg_regexp = description.arg_regexp
inner = if(type == @previous_type)
"And "
else
"#{type.to_s.capitalize} "
end
i = -1
inner += desc_string.gsub(arg_regexp) { |param| spans[i+=1] }
@scenario_text += " #{inner}\n"
if type == :'given scenario'
@previous_type = :given
else
@previous_type = type
end
end
end
end
end
end
end