lib/lopata/observers/console_output_observer.rb in lopata-0.1.1 vs lib/lopata/observers/console_output_observer.rb in lopata-0.1.2
- old
+ new
@@ -1,5 +1,7 @@
+require_relative 'backtrace_formatter'
+
module Lopata
module Observers
class ConsoleOutputObserver < BaseObserver
def finished(world)
total = world.scenarios.length
@@ -9,54 +11,67 @@
end
details = counts.empty? ? "" : "(%s)" % counts.join(', ')
puts "#{total} scenario%s %s" % [total == 1 ? '' : 's', details]
end
- def step_finished(step)
- @failed_steps << step if step.failed?
- end
-
- def scenario_started(scenario)
- @failed_steps = []
- end
-
def scenario_finished(scenario)
message = "#{scenario.title} #{bold(scenario.status.to_s.upcase)}"
puts colored(message, scenario.status)
+ return unless scenario.failed?
- @failed_steps.each do |step|
- if step.exception
- puts step.exception.message
- puts step.exception.backtrace.join("\n")
- puts
- end
+ scenario.steps_in_running_order.each do |step|
+ puts colored(" #{status_marker(step.status)} #{step.title}", step.status)
+ puts indent(4, backtrace_formatter.error_message(step.exception, include_backtrace: true)) if step.failed?
end
end
private
def colored(text, status)
case status
when :failed then red(text)
when :passed then green(text)
+ when :skipped then cyan(text)
+ when :pending then yellow(text)
else text
end
end
- def red(text)
- wrap(text, 31)
+ {
+ red: 31,
+ green: 32,
+ cyan: 36,
+ yellow: 33,
+ bold: 1,
+ }.each do |color, code|
+ define_method(color) do |text|
+ wrap(text, code)
+ end
end
- def green(text)
- wrap(text, 32)
+ def wrap(text, code)
+ "\e[#{code}m#{text}\e[0m"
end
- def bold(text)
- wrap(text, 1)
+ def backtrace_formatter
+ @backtrace_formatter ||= Lopata::Observers::BacktraceFormatter.new
end
- def wrap(text, code)
- "\e[#{code}m#{text}\e[0m"
+ def status_marker(status)
+ case status
+ when :failed then "[!]"
+ when :skipped then "[-]"
+ when :pending then "[?]"
+ else "[+]"
+ end
end
+
+ # Adds indent to text
+ # @param cols [Number] number of spaces to be added
+ # @param text [String] text to add indent
+ # @return [String] text with indent
+ def indent(cols, text)
+ text.split("\n").map { |line| " " * cols + line }.join("\n")
+ end
end
end
-end
\ No newline at end of file
+end