Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / lib / spec / runner / formatter / base_text_formatter.rb
blob02a95e51df6ca4dc77e0ca2abaede0053e26176c
1 module Spec
2   module Runner
3     module Formatter
4       # Baseclass for text-based formatters. Can in fact be used for
5       # non-text based ones too - just ignore the +output+ constructor
6       # argument.
7       class BaseTextFormatter < BaseFormatter
8         # Creates a new instance that will write to +where+. If +where+ is a
9         # String, output will be written to the File with that name, otherwise
10         # +where+ is exected to be an IO (or an object that responds to #puts and #write).
11         def initialize(options, where)
12           super
13           if where.is_a?(String)
14             @output = File.open(where, 'w')
15           elsif where == STDOUT
16             @output = Kernel
17             def @output.flush
18               STDOUT.flush
19             end
20           else
21             @output = where
22           end
23           @snippet_extractor = SnippetExtractor.new
24           @pending_examples = []
25         end
26         
27         def example_pending(behaviour_name, example_name, message)
28           @pending_examples << ["#{behaviour_name} #{example_name}", message]
29         end
30         
31         def dump_failure(counter, failure)
32           @output.puts
33           @output.puts "#{counter.to_s})"
34           @output.puts colourise("#{failure.header}\n#{failure.exception.message}", failure)
35           @output.puts format_backtrace(failure.exception.backtrace)
36           @output.flush
37         end
38         
39         def colourise(s, failure)
40           if(failure.expectation_not_met?)
41             red(s)
42           elsif(failure.pending_fixed?)
43             blue(s)
44           else
45             magenta(s)
46           end
47         end
48       
49         def dump_summary(duration, example_count, failure_count, pending_count)
50           return if dry_run?
51           @output.puts
52           @output.puts "Finished in #{duration} seconds"
53           @output.puts
55           summary = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
56           summary << ", #{pending_count} pending" if pending_count > 0  
58           if failure_count == 0
59             if pending_count > 0
60               @output.puts yellow(summary)
61             else
62               @output.puts green(summary)
63             end
64           else
65             @output.puts red(summary)
66           end
67           @output.flush
68         end
70         def dump_pending
71           unless @pending_examples.empty?
72             @output.puts
73             @output.puts "Pending:"
74             @pending_examples.each do |pending_example|
75               @output.puts "#{pending_example[0]} (#{pending_example[1]})" 
76             end
77           end
78           @output.flush
79         end
80         
81         def close
82           if IO === @output
83             @output.close 
84           end
85         end
86         
87         def format_backtrace(backtrace)
88           return "" if backtrace.nil?
89           backtrace.map { |line| backtrace_line(line) }.join("\n")
90         end
91       
92       protected
94         def colour?
95           @options.colour ? true : false
96         end
98         def dry_run?
99           @options.dry_run ? true : false
100         end
101         
102         def backtrace_line(line)
103           line.sub(/\A([^:]+:\d+)$/, '\\1:')
104         end
106         def colour(text, colour_code)
107           return text unless colour? && output_to_tty?
108           "#{colour_code}#{text}\e[0m"
109         end
111         def output_to_tty?
112           begin
113             @output == Kernel || @output.tty?
114           rescue NoMethodError
115             false
116           end
117         end
118         
119         def green(text); colour(text, "\e[32m"); end
120         def red(text); colour(text, "\e[31m"); end
121         def magenta(text); colour(text, "\e[35m"); end
122         def yellow(text); colour(text, "\e[33m"); end
123         def blue(text); colour(text, "\e[34m"); end
124         
125       end
126     end
127   end