]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/runner/reporter.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 / reporter.rb
1 module Spec
2   module Runner
3     class Reporter
4       attr_reader :options, :example_groups
5       
6       def initialize(options)
7         @options = options
8         @options.reporter = self
9         clear
10       end
11       
12       def add_example_group(example_group)
13         formatters.each do |f|
14           f.add_example_group(example_group)
15         end
16         example_groups << example_group
17       end
18       
19       def example_started(example)
20         formatters.each{|f| f.example_started(example)}
21       end
22       
23       def example_finished(example, error=nil)
24         @examples << example
25         
26         if error.nil?
27           example_passed(example)
28         elsif Spec::Example::ExamplePendingError === error
29           example_pending(example_groups.last, example, error.message)
30         else
31           example_failed(example, error)
32         end
33       end
34
35       def failure(example, error)
36         backtrace_tweaker.tweak_backtrace(error)
37         example_name = "#{example_groups.last.description} #{example.description}"
38         failure = Failure.new(example_name, error)
39         @failures << failure
40         formatters.each do |f|
41           f.example_failed(example, @failures.length, failure)
42         end
43       end
44       alias_method :example_failed, :failure
45
46       def start(number_of_examples)
47         clear
48         @start_time = Time.new
49         formatters.each{|f| f.start(number_of_examples)}
50       end
51   
52       def end
53         @end_time = Time.new
54       end
55   
56       # Dumps the summary and returns the total number of failures
57       def dump
58         formatters.each{|f| f.start_dump}
59         dump_pending
60         dump_failures
61         formatters.each do |f|
62           f.dump_summary(duration, @examples.length, @failures.length, @pending_count)
63           f.close
64         end
65         @failures.length
66       end
67
68     private
69
70       def formatters
71         @options.formatters
72       end
73
74       def backtrace_tweaker
75         @options.backtrace_tweaker
76       end
77   
78       def clear
79         @example_groups = []
80         @failures = []
81         @pending_count = 0
82         @examples = []
83         @start_time = nil
84         @end_time = nil
85       end
86   
87       def dump_failures
88         return if @failures.empty?
89         @failures.inject(1) do |index, failure|
90           formatters.each{|f| f.dump_failure(index, failure)}
91           index + 1
92         end
93       end
94       def dump_pending
95         formatters.each{|f| f.dump_pending}
96       end
97
98       def duration
99         return @end_time - @start_time unless (@end_time.nil? or @start_time.nil?)
100         return "0.0"
101       end
102       
103       def example_passed(example)
104         formatters.each{|f| f.example_passed(example)}
105       end
106       
107       def example_pending(example_group, example, message="Not Yet Implemented")
108         @pending_count += 1
109         formatters.each do |f|
110           f.example_pending(example_group.description, example, message)
111         end
112       end
113       
114       class Failure
115         attr_reader :exception
116         
117         def initialize(example_name, exception)
118           @example_name = example_name
119           @exception = exception
120         end
121
122         def header
123           if expectation_not_met?
124             "'#{@example_name}' FAILED"
125           elsif pending_fixed?
126             "'#{@example_name}' FIXED"
127           else
128             "#{@exception.class.name} in '#{@example_name}'"
129           end
130         end
131         
132         def pending_fixed?
133           @exception.is_a?(Spec::Example::PendingExampleFixedError)
134         end
135
136         def expectation_not_met?
137           @exception.is_a?(Spec::Expectations::ExpectationNotMetError)
138         end
139
140       end
141     end
142   end
143 end