Rspec Steps C0 Coverage Information - RCov

rcov/ruby/1.8/gems/rspec-mocks-2.5.0/lib/rspec/mocks/method_double.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
rcov/ruby/1.8/gems/rspec-mocks-2.5.0/lib/rspec/mocks/method_double.rb 165 141
31.52%
20.57%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 module RSpec
2   module Mocks
3     class MethodDouble < Hash
4       attr_reader :method_name
5 
6       def initialize(object, method_name, proxy)
7         @method_name = method_name
8         @object = object
9         @proxy = proxy
10         @stashed = false
11         store(:expectations, [])
12         store(:stubs, [])
13       end
14 
15       def expectations
16         self[:expectations]
17       end
18 
19       def stubs
20         self[:stubs]
21       end
22 
23       def visibility
24         if Mock === @object
25           'public'
26         elsif object_singleton_class.private_method_defined?(@method_name)
27           'private'
28         elsif object_singleton_class.protected_method_defined?(@method_name)
29           'protected'
30         else
31           'public'
32         end
33       end
34 
35       def object_singleton_class
36         class << @object; self; end
37       end
38 
39       def obfuscate(method_name)
40         "obfuscated_by_rspec_mocks__#{method_name}"
41       end
42 
43       def stashed_method_name
44         obfuscate(method_name)
45       end
46 
47       def object_responds_to?(method_name)
48         if @proxy.already_proxied_respond_to?
49           @object.__send__(obfuscate(:respond_to?), method_name)
50         elsif method_name == :respond_to?
51           @proxy.already_proxied_respond_to
52         else
53           @object.respond_to?(method_name, true)
54         end
55       end
56 
57       def configure_method
58         RSpec::Mocks::space.add(@object) if RSpec::Mocks::space
59         warn_if_nil_class
60         unless @stashed
61           stash_original_method
62           define_proxy_method
63         end
64       end
65 
66       def stash_original_method
67         stashed = stashed_method_name
68         orig = @method_name
69         object_singleton_class.class_eval do
70           alias_method(stashed, orig) if method_defined?(orig) || private_method_defined?(orig)
71         end
72         @stashed = true
73       end
74 
75       def define_proxy_method
76         method_name = @method_name
77         visibility_for_method = "#{visibility} :#{method_name}"
78         object_singleton_class.class_eval(<<-EOF, __FILE__, __LINE__)
79           def #{method_name}(*args, &block)
80             __mock_proxy.message_received :#{method_name}, *args, &block
81           end
82           #{visibility_for_method}
83         EOF
84       end
85 
86       def restore_original_method
87         if @stashed
88           method_name = @method_name
89           stashed_method_name = self.stashed_method_name
90           object_singleton_class.instance_eval do
91             remove_method method_name
92             if method_defined?(stashed_method_name) || private_method_defined?(stashed_method_name)
93               alias_method method_name, stashed_method_name
94               remove_method stashed_method_name
95             end
96           end
97           @stashed = false
98         end
99       end
100 
101       def verify
102         expectations.each {|e| e.verify_messages_received}
103       end
104 
105       def reset
106         reset_nil_expectations_warning
107         restore_original_method
108         clear
109       end
110 
111       def clear
112         expectations.clear
113         stubs.clear
114       end
115 
116       def add_expectation(error_generator, expectation_ordering, expected_from, opts, &block)
117         configure_method
118         expectation = if existing_stub = stubs.first
119           existing_stub.build_child(expected_from, block, 1, opts)
120         else
121           MessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, block, 1, opts)
122         end
123         expectations << expectation
124         expectation
125       end
126 
127       def add_negative_expectation(error_generator, expectation_ordering, expected_from, &implementation)
128         configure_method
129         expectation = NegativeMessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, implementation)
130         expectations.unshift expectation
131         expectation
132       end
133 
134       def add_stub(error_generator, expectation_ordering, expected_from, opts={}, &implementation)
135         configure_method
136         stub = MessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, nil, :any, opts, &implementation)
137         stubs.unshift stub
138         stub
139       end
140       
141       def remove_stub
142         raise_method_not_stubbed_error if stubs.empty?
143         expectations.empty? ? reset : stubs.clear
144       end
145 
146       def proxy_for_nil_class?
147         @object.nil?
148       end
149 
150       def warn_if_nil_class
151         if proxy_for_nil_class? & RSpec::Mocks::Proxy.warn_about_expectations_on_nil
152           Kernel.warn("An expectation of :#{@method_name} was set on nil. Called from #{caller[4]}. Use allow_message_expectations_on_nil to disable warnings.")
153         end
154       end
155       
156       def raise_method_not_stubbed_error
157         raise MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed" 
158       end
159 
160       def reset_nil_expectations_warning
161         RSpec::Mocks::Proxy.warn_about_expectations_on_nil = true if proxy_for_nil_class?
162       end
163     end
164   end
165 end

Generated on Fri Apr 22 17:22:41 -0700 2011 with rcov 0.9.8