]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/runner/formatter/base_text_formatter.rb
Show whether a trace is public or private in the trace list, so that a user can easil...
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / runner / formatter / base_text_formatter.rb
1 require 'spec/runner/formatter/base_formatter'
2
3 module Spec
4   module Runner
5     module Formatter
6       # Baseclass for text-based formatters. Can in fact be used for
7       # non-text based ones too - just ignore the +output+ constructor
8       # argument.
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)
15           super
16           if where.is_a?(String)
17             @output = File.open(where, 'w')
18           elsif where == STDOUT
19             @output = Kernel
20             def @output.flush
21               STDOUT.flush
22             end
23           else
24             @output = where
25           end
26           @pending_examples = []
27         end
28         
29         def example_pending(example_group_description, example, message)
30           @pending_examples << ["#{example_group_description} #{example.description}", message]
31         end
32         
33         def dump_failure(counter, failure)
34           @output.puts
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)
38           @output.flush
39         end
40         
41         def colourise(s, failure)
42           if(failure.expectation_not_met?)
43             red(s)
44           elsif(failure.pending_fixed?)
45             blue(s)
46           else
47             magenta(s)
48           end
49         end
50       
51         def dump_summary(duration, example_count, failure_count, pending_count)
52           return if dry_run?
53           @output.puts
54           @output.puts "Finished in #{duration} seconds"
55           @output.puts
56
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  
59
60           if failure_count == 0
61             if pending_count > 0
62               @output.puts yellow(summary)
63             else
64               @output.puts green(summary)
65             end
66           else
67             @output.puts red(summary)
68           end
69           @output.flush
70         end
71
72         def dump_pending
73           unless @pending_examples.empty?
74             @output.puts
75             @output.puts "Pending:"
76             @pending_examples.each do |pending_example|
77               @output.puts "#{pending_example[0]} (#{pending_example[1]})" 
78             end
79           end
80           @output.flush
81         end
82         
83         def close
84           if IO === @output
85             @output.close 
86           end
87         end
88         
89         def format_backtrace(backtrace)
90           return "" if backtrace.nil?
91           backtrace.map { |line| backtrace_line(line) }.join("\n")
92         end
93       
94       protected
95
96         def colour?
97           @options.colour ? true : false
98         end
99
100         def dry_run?
101           @options.dry_run ? true : false
102         end
103         
104         def backtrace_line(line)
105           line.sub(/\A([^:]+:\d+)$/, '\\1:')
106         end
107
108         def colour(text, colour_code)
109           return text unless colour? && output_to_tty?
110           "#{colour_code}#{text}\e[0m"
111         end
112
113         def output_to_tty?
114           begin
115             @output == Kernel || @output.tty?
116           rescue NoMethodError
117             false
118           end
119         end
120         
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
126         
127       end
128     end
129   end
130 end