1 require 'spec/runner/formatter/base_formatter'
6 # Baseclass for text-based formatters. Can in fact be used for
7 # non-text based ones too - just ignore the +output+ constructor
9 class BaseTextFormatter < BaseFormatter
10 attr_reader :output, :pending_examples
11 # Creates a new instance that will write to +where+. If +where+ is a
12 # String, output will be written to the File with that name, otherwise
13 # +where+ is exected to be an IO (or an object that responds to #puts and #write).
14 def initialize(options, where)
16 if where.is_a?(String)
17 @output = File.open(where, 'w')
26 @pending_examples = []
29 def example_pending(example_group_description, example, message)
30 @pending_examples << ["#{example_group_description} #{example.description}", message]
33 def dump_failure(counter, failure)
35 @output.puts "#{counter.to_s})"
36 @output.puts colourise("#{failure.header}\n#{failure.exception.message}", failure)
37 @output.puts format_backtrace(failure.exception.backtrace)
41 def colourise(s, failure)
42 if(failure.expectation_not_met?)
44 elsif(failure.pending_fixed?)
51 def dump_summary(duration, example_count, failure_count, pending_count)
54 @output.puts "Finished in #{duration} seconds"
57 summary = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
58 summary << ", #{pending_count} pending" if pending_count > 0
62 @output.puts yellow(summary)
64 @output.puts green(summary)
67 @output.puts red(summary)
73 unless @pending_examples.empty?
75 @output.puts "Pending:"
76 @pending_examples.each do |pending_example|
77 @output.puts "#{pending_example[0]} (#{pending_example[1]})"
89 def format_backtrace(backtrace)
90 return "" if backtrace.nil?
91 backtrace.map { |line| backtrace_line(line) }.join("\n")
97 @options.colour ? true : false
101 @options.dry_run ? true : false
104 def backtrace_line(line)
105 line.sub(/\A([^:]+:\d+)$/, '\\1:')
108 def colour(text, colour_code)
109 return text unless colour? && output_to_tty?
110 "#{colour_code}#{text}\e[0m"
115 @output == Kernel || @output.tty?
121 def green(text); colour(text, "\e[32m"); end
122 def red(text); colour(text, "\e[31m"); end
123 def magenta(text); colour(text, "\e[35m"); end
124 def yellow(text); colour(text, "\e[33m"); end
125 def blue(text); colour(text, "\e[34m"); end