Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / lib / spec / runner / reporter.rb
blob7b78c0acf7a351c2106565488d4a941d670835af
1 module Spec
2   module Runner
3     class Reporter
4       
5       def initialize(formatters, backtrace_tweaker)
6         @formatters = formatters
7         @backtrace_tweaker = backtrace_tweaker
8         clear!
9       end
10       
11       def add_behaviour(name)
12         @formatters.each{|f| f.add_behaviour(name)}
13         @behaviour_names << name
14       end
15       
16       def example_started(name)
17         @formatters.each{|f| f.example_started(name)}
18       end
19       
20       def example_finished(name, error=nil, failure_location=nil, not_implemented = false)
21         @example_names << name
22         
23         if not_implemented
24           example_not_implemented(name)
25         elsif error.nil?
26           example_passed(name)
27         else
28           example_failed(name, error, failure_location)
29         end
30       end
32       def start(number_of_examples)
33         clear!
34         @start_time = Time.new
35         @formatters.each{|f| f.start(number_of_examples)}
36       end
37   
38       def end
39         @end_time = Time.new
40       end
41   
42       # Dumps the summary and returns the total number of failures
43       def dump
44         @formatters.each{|f| f.start_dump}
45         dump_failures
46         @formatters.each{|f| f.dump_summary(duration, @example_names.length, @failures.length, @not_implemented_count)}
47         @failures.length
48       end
50     private
51   
52       def clear!
53         @behaviour_names = []
54         @failures = []
55         @not_implemented_count = 0
56         @example_names = []
57         @start_time = nil
58         @end_time = nil
59       end
60   
61       def dump_failures
62         return if @failures.empty?
63         @failures.inject(1) do |index, failure|
64           @formatters.each{|f| f.dump_failure(index, failure)}
65           index + 1
66         end
67       end
69       def duration
70         return @end_time - @start_time unless (@end_time.nil? or @start_time.nil?)
71         return "0.0"
72       end
73       
74       def example_passed(name)
75         @formatters.each{|f| f.example_passed(name)}
76       end
78       def example_failed(name, error, failure_location)
79         @backtrace_tweaker.tweak_backtrace(error, failure_location)
80         example_name = "#{@behaviour_names.last} #{name}"
81         failure = Failure.new(example_name, error)
82         @failures << failure
83         @formatters.each{|f| f.example_failed(name, @failures.length, failure)}
84       end
85       
86       def example_not_implemented(name)
87         @not_implemented_count += 1
88         @formatters.each{|f| f.example_not_implemented(name)}
89       end
90       
91       class Failure
92         attr_reader :exception
93         
94         def initialize(example_name, exception)
95           @example_name = example_name
96           @exception = exception
97         end
99         def header
100           if expectation_not_met?
101             "'#{@example_name}' FAILED"
102           else
103             "#{@exception.class.name} in '#{@example_name}'"
104           end
105         end
106         
107         def expectation_not_met?
108           @exception.is_a?(Spec::Expectations::ExpectationNotMetError)
109         end
111       end
112     end
113   end